|
40 | 40 | import static org.junit.Assert.fail; |
41 | 41 |
|
42 | 42 | import com.google.api.client.json.GenericJson; |
| 43 | +import com.google.api.client.json.Json; |
43 | 44 | import com.google.api.client.json.JsonParser; |
| 45 | +import com.google.api.client.testing.http.MockHttpTransport; |
44 | 46 | import com.google.api.client.testing.http.MockLowLevelHttpRequest; |
| 47 | +import com.google.api.client.testing.http.MockLowLevelHttpResponse; |
45 | 48 | import com.google.api.client.util.Clock; |
46 | 49 | import com.google.auth.TestUtils; |
47 | 50 | import com.google.auth.oauth2.ExternalAccountCredentialsTest.MockExternalAccountCredentialsTransportFactory; |
@@ -1400,105 +1403,32 @@ public AwsSecurityCredentials getCredentials(ExternalAccountSupplierContext cont |
1400 | 1403 | } |
1401 | 1404 | } |
1402 | 1405 |
|
1403 | | - // @Test |
1404 | | - // public void testRefresh_trustBoundarySuccess() throws IOException { |
1405 | | - // TestEnvironmentProvider environmentProvider = new TestEnvironmentProvider(); |
1406 | | - // TrustBoundary.setEnvironmentProviderForTest(environmentProvider); |
1407 | | - // environmentProvider.setEnv("GOOGLE_AUTH_TRUST_BOUNDARY_ENABLE_EXPERIMENT", "1"); |
1408 | | - // |
1409 | | - // MockHttpTransport mockHttpTransport = |
1410 | | - // new MockHttpTransport.Builder() |
1411 | | - // // AWS region call |
1412 | | - // .setLowLevelHttpResponse(new MockLowLevelHttpResponse().setContent("us-east-1a")) |
1413 | | - // // AWS IAM role name call |
1414 | | - // .setLowLevelHttpResponse(new MockLowLevelHttpResponse().setContent("roleName")) |
1415 | | - // // AWS credentials call |
1416 | | - // .setLowLevelHttpResponse( |
1417 | | - // new MockLowLevelHttpResponse() |
1418 | | - // .setContent( |
1419 | | - // |
1420 | | - // "{\"Code\":\"Success\",\"AccessKeyId\":\"accessKeyId\",\"SecretAccessKey\":\"secretAccessKey\",\"Token\":\"token\"}")) |
1421 | | - // // STS token call |
1422 | | - // .setLowLevelHttpResponse( |
1423 | | - // new MockLowLevelHttpResponse() |
1424 | | - // .setContentType(Json.MEDIA_TYPE) |
1425 | | - // .setContent( |
1426 | | - // String.format( |
1427 | | - // "{\"access_token\": \"%s\", \"expires_in\": %s, \"token_type\": |
1428 | | - // \"Bearer\"}", |
1429 | | - // "sts_access_token", 3600))) |
1430 | | - // // Trust boundary call |
1431 | | - // .setLowLevelHttpResponse( |
1432 | | - // new MockLowLevelHttpResponse() |
1433 | | - // .setContentType(Json.MEDIA_TYPE) |
1434 | | - // .setContent( |
1435 | | - // "{\"locations\": [\"us-central1\"], \"encodedLocations\": \"0x1\"}")) |
1436 | | - // .build(); |
1437 | | - // |
1438 | | - // AwsCredentials credentials = |
1439 | | - // AwsCredentials.newBuilder() |
1440 | | - // .setHttpTransportFactory(() -> mockHttpTransport) |
1441 | | - // .setAudience( |
1442 | | - // |
1443 | | - // "//iam.googleapis.com/projects/12345/locations/global/workloadIdentityPools/pool/providers/provider") |
1444 | | - // .setSubjectTokenType("subjectTokenType") |
1445 | | - // .setTokenUrl(STS_URL) |
1446 | | - // .setCredentialSource(AWS_CREDENTIAL_SOURCE) |
1447 | | - // .build(); |
1448 | | - // |
1449 | | - // credentials.refresh(); |
1450 | | - // |
1451 | | - // TrustBoundary trustBoundary = credentials.getTrustBoundary(); |
1452 | | - // assertNotNull(trustBoundary); |
1453 | | - // assertEquals("0x1", trustBoundary.getEncodedLocations()); |
1454 | | - // } |
1455 | | - // |
1456 | | - // @Test |
1457 | | - // public void testRefresh_trustBoundaryFails() throws IOException { |
1458 | | - // TestEnvironmentProvider environmentProvider = new TestEnvironmentProvider(); |
1459 | | - // TrustBoundary.setEnvironmentProviderForTest(environmentProvider); |
1460 | | - // environmentProvider.setEnv("GOOGLE_AUTH_TRUST_BOUNDARY_ENABLE_EXPERIMENT", "1"); |
1461 | | - // |
1462 | | - // MockHttpTransport mockHttpTransport = |
1463 | | - // new MockHttpTransport.Builder() |
1464 | | - // .setLowLevelHttpResponse(new MockLowLevelHttpResponse().setContent("us-east-1a")) |
1465 | | - // .setLowLevelHttpResponse(new MockLowLevelHttpResponse().setContent("roleName")) |
1466 | | - // .setLowLevelHttpResponse( |
1467 | | - // new MockLowLevelHttpResponse() |
1468 | | - // .setContent( |
1469 | | - // |
1470 | | - // "{\"Code\":\"Success\",\"AccessKeyId\":\"accessKeyId\",\"SecretAccessKey\":\"secretAccessKey\",\"Token\":\"token\"}")) |
1471 | | - // .setLowLevelHttpResponse( |
1472 | | - // new MockLowLevelHttpResponse() |
1473 | | - // .setContentType(Json.MEDIA_TYPE) |
1474 | | - // .setContent( |
1475 | | - // String.format( |
1476 | | - // "{\"access_token\": \"%s\", \"expires_in\": %s, \"token_type\": |
1477 | | - // \"Bearer\"}", |
1478 | | - // "sts_access_token", 3600))) |
1479 | | - // .setLowLevelHttpResponse( |
1480 | | - // new MockLowLevelHttpResponse() |
1481 | | - // .setStatusCode(404) |
1482 | | - // .setContent("{\"error\": \"not found\"}")) |
1483 | | - // .build(); |
1484 | | - // |
1485 | | - // AwsCredentials credentials = |
1486 | | - // AwsCredentials.newBuilder() |
1487 | | - // .setHttpTransportFactory(() -> mockHttpTransport) |
1488 | | - // .setAudience( |
1489 | | - // |
1490 | | - // "//iam.googleapis.com/projects/12345/locations/global/workloadIdentityPools/pool/providers/provider") |
1491 | | - // .setSubjectTokenType("subjectTokenType") |
1492 | | - // .setTokenUrl(STS_URL) |
1493 | | - // .setCredentialSource(AWS_CREDENTIAL_SOURCE) |
1494 | | - // .build(); |
1495 | | - // |
1496 | | - // try { |
1497 | | - // credentials.refresh(); |
1498 | | - // fail("Expected IOException to be thrown."); |
1499 | | - // } catch (IOException e) { |
1500 | | - // assertEquals( |
1501 | | - // "Failed to refresh trust boundary and no cached value is available.", e.getMessage()); |
1502 | | - // } |
1503 | | - // } |
| 1406 | + @Test |
| 1407 | + public void testRefresh_trustBoundarySuccess() throws IOException { |
| 1408 | + TestEnvironmentProvider environmentProvider = new TestEnvironmentProvider(); |
| 1409 | + TrustBoundary.setEnvironmentProviderForTest(environmentProvider); |
| 1410 | + environmentProvider.setEnv("GOOGLE_AUTH_TRUST_BOUNDARY_ENABLE_EXPERIMENT", "1"); |
| 1411 | + |
| 1412 | + MockExternalAccountCredentialsTransportFactory transportFactory = |
| 1413 | + new MockExternalAccountCredentialsTransportFactory(); |
| 1414 | + |
| 1415 | + AwsSecurityCredentialsSupplier supplier = |
| 1416 | + new TestAwsSecurityCredentialsSupplier("test", programmaticAwsCreds, null, null); |
| 1417 | + |
| 1418 | + AwsCredentials awsCredential = |
| 1419 | + AwsCredentials.newBuilder() |
| 1420 | + .setAwsSecurityCredentialsSupplier(supplier) |
| 1421 | + .setHttpTransportFactory(transportFactory) |
| 1422 | + .setAudience( |
| 1423 | + "//iam.googleapis.com/projects/12345/locations/global/workloadIdentityPools/pool/providers/provider") |
| 1424 | + .setTokenUrl(STS_URL) |
| 1425 | + .setSubjectTokenType("subjectTokenType") |
| 1426 | + .build(); |
| 1427 | + |
| 1428 | + awsCredential.refreshAccessToken(); |
| 1429 | + |
| 1430 | + TrustBoundary trustBoundary = awsCredential.getTrustBoundary(); |
| 1431 | + assertNotNull(trustBoundary); |
| 1432 | + assertEquals("0x800000", trustBoundary.getEncodedLocations()); |
| 1433 | + } |
1504 | 1434 | } |
0 commit comments