blob: 4fbed12565fd07b4b3d9ac2c6e528cd43a865a29 [file] [log] [blame]
khenaidoo257f3192021-12-15 16:46:37 -05001/*
2 *
3 * Copyright 2020 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19// Package insecure provides an implementation of the
20// credentials.TransportCredentials interface which disables transport security.
21package insecure
22
23import (
24 "context"
25 "net"
26
27 "google.golang.org/grpc/credentials"
28)
29
30// NewCredentials returns a credentials which disables transport security.
31//
32// Note that using this credentials with per-RPC credentials which require
33// transport security is incompatible and will cause grpc.Dial() to fail.
34func NewCredentials() credentials.TransportCredentials {
35 return insecureTC{}
36}
37
38// insecureTC implements the insecure transport credentials. The handshake
39// methods simply return the passed in net.Conn and set the security level to
40// NoSecurity.
41type insecureTC struct{}
42
43func (insecureTC) ClientHandshake(ctx context.Context, _ string, conn net.Conn) (net.Conn, credentials.AuthInfo, error) {
44 return conn, info{credentials.CommonAuthInfo{SecurityLevel: credentials.NoSecurity}}, nil
45}
46
47func (insecureTC) ServerHandshake(conn net.Conn) (net.Conn, credentials.AuthInfo, error) {
48 return conn, info{credentials.CommonAuthInfo{SecurityLevel: credentials.NoSecurity}}, nil
49}
50
51func (insecureTC) Info() credentials.ProtocolInfo {
52 return credentials.ProtocolInfo{SecurityProtocol: "insecure"}
53}
54
55func (insecureTC) Clone() credentials.TransportCredentials {
56 return insecureTC{}
57}
58
59func (insecureTC) OverrideServerName(string) error {
60 return nil
61}
62
63// info contains the auth information for an insecure connection.
64// It implements the AuthInfo interface.
65type info struct {
66 credentials.CommonAuthInfo
67}
68
69// AuthType returns the type of info as a string.
70func (info) AuthType() string {
71 return "insecure"
72}