cba.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // Copyright 2020 Google LLC.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // cba.go (certificate-based access) contains utils for implementing Device Certificate
  5. // Authentication according to https://google.aip.dev/auth/4114 and Default Credentials
  6. // for Google Cloud Virtual Environments according to https://google.aip.dev/auth/4115.
  7. //
  8. // The overall logic for DCA is as follows:
  9. // 1. If both endpoint override and client certificate are specified, use them as is.
  10. // 2. If user does not specify client certificate, we will attempt to use default
  11. // client certificate.
  12. // 3. If user does not specify endpoint override, we will use defaultMtlsEndpoint if
  13. // client certificate is available and defaultEndpoint otherwise.
  14. //
  15. // Implications of the above logic:
  16. // 1. If the user specifies a non-mTLS endpoint override but client certificate is
  17. // available, we will pass along the cert anyway and let the server decide what to do.
  18. // 2. If the user specifies an mTLS endpoint override but client certificate is not
  19. // available, we will not fail-fast, but let backend throw error when connecting.
  20. //
  21. // If running within Google's cloud environment, and client certificate is not specified
  22. // and not available through DCA, we will try mTLS with credentials held by
  23. // the Secure Session Agent, which is part of Google's cloud infrastructure.
  24. //
  25. // We would like to avoid introducing client-side logic that parses whether the
  26. // endpoint override is an mTLS url, since the url pattern may change at anytime.
  27. //
  28. // This package is not intended for use by end developers. Use the
  29. // google.golang.org/api/option package to configure API clients.
  30. // Package internal supports the options and transport packages.
  31. package internal
  32. import (
  33. "context"
  34. "crypto/tls"
  35. "errors"
  36. "net"
  37. "net/url"
  38. "os"
  39. "strings"
  40. "github.com/google/s2a-go"
  41. "github.com/google/s2a-go/fallback"
  42. "google.golang.org/api/internal/cert"
  43. "google.golang.org/grpc/credentials"
  44. )
  45. const (
  46. mTLSModeAlways = "always"
  47. mTLSModeNever = "never"
  48. mTLSModeAuto = "auto"
  49. // Experimental: if true, the code will try MTLS with S2A as the default for transport security. Default value is false.
  50. googleAPIUseS2AEnv = "EXPERIMENTAL_GOOGLE_API_USE_S2A"
  51. universeDomainPlaceholder = "UNIVERSE_DOMAIN"
  52. )
  53. var (
  54. errUniverseNotSupportedMTLS = errors.New("mTLS is not supported in any universe other than googleapis.com")
  55. )
  56. // getClientCertificateSourceAndEndpoint is a convenience function that invokes
  57. // getClientCertificateSource and getEndpoint sequentially and returns the client
  58. // cert source and endpoint as a tuple.
  59. func getClientCertificateSourceAndEndpoint(settings *DialSettings) (cert.Source, string, error) {
  60. clientCertSource, err := getClientCertificateSource(settings)
  61. if err != nil {
  62. return nil, "", err
  63. }
  64. endpoint, err := getEndpoint(settings, clientCertSource)
  65. if err != nil {
  66. return nil, "", err
  67. }
  68. // TODO(chrisdsmith): https://github.com/googleapis/google-api-go-client/issues/2359
  69. if settings.Endpoint == "" && !settings.IsUniverseDomainGDU() && settings.DefaultEndpointTemplate != "" {
  70. // TODO(chrisdsmith): https://github.com/googleapis/google-api-go-client/issues/2359
  71. // if settings.DefaultEndpointTemplate == "" {
  72. // return nil, "", errors.New("internaloption.WithDefaultEndpointTemplate is required if option.WithUniverseDomain is not googleapis.com")
  73. // }
  74. endpoint = resolvedDefaultEndpoint(settings)
  75. }
  76. return clientCertSource, endpoint, nil
  77. }
  78. type transportConfig struct {
  79. clientCertSource cert.Source // The client certificate source.
  80. endpoint string // The corresponding endpoint to use based on client certificate source.
  81. s2aAddress string // The S2A address if it can be used, otherwise an empty string.
  82. s2aMTLSEndpoint string // The MTLS endpoint to use with S2A.
  83. }
  84. func getTransportConfig(settings *DialSettings) (*transportConfig, error) {
  85. clientCertSource, endpoint, err := getClientCertificateSourceAndEndpoint(settings)
  86. if err != nil {
  87. return nil, err
  88. }
  89. defaultTransportConfig := transportConfig{
  90. clientCertSource: clientCertSource,
  91. endpoint: endpoint,
  92. s2aAddress: "",
  93. s2aMTLSEndpoint: "",
  94. }
  95. if !shouldUseS2A(clientCertSource, settings) {
  96. return &defaultTransportConfig, nil
  97. }
  98. if !settings.IsUniverseDomainGDU() {
  99. return nil, errUniverseNotSupportedMTLS
  100. }
  101. s2aAddress := GetS2AAddress()
  102. if s2aAddress == "" {
  103. return &defaultTransportConfig, nil
  104. }
  105. return &transportConfig{
  106. clientCertSource: clientCertSource,
  107. endpoint: endpoint,
  108. s2aAddress: s2aAddress,
  109. s2aMTLSEndpoint: settings.DefaultMTLSEndpoint,
  110. }, nil
  111. }
  112. // getClientCertificateSource returns a default client certificate source, if
  113. // not provided by the user.
  114. //
  115. // A nil default source can be returned if the source does not exist. Any exceptions
  116. // encountered while initializing the default source will be reported as client
  117. // error (ex. corrupt metadata file).
  118. //
  119. // Important Note: For now, the environment variable GOOGLE_API_USE_CLIENT_CERTIFICATE
  120. // must be set to "true" to allow certificate to be used (including user provided
  121. // certificates). For details, see AIP-4114.
  122. func getClientCertificateSource(settings *DialSettings) (cert.Source, error) {
  123. if !isClientCertificateEnabled() {
  124. return nil, nil
  125. } else if settings.ClientCertSource != nil {
  126. return settings.ClientCertSource, nil
  127. } else {
  128. return cert.DefaultSource()
  129. }
  130. }
  131. func isClientCertificateEnabled() bool {
  132. useClientCert := os.Getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE")
  133. // TODO(andyrzhao): Update default to return "true" after DCA feature is fully released.
  134. return strings.ToLower(useClientCert) == "true"
  135. }
  136. // getEndpoint returns the endpoint for the service, taking into account the
  137. // user-provided endpoint override "settings.Endpoint".
  138. //
  139. // If no endpoint override is specified, we will either return the default endpoint or
  140. // the default mTLS endpoint if a client certificate is available.
  141. //
  142. // You can override the default endpoint choice (mtls vs. regular) by setting the
  143. // GOOGLE_API_USE_MTLS_ENDPOINT environment variable.
  144. //
  145. // If the endpoint override is an address (host:port) rather than full base
  146. // URL (ex. https://...), then the user-provided address will be merged into
  147. // the default endpoint. For example, WithEndpoint("myhost:8000") and
  148. // WithDefaultEndpoint("https://foo.com/bar/baz") will return "https://myhost:8080/bar/baz"
  149. func getEndpoint(settings *DialSettings, clientCertSource cert.Source) (string, error) {
  150. if settings.Endpoint == "" {
  151. if isMTLS(clientCertSource) {
  152. if !settings.IsUniverseDomainGDU() {
  153. return "", errUniverseNotSupportedMTLS
  154. }
  155. return settings.DefaultMTLSEndpoint, nil
  156. }
  157. return resolvedDefaultEndpoint(settings), nil
  158. }
  159. if strings.Contains(settings.Endpoint, "://") {
  160. // User passed in a full URL path, use it verbatim.
  161. return settings.Endpoint, nil
  162. }
  163. if resolvedDefaultEndpoint(settings) == "" {
  164. // If DefaultEndpoint is not configured, use the user provided endpoint verbatim.
  165. // This allows a naked "host[:port]" URL to be used with GRPC Direct Path.
  166. return settings.Endpoint, nil
  167. }
  168. // Assume user-provided endpoint is host[:port], merge it with the default endpoint.
  169. return mergeEndpoints(resolvedDefaultEndpoint(settings), settings.Endpoint)
  170. }
  171. func isMTLS(clientCertSource cert.Source) bool {
  172. mtlsMode := getMTLSMode()
  173. return mtlsMode == mTLSModeAlways || (clientCertSource != nil && mtlsMode == mTLSModeAuto)
  174. }
  175. // resolvedDefaultEndpoint returns the DefaultEndpointTemplate merged with the
  176. // Universe Domain if the DefaultEndpointTemplate is set, otherwise returns the
  177. // deprecated DefaultEndpoint value.
  178. func resolvedDefaultEndpoint(settings *DialSettings) string {
  179. if settings.DefaultEndpointTemplate == "" {
  180. return settings.DefaultEndpoint
  181. }
  182. return strings.Replace(settings.DefaultEndpointTemplate, universeDomainPlaceholder, settings.GetUniverseDomain(), 1)
  183. }
  184. func getMTLSMode() string {
  185. mode := os.Getenv("GOOGLE_API_USE_MTLS_ENDPOINT")
  186. if mode == "" {
  187. mode = os.Getenv("GOOGLE_API_USE_MTLS") // Deprecated.
  188. }
  189. if mode == "" {
  190. return mTLSModeAuto
  191. }
  192. return strings.ToLower(mode)
  193. }
  194. func mergeEndpoints(baseURL, newHost string) (string, error) {
  195. u, err := url.Parse(fixScheme(baseURL))
  196. if err != nil {
  197. return "", err
  198. }
  199. return strings.Replace(baseURL, u.Host, newHost, 1), nil
  200. }
  201. func fixScheme(baseURL string) string {
  202. if !strings.Contains(baseURL, "://") {
  203. return "https://" + baseURL
  204. }
  205. return baseURL
  206. }
  207. // GetGRPCTransportConfigAndEndpoint returns an instance of credentials.TransportCredentials, and the
  208. // corresponding endpoint to use for GRPC client.
  209. func GetGRPCTransportConfigAndEndpoint(settings *DialSettings) (credentials.TransportCredentials, string, error) {
  210. config, err := getTransportConfig(settings)
  211. if err != nil {
  212. return nil, "", err
  213. }
  214. defaultTransportCreds := credentials.NewTLS(&tls.Config{
  215. GetClientCertificate: config.clientCertSource,
  216. })
  217. if config.s2aAddress == "" {
  218. return defaultTransportCreds, config.endpoint, nil
  219. }
  220. var fallbackOpts *s2a.FallbackOptions
  221. // In case of S2A failure, fall back to the endpoint that would've been used without S2A.
  222. if fallbackHandshake, err := fallback.DefaultFallbackClientHandshakeFunc(config.endpoint); err == nil {
  223. fallbackOpts = &s2a.FallbackOptions{
  224. FallbackClientHandshakeFunc: fallbackHandshake,
  225. }
  226. }
  227. s2aTransportCreds, err := s2a.NewClientCreds(&s2a.ClientOptions{
  228. S2AAddress: config.s2aAddress,
  229. FallbackOpts: fallbackOpts,
  230. })
  231. if err != nil {
  232. // Use default if we cannot initialize S2A client transport credentials.
  233. return defaultTransportCreds, config.endpoint, nil
  234. }
  235. return s2aTransportCreds, config.s2aMTLSEndpoint, nil
  236. }
  237. // GetHTTPTransportConfigAndEndpoint returns a client certificate source, a function for dialing MTLS with S2A,
  238. // and the endpoint to use for HTTP client.
  239. func GetHTTPTransportConfigAndEndpoint(settings *DialSettings) (cert.Source, func(context.Context, string, string) (net.Conn, error), string, error) {
  240. config, err := getTransportConfig(settings)
  241. if err != nil {
  242. return nil, nil, "", err
  243. }
  244. if config.s2aAddress == "" {
  245. return config.clientCertSource, nil, config.endpoint, nil
  246. }
  247. var fallbackOpts *s2a.FallbackOptions
  248. // In case of S2A failure, fall back to the endpoint that would've been used without S2A.
  249. if fallbackURL, err := url.Parse(config.endpoint); err == nil {
  250. if fallbackDialer, fallbackServerAddr, err := fallback.DefaultFallbackDialerAndAddress(fallbackURL.Hostname()); err == nil {
  251. fallbackOpts = &s2a.FallbackOptions{
  252. FallbackDialer: &s2a.FallbackDialer{
  253. Dialer: fallbackDialer,
  254. ServerAddr: fallbackServerAddr,
  255. },
  256. }
  257. }
  258. }
  259. dialTLSContextFunc := s2a.NewS2ADialTLSContextFunc(&s2a.ClientOptions{
  260. S2AAddress: config.s2aAddress,
  261. FallbackOpts: fallbackOpts,
  262. })
  263. return nil, dialTLSContextFunc, config.s2aMTLSEndpoint, nil
  264. }
  265. func shouldUseS2A(clientCertSource cert.Source, settings *DialSettings) bool {
  266. // If client cert is found, use that over S2A.
  267. if clientCertSource != nil {
  268. return false
  269. }
  270. // If EXPERIMENTAL_GOOGLE_API_USE_S2A is not set to true, skip S2A.
  271. if !isGoogleS2AEnabled() {
  272. return false
  273. }
  274. // If DefaultMTLSEndpoint is not set or has endpoint override, skip S2A.
  275. if settings.DefaultMTLSEndpoint == "" || settings.Endpoint != "" {
  276. return false
  277. }
  278. // If custom HTTP client is provided, skip S2A.
  279. if settings.HTTPClient != nil {
  280. return false
  281. }
  282. return !settings.EnableDirectPath && !settings.EnableDirectPathXds
  283. }
  284. func isGoogleS2AEnabled() bool {
  285. return strings.ToLower(os.Getenv(googleAPIUseS2AEnv)) == "true"
  286. }