aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/google.golang.org/grpc/credentials
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/google.golang.org/grpc/credentials')
-rw-r--r--vendor/google.golang.org/grpc/credentials/credentials.go219
-rw-r--r--vendor/google.golang.org/grpc/credentials/credentials_util_go17.go60
-rw-r--r--vendor/google.golang.org/grpc/credentials/credentials_util_go18.go38
-rw-r--r--vendor/google.golang.org/grpc/credentials/credentials_util_pre_go17.go57
4 files changed, 374 insertions, 0 deletions
diff --git a/vendor/google.golang.org/grpc/credentials/credentials.go b/vendor/google.golang.org/grpc/credentials/credentials.go
new file mode 100644
index 0000000..2475fe8
--- /dev/null
+++ b/vendor/google.golang.org/grpc/credentials/credentials.go
@@ -0,0 +1,219 @@
1/*
2 *
3 * Copyright 2014 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 credentials implements various credentials supported by gRPC library,
20// which encapsulate all the state needed by a client to authenticate with a
21// server and make various assertions, e.g., about the client's identity, role,
22// or whether it is authorized to make a particular call.
23package credentials // import "google.golang.org/grpc/credentials"
24
25import (
26 "crypto/tls"
27 "crypto/x509"
28 "errors"
29 "fmt"
30 "io/ioutil"
31 "net"
32 "strings"
33
34 "golang.org/x/net/context"
35)
36
37var (
38 // alpnProtoStr are the specified application level protocols for gRPC.
39 alpnProtoStr = []string{"h2"}
40)
41
42// PerRPCCredentials defines the common interface for the credentials which need to
43// attach security information to every RPC (e.g., oauth2).
44type PerRPCCredentials interface {
45 // GetRequestMetadata gets the current request metadata, refreshing
46 // tokens if required. This should be called by the transport layer on
47 // each request, and the data should be populated in headers or other
48 // context. uri is the URI of the entry point for the request. When
49 // supported by the underlying implementation, ctx can be used for
50 // timeout and cancellation.
51 // TODO(zhaoq): Define the set of the qualified keys instead of leaving
52 // it as an arbitrary string.
53 GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error)
54 // RequireTransportSecurity indicates whether the credentials requires
55 // transport security.
56 RequireTransportSecurity() bool
57}
58
59// ProtocolInfo provides information regarding the gRPC wire protocol version,
60// security protocol, security protocol version in use, server name, etc.
61type ProtocolInfo struct {
62 // ProtocolVersion is the gRPC wire protocol version.
63 ProtocolVersion string
64 // SecurityProtocol is the security protocol in use.
65 SecurityProtocol string
66 // SecurityVersion is the security protocol version.
67 SecurityVersion string
68 // ServerName is the user-configured server name.
69 ServerName string
70}
71
72// AuthInfo defines the common interface for the auth information the users are interested in.
73type AuthInfo interface {
74 AuthType() string
75}
76
77var (
78 // ErrConnDispatched indicates that rawConn has been dispatched out of gRPC
79 // and the caller should not close rawConn.
80 ErrConnDispatched = errors.New("credentials: rawConn is dispatched out of gRPC")
81)
82
83// TransportCredentials defines the common interface for all the live gRPC wire
84// protocols and supported transport security protocols (e.g., TLS, SSL).
85type TransportCredentials interface {
86 // ClientHandshake does the authentication handshake specified by the corresponding
87 // authentication protocol on rawConn for clients. It returns the authenticated
88 // connection and the corresponding auth information about the connection.
89 // Implementations must use the provided context to implement timely cancellation.
90 // gRPC will try to reconnect if the error returned is a temporary error
91 // (io.EOF, context.DeadlineExceeded or err.Temporary() == true).
92 // If the returned error is a wrapper error, implementations should make sure that
93 // the error implements Temporary() to have the correct retry behaviors.
94 ClientHandshake(context.Context, string, net.Conn) (net.Conn, AuthInfo, error)
95 // ServerHandshake does the authentication handshake for servers. It returns
96 // the authenticated connection and the corresponding auth information about
97 // the connection.
98 ServerHandshake(net.Conn) (net.Conn, AuthInfo, error)
99 // Info provides the ProtocolInfo of this TransportCredentials.
100 Info() ProtocolInfo
101 // Clone makes a copy of this TransportCredentials.
102 Clone() TransportCredentials
103 // OverrideServerName overrides the server name used to verify the hostname on the returned certificates from the server.
104 // gRPC internals also use it to override the virtual hosting name if it is set.
105 // It must be called before dialing. Currently, this is only used by grpclb.
106 OverrideServerName(string) error
107}
108
109// TLSInfo contains the auth information for a TLS authenticated connection.
110// It implements the AuthInfo interface.
111type TLSInfo struct {
112 State tls.ConnectionState
113}
114
115// AuthType returns the type of TLSInfo as a string.
116func (t TLSInfo) AuthType() string {
117 return "tls"
118}
119
120// tlsCreds is the credentials required for authenticating a connection using TLS.
121type tlsCreds struct {
122 // TLS configuration
123 config *tls.Config
124}
125
126func (c tlsCreds) Info() ProtocolInfo {
127 return ProtocolInfo{
128 SecurityProtocol: "tls",
129 SecurityVersion: "1.2",
130 ServerName: c.config.ServerName,
131 }
132}
133
134func (c *tlsCreds) ClientHandshake(ctx context.Context, addr string, rawConn net.Conn) (_ net.Conn, _ AuthInfo, err error) {
135 // use local cfg to avoid clobbering ServerName if using multiple endpoints
136 cfg := cloneTLSConfig(c.config)
137 if cfg.ServerName == "" {
138 colonPos := strings.LastIndex(addr, ":")
139 if colonPos == -1 {
140 colonPos = len(addr)
141 }
142 cfg.ServerName = addr[:colonPos]
143 }
144 conn := tls.Client(rawConn, cfg)
145 errChannel := make(chan error, 1)
146 go func() {
147 errChannel <- conn.Handshake()
148 }()
149 select {
150 case err := <-errChannel:
151 if err != nil {
152 return nil, nil, err
153 }
154 case <-ctx.Done():
155 return nil, nil, ctx.Err()
156 }
157 return conn, TLSInfo{conn.ConnectionState()}, nil
158}
159
160func (c *tlsCreds) ServerHandshake(rawConn net.Conn) (net.Conn, AuthInfo, error) {
161 conn := tls.Server(rawConn, c.config)
162 if err := conn.Handshake(); err != nil {
163 return nil, nil, err
164 }
165 return conn, TLSInfo{conn.ConnectionState()}, nil
166}
167
168func (c *tlsCreds) Clone() TransportCredentials {
169 return NewTLS(c.config)
170}
171
172func (c *tlsCreds) OverrideServerName(serverNameOverride string) error {
173 c.config.ServerName = serverNameOverride
174 return nil
175}
176
177// NewTLS uses c to construct a TransportCredentials based on TLS.
178func NewTLS(c *tls.Config) TransportCredentials {
179 tc := &tlsCreds{cloneTLSConfig(c)}
180 tc.config.NextProtos = alpnProtoStr
181 return tc
182}
183
184// NewClientTLSFromCert constructs TLS credentials from the input certificate for client.
185// serverNameOverride is for testing only. If set to a non empty string,
186// it will override the virtual host name of authority (e.g. :authority header field) in requests.
187func NewClientTLSFromCert(cp *x509.CertPool, serverNameOverride string) TransportCredentials {
188 return NewTLS(&tls.Config{ServerName: serverNameOverride, RootCAs: cp})
189}
190
191// NewClientTLSFromFile constructs TLS credentials from the input certificate file for client.
192// serverNameOverride is for testing only. If set to a non empty string,
193// it will override the virtual host name of authority (e.g. :authority header field) in requests.
194func NewClientTLSFromFile(certFile, serverNameOverride string) (TransportCredentials, error) {
195 b, err := ioutil.ReadFile(certFile)
196 if err != nil {
197 return nil, err
198 }
199 cp := x509.NewCertPool()
200 if !cp.AppendCertsFromPEM(b) {
201 return nil, fmt.Errorf("credentials: failed to append certificates")
202 }
203 return NewTLS(&tls.Config{ServerName: serverNameOverride, RootCAs: cp}), nil
204}
205
206// NewServerTLSFromCert constructs TLS credentials from the input certificate for server.
207func NewServerTLSFromCert(cert *tls.Certificate) TransportCredentials {
208 return NewTLS(&tls.Config{Certificates: []tls.Certificate{*cert}})
209}
210
211// NewServerTLSFromFile constructs TLS credentials from the input certificate file and key
212// file for server.
213func NewServerTLSFromFile(certFile, keyFile string) (TransportCredentials, error) {
214 cert, err := tls.LoadX509KeyPair(certFile, keyFile)
215 if err != nil {
216 return nil, err
217 }
218 return NewTLS(&tls.Config{Certificates: []tls.Certificate{cert}}), nil
219}
diff --git a/vendor/google.golang.org/grpc/credentials/credentials_util_go17.go b/vendor/google.golang.org/grpc/credentials/credentials_util_go17.go
new file mode 100644
index 0000000..60409aa
--- /dev/null
+++ b/vendor/google.golang.org/grpc/credentials/credentials_util_go17.go
@@ -0,0 +1,60 @@
1// +build go1.7
2// +build !go1.8
3
4/*
5 *
6 * Copyright 2016 gRPC authors.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22package credentials
23
24import (
25 "crypto/tls"
26)
27
28// cloneTLSConfig returns a shallow clone of the exported
29// fields of cfg, ignoring the unexported sync.Once, which
30// contains a mutex and must not be copied.
31//
32// If cfg is nil, a new zero tls.Config is returned.
33func cloneTLSConfig(cfg *tls.Config) *tls.Config {
34 if cfg == nil {
35 return &tls.Config{}
36 }
37 return &tls.Config{
38 Rand: cfg.Rand,
39 Time: cfg.Time,
40 Certificates: cfg.Certificates,
41 NameToCertificate: cfg.NameToCertificate,
42 GetCertificate: cfg.GetCertificate,
43 RootCAs: cfg.RootCAs,
44 NextProtos: cfg.NextProtos,
45 ServerName: cfg.ServerName,
46 ClientAuth: cfg.ClientAuth,
47 ClientCAs: cfg.ClientCAs,
48 InsecureSkipVerify: cfg.InsecureSkipVerify,
49 CipherSuites: cfg.CipherSuites,
50 PreferServerCipherSuites: cfg.PreferServerCipherSuites,
51 SessionTicketsDisabled: cfg.SessionTicketsDisabled,
52 SessionTicketKey: cfg.SessionTicketKey,
53 ClientSessionCache: cfg.ClientSessionCache,
54 MinVersion: cfg.MinVersion,
55 MaxVersion: cfg.MaxVersion,
56 CurvePreferences: cfg.CurvePreferences,
57 DynamicRecordSizingDisabled: cfg.DynamicRecordSizingDisabled,
58 Renegotiation: cfg.Renegotiation,
59 }
60}
diff --git a/vendor/google.golang.org/grpc/credentials/credentials_util_go18.go b/vendor/google.golang.org/grpc/credentials/credentials_util_go18.go
new file mode 100644
index 0000000..93f0e1d
--- /dev/null
+++ b/vendor/google.golang.org/grpc/credentials/credentials_util_go18.go
@@ -0,0 +1,38 @@
1// +build go1.8
2
3/*
4 *
5 * Copyright 2017 gRPC authors.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 */
20
21package credentials
22
23import (
24 "crypto/tls"
25)
26
27// cloneTLSConfig returns a shallow clone of the exported
28// fields of cfg, ignoring the unexported sync.Once, which
29// contains a mutex and must not be copied.
30//
31// If cfg is nil, a new zero tls.Config is returned.
32func cloneTLSConfig(cfg *tls.Config) *tls.Config {
33 if cfg == nil {
34 return &tls.Config{}
35 }
36
37 return cfg.Clone()
38}
diff --git a/vendor/google.golang.org/grpc/credentials/credentials_util_pre_go17.go b/vendor/google.golang.org/grpc/credentials/credentials_util_pre_go17.go
new file mode 100644
index 0000000..d6bbcc9
--- /dev/null
+++ b/vendor/google.golang.org/grpc/credentials/credentials_util_pre_go17.go
@@ -0,0 +1,57 @@
1// +build !go1.7
2
3/*
4 *
5 * Copyright 2016 gRPC authors.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 */
20
21package credentials
22
23import (
24 "crypto/tls"
25)
26
27// cloneTLSConfig returns a shallow clone of the exported
28// fields of cfg, ignoring the unexported sync.Once, which
29// contains a mutex and must not be copied.
30//
31// If cfg is nil, a new zero tls.Config is returned.
32func cloneTLSConfig(cfg *tls.Config) *tls.Config {
33 if cfg == nil {
34 return &tls.Config{}
35 }
36 return &tls.Config{
37 Rand: cfg.Rand,
38 Time: cfg.Time,
39 Certificates: cfg.Certificates,
40 NameToCertificate: cfg.NameToCertificate,
41 GetCertificate: cfg.GetCertificate,
42 RootCAs: cfg.RootCAs,
43 NextProtos: cfg.NextProtos,
44 ServerName: cfg.ServerName,
45 ClientAuth: cfg.ClientAuth,
46 ClientCAs: cfg.ClientCAs,
47 InsecureSkipVerify: cfg.InsecureSkipVerify,
48 CipherSuites: cfg.CipherSuites,
49 PreferServerCipherSuites: cfg.PreferServerCipherSuites,
50 SessionTicketsDisabled: cfg.SessionTicketsDisabled,
51 SessionTicketKey: cfg.SessionTicketKey,
52 ClientSessionCache: cfg.ClientSessionCache,
53 MinVersion: cfg.MinVersion,
54 MaxVersion: cfg.MaxVersion,
55 CurvePreferences: cfg.CurvePreferences,
56 }
57}