Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: empty string check for aws url validation #1089

Merged
merged 5 commits into from Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 14 additions & 11 deletions oauth2_http/java/com/google/auth/oauth2/AwsCredentials.java
Expand Up @@ -137,18 +137,21 @@ private void validateMetadataServerUrls() {
validateMetadataServerUrlIfAny(this.imdsv2SessionTokenUrl, "imdsv2_session_token_url");
}

private static void validateMetadataServerUrlIfAny(String urlString, String nameInConfig) {
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, nameInConfig));
}
} catch (MalformedURLException malformedURLException) {
throw new IllegalArgumentException(malformedURLException);
@VisibleForTesting
static void validateMetadataServerUrlIfAny(String urlString, String nameInConfig) {
if (urlString == null || urlString.trim().length() == 0) {
return;
}

try {
URL url = new URL(urlString);
String host = url.getHost();
if (!host.equals("169.254.169.254") && !host.equals("[fd00:ec2::254]")) {
sai-sunder-s marked this conversation as resolved.
Show resolved Hide resolved
throw new IllegalArgumentException(
String.format("Invalid host %s for %s.", host, nameInConfig));
}
} catch (MalformedURLException malformedURLException) {
throw new IllegalArgumentException(malformedURLException);
}
}
}
Expand Down
Expand Up @@ -475,10 +475,22 @@ public void getAwsSecurityCredentials_fromEnvironmentVariablesWithToken() throws
.setEnv("AWS_SECRET_ACCESS_KEY", "awsSecretAccessKey")
.setEnv("AWS_SESSION_TOKEN", "awsSessionToken");

AwsCredentialSource credSource =
new AwsCredentialSource(
new HashMap<String, Object>() {
{
put("environment_id", "aws1");
put("region_url", "");
put("url", "");
put("regional_cred_verification_url", "regionalCredVerificationUrl");
}
});

AwsCredentials testAwsCredentials =
(AwsCredentials)
AwsCredentials.newBuilder(AWS_CREDENTIAL)
.setEnvironmentProvider(environmentProvider)
.setCredentialSource(credSource)
.build();

AwsSecurityCredentials credentials =
Expand All @@ -489,6 +501,66 @@ public void getAwsSecurityCredentials_fromEnvironmentVariablesWithToken() throws
assertEquals("awsSessionToken", credentials.getToken());
}

@Test
public void getAwsSecurityCredentials_fromEnvironmentVariables_noMetadataServerCall()
throws IOException {
TestEnvironmentProvider environmentProvider = new TestEnvironmentProvider();
environmentProvider
.setEnv("AWS_ACCESS_KEY_ID", "awsAccessKeyId")
.setEnv("AWS_SECRET_ACCESS_KEY", "awsSecretAccessKey")
.setEnv("AWS_SESSION_TOKEN", "awsSessionToken");

AwsCredentials testAwsCredentials =
(AwsCredentials)
AwsCredentials.newBuilder(AWS_CREDENTIAL)
.setEnvironmentProvider(environmentProvider)
.build();

AwsSecurityCredentials credentials =
testAwsCredentials.getAwsSecurityCredentials(EMPTY_METADATA_HEADERS);

assertEquals("awsAccessKeyId", credentials.getAccessKeyId());
assertEquals("awsSecretAccessKey", credentials.getSecretAccessKey());
assertEquals("awsSessionToken", credentials.getToken());
}

@Test
public void validateMetadataServerUrlIfAny_validUrls() {
sai-sunder-s marked this conversation as resolved.
Show resolved Hide resolved
String[] urls = {
"http://[fd00:ec2::254]/region",
"http://169.254.169.254",
"http://169.254.169.254/xyz",
" ",
"",
null
};
for (String url : urls) {
AwsCredentialSource.validateMetadataServerUrlIfAny(url, "url");
}
}

@Test
public void validateMetadataServerUrlIfAny_invalidUrls() {
Map<String, String> urls = new HashMap<String, String>();
urls.put("http://[fd00:ec2::255]/region", "[fd00:ec2::255]");
urls.put("http://fake.com/region", "fake.com");
urls.put("http://169.254.169.255", "169.254.169.255");

for (Map.Entry<String, String> entry : urls.entrySet()) {
IllegalArgumentException e =
assertThrows(
IllegalArgumentException.class,
new ThrowingRunnable() {
@Override
public void run() throws Throwable {
AwsCredentialSource.validateMetadataServerUrlIfAny(entry.getKey(), "url");
}
});

assertEquals(String.format("Invalid host %s for url.", entry.getValue()), e.getMessage());
}
}

@Test
public void getAwsSecurityCredentials_fromMetadataServer() throws IOException {
MockExternalAccountCredentialsTransportFactory transportFactory =
Expand Down