credentials.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. *
  3. * Copyright 2014, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. // Package credentials implements various credentials supported by gRPC library,
  34. // which encapsulate all the state needed by a client to authenticate with a
  35. // server and make various assertions, e.g., about the client's identity, role,
  36. // or whether it is authorized to make a particular call.
  37. package credentials // import "google.golang.org/grpc/credentials"
  38. import (
  39. "crypto/tls"
  40. "crypto/x509"
  41. "errors"
  42. "fmt"
  43. "io/ioutil"
  44. "net"
  45. "strings"
  46. "golang.org/x/net/context"
  47. )
  48. var (
  49. // alpnProtoStr are the specified application level protocols for gRPC.
  50. alpnProtoStr = []string{"h2"}
  51. )
  52. // PerRPCCredentials defines the common interface for the credentials which need to
  53. // attach security information to every RPC (e.g., oauth2).
  54. type PerRPCCredentials interface {
  55. // GetRequestMetadata gets the current request metadata, refreshing
  56. // tokens if required. This should be called by the transport layer on
  57. // each request, and the data should be populated in headers or other
  58. // context. uri is the URI of the entry point for the request. When
  59. // supported by the underlying implementation, ctx can be used for
  60. // timeout and cancellation.
  61. // TODO(zhaoq): Define the set of the qualified keys instead of leaving
  62. // it as an arbitrary string.
  63. GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error)
  64. // RequireTransportSecurity indicates whether the credentials requires
  65. // transport security.
  66. RequireTransportSecurity() bool
  67. }
  68. // ProtocolInfo provides information regarding the gRPC wire protocol version,
  69. // security protocol, security protocol version in use, server name, etc.
  70. type ProtocolInfo struct {
  71. // ProtocolVersion is the gRPC wire protocol version.
  72. ProtocolVersion string
  73. // SecurityProtocol is the security protocol in use.
  74. SecurityProtocol string
  75. // SecurityVersion is the security protocol version.
  76. SecurityVersion string
  77. // ServerName is the user-configured server name.
  78. ServerName string
  79. }
  80. // AuthInfo defines the common interface for the auth information the users are interested in.
  81. type AuthInfo interface {
  82. AuthType() string
  83. }
  84. var (
  85. // ErrConnDispatched indicates that rawConn has been dispatched out of gRPC
  86. // and the caller should not close rawConn.
  87. ErrConnDispatched = errors.New("credentials: rawConn is dispatched out of gRPC")
  88. )
  89. // TransportCredentials defines the common interface for all the live gRPC wire
  90. // protocols and supported transport security protocols (e.g., TLS, SSL).
  91. type TransportCredentials interface {
  92. // ClientHandshake does the authentication handshake specified by the corresponding
  93. // authentication protocol on rawConn for clients. It returns the authenticated
  94. // connection and the corresponding auth information about the connection.
  95. // Implementations must use the provided context to implement timely cancellation.
  96. // gRPC will try to reconnect if the error returned is a temporary error
  97. // (io.EOF, context.DeadlineExceeded or err.Temporary() == true).
  98. // If the returned error is a wrapper error, implementations should make sure that
  99. // the error implements Temporary() to have the correct retry behaviors.
  100. ClientHandshake(context.Context, string, net.Conn) (net.Conn, AuthInfo, error)
  101. // ServerHandshake does the authentication handshake for servers. It returns
  102. // the authenticated connection and the corresponding auth information about
  103. // the connection.
  104. ServerHandshake(net.Conn) (net.Conn, AuthInfo, error)
  105. // Info provides the ProtocolInfo of this TransportCredentials.
  106. Info() ProtocolInfo
  107. // Clone makes a copy of this TransportCredentials.
  108. Clone() TransportCredentials
  109. // OverrideServerName overrides the server name used to verify the hostname on the returned certificates from the server.
  110. // gRPC internals also use it to override the virtual hosting name if it is set.
  111. // It must be called before dialing. Currently, this is only used by grpclb.
  112. OverrideServerName(string) error
  113. }
  114. // TLSInfo contains the auth information for a TLS authenticated connection.
  115. // It implements the AuthInfo interface.
  116. type TLSInfo struct {
  117. State tls.ConnectionState
  118. }
  119. // AuthType returns the type of TLSInfo as a string.
  120. func (t TLSInfo) AuthType() string {
  121. return "tls"
  122. }
  123. // tlsCreds is the credentials required for authenticating a connection using TLS.
  124. type tlsCreds struct {
  125. // TLS configuration
  126. config *tls.Config
  127. }
  128. func (c tlsCreds) Info() ProtocolInfo {
  129. return ProtocolInfo{
  130. SecurityProtocol: "tls",
  131. SecurityVersion: "1.2",
  132. ServerName: c.config.ServerName,
  133. }
  134. }
  135. func (c *tlsCreds) ClientHandshake(ctx context.Context, addr string, rawConn net.Conn) (_ net.Conn, _ AuthInfo, err error) {
  136. // use local cfg to avoid clobbering ServerName if using multiple endpoints
  137. cfg := cloneTLSConfig(c.config)
  138. if cfg.ServerName == "" {
  139. colonPos := strings.LastIndex(addr, ":")
  140. if colonPos == -1 {
  141. colonPos = len(addr)
  142. }
  143. cfg.ServerName = addr[:colonPos]
  144. }
  145. conn := tls.Client(rawConn, cfg)
  146. errChannel := make(chan error, 1)
  147. go func() {
  148. errChannel <- conn.Handshake()
  149. }()
  150. select {
  151. case err := <-errChannel:
  152. if err != nil {
  153. return nil, nil, err
  154. }
  155. case <-ctx.Done():
  156. return nil, nil, ctx.Err()
  157. }
  158. return conn, TLSInfo{conn.ConnectionState()}, nil
  159. }
  160. func (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. func (c *tlsCreds) Clone() TransportCredentials {
  168. return NewTLS(c.config)
  169. }
  170. func (c *tlsCreds) OverrideServerName(serverNameOverride string) error {
  171. c.config.ServerName = serverNameOverride
  172. return nil
  173. }
  174. // NewTLS uses c to construct a TransportCredentials based on TLS.
  175. func NewTLS(c *tls.Config) TransportCredentials {
  176. tc := &tlsCreds{cloneTLSConfig(c)}
  177. tc.config.NextProtos = alpnProtoStr
  178. return tc
  179. }
  180. // NewClientTLSFromCert constructs TLS credentials from the input certificate for client.
  181. // serverNameOverride is for testing only. If set to a non empty string,
  182. // it will override the virtual host name of authority (e.g. :authority header field) in requests.
  183. func NewClientTLSFromCert(cp *x509.CertPool, serverNameOverride string) TransportCredentials {
  184. return NewTLS(&tls.Config{ServerName: serverNameOverride, RootCAs: cp})
  185. }
  186. // NewClientTLSFromFile constructs TLS credentials from the input certificate file for client.
  187. // serverNameOverride is for testing only. If set to a non empty string,
  188. // it will override the virtual host name of authority (e.g. :authority header field) in requests.
  189. func NewClientTLSFromFile(certFile, serverNameOverride string) (TransportCredentials, error) {
  190. b, err := ioutil.ReadFile(certFile)
  191. if err != nil {
  192. return nil, err
  193. }
  194. cp := x509.NewCertPool()
  195. if !cp.AppendCertsFromPEM(b) {
  196. return nil, fmt.Errorf("credentials: failed to append certificates")
  197. }
  198. return NewTLS(&tls.Config{ServerName: serverNameOverride, RootCAs: cp}), nil
  199. }
  200. // NewServerTLSFromCert constructs TLS credentials from the input certificate for server.
  201. func NewServerTLSFromCert(cert *tls.Certificate) TransportCredentials {
  202. return NewTLS(&tls.Config{Certificates: []tls.Certificate{*cert}})
  203. }
  204. // NewServerTLSFromFile constructs TLS credentials from the input certificate file and key
  205. // file for server.
  206. func NewServerTLSFromFile(certFile, keyFile string) (TransportCredentials, error) {
  207. cert, err := tls.LoadX509KeyPair(certFile, keyFile)
  208. if err != nil {
  209. return nil, err
  210. }
  211. return NewTLS(&tls.Config{Certificates: []tls.Certificate{cert}}), nil
  212. }