Skip to content

Commit 093f73b

Browse files
committed
fix merging errors with master
Signed-off-by: Matt Hoey <[email protected]>
1 parent 7e914dd commit 093f73b

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

connector/google/google.go

+26-26
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (c *Config) Open(id string, logger log.Logger) (conn connector.Connector, e
7171
scopes = append(scopes, "profile", "email")
7272
}
7373

74-
srv, err := createDirectoryService(c.ServiceAccountFilePath, c.AdminEmail)
74+
srv, err := createDirectoryService(c.ServiceAccountFilePath, c.AdminEmail, logger)
7575
if err != nil {
7676
cancel()
7777
return nil, fmt.Errorf("could not create directory service: %v", err)
@@ -287,35 +287,35 @@ func (c *googleConnector) getGroups(email string, fetchTransitiveGroupMembership
287287
return userGroups, nil
288288
}
289289

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")
303296
}
304297

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
312300

313301
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)
317316
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)
319318
}
320-
return srv, nil
319+
config.Subject = email
320+
return admin.NewService(ctx, option.WithHTTPClient(config.Client(ctx)))
321321
}

0 commit comments

Comments
 (0)