@@ -304,48 +304,51 @@ void identityPoolCredentials_withCertificateBoundWorkloadAndActorToken() throws
304304 File .createTempFile (
305305 "ITWorkloadIdentityFederation_cert_actor" , /* suffix= */ null , /* directory= */ null );
306306 tokenFile .deleteOnExit ();
307-
308- GenericJson tokenJson = new GenericJson ();
309- tokenJson .setFactory (OAuth2Utils .JSON_FACTORY );
310- tokenJson .put ("subject_token" , subjectToken );
311- tokenJson .put ("actor_token" , actorToken );
312-
313- OAuth2Utils .writeInputStreamToFile (
314- new ByteArrayInputStream (tokenJson .toPrettyString ().getBytes (StandardCharsets .UTF_8 )),
315- tokenFile .getAbsolutePath ());
316-
317- GenericJson config = new GenericJson ();
318- config .put ("type" , "external_account" );
319- config .put ("audience" , OIDC_AUDIENCE );
320- config .put ("subject_token_type" , "urn:ietf:params:oauth:token-type:jwt" );
321- config .put ("actor_token_type" , "urn:ietf:params:oauth:token-type:jwt" );
322- config .put ("token_url" , "https://sts.mtls.googleapis.com/v1/token" );
323- config .put (
324- "service_account_impersonation_url" ,
325- String .format (
326- "https://iamcredentials.mtls.googleapis.com/v1/projects/-/serviceAccounts/%s:generateAccessToken" ,
327- clientEmail ));
328-
329- GenericJson credentialSource = new GenericJson ();
330- credentialSource .put ("file" , tokenFile .getAbsolutePath ());
331-
332- GenericJson format = new GenericJson ();
333- format .put ("type" , "json" );
334- format .put ("subject_token_field_name" , "subject_token" );
335- format .put ("actor_token_field_name" , "actor_token" );
336- credentialSource .put ("format" , format );
337-
338- GenericJson certificate = new GenericJson ();
339- certificate .put ("certificate_config_location" , "testresources/mtls/certificate_config.json" );
340- credentialSource .put ("certificate" , certificate );
341-
342- config .put ("credential_source" , credentialSource );
343-
344- IdentityPoolCredentials identityPoolCredentials =
345- (IdentityPoolCredentials )
346- ExternalAccountCredentials .fromJson (config , OAuth2Utils .HTTP_TRANSPORT_FACTORY );
347-
348- callGcs (identityPoolCredentials );
307+ try {
308+ GenericJson tokenJson = new GenericJson ();
309+ tokenJson .setFactory (OAuth2Utils .JSON_FACTORY );
310+ tokenJson .put ("subject_token" , subjectToken );
311+ tokenJson .put ("actor_token" , actorToken );
312+
313+ OAuth2Utils .writeInputStreamToFile (
314+ new ByteArrayInputStream (tokenJson .toPrettyString ().getBytes (StandardCharsets .UTF_8 )),
315+ tokenFile .getAbsolutePath ());
316+
317+ GenericJson config = new GenericJson ();
318+ config .put ("type" , "external_account" );
319+ config .put ("audience" , OIDC_AUDIENCE );
320+ config .put ("subject_token_type" , "urn:ietf:params:oauth:token-type:jwt" );
321+ config .put ("actor_token_type" , "urn:ietf:params:oauth:token-type:jwt" );
322+ config .put ("token_url" , "https://sts.mtls.googleapis.com/v1/token" );
323+ config .put (
324+ "service_account_impersonation_url" ,
325+ String .format (
326+ "https://iamcredentials.mtls.googleapis.com/v1/projects/-/serviceAccounts/%s:generateAccessToken" ,
327+ clientEmail ));
328+
329+ GenericJson credentialSource = new GenericJson ();
330+ credentialSource .put ("file" , tokenFile .getAbsolutePath ());
331+
332+ GenericJson format = new GenericJson ();
333+ format .put ("type" , "json" );
334+ format .put ("subject_token_field_name" , "subject_token" );
335+ format .put ("actor_token_field_name" , "actor_token" );
336+ credentialSource .put ("format" , format );
337+
338+ GenericJson certificate = new GenericJson ();
339+ certificate .put ("certificate_config_location" , "testresources/mtls/certificate_config.json" );
340+ credentialSource .put ("certificate" , certificate );
341+
342+ config .put ("credential_source" , credentialSource );
343+
344+ IdentityPoolCredentials identityPoolCredentials =
345+ (IdentityPoolCredentials )
346+ ExternalAccountCredentials .fromJson (config , OAuth2Utils .HTTP_TRANSPORT_FACTORY );
347+
348+ callGcs (identityPoolCredentials );
349+ } finally {
350+ tokenFile .delete ();
351+ }
349352 }
350353
351354 /**
@@ -418,45 +421,48 @@ void identityPoolCredentials_directSts_withCertificateBoundWorkloadAndActorToken
418421 /* suffix= */ null ,
419422 /* directory= */ null );
420423 tokenFile .deleteOnExit ();
421-
422- GenericJson tokenJson = new GenericJson ();
423- tokenJson .setFactory (OAuth2Utils .JSON_FACTORY );
424- tokenJson .put ("subject_token" , subjectToken );
425- tokenJson .put ("actor_token" , actorToken );
426-
427- OAuth2Utils .writeInputStreamToFile (
428- new ByteArrayInputStream (tokenJson .toPrettyString ().getBytes (StandardCharsets .UTF_8 )),
429- tokenFile .getAbsolutePath ());
430-
431- GenericJson config = new GenericJson ();
432- config .put ("type" , "external_account" );
433- config .put ("audience" , OIDC_AUDIENCE );
434- config .put ("subject_token_type" , "urn:ietf:params:oauth:token-type:jwt" );
435- config .put ("actor_token_type" , "urn:ietf:params:oauth:token-type:jwt" );
436- config .put ("token_url" , "https://sts.mtls.googleapis.com/v1/token" );
437-
438- GenericJson credentialSource = new GenericJson ();
439- credentialSource .put ("file" , tokenFile .getAbsolutePath ());
440-
441- GenericJson format = new GenericJson ();
442- format .put ("type" , "json" );
443- format .put ("subject_token_field_name" , "subject_token" );
444- format .put ("actor_token_field_name" , "actor_token" );
445- credentialSource .put ("format" , format );
446-
447- GenericJson certificate = new GenericJson ();
448- certificate .put ("certificate_config_location" , "testresources/mtls/certificate_config.json" );
449- credentialSource .put ("certificate" , certificate );
450-
451- config .put ("credential_source" , credentialSource );
452-
453- IdentityPoolCredentials identityPoolCredentials =
454- (IdentityPoolCredentials )
455- ExternalAccountCredentials .fromJson (config , OAuth2Utils .HTTP_TRANSPORT_FACTORY );
456-
457- AccessToken accessToken = identityPoolCredentials .refreshAccessToken ();
458- assertNotNull (accessToken );
459- assertNotNull (accessToken .getTokenValue ());
424+ try {
425+ GenericJson tokenJson = new GenericJson ();
426+ tokenJson .setFactory (OAuth2Utils .JSON_FACTORY );
427+ tokenJson .put ("subject_token" , subjectToken );
428+ tokenJson .put ("actor_token" , actorToken );
429+
430+ OAuth2Utils .writeInputStreamToFile (
431+ new ByteArrayInputStream (tokenJson .toPrettyString ().getBytes (StandardCharsets .UTF_8 )),
432+ tokenFile .getAbsolutePath ());
433+
434+ GenericJson config = new GenericJson ();
435+ config .put ("type" , "external_account" );
436+ config .put ("audience" , OIDC_AUDIENCE );
437+ config .put ("subject_token_type" , "urn:ietf:params:oauth:token-type:jwt" );
438+ config .put ("actor_token_type" , "urn:ietf:params:oauth:token-type:jwt" );
439+ config .put ("token_url" , "https://sts.mtls.googleapis.com/v1/token" );
440+
441+ GenericJson credentialSource = new GenericJson ();
442+ credentialSource .put ("file" , tokenFile .getAbsolutePath ());
443+
444+ GenericJson format = new GenericJson ();
445+ format .put ("type" , "json" );
446+ format .put ("subject_token_field_name" , "subject_token" );
447+ format .put ("actor_token_field_name" , "actor_token" );
448+ credentialSource .put ("format" , format );
449+
450+ GenericJson certificate = new GenericJson ();
451+ certificate .put ("certificate_config_location" , "testresources/mtls/certificate_config.json" );
452+ credentialSource .put ("certificate" , certificate );
453+
454+ config .put ("credential_source" , credentialSource );
455+
456+ IdentityPoolCredentials identityPoolCredentials =
457+ (IdentityPoolCredentials )
458+ ExternalAccountCredentials .fromJson (config , OAuth2Utils .HTTP_TRANSPORT_FACTORY );
459+
460+ AccessToken accessToken = identityPoolCredentials .refreshAccessToken ();
461+ assertNotNull (accessToken );
462+ assertNotNull (accessToken .getTokenValue ());
463+ } finally {
464+ tokenFile .delete ();
465+ }
460466 }
461467
462468 /**
0 commit comments