-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: Custom Credential Supplier Documentation #10203
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
Open
vverman
wants to merge
2
commits into
GoogleCloudPlatform:main
Choose a base branch
from
vverman:feat/document-custom-credential-suppliers
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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
156 changes: 156 additions & 0 deletions
156
auth/src/main/java/com/google/cloud/auth/samples/CustomCredentialSupplierAwsWorkload.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.auth.samples; | ||
|
|
||
| // [START auth_custom_credential_supplier_aws] | ||
| import com.google.auth.oauth2.AwsCredentials; | ||
| import com.google.auth.oauth2.AwsSecurityCredentials; | ||
| import com.google.auth.oauth2.AwsSecurityCredentialsSupplier; | ||
| import com.google.auth.oauth2.ExternalAccountSupplierContext; | ||
| import com.google.auth.oauth2.GoogleCredentials; | ||
| import com.google.cloud.storage.Bucket; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
| import java.io.IOException; | ||
| import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; | ||
| import software.amazon.awssdk.auth.credentials.AwsSessionCredentials; | ||
| import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain; | ||
|
|
||
| // [END auth_custom_credential_supplier_aws] | ||
|
|
||
| /** | ||
| * This sample demonstrates how to use a custom AWS security credentials supplier to authenticate to | ||
| * Google Cloud Storage using AWS Workload Identity Federation. | ||
| */ | ||
| public class CustomCredentialSupplierAwsWorkload { | ||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| // The audience for the workload identity federation. | ||
| // Format: //iam.googleapis.com/projects/<project-number>/locations/global/ | ||
| // workloadIdentityPools/<pool-id>/providers/<provider-id> | ||
| String gcpWorkloadAudience = System.getenv("GCP_WORKLOAD_AUDIENCE"); | ||
|
|
||
| // The bucket to fetch data from. | ||
| String gcsBucketName = System.getenv("GCS_BUCKET_NAME"); | ||
|
|
||
| // (Optional) The service account impersonation URL. | ||
| String saImpersonationUrl = System.getenv("GCP_SERVICE_ACCOUNT_IMPERSONATION_URL"); | ||
|
|
||
| if (gcpWorkloadAudience == null || gcsBucketName == null) { | ||
| System.err.println( | ||
| "Error: GCP_WORKLOAD_AUDIENCE and GCS_BUCKET_NAME environment variables are required."); | ||
| return; | ||
| } | ||
|
|
||
| System.out.println("Getting metadata for bucket: " + gcsBucketName + "..."); | ||
| Bucket bucket = | ||
| authenticateWithAwsCredentials(gcpWorkloadAudience, saImpersonationUrl, gcsBucketName); | ||
|
|
||
| System.out.println(" --- SUCCESS! ---"); | ||
| System.out.printf("Bucket Name: %s%n", bucket.getName()); | ||
| System.out.printf("Bucket Location: %s%n", bucket.getLocation()); | ||
| } | ||
|
|
||
| /** | ||
| * Authenticates using a custom AWS credential supplier and retrieves bucket metadata. | ||
| * | ||
| * @param gcpWorkloadAudience The WIF provider audience. | ||
| * @param saImpersonationUrl Optional service account impersonation URL. | ||
| * @param gcsBucketName The GCS bucket name. | ||
| * @return The Bucket object containing metadata. | ||
| * @throws IOException If authentication fails. | ||
| */ | ||
| // [START auth_custom_credential_supplier_aws] | ||
| public static Bucket authenticateWithAwsCredentials( | ||
| String gcpWorkloadAudience, String saImpersonationUrl, String gcsBucketName) | ||
| throws IOException { | ||
|
|
||
| // 1. Instantiate the custom supplier. | ||
| CustomAwsSupplier customSupplier = new CustomAwsSupplier(); | ||
|
|
||
| // 2. Configure the AwsCredentials options. | ||
| AwsCredentials.Builder credentialsBuilder = | ||
| AwsCredentials.newBuilder() | ||
| .setAudience(gcpWorkloadAudience) | ||
| // This token type indicates that the subject token is an AWS Signature Version 4 signed | ||
| // request. This is required for AWS Workload Identity Federation. | ||
| .setSubjectTokenType("urn:ietf:params:aws:token-type:aws4_request") | ||
| .setAwsSecurityCredentialsSupplier(customSupplier); | ||
|
|
||
| if (saImpersonationUrl != null) { | ||
| credentialsBuilder.setServiceAccountImpersonationUrl(saImpersonationUrl); | ||
| } | ||
|
|
||
| GoogleCredentials credentials = credentialsBuilder.build(); | ||
|
|
||
| // 3. Use the credentials to make an authenticated request. | ||
| Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService(); | ||
|
|
||
| return storage.get(gcsBucketName); | ||
| } | ||
|
|
||
| /** | ||
| * Custom AWS Security Credentials Supplier. | ||
| * | ||
| * <p>This implementation resolves AWS credentials and regions using the default provider chains | ||
| * from the AWS SDK (v2). This supports environment variables, ~/.aws/credentials, and EC2/EKS | ||
| * metadata. | ||
| */ | ||
| private static class CustomAwsSupplier implements AwsSecurityCredentialsSupplier { | ||
| private final AwsCredentialsProvider awsCredentialsProvider; | ||
| private String region; | ||
|
|
||
| public CustomAwsSupplier() { | ||
| // The AWS SDK handles memoization and refreshing internally. | ||
| this.awsCredentialsProvider = DefaultCredentialsProvider.create(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getRegion(ExternalAccountSupplierContext context) { | ||
| if (this.region == null) { | ||
| Region awsRegion = new DefaultAwsRegionProviderChain().getRegion(); | ||
| if (awsRegion == null) { | ||
| throw new IllegalStateException( | ||
| "Unable to resolve AWS region. Ensure AWS_REGION is set or configured."); | ||
| } | ||
| this.region = awsRegion.id(); | ||
| } | ||
| return this.region; | ||
| } | ||
|
|
||
| @Override | ||
| public AwsSecurityCredentials getCredentials(ExternalAccountSupplierContext context) { | ||
| software.amazon.awssdk.auth.credentials.AwsCredentials credentials = | ||
| this.awsCredentialsProvider.resolveCredentials(); | ||
|
|
||
| if (credentials == null) { | ||
| throw new IllegalStateException("Unable to resolve AWS credentials."); | ||
| } | ||
|
|
||
| String sessionToken = null; | ||
| if (credentials instanceof AwsSessionCredentials) { | ||
| sessionToken = ((AwsSessionCredentials) credentials).sessionToken(); | ||
| } | ||
|
|
||
| return new AwsSecurityCredentials( | ||
| credentials.accessKeyId(), credentials.secretAccessKey(), sessionToken); | ||
| } | ||
| } | ||
| // [END auth_custom_credential_supplier_aws] | ||
| } | ||
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.
The
getRegionmethod performs lazy initialization of theregionfield, but it's not thread-safe. If multiple threads call this method concurrently whenregionisnull, they could all attempt to resolve the region, which is inefficient. SinceGoogleCredentialsobjects can be shared across threads, their components should be thread-safe.To ensure thread safety, you can add the
synchronizedkeyword to the method signature.