Skip to content

Commit f69db91

Browse files
committed
test(oauth2): ensure executor shutdown and temp file cleanup in mTLS integration tests
1 parent 87681d1 commit f69db91

2 files changed

Lines changed: 113 additions & 105 deletions

File tree

‎google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ITWorkloadIdentityFederationTest.java‎

Lines changed: 87 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

‎google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/MtlsPipelineLocalTest.java‎

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -589,32 +589,34 @@ public void handle(HttpExchange exchange) throws IOException {
589589

590590
int concurrency = 8;
591591
ExecutorService clientExecutor = Executors.newFixedThreadPool(concurrency);
592-
CountDownLatch startLatch = new CountDownLatch(1);
593-
List<Future<AccessToken>> futures = new ArrayList<>();
594-
595-
for (int i = 0; i < concurrency; i++) {
596-
futures.add(
597-
clientExecutor.submit(
598-
new Callable<AccessToken>() {
599-
@Override
600-
public AccessToken call() throws Exception {
601-
startLatch.await();
602-
return credentials.refreshAccessToken();
603-
}
604-
}));
605-
}
592+
try {
593+
CountDownLatch startLatch = new CountDownLatch(1);
594+
List<Future<AccessToken>> futures = new ArrayList<>();
595+
596+
for (int i = 0; i < concurrency; i++) {
597+
futures.add(
598+
clientExecutor.submit(
599+
new Callable<AccessToken>() {
600+
@Override
601+
public AccessToken call() throws Exception {
602+
startLatch.await();
603+
return credentials.refreshAccessToken();
604+
}
605+
}));
606+
}
606607

607-
// Release all client threads concurrently
608-
startLatch.countDown();
608+
// Release all client threads concurrently
609+
startLatch.countDown();
609610

610-
for (Future<AccessToken> future : futures) {
611-
AccessToken token = future.get(10, TimeUnit.SECONDS);
612-
assertNotNull(token);
613-
assertTrue(token.getTokenValue().startsWith("concurrent_token_"));
611+
for (Future<AccessToken> future : futures) {
612+
AccessToken token = future.get(10, TimeUnit.SECONDS);
613+
assertNotNull(token);
614+
assertTrue(token.getTokenValue().startsWith("concurrent_token_"));
615+
}
616+
} finally {
617+
clientExecutor.shutdownNow();
618+
assertTrue(clientExecutor.awaitTermination(5, TimeUnit.SECONDS));
614619
}
615-
616-
clientExecutor.shutdown();
617-
assertTrue(clientExecutor.awaitTermination(5, TimeUnit.SECONDS));
618620
assertEquals(concurrency, requestCounter.get());
619621
}
620622

@@ -943,7 +945,7 @@ public void handle(HttpExchange exchange) throws IOException {
943945

944946
int count = iamRequestCount.incrementAndGet();
945947
if (count == 1) {
946-
// Update the cert files on disk on 401 (Cert A -> Cert B)
948+
// Rewrite the cert files on disk on 401 to trigger X509Provider reload and retry
947949
Files.write(dynamicCertFile, Files.readAllBytes(Paths.get(TEST_CERT_PATH)));
948950
Files.write(dynamicKeyFile, Files.readAllBytes(Paths.get(TEST_KEY_PATH)));
949951

0 commit comments

Comments
 (0)