@@ -42,6 +42,17 @@ type Credentials struct {
42
42
// running on Google Cloud Platform.
43
43
JSON []byte
44
44
45
+ // UniverseDomainProvider returns the default service domain for a given
46
+ // Cloud universe. Optional.
47
+ //
48
+ // On GCE, UniverseDomainProvider should return the universe domain value
49
+ // from Google Compute Engine (GCE)'s metadata server. See also [The attached service
50
+ // account](https://cloud.google.com/docs/authentication/application-default-credentials#attached-sa).
51
+ // If the GCE metadata server returns a 404 error, the default universe
52
+ // domain value should be returned. If the GCE metadata server returns an
53
+ // error other than 404, the error should be returned.
54
+ UniverseDomainProvider func () (string , error )
55
+
45
56
udMu sync.Mutex // guards universeDomain
46
57
// universeDomain is the default service domain for a given Cloud universe.
47
58
universeDomain string
@@ -64,54 +75,32 @@ func (c *Credentials) UniverseDomain() string {
64
75
}
65
76
66
77
// GetUniverseDomain returns the default service domain for a given Cloud
67
- // universe.
78
+ // universe. If present, UniverseDomainProvider will be invoked and its return
79
+ // value will be cached.
68
80
//
69
81
// The default value is "googleapis.com".
70
- //
71
- // It obtains the universe domain from the attached service account on GCE when
72
- // authenticating via the GCE metadata server. See also [The attached service
73
- // account](https://cloud.google.com/docs/authentication/application-default-credentials#attached-sa).
74
- // If the GCE metadata server returns a 404 error, the default value is
75
- // returned. If the GCE metadata server returns an error other than 404, the
76
- // error is returned.
77
82
func (c * Credentials ) GetUniverseDomain () (string , error ) {
78
83
c .udMu .Lock ()
79
84
defer c .udMu .Unlock ()
80
- if c .universeDomain == "" && metadata .OnGCE () {
81
- // If we're on Google Compute Engine, an App Engine standard second
82
- // generation runtime, or App Engine flexible, use the metadata server.
83
- err := c .computeUniverseDomain ()
85
+ if c .universeDomain == "" && c .UniverseDomainProvider != nil {
86
+ // On Google Compute Engine, an App Engine standard second generation
87
+ // runtime, or App Engine flexible, use an externally provided function
88
+ // to request the universe domain from the metadata server.
89
+ ud , err := c .UniverseDomainProvider ()
84
90
if err != nil {
85
91
return "" , err
86
92
}
93
+ c .universeDomain = ud
87
94
}
88
- // If not on Google Compute Engine, or in case of any non-error path in
89
- // computeUniverseDomain that did not set universeDomain, set the default
90
- // universe domain.
95
+ // If no UniverseDomainProvider (meaning not on Google Compute Engine) , or
96
+ // in case of any (non-error) empty return value from
97
+ // UniverseDomainProvider, set the default universe domain.
91
98
if c .universeDomain == "" {
92
99
c .universeDomain = defaultUniverseDomain
93
100
}
94
101
return c .universeDomain , nil
95
102
}
96
103
97
- // computeUniverseDomain fetches the default service domain for a given Cloud
98
- // universe from Google Compute Engine (GCE)'s metadata server. It's only valid
99
- // to use this method if your program is running on a GCE instance.
100
- func (c * Credentials ) computeUniverseDomain () error {
101
- var err error
102
- c .universeDomain , err = metadata .Get ("universe/universe_domain" )
103
- if err != nil {
104
- if _ , ok := err .(metadata.NotDefinedError ); ok {
105
- // http.StatusNotFound (404)
106
- c .universeDomain = defaultUniverseDomain
107
- return nil
108
- } else {
109
- return err
110
- }
111
- }
112
- return nil
113
- }
114
-
115
104
// DefaultCredentials is the old name of Credentials.
116
105
//
117
106
// Deprecated: use Credentials instead.
@@ -226,10 +215,23 @@ func FindDefaultCredentialsWithParams(ctx context.Context, params CredentialsPar
226
215
// or App Engine flexible, use the metadata server.
227
216
if metadata .OnGCE () {
228
217
id , _ := metadata .ProjectID ()
218
+ universeDomainProvider := func () (string , error ) {
219
+ universeDomain , err := metadata .Get ("universe/universe_domain" )
220
+ if err != nil {
221
+ if _ , ok := err .(metadata.NotDefinedError ); ok {
222
+ // http.StatusNotFound (404)
223
+ return defaultUniverseDomain , nil
224
+ } else {
225
+ return "" , err
226
+ }
227
+ }
228
+ return universeDomain , nil
229
+ }
229
230
return & Credentials {
230
- ProjectID : id ,
231
- TokenSource : computeTokenSource ("" , params .EarlyTokenRefresh , params .Scopes ... ),
232
- universeDomain : params .UniverseDomain ,
231
+ ProjectID : id ,
232
+ TokenSource : computeTokenSource ("" , params .EarlyTokenRefresh , params .Scopes ... ),
233
+ UniverseDomainProvider : universeDomainProvider ,
234
+ universeDomain : params .UniverseDomain ,
233
235
}, nil
234
236
}
235
237
0 commit comments