Skip to content

Commit

Permalink
static method and split tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sai-sunder-s committed Nov 8, 2022
1 parent c06c896 commit 1fa7358
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
10 changes: 5 additions & 5 deletions oauth2_http/java/com/google/auth/oauth2/AwsCredentials.java
Expand Up @@ -132,19 +132,19 @@ static class AwsCredentialSource extends CredentialSource {
}

private void validateMetadataServerUrls() {
this.validateMetadataServerUrlIfAny(this.regionUrl, "region_url");
this.validateMetadataServerUrlIfAny(this.url, "url");
this.validateMetadataServerUrlIfAny(this.imdsv2SessionTokenUrl, "imdsv2_session_token_url");
validateMetadataServerUrlIfAny(this.regionUrl, "region_url");
validateMetadataServerUrlIfAny(this.url, "url");
validateMetadataServerUrlIfAny(this.imdsv2SessionTokenUrl, "imdsv2_session_token_url");
}

private void validateMetadataServerUrlIfAny(String urlString, String nameOfData) {
private static void validateMetadataServerUrlIfAny(String urlString, String nameOfData) {
if (urlString != null) {
try {
URL url = new URL(urlString);
String host = url.getHost();
if (!host.equals("169.254.169.254") && !host.equals("[fd00:ec2::254]")) {
throw new IllegalArgumentException(
String.format("Invalid host %s for %s", host, nameOfData));
String.format("Invalid host %s for %s.", host, nameOfData));
}
} catch (MalformedURLException malformedURLException) {
throw new IllegalArgumentException(malformedURLException);
Expand Down
Expand Up @@ -103,23 +103,16 @@ public class AwsCredentialsTest {
.build();

@Test
public void test_awsCredentialSource() {
String regionUrl = "http://[fd00:ec2::254]/region";
String url = "http://[fd00:ec2::254]";
String imdsv2SessionTokenUrl = "http://[fd00:ec2::254]/imdsv2";
Map<String, Object> credentialSourceMap = new HashMap<>();
credentialSourceMap.put("environment_id", "aws1");
credentialSourceMap.put("region_url", regionUrl);
credentialSourceMap.put("url", url);
credentialSourceMap.put("imdsv2_session_token_url", imdsv2SessionTokenUrl);
credentialSourceMap.put("regional_cred_verification_url", GET_CALLER_IDENTITY_URL);

// If no exception is thrown, it means the urls were valid
new AwsCredentialSource(credentialSourceMap);
public void test_awsCredentialSource_ipv6() {
// If no exception is thrown, it means the urls were valid.
new AwsCredentialSource(buildAwsIpv6CredentialSourceMap());
}

@Test
public void test_awsCredentialSource_invalid_urls() {
String keys[] = {"region_url", "url", "imdsv2_session_token_url"};
for (String key : keys) {
Map<String, Object> credentialSourceWithInvalidUrl = new HashMap<>(credentialSourceMap);
Map<String, Object> credentialSourceWithInvalidUrl = buildAwsIpv6CredentialSourceMap();
credentialSourceWithInvalidUrl.put(key, "https://badhost.com/fake");
IllegalArgumentException e =
assertThrows(
Expand All @@ -131,7 +124,7 @@ public void run() throws Throwable {
}
});

assertEquals(String.format("Invalid host %s for %s", "badhost.com", key), e.getMessage());
assertEquals(String.format("Invalid host badhost.com for %s.", key), e.getMessage());
}
}

Expand Down Expand Up @@ -768,6 +761,20 @@ private static AwsCredentialSource buildAwsCredentialSource(
return new AwsCredentialSource(credentialSourceMap);
}

private static Map<String, Object> buildAwsIpv6CredentialSourceMap() {
String regionUrl = "http://[fd00:ec2::254]/region";
String url = "http://[fd00:ec2::254]";
String imdsv2SessionTokenUrl = "http://[fd00:ec2::254]/imdsv2";
Map<String, Object> credentialSourceMap = new HashMap<>();
credentialSourceMap.put("environment_id", "aws1");
credentialSourceMap.put("region_url", regionUrl);
credentialSourceMap.put("url", url);
credentialSourceMap.put("imdsv2_session_token_url", imdsv2SessionTokenUrl);
credentialSourceMap.put("regional_cred_verification_url", GET_CALLER_IDENTITY_URL);

return credentialSourceMap;
}

static InputStream writeAwsCredentialsStream(String stsUrl, String regionUrl, String metadataUrl)
throws IOException {
GenericJson json = new GenericJson();
Expand Down

0 comments on commit 1fa7358

Please sign in to comment.