From c27e6a89b0ec199909073556e8ca83a3ae3be724 Mon Sep 17 00:00:00 2001 From: Peter Brunner Date: Fri, 24 Mar 2023 07:11:21 -0400 Subject: [PATCH] drive: add env_auth to drive provider This change provides the ability to pass `env_auth` as a parameter to the drive provider. This enables the provider to pull IAM credentials from the environment or instance metadata. Previously if no auth method was given it would default to requesting oauth. --- backend/drive/drive.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/backend/drive/drive.go b/backend/drive/drive.go index c3cf25ded..5f1472ccf 100644 --- a/backend/drive/drive.go +++ b/backend/drive/drive.go @@ -202,7 +202,7 @@ func init() { m.Set("root_folder_id", "appDataFolder") } - if opt.ServiceAccountFile == "" && opt.ServiceAccountCredentials == "" { + if opt.ServiceAccountFile == "" && opt.ServiceAccountCredentials == "" && !opt.EnvAuth { return oauthutil.ConfigOut("teamdrive", &oauthutil.Options{ OAuth2Config: driveConfig, }) @@ -598,6 +598,18 @@ resource key is no needed. // Encode invalid UTF-8 bytes as json doesn't handle them properly. // Don't encode / as it's a valid name character in drive. Default: encoder.EncodeInvalidUtf8, + }, { + Name: "env_auth", + Help: "Get IAM credentials from runtime (environment variables or instance meta data if no env vars).\n\nOnly applies if service_account_file and service_account_credentials is blank.", + Default: false, + Advanced: true, + Examples: []fs.OptionExample{{ + Value: "false", + Help: "Enter credentials in the next step.", + }, { + Value: "true", + Help: "Get GCP IAM credentials from the environment (env vars or IAM).", + }}, }}...), }) @@ -654,6 +666,7 @@ type Options struct { SkipDanglingShortcuts bool `config:"skip_dangling_shortcuts"` ResourceKey string `config:"resource_key"` Enc encoder.MultiEncoder `config:"encoding"` + EnvAuth bool `config:"env_auth"` } // Fs represents a remote drive server @@ -1122,6 +1135,12 @@ func createOAuthClient(ctx context.Context, opt *Options, name string, m configm if err != nil { return nil, fmt.Errorf("failed to create oauth client from service account: %w", err) } + } else if opt.EnvAuth { + scopes := driveScopes(opt.Scope) + oAuthClient, err = google.DefaultClient(ctx, scopes...) + if err != nil { + return nil, fmt.Errorf("failed to create client from environment: %w", err) + } } else { oAuthClient, _, err = oauthutil.NewClientWithBaseClient(ctx, name, m, driveConfig, getClient(ctx, opt)) if err != nil {