@@ -71,7 +71,7 @@ func (c *Config) Open(id string, logger log.Logger) (conn connector.Connector, e
71
71
scopes = append (scopes , "profile" , "email" )
72
72
}
73
73
74
- srv , err := createDirectoryService (c .ServiceAccountFilePath , c .AdminEmail )
74
+ srv , err := createDirectoryService (c .ServiceAccountFilePath , c .AdminEmail , logger )
75
75
if err != nil {
76
76
cancel ()
77
77
return nil , fmt .Errorf ("could not create directory service: %v" , err )
@@ -287,35 +287,35 @@ func (c *googleConnector) getGroups(email string, fetchTransitiveGroupMembership
287
287
return userGroups , nil
288
288
}
289
289
290
- // createDirectoryService loads a google service account credentials file,
291
- // sets up super user impersonation and creates an admin client for calling
292
- // the google admin api
293
- func createDirectoryService (serviceAccountFilePath string , email string ) (* admin.Service , error ) {
294
- if serviceAccountFilePath == "" && email == "" {
295
- return nil , nil
296
- }
297
- if serviceAccountFilePath == "" || email == "" {
298
- return nil , fmt .Errorf ("directory service requires both serviceAccountFilePath and adminEmail" )
299
- }
300
- jsonCredentials , err := os .ReadFile (serviceAccountFilePath )
301
- if err != nil {
302
- return nil , fmt .Errorf ("error reading credentials from file: %v" , err )
290
+ // createDirectoryService sets up super user impersonation and creates an admin client for calling
291
+ // the google admin api. If no serviceAccountFilePath is defined, the application default credential
292
+ // is used.
293
+ func createDirectoryService (serviceAccountFilePath , email string , logger log.Logger ) (* admin.Service , error ) {
294
+ if email == "" {
295
+ return nil , fmt .Errorf ("directory service requires adminEmail" )
303
296
}
304
297
305
- config , err := google .JWTConfigFromJSON (jsonCredentials , admin .AdminDirectoryGroupReadonlyScope )
306
- if err != nil {
307
- return nil , fmt .Errorf ("unable to parse client secret file to config: %v" , err )
308
- }
309
-
310
- // Impersonate an admin. This is mandatory for the admin APIs.
311
- config .Subject = email
298
+ var jsonCredentials []byte
299
+ var err error
312
300
313
301
ctx := context .Background ()
314
- client := config .Client (ctx )
315
-
316
- srv , err := admin .NewService (ctx , option .WithHTTPClient (client ))
302
+ if serviceAccountFilePath == "" {
303
+ logger .Warn ("the application default credential is used since the service account file path is not used" )
304
+ credential , err := google .FindDefaultCredentials (ctx )
305
+ if err != nil {
306
+ return nil , fmt .Errorf ("failed to fetch application default credentials: %w" , err )
307
+ }
308
+ jsonCredentials = credential .JSON
309
+ } else {
310
+ jsonCredentials , err = os .ReadFile (serviceAccountFilePath )
311
+ if err != nil {
312
+ return nil , fmt .Errorf ("error reading credentials from file: %v" , err )
313
+ }
314
+ }
315
+ config , err := google .JWTConfigFromJSON (jsonCredentials , admin .AdminDirectoryGroupReadonlyScope )
317
316
if err != nil {
318
- return nil , fmt .Errorf ("unable to create directory service %v" , err )
317
+ return nil , fmt .Errorf ("unable to parse credentials to config: %v" , err )
319
318
}
320
- return srv , nil
319
+ config .Subject = email
320
+ return admin .NewService (ctx , option .WithHTTPClient (config .Client (ctx )))
321
321
}
0 commit comments