@@ -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