khenaidoo | 257f319 | 2021-12-15 16:46:37 -0500 | [diff] [blame^] | 1 | /* |
| 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. |
| 21 | package insecure |
| 22 | |
| 23 | import ( |
| 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. |
| 34 | func 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. |
| 41 | type insecureTC struct{} |
| 42 | |
| 43 | func (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 | |
| 47 | func (insecureTC) ServerHandshake(conn net.Conn) (net.Conn, credentials.AuthInfo, error) { |
| 48 | return conn, info{credentials.CommonAuthInfo{SecurityLevel: credentials.NoSecurity}}, nil |
| 49 | } |
| 50 | |
| 51 | func (insecureTC) Info() credentials.ProtocolInfo { |
| 52 | return credentials.ProtocolInfo{SecurityProtocol: "insecure"} |
| 53 | } |
| 54 | |
| 55 | func (insecureTC) Clone() credentials.TransportCredentials { |
| 56 | return insecureTC{} |
| 57 | } |
| 58 | |
| 59 | func (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. |
| 65 | type info struct { |
| 66 | credentials.CommonAuthInfo |
| 67 | } |
| 68 | |
| 69 | // AuthType returns the type of info as a string. |
| 70 | func (info) AuthType() string { |
| 71 | return "insecure" |
| 72 | } |