Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update config.go and framework_config.go to use transport.Creds for all cases #7875

Merged
merged 4 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ func GetCredentials(ctx context.Context, data ProviderModel, initialCredentialsO
return *creds
}

creds, err := googleoauth.CredentialsFromJSON(ctx, []byte(contents), clientScopes...)
creds, err := transport.Creds(ctx, option.WithCredentialsJSON([]byte(contents)), option.WithScopes(clientScopes...))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: transport.Creds ultimately calls out to CredentialsFromJSONWithParams, so this is almost certainly compatible: https://github.com/googleapis/google-api-go-client/blob/cf0df64489c5086a57b9156d9648fe8342b22408/internal/creds.go#LL108C1-L108C1

rileykarson marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
diags.AddError("unable to parse credentials", err.Error())
return googleoauth.Credentials{}
Expand All @@ -625,14 +625,12 @@ func GetCredentials(ctx context.Context, data ProviderModel, initialCredentialsO

tflog.Info(ctx, "Authenticating using DefaultClient...")
tflog.Info(ctx, fmt.Sprintf(" -- Scopes: %s", clientScopes))
defaultTS, err := googleoauth.DefaultTokenSource(context.Background(), clientScopes...)
creds, err := transport.Creds(context.Background(), option.WithScopes(clientScopes...))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: DefaultTokenSource calls FindDefaultCredentials and extracts the TokenSource from the credenetials (which we then use to reconstruct a credentials object...), Creds calls FindDefaultCredentials and returns the credentials.

if err != nil {
diags.AddError(fmt.Sprintf("Attempted to load application default credentials since neither `credentials` nor `access_token` was set in the provider block. "+
"No credentials loaded. To use your gcloud credentials, run 'gcloud auth application-default login'"), err.Error())
return googleoauth.Credentials{}
}

return googleoauth.Credentials{
TokenSource: defaultTS,
}
return *creds
}
8 changes: 3 additions & 5 deletions mmv1/third_party/terraform/transport/config.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ func (c *Config) GetCredentials(clientScopes []string, initialCredentialsOnly bo
return *creds, nil
}

creds, err := googleoauth.CredentialsFromJSON(c.Context, []byte(contents), clientScopes...)
creds, err := transport.Creds(c.Context, option.WithCredentialsJSON([]byte(contents)), option.WithScopes(clientScopes...))
if err != nil {
return googleoauth.Credentials{}, fmt.Errorf("unable to parse credentials from '%s': %s", contents, err)
}
Expand All @@ -1183,14 +1183,12 @@ func (c *Config) GetCredentials(clientScopes []string, initialCredentialsOnly bo

log.Printf("[INFO] Authenticating using DefaultClient...")
log.Printf("[INFO] -- Scopes: %s", clientScopes)
defaultTS, err := googleoauth.DefaultTokenSource(context.Background(), clientScopes...)
creds, err := transport.Creds(context.Background(), option.WithScopes(clientScopes...))
if err != nil {
return googleoauth.Credentials{}, fmt.Errorf("Attempted to load application default credentials since neither `credentials` nor `access_token` was set in the provider block. No credentials loaded. To use your gcloud credentials, run 'gcloud auth application-default login'. Original error: %w", err)
}

return googleoauth.Credentials{
TokenSource: defaultTS,
}, err
return *creds, nil
andyrzhao marked this conversation as resolved.
Show resolved Hide resolved
}

// Remove the `/{{version}}/` from a base path if present.
Expand Down