Skip to content

Commit 78cc927

Browse files
committed
Added documentation for env variables.
1 parent faa55a7 commit 78cc927

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,20 @@ public static void main(String[] args) {
4848
// //iam.googleapis.com/projects/<project-number>/locations/global/workloadIdentityPools/<pool-id>/providers/<provider-id>
4949
String gcpWorkloadAudience = System.getenv("GCP_WORKLOAD_AUDIENCE");
5050

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:
51+
// 3. GCP_SERVICE_ACCOUNT_IMPERSONATION_URL (optional):
52+
// The service account impersonation URL. It should follow the format:
5453
// https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/<service-account-email>:generateAccessToken
54+
// If not provided, you should grant access to the GCP bucket to the principal directly.
5555
String saImpersonationUrl = System.getenv("GCP_SERVICE_ACCOUNT_IMPERSONATION_URL");
5656

5757
// 4. GCS_BUCKET_NAME:
5858
// The name of the bucket that you wish to fetch data for.
5959
String gcsBucketName = System.getenv("GCS_BUCKET_NAME");
6060

61-
if (gcpWorkloadAudience == null || saImpersonationUrl == null || gcsBucketName == null) {
61+
if (gcpWorkloadAudience == null || gcsBucketName == null) {
6262
System.out.println(
6363
"Missing required environment variables. Please check your environment settings. "
64-
+ "Required: GCP_WORKLOAD_AUDIENCE, GCP_SERVICE_ACCOUNT_IMPERSONATION_URL, GCS_BUCKET_NAME");
64+
+ "Required: GCP_WORKLOAD_AUDIENCE, GCS_BUCKET_NAME");
6565
return;
6666
}
6767

@@ -74,15 +74,19 @@ public static void customCredentialSupplierAwsWorkload(
7474
CustomAwsSupplier customSupplier = new CustomAwsSupplier();
7575

7676
// 2. Configure the AwsCredentials options.
77-
GoogleCredentials credentials =
77+
AwsCredentials.Builder credentialsBuilder =
7878
AwsCredentials.newBuilder()
7979
.setAudience(gcpWorkloadAudience)
8080
// This token type indicates that the subject token is an AWS Signature Version 4 signed
8181
// request. This is required for AWS Workload Identity Federation.
8282
.setSubjectTokenType("urn:ietf:params:aws:token-type:aws4_request")
83-
.setServiceAccountImpersonationUrl(saImpersonationUrl)
84-
.setAwsSecurityCredentialsSupplier(customSupplier)
85-
.build();
83+
.setAwsSecurityCredentialsSupplier(customSupplier);
84+
85+
if (saImpersonationUrl != null) {
86+
credentialsBuilder.setServiceAccountImpersonationUrl(saImpersonationUrl);
87+
}
88+
89+
GoogleCredentials credentials = credentialsBuilder.build();
8690

8791
// 3. Use the credentials to make an authenticated request.
8892
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

samples/snippets/src/main/java/CustomCredentialSupplierOktaWorkload.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ public static void main(String[] args) {
4747
// //iam.googleapis.com/projects/<project-number>/locations/global/workloadIdentityPools/<pool-id>/providers/<provider-id>
4848
String gcpWorkloadAudience = System.getenv("GCP_WORKLOAD_AUDIENCE");
4949

50-
// 2. GCP_SERVICE_ACCOUNT_IMPERSONATION_URL:
51-
// The service account impersonation URL. This is the URL for impersonating a service account,
52-
// in the following format:
50+
// 2. GCP_SERVICE_ACCOUNT_IMPERSONATION_URL (optional):
51+
// The service account impersonation URL. In the following format:
5352
// https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/<service-account-email>:generateAccessToken
53+
// If not provided, you should grant access to the GCP bucket to the principal directly.
5454
String serviceAccountImpersonationUrl = System.getenv("GCP_SERVICE_ACCOUNT_IMPERSONATION_URL");
5555

5656
// 3. GCS_BUCKET_NAME:
@@ -75,14 +75,13 @@ public static void main(String[] args) {
7575
String oktaClientSecret = System.getenv("OKTA_CLIENT_SECRET");
7676

7777
if (gcpWorkloadAudience == null
78-
|| serviceAccountImpersonationUrl == null
7978
|| gcsBucketName == null
8079
|| oktaDomain == null
8180
|| oktaClientId == null
8281
|| oktaClientSecret == null) {
8382
System.out.println(
8483
"Missing required environment variables. Please check your environment settings. "
85-
+ "Required: GCP_WORKLOAD_AUDIENCE, GCP_SERVICE_ACCOUNT_IMPERSONATION_URL, "
84+
+ "Required: GCP_WORKLOAD_AUDIENCE, "
8685
+ "GCS_BUCKET_NAME, OKTA_DOMAIN, OKTA_CLIENT_ID, OKTA_CLIENT_SECRET");
8786
return;
8887
}
@@ -108,14 +107,20 @@ public static void customCredentialSupplierOktaWorkload(
108107
new OktaClientCredentialsSupplier(oktaDomain, oktaClientId, oktaClientSecret);
109108

110109
// 2. Instantiate an IdentityPoolCredentials with the required configuration.
111-
GoogleCredentials credentials =
110+
IdentityPoolCredentials.Builder credentialsBuilder =
112111
IdentityPoolCredentials.newBuilder()
113112
.setAudience(gcpWorkloadAudience)
113+
// This token type indicates that the subject token is a JSON Web Token (JWT).
114+
// This is required for Workload Identity Federation with an OIDC provider like Okta.
114115
.setSubjectTokenType("urn:ietf:params:oauth:token-type:jwt")
115116
.setTokenUrl("https://sts.googleapis.com/v1/token")
116-
.setSubjectTokenSupplier(oktaSupplier)
117-
.setServiceAccountImpersonationUrl(serviceAccountImpersonationUrl)
118-
.build();
117+
.setSubjectTokenSupplier(oktaSupplier);
118+
119+
if (serviceAccountImpersonationUrl != null) {
120+
credentialsBuilder.setServiceAccountImpersonationUrl(serviceAccountImpersonationUrl);
121+
}
122+
123+
GoogleCredentials credentials = credentialsBuilder.build();
119124

120125
// 3. Use the credentials to make an authenticated request.
121126
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

0 commit comments

Comments
 (0)