-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
chore: update samples for new auth #4383
Open
codyoss
wants to merge
7
commits into
GoogleCloudPlatform:main
Choose a base branch
from
codyoss:new-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
228b4ed
chore: update samples for new auth
codyoss a5187c2
Merge branch 'main' into new-auth
codyoss d19eb90
fixup tests
codyoss 17dc4cb
Merge branch 'new-auth' of github.com:codyoss/golang-samples into new…
codyoss 65e293f
Merge branch 'main' into new-auth
telpirion 7d74910
cleanup nit
codyoss bd1081d
Merge branch 'main' into new-auth
codyoss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,51 +21,53 @@ import ( | |
"io" | ||
"time" | ||
|
||
"golang.org/x/oauth2/google" | ||
"google.golang.org/api/impersonate" | ||
"google.golang.org/api/option" | ||
"cloud.google.com/go/auth/credentials" | ||
"cloud.google.com/go/auth/credentials/impersonate" | ||
) | ||
|
||
// getAccessTokenFromImpersonatedCredentials uses a service account (SA1) to impersonate | ||
// another service account (SA2) and obtain OAuth2 token for the impersonated account. | ||
// another service account (SA2) and obtain a token for the impersonated account. | ||
// To obtain a token for SA2, SA1 should have the "roles/iam.serviceAccountTokenCreator" permission on SA2. | ||
func getAccessTokenFromImpersonatedCredentials(w io.Writer, impersonatedServiceAccount, scope string) error { | ||
// impersonatedServiceAccount := "[email protected]" | ||
// scope := "https://www.googleapis.com/auth/cloud-platform" | ||
|
||
ctx := context.Background() | ||
|
||
// Construct the GoogleCredentials object which obtains the default configuration from your | ||
// working environment. | ||
credentials, err := google.FindDefaultCredentials(ctx, scope) | ||
// Construct the Credentials object which obtains the default configuration | ||
// from your working environment. | ||
creds, err := credentials.DetectDefault(&credentials.DetectOptions{ | ||
Scopes: []string{scope}, | ||
}) | ||
if err != nil { | ||
fmt.Fprintf(w, "failed to generate default credentials: %v", err) | ||
return fmt.Errorf("failed to generate default credentials: %w", err) | ||
} | ||
|
||
ts, err := impersonate.CredentialsTokenSource(ctx, impersonate.CredentialsConfig{ | ||
impCreds, err := impersonate.NewCredentials(&impersonate.CredentialsOptions{ | ||
TargetPrincipal: impersonatedServiceAccount, | ||
Scopes: []string{scope}, | ||
Lifetime: 300 * time.Second, | ||
// delegates: The chained list of delegates required to grant the final accessToken. | ||
// For more information, see: | ||
// https://cloud.google.com/iam/docs/create-short-lived-credentials-direct#sa-credentials-permissions | ||
// Delegates is NOT USED here. | ||
Delegates: []string{}, | ||
}, option.WithCredentials(credentials)) | ||
Delegates: []string{}, | ||
Credentials: creds, | ||
}) | ||
if err != nil { | ||
fmt.Fprintf(w, "CredentialsTokenSource error: %v", err) | ||
return fmt.Errorf("CredentialsTokenSource error: %w", err) | ||
fmt.Fprintf(w, "NewCredentials error: %v", err) | ||
return fmt.Errorf("NewCredentials error: %w", err) | ||
} | ||
|
||
// Get the OAuth2 token. | ||
// Once you've obtained the OAuth2 token, you can use it to make an authenticated call. | ||
t, err := ts.Token() | ||
// Get the token. Once you've obtained the token, you can use it to make a | ||
// authenticated call. | ||
t, err := impCreds.Token(ctx) | ||
if err != nil { | ||
fmt.Fprintf(w, "failed to receive token: %v", err) | ||
return fmt.Errorf("failed to receive token: %w", err) | ||
} | ||
fmt.Fprintf(w, "Generated OAuth2 token with length %d.\n", len(t.AccessToken)) | ||
fmt.Fprintf(w, "Generated OAuth2 token with length %d.\n", len(t.Value)) | ||
|
||
return nil | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
observation: this is interesting that it's no longer necessary to provide a context object for new credentials.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The context for the old libraries has one thing they were used for: a way to shove an http.Client into the context that was later used during token exchanges. This is now an explicit param on the options struct. Everything in auth is evaluated lazy so you don't need a context when creating objects as no network calls are made.