s2a.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package transport
  15. import (
  16. "context"
  17. "encoding/json"
  18. "fmt"
  19. "log"
  20. "os"
  21. "strconv"
  22. "sync"
  23. "cloud.google.com/go/auth/internal/transport/cert"
  24. "cloud.google.com/go/compute/metadata"
  25. )
  26. const (
  27. configEndpointSuffix = "instance/platform-security/auto-mtls-configuration"
  28. )
  29. var (
  30. mtlsConfiguration *mtlsConfig
  31. mtlsOnce sync.Once
  32. )
  33. // GetS2AAddress returns the S2A address to be reached via plaintext connection.
  34. // Returns empty string if not set or invalid.
  35. func GetS2AAddress() string {
  36. getMetadataMTLSAutoConfig()
  37. if !mtlsConfiguration.valid() {
  38. return ""
  39. }
  40. return mtlsConfiguration.S2A.PlaintextAddress
  41. }
  42. // GetMTLSS2AAddress returns the S2A address to be reached via MTLS connection.
  43. // Returns empty string if not set or invalid.
  44. func GetMTLSS2AAddress() string {
  45. getMetadataMTLSAutoConfig()
  46. if !mtlsConfiguration.valid() {
  47. return ""
  48. }
  49. return mtlsConfiguration.S2A.MTLSAddress
  50. }
  51. // mtlsConfig contains the configuration for establishing MTLS connections with Google APIs.
  52. type mtlsConfig struct {
  53. S2A *s2aAddresses `json:"s2a"`
  54. }
  55. func (c *mtlsConfig) valid() bool {
  56. return c != nil && c.S2A != nil
  57. }
  58. // s2aAddresses contains the plaintext and/or MTLS S2A addresses.
  59. type s2aAddresses struct {
  60. // PlaintextAddress is the plaintext address to reach S2A
  61. PlaintextAddress string `json:"plaintext_address"`
  62. // MTLSAddress is the MTLS address to reach S2A
  63. MTLSAddress string `json:"mtls_address"`
  64. }
  65. func getMetadataMTLSAutoConfig() {
  66. var err error
  67. mtlsOnce.Do(func() {
  68. mtlsConfiguration, err = queryConfig()
  69. if err != nil {
  70. log.Printf("Getting MTLS config failed: %v", err)
  71. }
  72. })
  73. }
  74. var httpGetMetadataMTLSConfig = func() (string, error) {
  75. return metadata.GetWithContext(context.Background(), configEndpointSuffix)
  76. }
  77. func queryConfig() (*mtlsConfig, error) {
  78. resp, err := httpGetMetadataMTLSConfig()
  79. if err != nil {
  80. return nil, fmt.Errorf("querying MTLS config from MDS endpoint failed: %w", err)
  81. }
  82. var config mtlsConfig
  83. err = json.Unmarshal([]byte(resp), &config)
  84. if err != nil {
  85. return nil, fmt.Errorf("unmarshalling MTLS config from MDS endpoint failed: %w", err)
  86. }
  87. if config.S2A == nil {
  88. return nil, fmt.Errorf("returned MTLS config from MDS endpoint is invalid: %v", config)
  89. }
  90. return &config, nil
  91. }
  92. func shouldUseS2A(clientCertSource cert.Provider, opts *Options) bool {
  93. // If client cert is found, use that over S2A.
  94. if clientCertSource != nil {
  95. return false
  96. }
  97. // If EXPERIMENTAL_GOOGLE_API_USE_S2A is not set to true, skip S2A.
  98. if !isGoogleS2AEnabled() {
  99. return false
  100. }
  101. // If DefaultMTLSEndpoint is not set or has endpoint override, skip S2A.
  102. if opts.DefaultMTLSEndpoint == "" || opts.Endpoint != "" {
  103. return false
  104. }
  105. // If custom HTTP client is provided, skip S2A.
  106. if opts.Client != nil {
  107. return false
  108. }
  109. // If directPath is enabled, skip S2A.
  110. return !opts.EnableDirectPath && !opts.EnableDirectPathXds
  111. }
  112. func isGoogleS2AEnabled() bool {
  113. b, err := strconv.ParseBool(os.Getenv(googleAPIUseS2AEnv))
  114. if err != nil {
  115. return false
  116. }
  117. return b
  118. }