|
| 1 | +/* |
| 2 | + * Copyright 2010-2019 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.stubFor; |
| 25 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; |
| 26 | +import static org.hamcrest.Matchers.instanceOf; |
| 27 | +import com.github.tomakehurst.wiremock.client.WireMock; |
| 28 | +import com.github.tomakehurst.wiremock.junit.WireMockRule; |
| 29 | +import java.net.SocketTimeoutException; |
| 30 | +import java.time.Duration; |
| 31 | +import java.time.Instant; |
| 32 | +import org.junit.AfterClass; |
| 33 | +import org.junit.Before; |
| 34 | +import org.junit.Rule; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.rules.ExpectedException; |
| 37 | +import software.amazon.awssdk.core.SdkSystemSetting; |
| 38 | +import software.amazon.awssdk.core.exception.SdkClientException; |
| 39 | +import software.amazon.awssdk.core.internal.util.UserAgentUtils; |
| 40 | +import software.amazon.awssdk.utils.DateUtils; |
| 41 | + |
| 42 | +public class InstanceProfileCredentialsProviderTest { |
| 43 | + private static final String TOKEN_RESOURCE_PATH = "/latest/api/token"; |
| 44 | + private static final String CREDENTIALS_RESOURCE_PATH = "/latest/meta-data/iam/security-credentials/"; |
| 45 | + private static final String STUB_CREDENTIALS = "{\"AccessKeyId\":\"ACCESS_KEY_ID\",\"SecretAccessKey\":\"SECRET_ACCESS_KEY\"," |
| 46 | + + "\"Expiration\":\"" + DateUtils.formatIso8601Date(Instant.now().plus(Duration.ofDays(1))) |
| 47 | + + "\"}"; |
| 48 | + private static final String TOKEN_HEADER = "x-aws-ec2-metadata-token"; |
| 49 | + private static final String EC2_METADATA_TOKEN_TTL_HEADER = "x-aws-ec2-metadata-token-ttl-seconds"; |
| 50 | + |
| 51 | + |
| 52 | + @Rule |
| 53 | + public ExpectedException thrown = ExpectedException.none(); |
| 54 | + |
| 55 | + @Rule |
| 56 | + public WireMockRule mockMetadataEndpoint = new WireMockRule(); |
| 57 | + |
| 58 | + @Before |
| 59 | + public void methodSetup() { |
| 60 | + System.setProperty(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT.property(), "http://localhost:" + mockMetadataEndpoint.port()); |
| 61 | + } |
| 62 | + |
| 63 | + @AfterClass |
| 64 | + public static void teardown() { |
| 65 | + System.clearProperty(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT.property()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void resolveCredentials_metadataLookupDisabled_throws() { |
| 70 | + System.setProperty(SdkSystemSetting.AWS_EC2_METADATA_DISABLED.property(), "true"); |
| 71 | + thrown.expect(SdkClientException.class); |
| 72 | + thrown.expectMessage("Loading credentials from local endpoint is disabled"); |
| 73 | + try { |
| 74 | + InstanceProfileCredentialsProvider.builder().build().resolveCredentials(); |
| 75 | + } finally { |
| 76 | + System.clearProperty(SdkSystemSetting.AWS_EC2_METADATA_DISABLED.property()); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void resolveCredentials_requestsIncludeUserAgent() { |
| 82 | + String stubToken = "some-token"; |
| 83 | + stubFor(put(urlPathEqualTo(TOKEN_RESOURCE_PATH)).willReturn(aResponse().withBody(stubToken))); |
| 84 | + stubFor(get(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH)).willReturn(aResponse().withBody("some-profile"))); |
| 85 | + stubFor(get(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH + "some-profile")).willReturn(aResponse().withBody(STUB_CREDENTIALS))); |
| 86 | + |
| 87 | + InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.builder().build(); |
| 88 | + |
| 89 | + provider.resolveCredentials(); |
| 90 | + |
| 91 | + String userAgentHeader = "User-Agent"; |
| 92 | + String userAgent = UserAgentUtils.getUserAgent(); |
| 93 | + WireMock.verify(putRequestedFor(urlPathEqualTo(TOKEN_RESOURCE_PATH)).withHeader(userAgentHeader, equalTo(userAgent))); |
| 94 | + WireMock.verify(getRequestedFor(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH)).withHeader(userAgentHeader, equalTo(userAgent))); |
| 95 | + WireMock.verify(getRequestedFor(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH + "some-profile")).withHeader(userAgentHeader, equalTo(userAgent))); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void resolveCredentials_queriesTokenResource() { |
| 100 | + stubFor(put(urlPathEqualTo(TOKEN_RESOURCE_PATH)).willReturn(aResponse().withBody("some-token"))); |
| 101 | + stubFor(get(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH)).willReturn(aResponse().withBody("some-profile"))); |
| 102 | + stubFor(get(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH + "some-profile")).willReturn(aResponse().withBody(STUB_CREDENTIALS))); |
| 103 | + |
| 104 | + InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.builder().build(); |
| 105 | + |
| 106 | + provider.resolveCredentials(); |
| 107 | + |
| 108 | + WireMock.verify(putRequestedFor(urlPathEqualTo(TOKEN_RESOURCE_PATH)).withHeader(EC2_METADATA_TOKEN_TTL_HEADER, equalTo("21600"))); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void resolveCredentials_queriesTokenResource_includedInCredentialsRequests() { |
| 113 | + String stubToken = "some-token"; |
| 114 | + stubFor(put(urlPathEqualTo(TOKEN_RESOURCE_PATH)).willReturn(aResponse().withBody(stubToken))); |
| 115 | + stubFor(get(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH)).willReturn(aResponse().withBody("some-profile"))); |
| 116 | + stubFor(get(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH + "some-profile")).willReturn(aResponse().withBody(STUB_CREDENTIALS))); |
| 117 | + |
| 118 | + InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.builder().build(); |
| 119 | + |
| 120 | + provider.resolveCredentials(); |
| 121 | + |
| 122 | + WireMock.verify(getRequestedFor(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH)).withHeader(TOKEN_HEADER, equalTo(stubToken))); |
| 123 | + WireMock.verify(getRequestedFor(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH + "some-profile")).withHeader(TOKEN_HEADER, equalTo(stubToken))); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void resolveCredentials_queriesTokenResource_404Error_fallbackToInsecure() { |
| 128 | + stubFor(put(urlPathEqualTo(TOKEN_RESOURCE_PATH)).willReturn(aResponse().withStatus(404).withBody("oops"))); |
| 129 | + stubFor(get(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH)).willReturn(aResponse().withBody("some-profile"))); |
| 130 | + stubFor(get(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH + "some-profile")).willReturn(aResponse().withBody(STUB_CREDENTIALS))); |
| 131 | + |
| 132 | + InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.builder().build(); |
| 133 | + |
| 134 | + provider.resolveCredentials(); |
| 135 | + |
| 136 | + WireMock.verify(getRequestedFor(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH))); |
| 137 | + WireMock.verify(getRequestedFor(urlPathEqualTo(CREDENTIALS_RESOURCE_PATH + "some-profile"))); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void resolveCredentials_queriesTokenResource_400Error_throws() { |
| 142 | + thrown.expect(SdkClientException.class); |
| 143 | + thrown.expectMessage("Unable to load credentials"); |
| 144 | + |
| 145 | + stubFor(put(urlPathEqualTo(TOKEN_RESOURCE_PATH)).willReturn(aResponse().withStatus(400).withBody("oops"))); |
| 146 | + |
| 147 | + InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.builder().build(); |
| 148 | + |
| 149 | + provider.resolveCredentials(); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void resolveCredentials_queriesTokenResource_socketTimeout_throws() { |
| 154 | + thrown.expect(SdkClientException.class); |
| 155 | + thrown.expectCause(instanceOf(SocketTimeoutException.class)); |
| 156 | + thrown.expectMessage("Unable to load credentials"); |
| 157 | + |
| 158 | + stubFor(put(urlPathEqualTo(TOKEN_RESOURCE_PATH)).willReturn(aResponse().withBody("some-token").withFixedDelay(Integer.MAX_VALUE))); |
| 159 | + |
| 160 | + InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.builder().build(); |
| 161 | + |
| 162 | + provider.resolveCredentials(); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void resolveCredentials_endpointSettingEmpty_throws() { |
| 167 | + thrown.expect(SdkClientException.class); |
| 168 | + |
| 169 | + System.setProperty(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT.property(), ""); |
| 170 | + InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.builder().build(); |
| 171 | + |
| 172 | + provider.resolveCredentials(); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + public void resolveCredentials_endpointSettingHostNotExists_throws() { |
| 177 | + thrown.expect(SdkClientException.class); |
| 178 | + |
| 179 | + System.setProperty(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT.property(), "some-host-that-does-not-exist"); |
| 180 | + InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.builder().build(); |
| 181 | + |
| 182 | + provider.resolveCredentials(); |
| 183 | + } |
| 184 | +} |
0 commit comments