Skip to content

Commit d6d4d4c

Browse files
committed
Listed out imports. Added more user friendly documentation.
1 parent f17cd91 commit d6d4d4c

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

samples/snippets/src/main/java/CustomCredentialSupplierAwsWorkload.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
import com.google.auth.oauth2.*;
17+
import com.google.auth.oauth2.AwsCredentials;
18+
import com.google.auth.oauth2.AwsSecurityCredentials;
19+
import com.google.auth.oauth2.AwsSecurityCredentialsSupplier;
20+
import com.google.auth.oauth2.ExternalAccountSupplierContext;
21+
import com.google.auth.oauth2.GoogleCredentials;
1822
import com.google.cloud.storage.Bucket;
1923
import com.google.cloud.storage.Storage;
2024
import com.google.cloud.storage.StorageOptions;
@@ -32,8 +36,26 @@ public class CustomCredentialSupplierAwsWorkload {
3236

3337
public static void main(String[] args) {
3438
// TODO(Developer): Set these environment variable values.
39+
40+
// 1. AWS Credentials:
41+
// If running on a local system, the user must set AWS_REGION, AWS_ACCESS_KEY_ID, and
42+
// AWS_SECRET_ACCESS_KEY as environment variables. If running in an AWS environment (e.g.,
43+
// ECS, EKS), these variables will be auto-detected.
44+
45+
// 2. GCP_WORKLOAD_AUDIENCE:
46+
// The audience for the workload identity federation. This is the full resource name of the
47+
// Workload Identity Pool Provider, in the following format:
48+
// //iam.googleapis.com/projects/<project-number>/locations/global/workloadIdentityPools/<pool-id>/providers/<provider-id>
3549
String gcpWorkloadAudience = System.getenv("GCP_WORKLOAD_AUDIENCE");
50+
51+
// 3. GCP_SERVICE_ACCOUNT_IMPERSONATION_URL:
52+
// The service account impersonation URL. This is the URL for impersonating a service account,
53+
// in the following format:
54+
// https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/<service-account-email>:generateAccessToken
3655
String saImpersonationUrl = System.getenv("GCP_SERVICE_ACCOUNT_IMPERSONATION_URL");
56+
57+
// 4. GCS_BUCKET_NAME:
58+
// The name of the bucket that you wish to fetch data for.
3759
String gcsBucketName = System.getenv("GCS_BUCKET_NAME");
3860

3961
if (gcpWorkloadAudience == null
@@ -57,6 +79,8 @@ public static void customCredentialSupplierAwsWorkload(
5779
GoogleCredentials credentials =
5880
AwsCredentials.newBuilder()
5981
.setAudience(gcpWorkloadAudience)
82+
// This token type indicates that the subject token is an AWS Signature Version 4 signed
83+
// request. This is required for AWS Workload Identity Federation.
6084
.setSubjectTokenType("urn:ietf:params:aws:token-type:aws4_request")
6185
.setServiceAccountImpersonationUrl(saImpersonationUrl)
6286
.setAwsSecurityCredentialsSupplier(customSupplier)
@@ -65,7 +89,7 @@ public static void customCredentialSupplierAwsWorkload(
6589
// 3. Use the credentials to make an authenticated request.
6690
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
6791

68-
System.out.println("[Test] Getting metadata for bucket: " + gcsBucketName + "...");
92+
System.out.println("Getting metadata for bucket: " + gcsBucketName + "...");
6993
Bucket bucket = storage.get(gcsBucketName);
7094
System.out.println(" --- SUCCESS! ---");
7195
System.out.println("Successfully authenticated and retrieved bucket data:");

0 commit comments

Comments
 (0)