podman
98 строк · 3.1 Кб
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.
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 {
35return 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) {
44return conn, info{credentials.CommonAuthInfo{SecurityLevel: credentials.NoSecurity}}, nil
45}
46
47func (insecureTC) ServerHandshake(conn net.Conn) (net.Conn, credentials.AuthInfo, error) {
48return conn, info{credentials.CommonAuthInfo{SecurityLevel: credentials.NoSecurity}}, nil
49}
50
51func (insecureTC) Info() credentials.ProtocolInfo {
52return credentials.ProtocolInfo{SecurityProtocol: "insecure"}
53}
54
55func (insecureTC) Clone() credentials.TransportCredentials {
56return insecureTC{}
57}
58
59func (insecureTC) OverrideServerName(string) error {
60return nil
61}
62
63// info contains the auth information for an insecure connection.
64// It implements the AuthInfo interface.
65type info struct {
66credentials.CommonAuthInfo
67}
68
69// AuthType returns the type of info as a string.
70func (info) AuthType() string {
71return "insecure"
72}
73
74// insecureBundle implements an insecure bundle.
75// An insecure bundle provides a thin wrapper around insecureTC to support
76// the credentials.Bundle interface.
77type insecureBundle struct{}
78
79// NewBundle returns a bundle with disabled transport security and no per rpc credential.
80func NewBundle() credentials.Bundle {
81return insecureBundle{}
82}
83
84// NewWithMode returns a new insecure Bundle. The mode is ignored.
85func (insecureBundle) NewWithMode(string) (credentials.Bundle, error) {
86return insecureBundle{}, nil
87}
88
89// PerRPCCredentials returns an nil implementation as insecure
90// bundle does not support a per rpc credential.
91func (insecureBundle) PerRPCCredentials() credentials.PerRPCCredentials {
92return nil
93}
94
95// TransportCredentials returns the underlying insecure transport credential.
96func (insecureBundle) TransportCredentials() credentials.TransportCredentials {
97return NewCredentials()
98}
99