|
| 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.AssertionsForClassTypes.assertThat; |
| 19 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; |
| 20 | + |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Optional; |
| 24 | +import java.util.function.Consumer; |
| 25 | +import org.junit.jupiter.api.AfterEach; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.params.ParameterizedTest; |
| 28 | +import org.junit.jupiter.params.provider.Arguments; |
| 29 | +import org.junit.jupiter.params.provider.MethodSource; |
| 30 | +import software.amazon.awssdk.core.SdkSystemSetting; |
| 31 | +import software.amazon.awssdk.core.exception.SdkClientException; |
| 32 | +import software.amazon.awssdk.testutils.EnvironmentVariableHelper; |
| 33 | +import software.amazon.awssdk.utils.Pair; |
| 34 | + |
| 35 | +class SystemSettingCredentialsProvidersTest { |
| 36 | + |
| 37 | + private static final Pair<SdkSystemSetting, String> ACCESS_KEY_ID = Pair.of(SdkSystemSetting.AWS_ACCESS_KEY_ID, "access"); |
| 38 | + private static final Pair<SdkSystemSetting, String> SECRET_KEY = Pair.of(SdkSystemSetting.AWS_SECRET_ACCESS_KEY, "secret"); |
| 39 | + private static final Pair<SdkSystemSetting, String> SESSION_TOKEN = Pair.of(SdkSystemSetting.AWS_SESSION_TOKEN, "token"); |
| 40 | + private static final Pair<SdkSystemSetting, String> ACCOUNT_ID = Pair.of(SdkSystemSetting.AWS_ACCOUNT_ID, "accountid"); |
| 41 | + private static final EnvironmentVariableHelper ENVIRONMENT_VARIABLE_HELPER = new EnvironmentVariableHelper(); |
| 42 | + |
| 43 | + @BeforeEach |
| 44 | + public void setup() { |
| 45 | + clearSettings(); |
| 46 | + } |
| 47 | + |
| 48 | + @AfterEach |
| 49 | + public void teardown() { |
| 50 | + clearSettings(); |
| 51 | + } |
| 52 | + |
| 53 | + public static void clearSettings() { |
| 54 | + ENVIRONMENT_VARIABLE_HELPER.reset(); |
| 55 | + System.clearProperty(SdkSystemSetting.AWS_ACCESS_KEY_ID.property()); |
| 56 | + System.clearProperty(SdkSystemSetting.AWS_SECRET_ACCESS_KEY.property()); |
| 57 | + System.clearProperty(SdkSystemSetting.AWS_SESSION_TOKEN.property()); |
| 58 | + System.clearProperty(SdkSystemSetting.AWS_ACCOUNT_ID.property()); |
| 59 | + } |
| 60 | + |
| 61 | + @ParameterizedTest(name = "{index} - {0}") |
| 62 | + @MethodSource("config") |
| 63 | + void configureEnvVars_resolveCredentials(String description, |
| 64 | + List<Pair<SdkSystemSetting, String>> systemSettings, |
| 65 | + Consumer<AwsCredentials> expected) { |
| 66 | + configureEnvironmentVariables(systemSettings); |
| 67 | + EnvironmentVariableCredentialsProvider provider = EnvironmentVariableCredentialsProvider.create(); |
| 68 | + if (expected != null) { |
| 69 | + assertThat(provider.resolveCredentials()).satisfies(expected); |
| 70 | + } else { |
| 71 | + assertThatThrownBy(provider::resolveCredentials).isInstanceOf(SdkClientException.class); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @ParameterizedTest(name = "{index} - {0}") |
| 76 | + @MethodSource("config") |
| 77 | + void configureSystemProperties_resolveCredentials(String description, |
| 78 | + List<Pair<SdkSystemSetting, String>> systemSettings, |
| 79 | + Consumer<AwsCredentials> expected) { |
| 80 | + configureSystemProperties(systemSettings); |
| 81 | + SystemPropertyCredentialsProvider provider = SystemPropertyCredentialsProvider.create(); |
| 82 | + if (expected != null) { |
| 83 | + assertThat(provider.resolveCredentials()).satisfies(expected); |
| 84 | + } else { |
| 85 | + assertThatThrownBy(provider::resolveCredentials).isInstanceOf(SdkClientException.class); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private static List<Arguments> config() { |
| 90 | + return Arrays.asList( |
| 91 | + Arguments.of("When access key id and secret is set, return basic credentials", |
| 92 | + Arrays.asList(ACCESS_KEY_ID, SECRET_KEY), |
| 93 | + (Consumer<AwsCredentials>) awsCredentials -> { |
| 94 | + assertThat(awsCredentials.accessKeyId()).isEqualTo("access"); |
| 95 | + assertThat(awsCredentials.secretAccessKey()).isEqualTo("secret"); |
| 96 | + assertThat(awsCredentials).isNotInstanceOf(AwsSessionCredentials.class); |
| 97 | + assertThat(awsCredentials).hasFieldOrPropertyWithValue("accountId", null); |
| 98 | + }), |
| 99 | + Arguments.of("When access key id, secret and token is set, return session credentials", |
| 100 | + Arrays.asList(ACCESS_KEY_ID, SECRET_KEY, SESSION_TOKEN), |
| 101 | + (Consumer<AwsCredentials>) awsCredentials -> { |
| 102 | + assertThat(awsCredentials).isInstanceOf(AwsSessionCredentials.class); |
| 103 | + assertThat(((AwsSessionCredentials) awsCredentials).sessionToken()).isEqualTo("token"); |
| 104 | + }), |
| 105 | + Arguments.of("When access key id is null, throw exception", Arrays.asList(SECRET_KEY), null), |
| 106 | + Arguments.of("When secret key is null, throw exception", Arrays.asList(ACCESS_KEY_ID), null), |
| 107 | + Arguments.of("When account id is set, return basic credentials with account id", |
| 108 | + Arrays.asList(ACCESS_KEY_ID, SECRET_KEY, ACCOUNT_ID), |
| 109 | + (Consumer<AwsCredentials>) awsCredentials -> { |
| 110 | + assertThat(awsCredentials.accessKeyId()).isEqualTo("access"); |
| 111 | + assertThat(awsCredentials.secretAccessKey()).isEqualTo("secret"); |
| 112 | + assertThat(awsCredentials.accountId()).isPresent().isEqualTo(Optional.of("accountid")); |
| 113 | + assertThat(awsCredentials).isNotInstanceOf(AwsSessionCredentials.class); |
| 114 | + }), |
| 115 | + Arguments.of("When account id and token is set, return session credentials with account id", |
| 116 | + Arrays.asList(ACCESS_KEY_ID, SECRET_KEY, SESSION_TOKEN, ACCOUNT_ID), |
| 117 | + (Consumer<AwsCredentials>) awsCredentials -> { |
| 118 | + assertThat(awsCredentials.accessKeyId()).isEqualTo("access"); |
| 119 | + assertThat(awsCredentials.secretAccessKey()).isEqualTo("secret"); |
| 120 | + assertThat(awsCredentials.accountId()).isPresent().isEqualTo(Optional.of("accountid")); |
| 121 | + assertThat(awsCredentials).isInstanceOf(AwsSessionCredentials.class); |
| 122 | + }) |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + private void configureEnvironmentVariables(List<Pair<SdkSystemSetting, String>> systemSettings) { |
| 127 | + for (Pair<SdkSystemSetting, String> setting : systemSettings) { |
| 128 | + ENVIRONMENT_VARIABLE_HELPER.set(setting.left(), setting.right()); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private void configureSystemProperties(List<Pair<SdkSystemSetting, String>> systemSettings) { |
| 133 | + for (Pair<SdkSystemSetting, String> setting : systemSettings) { |
| 134 | + System.setProperty(setting.left().property(), setting.right()); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments