credentials.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // Package credentials implements various credentials supported by gRPC library,
  19. // which encapsulate all the state needed by a client to authenticate with a
  20. // server and make various assertions, e.g., about the client's identity, role,
  21. // or whether it is authorized to make a particular call.
  22. package credentials // import "google.golang.org/grpc/credentials"
  23. import (
  24. "context"
  25. "errors"
  26. "net"
  27. "github.com/golang/protobuf/proto"
  28. "google.golang.org/grpc/internal"
  29. )
  30. // PerRPCCredentials defines the common interface for the credentials which need to
  31. // attach security information to every RPC (e.g., oauth2).
  32. type PerRPCCredentials interface {
  33. // GetRequestMetadata gets the current request metadata, refreshing
  34. // tokens if required. This should be called by the transport layer on
  35. // each request, and the data should be populated in headers or other
  36. // context. If a status code is returned, it will be used as the status
  37. // for the RPC. uri is the URI of the entry point for the request.
  38. // When supported by the underlying implementation, ctx can be used for
  39. // timeout and cancellation. Additionally, RequestInfo data will be
  40. // available via ctx to this call.
  41. // TODO(zhaoq): Define the set of the qualified keys instead of leaving
  42. // it as an arbitrary string.
  43. GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error)
  44. // RequireTransportSecurity indicates whether the credentials requires
  45. // transport security.
  46. RequireTransportSecurity() bool
  47. }
  48. // ProtocolInfo provides information regarding the gRPC wire protocol version,
  49. // security protocol, security protocol version in use, server name, etc.
  50. type ProtocolInfo struct {
  51. // ProtocolVersion is the gRPC wire protocol version.
  52. ProtocolVersion string
  53. // SecurityProtocol is the security protocol in use.
  54. SecurityProtocol string
  55. // SecurityVersion is the security protocol version.
  56. SecurityVersion string
  57. // ServerName is the user-configured server name.
  58. ServerName string
  59. }
  60. // AuthInfo defines the common interface for the auth information the users are interested in.
  61. type AuthInfo interface {
  62. AuthType() string
  63. }
  64. // ErrConnDispatched indicates that rawConn has been dispatched out of gRPC
  65. // and the caller should not close rawConn.
  66. var ErrConnDispatched = errors.New("credentials: rawConn is dispatched out of gRPC")
  67. // TransportCredentials defines the common interface for all the live gRPC wire
  68. // protocols and supported transport security protocols (e.g., TLS, SSL).
  69. type TransportCredentials interface {
  70. // ClientHandshake does the authentication handshake specified by the corresponding
  71. // authentication protocol on rawConn for clients. It returns the authenticated
  72. // connection and the corresponding auth information about the connection.
  73. // Implementations must use the provided context to implement timely cancellation.
  74. // gRPC will try to reconnect if the error returned is a temporary error
  75. // (io.EOF, context.DeadlineExceeded or err.Temporary() == true).
  76. // If the returned error is a wrapper error, implementations should make sure that
  77. // the error implements Temporary() to have the correct retry behaviors.
  78. //
  79. // If the returned net.Conn is closed, it MUST close the net.Conn provided.
  80. ClientHandshake(context.Context, string, net.Conn) (net.Conn, AuthInfo, error)
  81. // ServerHandshake does the authentication handshake for servers. It returns
  82. // the authenticated connection and the corresponding auth information about
  83. // the connection.
  84. //
  85. // If the returned net.Conn is closed, it MUST close the net.Conn provided.
  86. ServerHandshake(net.Conn) (net.Conn, AuthInfo, error)
  87. // Info provides the ProtocolInfo of this TransportCredentials.
  88. Info() ProtocolInfo
  89. // Clone makes a copy of this TransportCredentials.
  90. Clone() TransportCredentials
  91. // OverrideServerName overrides the server name used to verify the hostname on the returned certificates from the server.
  92. // gRPC internals also use it to override the virtual hosting name if it is set.
  93. // It must be called before dialing. Currently, this is only used by grpclb.
  94. OverrideServerName(string) error
  95. }
  96. // Bundle is a combination of TransportCredentials and PerRPCCredentials.
  97. //
  98. // It also contains a mode switching method, so it can be used as a combination
  99. // of different credential policies.
  100. //
  101. // Bundle cannot be used together with individual TransportCredentials.
  102. // PerRPCCredentials from Bundle will be appended to other PerRPCCredentials.
  103. //
  104. // This API is experimental.
  105. type Bundle interface {
  106. TransportCredentials() TransportCredentials
  107. PerRPCCredentials() PerRPCCredentials
  108. // NewWithMode should make a copy of Bundle, and switch mode. Modifying the
  109. // existing Bundle may cause races.
  110. //
  111. // NewWithMode returns nil if the requested mode is not supported.
  112. NewWithMode(mode string) (Bundle, error)
  113. }
  114. // RequestInfo contains request data attached to the context passed to GetRequestMetadata calls.
  115. //
  116. // This API is experimental.
  117. type RequestInfo struct {
  118. // The method passed to Invoke or NewStream for this RPC. (For proto methods, this has the format "/some.Service/Method")
  119. Method string
  120. }
  121. // requestInfoKey is a struct to be used as the key when attaching a RequestInfo to a context object.
  122. type requestInfoKey struct{}
  123. // RequestInfoFromContext extracts the RequestInfo from the context if it exists.
  124. //
  125. // This API is experimental.
  126. func RequestInfoFromContext(ctx context.Context) (ri RequestInfo, ok bool) {
  127. ri, ok = ctx.Value(requestInfoKey{}).(RequestInfo)
  128. return
  129. }
  130. func init() {
  131. internal.NewRequestInfoContext = func(ctx context.Context, ri RequestInfo) context.Context {
  132. return context.WithValue(ctx, requestInfoKey{}, ri)
  133. }
  134. }
  135. // ChannelzSecurityInfo defines the interface that security protocols should implement
  136. // in order to provide security info to channelz.
  137. //
  138. // This API is experimental.
  139. type ChannelzSecurityInfo interface {
  140. GetSecurityValue() ChannelzSecurityValue
  141. }
  142. // ChannelzSecurityValue defines the interface that GetSecurityValue() return value
  143. // should satisfy. This interface should only be satisfied by *TLSChannelzSecurityValue
  144. // and *OtherChannelzSecurityValue.
  145. //
  146. // This API is experimental.
  147. type ChannelzSecurityValue interface {
  148. isChannelzSecurityValue()
  149. }
  150. // OtherChannelzSecurityValue defines the struct that non-TLS protocol should return
  151. // from GetSecurityValue(), which contains protocol specific security info. Note
  152. // the Value field will be sent to users of channelz requesting channel info, and
  153. // thus sensitive info should better be avoided.
  154. //
  155. // This API is experimental.
  156. type OtherChannelzSecurityValue struct {
  157. ChannelzSecurityValue
  158. Name string
  159. Value proto.Message
  160. }