|
| 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 org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 20 | +import static org.junit.Assert.assertNotNull; |
| 21 | +import static org.junit.Assert.fail; |
| 22 | + |
| 23 | +import org.junit.After; |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | +import software.amazon.awssdk.core.SdkSystemSetting; |
| 27 | +import software.amazon.awssdk.core.exception.SdkClientException; |
| 28 | + |
| 29 | +/** |
| 30 | + * Tests for {@link InstanceProfileCredentialsProvider} using the extended IMDS path. |
| 31 | + */ |
| 32 | +public class InstanceProfileCredentialsProviderExtendedIntegrationTest { |
| 33 | + private static final String EXTENDED_PATH = "/latest/meta-data/iam/security-credentials-extended/"; |
| 34 | + private EC2MetadataServiceMock mockServer; |
| 35 | + |
| 36 | + @Before |
| 37 | + public void setUp() throws Exception { |
| 38 | + mockServer = new EC2MetadataServiceMock(EXTENDED_PATH); |
| 39 | + mockServer.start(); |
| 40 | + } |
| 41 | + |
| 42 | + @After |
| 43 | + public void tearDown() { |
| 44 | + mockServer.stop(); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void resolveCredentials_withExtendedPath_includesAccountId() { |
| 49 | + mockServer.setAvailableSecurityCredentials("test-role"); |
| 50 | + mockServer.setResponseFileName("sessionResponseExtended"); |
| 51 | + |
| 52 | + InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.builder().build(); |
| 53 | + AwsCredentials credentials = provider.resolveCredentials(); |
| 54 | + |
| 55 | + assertThat(credentials.accountId()).isPresent(); |
| 56 | + assertThat(credentials.accountId().get()).isEqualTo("123456789012"); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void resolveCredentials_withExtendedPath_hasCorrectCredentials() { |
| 61 | + mockServer.setAvailableSecurityCredentials("test-role"); |
| 62 | + mockServer.setResponseFileName("sessionResponseExtended"); |
| 63 | + |
| 64 | + InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.builder().build(); |
| 65 | + AwsCredentials credentials = provider.resolveCredentials(); |
| 66 | + |
| 67 | + assertThat(credentials).isInstanceOf(AwsSessionCredentials.class); |
| 68 | + assertThat(credentials.accessKeyId()).isEqualTo("ACCESS_KEY_ID"); |
| 69 | + assertThat(credentials.secretAccessKey()).isEqualTo("SECRET_ACCESS_KEY"); |
| 70 | + assertThat(((AwsSessionCredentials) credentials).sessionToken()).isEqualTo("TOKEN"); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testSessionCredentials_MultipleInstanceProfiles() { |
| 75 | + mockServer.setAvailableSecurityCredentials("test-credentials"); |
| 76 | + mockServer.setResponseFileName("sessionResponseExtended"); |
| 77 | + |
| 78 | + InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.builder().build(); |
| 79 | + AwsCredentials credentials = provider.resolveCredentials(); |
| 80 | + |
| 81 | + assertThat(credentials).isInstanceOf(AwsSessionCredentials.class); |
| 82 | + assertThat(credentials.accessKeyId()).isEqualTo("ACCESS_KEY_ID"); |
| 83 | + assertThat(credentials.secretAccessKey()).isEqualTo("SECRET_ACCESS_KEY"); |
| 84 | + assertThat(((AwsSessionCredentials) credentials).sessionToken()).isEqualTo("TOKEN"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testNoInstanceProfiles() throws Exception { |
| 89 | + mockServer.setResponseFileName("sessionResponseExtended"); |
| 90 | + mockServer.setAvailableSecurityCredentials(""); |
| 91 | + |
| 92 | + try (InstanceProfileCredentialsProvider credentialsProvider = InstanceProfileCredentialsProvider.create()) { |
| 93 | + |
| 94 | + try { |
| 95 | + credentialsProvider.resolveCredentials(); |
| 96 | + fail("Expected an SdkClientException, but wasn't thrown"); |
| 97 | + } catch (SdkClientException ace) { |
| 98 | + assertNotNull(ace.getMessage()); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void resolveCredentials_withDisabledMetadata_throwsException() { |
| 105 | + System.setProperty(SdkSystemSetting.AWS_EC2_METADATA_DISABLED.property(), "true"); |
| 106 | + try { |
| 107 | + InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.builder().build(); |
| 108 | + assertThatThrownBy(() -> provider.resolveCredentials()) |
| 109 | + .isInstanceOf(SdkClientException.class) |
| 110 | + .hasMessageContaining("IMDS credentials have been disabled"); |
| 111 | + } finally { |
| 112 | + System.clearProperty(SdkSystemSetting.AWS_EC2_METADATA_DISABLED.property()); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void resolveCredentials_withNoAvailableCredentials_throwsException() { |
| 118 | + mockServer.setAvailableSecurityCredentials(""); |
| 119 | + mockServer.setResponseFileName("sessionResponseExtended"); |
| 120 | + |
| 121 | + InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.builder().build(); |
| 122 | + assertThatThrownBy(() -> provider.resolveCredentials()) |
| 123 | + .isInstanceOf(SdkClientException.class) |
| 124 | + .hasMessageContaining("Failed to load credentials from IMDS."); |
| 125 | + } |
| 126 | +} |
0 commit comments