@@ -42,6 +42,17 @@ type Credentials struct {
4242 // running on Google Cloud Platform.
4343 JSON []byte
4444
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+
4556 udMu sync.Mutex // guards universeDomain
4657 // universeDomain is the default service domain for a given Cloud universe.
4758 universeDomain string
@@ -64,54 +75,32 @@ func (c *Credentials) UniverseDomain() string {
6475}
6576
6677// 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.
6880//
6981// 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.
7782func (c * Credentials ) GetUniverseDomain () (string , error ) {
7883 c .udMu .Lock ()
7984 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 ()
8490 if err != nil {
8591 return "" , err
8692 }
93+ c .universeDomain = ud
8794 }
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.
9198 if c .universeDomain == "" {
9299 c .universeDomain = defaultUniverseDomain
93100 }
94101 return c .universeDomain , nil
95102}
96103
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-
115104// DefaultCredentials is the old name of Credentials.
116105//
117106// Deprecated: use Credentials instead.
@@ -226,10 +215,23 @@ func FindDefaultCredentialsWithParams(ctx context.Context, params CredentialsPar
226215 // or App Engine flexible, use the metadata server.
227216 if metadata .OnGCE () {
228217 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+ }
229230 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 ,
233235 }, nil
234236 }
235237
0 commit comments