s2a.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2023 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. package internal
  5. import (
  6. "encoding/json"
  7. "log"
  8. "sync"
  9. "time"
  10. "cloud.google.com/go/compute/metadata"
  11. )
  12. const configEndpointSuffix = "googleAutoMtlsConfiguration"
  13. // The period an MTLS config can be reused before needing refresh.
  14. var configExpiry = time.Hour
  15. // GetS2AAddress returns the S2A address to be reached via plaintext connection.
  16. func GetS2AAddress() string {
  17. c, err := getMetadataMTLSAutoConfig().Config()
  18. if err != nil {
  19. return ""
  20. }
  21. if !c.Valid() {
  22. return ""
  23. }
  24. return c.S2A.PlaintextAddress
  25. }
  26. type mtlsConfigSource interface {
  27. Config() (*mtlsConfig, error)
  28. }
  29. // mdsMTLSAutoConfigSource is an instance of reuseMTLSConfigSource, with metadataMTLSAutoConfig as its config source.
  30. var (
  31. mdsMTLSAutoConfigSource mtlsConfigSource
  32. once sync.Once
  33. )
  34. // getMetadataMTLSAutoConfig returns mdsMTLSAutoConfigSource, which is backed by config from MDS with auto-refresh.
  35. func getMetadataMTLSAutoConfig() mtlsConfigSource {
  36. once.Do(func() {
  37. mdsMTLSAutoConfigSource = &reuseMTLSConfigSource{
  38. src: &metadataMTLSAutoConfig{},
  39. }
  40. })
  41. return mdsMTLSAutoConfigSource
  42. }
  43. // reuseMTLSConfigSource caches a valid version of mtlsConfig, and uses `src` to refresh upon config expiry.
  44. // It implements the mtlsConfigSource interface, so calling Config() on it returns an mtlsConfig.
  45. type reuseMTLSConfigSource struct {
  46. src mtlsConfigSource // src.Config() is called when config is expired
  47. mu sync.Mutex // mutex guards config
  48. config *mtlsConfig // cached config
  49. }
  50. func (cs *reuseMTLSConfigSource) Config() (*mtlsConfig, error) {
  51. cs.mu.Lock()
  52. defer cs.mu.Unlock()
  53. if cs.config.Valid() {
  54. return cs.config, nil
  55. }
  56. c, err := cs.src.Config()
  57. if err != nil {
  58. return nil, err
  59. }
  60. cs.config = c
  61. return c, nil
  62. }
  63. // metadataMTLSAutoConfig is an implementation of the interface mtlsConfigSource
  64. // It has the logic to query MDS and return an mtlsConfig
  65. type metadataMTLSAutoConfig struct{}
  66. var httpGetMetadataMTLSConfig = func() (string, error) {
  67. return metadata.Get(configEndpointSuffix)
  68. }
  69. func (cs *metadataMTLSAutoConfig) Config() (*mtlsConfig, error) {
  70. resp, err := httpGetMetadataMTLSConfig()
  71. if err != nil {
  72. log.Printf("querying MTLS config from MDS endpoint failed: %v", err)
  73. return defaultMTLSConfig(), nil
  74. }
  75. var config mtlsConfig
  76. err = json.Unmarshal([]byte(resp), &config)
  77. if err != nil {
  78. log.Printf("unmarshalling MTLS config from MDS endpoint failed: %v", err)
  79. return defaultMTLSConfig(), nil
  80. }
  81. if config.S2A == nil {
  82. log.Printf("returned MTLS config from MDS endpoint is invalid: %v", config)
  83. return defaultMTLSConfig(), nil
  84. }
  85. // set new expiry
  86. config.Expiry = time.Now().Add(configExpiry)
  87. return &config, nil
  88. }
  89. func defaultMTLSConfig() *mtlsConfig {
  90. return &mtlsConfig{
  91. S2A: &s2aAddresses{
  92. PlaintextAddress: "",
  93. MTLSAddress: "",
  94. },
  95. Expiry: time.Now().Add(configExpiry),
  96. }
  97. }
  98. // s2aAddresses contains the plaintext and/or MTLS S2A addresses.
  99. type s2aAddresses struct {
  100. // PlaintextAddress is the plaintext address to reach S2A
  101. PlaintextAddress string `json:"plaintext_address"`
  102. // MTLSAddress is the MTLS address to reach S2A
  103. MTLSAddress string `json:"mtls_address"`
  104. }
  105. // mtlsConfig contains the configuration for establishing MTLS connections with Google APIs.
  106. type mtlsConfig struct {
  107. S2A *s2aAddresses `json:"s2a"`
  108. Expiry time.Time
  109. }
  110. func (c *mtlsConfig) Valid() bool {
  111. return c != nil && c.S2A != nil && !c.expired()
  112. }
  113. func (c *mtlsConfig) expired() bool {
  114. return c.Expiry.Before(time.Now())
  115. }