Skip to content

Commit a4699f1

Browse files
authored
Adding accountId support to environment variable credential provider (#4327)
* Adding accountId support to environment variable and system property credentials providers * Fixes a test case referencing the wrong credentials provider
1 parent c5c8267 commit a4699f1

File tree

3 files changed

+155
-2
lines changed

3 files changed

+155
-2
lines changed

core/auth/src/main/java/software/amazon/awssdk/auth/credentials/internal/SystemSettingsCredentialsProvider.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public AwsCredentials resolveCredentials() {
4848
String accessKey = trim(loadSetting(SdkSystemSetting.AWS_ACCESS_KEY_ID).orElse(null));
4949
String secretKey = trim(loadSetting(SdkSystemSetting.AWS_SECRET_ACCESS_KEY).orElse(null));
5050
String sessionToken = trim(loadSetting(SdkSystemSetting.AWS_SESSION_TOKEN).orElse(null));
51+
String accountId = trim(loadSetting(SdkSystemSetting.AWS_ACCOUNT_ID).orElse(null));
5152

5253
if (StringUtils.isBlank(accessKey)) {
5354
throw SdkClientException.builder()
@@ -67,8 +68,18 @@ public AwsCredentials resolveCredentials() {
6768
.build();
6869
}
6970

70-
return StringUtils.isBlank(sessionToken) ? AwsBasicCredentials.create(accessKey, secretKey)
71-
: AwsSessionCredentials.create(accessKey, secretKey, sessionToken);
71+
return StringUtils.isBlank(sessionToken) ?
72+
AwsBasicCredentials.builder()
73+
.accessKeyId(accessKey)
74+
.secretAccessKey(secretKey)
75+
.accountId(accountId)
76+
.build() :
77+
AwsSessionCredentials.builder()
78+
.accessKeyId(accessKey)
79+
.secretAccessKey(secretKey)
80+
.sessionToken(sessionToken)
81+
.accountId(accountId)
82+
.build();
7283
}
7384

7485
/**
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/SdkSystemSetting.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public enum SdkSystemSetting implements SystemSetting {
4545
*/
4646
AWS_SESSION_TOKEN("aws.sessionToken", null),
4747

48+
/**
49+
* Configure the AWS account id associated with credentials supplied through system properties.
50+
*/
51+
AWS_ACCOUNT_ID("aws.accountId", null),
52+
4853
/**
4954
* Configure the AWS web identity token file path.
5055
*/

0 commit comments

Comments
 (0)