3434import static org .junit .Assert .assertEquals ;
3535import static org .junit .Assert .assertNotNull ;
3636import static org .junit .Assert .assertNull ;
37+ import static org .junit .Assert .assertThrows ;
3738import static org .junit .Assert .assertTrue ;
3839import static org .junit .Assert .fail ;
3940
5354import java .util .List ;
5455import java .util .Map ;
5556import org .junit .Test ;
57+ import org .junit .function .ThrowingRunnable ;
5658import org .junit .runner .RunWith ;
5759import org .junit .runners .JUnit4 ;
5860
6163public class AwsCredentialsTest {
6264
6365 private static final String STS_URL = "https://sts.googleapis.com" ;
64- private static final String AWS_CREDENTIALS_URL = "https://www.aws-credentials.com" ;
65- private static final String AWS_CREDENTIALS_URL_WITH_ROLE =
66- "https://www.aws-credentials.com/roleName" ;
67- private static final String AWS_REGION_URL = "https://www.aws-region.com" ;
68- private static final String AWS_IMDSV2_SESSION_TOKEN_URL = "https://www.aws-session-token.com" ;
66+ private static final String AWS_CREDENTIALS_URL = "https://169.254.169.254" ;
67+ private static final String AWS_CREDENTIALS_URL_WITH_ROLE = "https://169.254.169.254/roleName" ;
68+ private static final String AWS_REGION_URL = "https://169.254.169.254/region" ;
69+ private static final String AWS_IMDSV2_SESSION_TOKEN_URL = "https://169.254.169.254/imdsv2" ;
6970 private static final String AWS_IMDSV2_SESSION_TOKEN = "sessiontoken" ;
7071
7172 private static final String GET_CALLER_IDENTITY_URL =
@@ -78,8 +79,8 @@ public class AwsCredentialsTest {
7879 new HashMap <String , Object >() {
7980 {
8081 put ("environment_id" , "aws1" );
81- put ("region_url" , "regionUrl" );
82- put ("url" , "url" );
82+ put ("region_url" , AWS_REGION_URL );
83+ put ("url" , AWS_CREDENTIALS_URL );
8384 put ("regional_cred_verification_url" , "regionalCredVerificationUrl" );
8485 }
8586 };
@@ -101,6 +102,32 @@ public class AwsCredentialsTest {
101102 .setCredentialSource (AWS_CREDENTIAL_SOURCE )
102103 .build ();
103104
105+ @ Test
106+ public void test_awsCredentialSource_ipv6 () {
107+ // If no exception is thrown, it means the urls were valid.
108+ new AwsCredentialSource (buildAwsIpv6CredentialSourceMap ());
109+ }
110+
111+ @ Test
112+ public void test_awsCredentialSource_invalid_urls () {
113+ String keys [] = {"region_url" , "url" , "imdsv2_session_token_url" };
114+ for (String key : keys ) {
115+ Map <String , Object > credentialSourceWithInvalidUrl = buildAwsIpv6CredentialSourceMap ();
116+ credentialSourceWithInvalidUrl .put (key , "https://badhost.com/fake" );
117+ IllegalArgumentException e =
118+ assertThrows (
119+ IllegalArgumentException .class ,
120+ new ThrowingRunnable () {
121+ @ Override
122+ public void run () throws Throwable {
123+ new AwsCredentialSource (credentialSourceWithInvalidUrl );
124+ }
125+ });
126+
127+ assertEquals (String .format ("Invalid host badhost.com for %s." , key ), e .getMessage ());
128+ }
129+ }
130+
104131 @ Test
105132 public void refreshAccessToken_withoutServiceAccountImpersonation () throws IOException {
106133 MockExternalAccountCredentialsTransportFactory transportFactory =
@@ -734,6 +761,20 @@ private static AwsCredentialSource buildAwsCredentialSource(
734761 return new AwsCredentialSource (credentialSourceMap );
735762 }
736763
764+ private static Map <String , Object > buildAwsIpv6CredentialSourceMap () {
765+ String regionUrl = "http://[fd00:ec2::254]/region" ;
766+ String url = "http://[fd00:ec2::254]" ;
767+ String imdsv2SessionTokenUrl = "http://[fd00:ec2::254]/imdsv2" ;
768+ Map <String , Object > credentialSourceMap = new HashMap <>();
769+ credentialSourceMap .put ("environment_id" , "aws1" );
770+ credentialSourceMap .put ("region_url" , regionUrl );
771+ credentialSourceMap .put ("url" , url );
772+ credentialSourceMap .put ("imdsv2_session_token_url" , imdsv2SessionTokenUrl );
773+ credentialSourceMap .put ("regional_cred_verification_url" , GET_CALLER_IDENTITY_URL );
774+
775+ return credentialSourceMap ;
776+ }
777+
737778 static InputStream writeAwsCredentialsStream (String stsUrl , String regionUrl , String metadataUrl )
738779 throws IOException {
739780 GenericJson json = new GenericJson ();
0 commit comments