Skip to content

Commit 77a99c9

Browse files
authored
fix: Remove AWS credential source validation. (#1177)
1 parent e6c23d3 commit 77a99c9

File tree

2 files changed

+4
-85
lines changed

2 files changed

+4
-85
lines changed

oauth2_http/java/com/google/auth/oauth2/AwsCredentials.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
import com.google.common.collect.ImmutableList;
4545
import java.io.IOException;
4646
import java.io.UnsupportedEncodingException;
47-
import java.net.MalformedURLException;
48-
import java.net.URL;
4947
import java.net.URLEncoder;
5048
import java.util.ArrayList;
5149
import java.util.Collection;
@@ -137,32 +135,6 @@ static class AwsCredentialSource extends CredentialSource {
137135
} else {
138136
this.imdsv2SessionTokenUrl = null;
139137
}
140-
141-
this.validateMetadataServerUrls();
142-
}
143-
144-
private void validateMetadataServerUrls() {
145-
validateMetadataServerUrlIfAny(this.regionUrl, "region_url");
146-
validateMetadataServerUrlIfAny(this.url, "url");
147-
validateMetadataServerUrlIfAny(this.imdsv2SessionTokenUrl, "imdsv2_session_token_url");
148-
}
149-
150-
@VisibleForTesting
151-
static void validateMetadataServerUrlIfAny(String urlString, String nameInConfig) {
152-
if (urlString == null || urlString.trim().length() == 0) {
153-
return;
154-
}
155-
156-
try {
157-
URL url = new URL(urlString);
158-
String host = url.getHost();
159-
if (!host.equals("169.254.169.254") && !host.equals("[fd00:ec2::254]")) {
160-
throw new IllegalArgumentException(
161-
String.format("Invalid host %s for %s.", host, nameInConfig));
162-
}
163-
} catch (MalformedURLException malformedURLException) {
164-
throw new IllegalArgumentException(malformedURLException);
165-
}
166138
}
167139
}
168140

oauth2_http/javatests/com/google/auth/oauth2/AwsCredentialsTest.java

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import static org.junit.Assert.assertEquals;
3636
import static org.junit.Assert.assertNotNull;
3737
import static org.junit.Assert.assertNull;
38-
import static org.junit.Assert.assertThrows;
3938
import static org.junit.Assert.assertTrue;
4039
import static org.junit.Assert.fail;
4140

@@ -57,7 +56,6 @@
5756
import java.util.List;
5857
import java.util.Map;
5958
import org.junit.Test;
60-
import org.junit.function.ThrowingRunnable;
6159
import org.junit.runner.RunWith;
6260
import org.junit.runners.JUnit4;
6361

@@ -106,28 +104,14 @@ public class AwsCredentialsTest extends BaseSerializationTest {
106104
.build();
107105

108106
@Test
109-
public void test_awsCredentialSource_ipv6() {
110-
// If no exception is thrown, it means the urls were valid.
111-
new AwsCredentialSource(buildAwsIpv6CredentialSourceMap());
112-
}
113-
114-
@Test
115-
public void test_awsCredentialSource_invalid_urls() {
107+
public void test_awsCredentialSource() {
116108
String keys[] = {"region_url", "url", "imdsv2_session_token_url"};
117109
for (String key : keys) {
118110
Map<String, Object> credentialSourceWithInvalidUrl = buildAwsIpv6CredentialSourceMap();
119111
credentialSourceWithInvalidUrl.put(key, "https://badhost.com/fake");
120-
IllegalArgumentException e =
121-
assertThrows(
122-
IllegalArgumentException.class,
123-
new ThrowingRunnable() {
124-
@Override
125-
public void run() throws Throwable {
126-
new AwsCredentialSource(credentialSourceWithInvalidUrl);
127-
}
128-
});
129-
130-
assertEquals(String.format("Invalid host badhost.com for %s.", key), e.getMessage());
112+
113+
// Should succeed as no validation is done.
114+
new AwsCredentialSource(credentialSourceWithInvalidUrl);
131115
}
132116
}
133117

@@ -613,43 +597,6 @@ public void getAwsSecurityCredentials_fromEnvironmentVariables_noMetadataServerC
613597
assertEquals("awsSessionToken", credentials.getToken());
614598
}
615599

616-
@Test
617-
public void validateMetadataServerUrlIfAny_validOrEmptyUrls() {
618-
String[] urls = {
619-
"http://[fd00:ec2::254]/region",
620-
"http://169.254.169.254",
621-
"http://169.254.169.254/xyz",
622-
" ",
623-
"",
624-
null
625-
};
626-
for (String url : urls) {
627-
AwsCredentialSource.validateMetadataServerUrlIfAny(url, "url");
628-
}
629-
}
630-
631-
@Test
632-
public void validateMetadataServerUrlIfAny_invalidUrls() {
633-
Map<String, String> urls = new HashMap<String, String>();
634-
urls.put("http://[fd00:ec2::255]/region", "[fd00:ec2::255]");
635-
urls.put("http://fake.com/region", "fake.com");
636-
urls.put("http://169.254.169.255", "169.254.169.255");
637-
638-
for (Map.Entry<String, String> entry : urls.entrySet()) {
639-
IllegalArgumentException e =
640-
assertThrows(
641-
IllegalArgumentException.class,
642-
new ThrowingRunnable() {
643-
@Override
644-
public void run() throws Throwable {
645-
AwsCredentialSource.validateMetadataServerUrlIfAny(entry.getKey(), "url");
646-
}
647-
});
648-
649-
assertEquals(String.format("Invalid host %s for url.", entry.getValue()), e.getMessage());
650-
}
651-
}
652-
653600
@Test
654601
public void getAwsSecurityCredentials_fromMetadataServer() throws IOException {
655602
MockExternalAccountCredentialsTransportFactory transportFactory =

0 commit comments

Comments
 (0)