|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.auth.credentials; |
| 17 | + |
| 18 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; |
| 20 | +import static com.github.tomakehurst.wiremock.client.WireMock.get; |
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; |
| 22 | +import static com.github.tomakehurst.wiremock.client.WireMock.put; |
| 23 | +import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor; |
| 24 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; |
| 25 | +import static com.github.tomakehurst.wiremock.client.WireMock.verify; |
| 26 | +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +import com.github.tomakehurst.wiremock.junit5.WireMockExtension; |
| 30 | +import com.github.tomakehurst.wiremock.junit5.WireMockTest; |
| 31 | +import org.junit.jupiter.api.AfterAll; |
| 32 | +import org.junit.jupiter.api.BeforeEach; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 35 | +import software.amazon.awssdk.core.SdkSystemSetting; |
| 36 | +import software.amazon.awssdk.testutils.EnvironmentVariableHelper; |
| 37 | +import software.amazon.awssdk.utils.DateUtils; |
| 38 | + |
| 39 | +import java.time.Duration; |
| 40 | +import java.time.Instant; |
| 41 | + |
| 42 | +/** |
| 43 | + * Tests verifying IMDS credential resolution with account ID support. |
| 44 | + */ |
| 45 | +@WireMockTest |
| 46 | +public class InstanceProfileCredentialsProviderAccountIDTest { |
| 47 | + private static final String TOKEN_RESOURCE_PATH = "/latest/api/token"; |
| 48 | + private static final String CREDENTIALS_RESOURCE_PATH = "/latest/meta-data/iam/security-credentials/"; |
| 49 | + private static final String CREDENTIALS_EXTENDED_RESOURCE_PATH = "/latest/meta-data/iam/security-credentials-extended/"; |
| 50 | + private static final String TOKEN_HEADER = "x-aws-ec2-metadata-token"; |
| 51 | + private static final String TOKEN_STUB = "some-token"; |
| 52 | + private static final String PROFILE_NAME = "some-profile"; |
| 53 | + private static final String EC2_METADATA_TOKEN_TTL_HEADER = "x-aws-ec2-metadata-token-ttl-seconds"; |
| 54 | + private static final String ACCOUNT_ID = "123456789012"; |
| 55 | + private static final EnvironmentVariableHelper environmentVariableHelper = new EnvironmentVariableHelper(); |
| 56 | + |
| 57 | + @RegisterExtension |
| 58 | + static WireMockExtension wireMockServer = WireMockExtension.newInstance() |
| 59 | + .options(wireMockConfig().dynamicPort()) |
| 60 | + .configureStaticDsl(true) |
| 61 | + .build(); |
| 62 | + |
| 63 | + @BeforeEach |
| 64 | + public void methodSetup() { |
| 65 | + environmentVariableHelper.reset(); |
| 66 | + System.setProperty(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT.property(), |
| 67 | + "http://localhost:" + wireMockServer.getPort()); |
| 68 | + } |
| 69 | + |
| 70 | + @AfterAll |
| 71 | + public static void teardown() { |
| 72 | + System.clearProperty(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT.property()); |
| 73 | + environmentVariableHelper.reset(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void resolveCredentials_usesExtendedEndpoint_withAccountId() { |
| 78 | + String credentialsWithAccountId = String.format( |
| 79 | + "{\"AccessKeyId\":\"ACCESS_KEY_ID\"," + |
| 80 | + "\"SecretAccessKey\":\"SECRET_ACCESS_KEY\"," + |
| 81 | + "\"Token\":\"SESSION_TOKEN\"," + |
| 82 | + "\"Expiration\":\"%s\"," + |
| 83 | + "\"AccountId\":\"%s\"}", |
| 84 | + DateUtils.formatIso8601Date(Instant.now().plus(Duration.ofDays(1))), |
| 85 | + ACCOUNT_ID |
| 86 | + ); |
| 87 | + |
| 88 | + stubSecureCredentialsResponse(aResponse().withBody(credentialsWithAccountId), true); |
| 89 | + InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.builder().build(); |
| 90 | + AwsCredentials credentials = provider.resolveCredentials(); |
| 91 | + |
| 92 | + assertThat(credentials.accessKeyId()).isEqualTo("ACCESS_KEY_ID"); |
| 93 | + assertThat(credentials.secretAccessKey()).isEqualTo("SECRET_ACCESS_KEY"); |
| 94 | + assertThat(((AwsSessionCredentials)credentials).sessionToken()).isEqualTo("SESSION_TOKEN"); |
| 95 | + assertThat(credentials.accountId()).hasValue(ACCOUNT_ID); |
| 96 | + verifyImdsCallWithToken(true); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void resolveCredentials_fallsBackToLegacy_noAccountId() { |
| 101 | + String credentialsWithoutAccountId = String.format( |
| 102 | + "{\"AccessKeyId\":\"ACCESS_KEY_ID\"," + |
| 103 | + "\"SecretAccessKey\":\"SECRET_ACCESS_KEY\"," + |
| 104 | + "\"Token\":\"SESSION_TOKEN\"," + |
| 105 | + "\"Expiration\":\"%s\"," + |
| 106 | + "\"Code\":\"Success\"}", // No AccountId field at all |
| 107 | + DateUtils.formatIso8601Date(Instant.now().plus(Duration.ofDays(1))) |
| 108 | + ); |
| 109 | + |
| 110 | + stubSecureCredentialsResponse(aResponse().withBody(credentialsWithoutAccountId), false); |
| 111 | + InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.builder().build(); |
| 112 | + AwsCredentials credentials = provider.resolveCredentials(); |
| 113 | + |
| 114 | + assertThat(credentials.accessKeyId()).isEqualTo("ACCESS_KEY_ID"); |
| 115 | + assertThat(credentials.secretAccessKey()).isEqualTo("SECRET_ACCESS_KEY"); |
| 116 | + assertThat(((AwsSessionCredentials)credentials).sessionToken()).isEqualTo("SESSION_TOKEN"); |
| 117 | + verifyImdsCallWithToken(false); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void resolveCredentials_cachesProfile_maintainsAccountId() { |
| 122 | + String credentialsWithAccountId = String.format( |
| 123 | + "{\"AccessKeyId\":\"ACCESS_KEY_ID\"," + |
| 124 | + "\"SecretAccessKey\":\"SECRET_ACCESS_KEY\"," + |
| 125 | + "\"Token\":\"SESSION_TOKEN\"," + |
| 126 | + "\"Expiration\":\"%s\"," + |
| 127 | + "\"AccountId\":\"%s\"}", |
| 128 | + DateUtils.formatIso8601Date(Instant.now().plus(Duration.ofDays(1))), |
| 129 | + ACCOUNT_ID |
| 130 | + ); |
| 131 | + |
| 132 | + stubSecureCredentialsResponse(aResponse().withBody(credentialsWithAccountId), true); |
| 133 | + InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.builder().build(); |
| 134 | + |
| 135 | + // First call |
| 136 | + AwsCredentials creds1 = provider.resolveCredentials(); |
| 137 | + assertThat(creds1.accountId()).hasValue(ACCOUNT_ID); |
| 138 | + |
| 139 | + // Second call - should use cached profile |
| 140 | + AwsCredentials creds2 = provider.resolveCredentials(); |
| 141 | + assertThat(creds2.accountId()).hasValue(ACCOUNT_ID); |
| 142 | + |
| 143 | + // Verify profile discovery only called once |
| 144 | + verify(1, getRequestedFor(urlPathEqualTo(CREDENTIALS_EXTENDED_RESOURCE_PATH))); |
| 145 | + } |
| 146 | + |
| 147 | + private void stubSecureCredentialsResponse(com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder responseDefinitionBuilder, boolean useExtended) { |
| 148 | + wireMockServer.stubFor(put(urlPathEqualTo(TOKEN_RESOURCE_PATH)).willReturn(aResponse().withBody(TOKEN_STUB))); |
| 149 | + String path = useExtended ? CREDENTIALS_EXTENDED_RESOURCE_PATH : CREDENTIALS_RESOURCE_PATH; |
| 150 | + |
| 151 | + if (useExtended) { |
| 152 | + wireMockServer.stubFor(get(urlPathEqualTo(path)).willReturn(aResponse().withBody(PROFILE_NAME))); |
| 153 | + wireMockServer.stubFor(get(urlPathEqualTo(path + PROFILE_NAME)).willReturn(responseDefinitionBuilder)); |
| 154 | + } else { |
| 155 | + // Extended endpoint fails, fallback to legacy |
| 156 | + wireMockServer.stubFor(get(urlPathEqualTo(CREDENTIALS_EXTENDED_RESOURCE_PATH)) |
| 157 | + .willReturn(aResponse().withStatus(404))); |
| 158 | + wireMockServer.stubFor(get(urlPathEqualTo(CREDENTIALS_EXTENDED_RESOURCE_PATH + PROFILE_NAME)) |
| 159 | + .willReturn(aResponse().withStatus(404))); |
| 160 | + wireMockServer.stubFor(get(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH)).willReturn(aResponse().withBody(PROFILE_NAME))); |
| 161 | + wireMockServer.stubFor(get(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH + PROFILE_NAME)).willReturn(responseDefinitionBuilder)); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private void verifyImdsCallWithToken(boolean useExtended) { |
| 166 | + verify(putRequestedFor(urlPathEqualTo(TOKEN_RESOURCE_PATH)) |
| 167 | + .withHeader(EC2_METADATA_TOKEN_TTL_HEADER, equalTo("21600"))); |
| 168 | + |
| 169 | + String path = useExtended ? CREDENTIALS_EXTENDED_RESOURCE_PATH : CREDENTIALS_RESOURCE_PATH; |
| 170 | + verify(getRequestedFor(urlPathEqualTo(path)) |
| 171 | + .withHeader(TOKEN_HEADER, equalTo(TOKEN_STUB))); |
| 172 | + verify(getRequestedFor(urlPathEqualTo(path + PROFILE_NAME)) |
| 173 | + .withHeader(TOKEN_HEADER, equalTo(TOKEN_STUB))); |
| 174 | + |
| 175 | + if (useExtended) { |
| 176 | + // Verify extended endpoint was tried first |
| 177 | + verify(getRequestedFor(urlPathEqualTo(CREDENTIALS_EXTENDED_RESOURCE_PATH))); |
| 178 | + verify(getRequestedFor(urlPathEqualTo(CREDENTIALS_EXTENDED_RESOURCE_PATH + PROFILE_NAME))); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments