Skip to content

Commit 82134e3

Browse files
authored
fix(rt): ignore empty env variable and system properties for AWS credentials (#1080)
1 parent b417e60 commit 82134e3

File tree

5 files changed

+55
-10
lines changed

5 files changed

+55
-10
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "1691b2fe-15ba-4bf8-8f27-382b59c792c8",
3+
"type": "bugfix",
4+
"description": "Ignore empty environment variable and system property strings when evaluating AWS credentials"
5+
}

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/auth/credentials/EnvironmentCredentialsProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class EnvironmentCredentialsProvider(
2727
) : CredentialsProvider {
2828

2929
private fun requireEnv(variable: String): String =
30-
getEnv(variable) ?: throw ProviderConfigurationException("Missing value for environment variable `$variable`")
30+
getEnv(variable)?.takeIf(String::isNotBlank) ?: throw ProviderConfigurationException("Missing value for environment variable `$variable`")
3131

3232
override suspend fun resolve(attributes: Attributes): Credentials {
3333
coroutineContext.trace<EnvironmentCredentialsProvider> {

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/auth/credentials/SystemPropertyCredentialsProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class SystemPropertyCredentialsProvider(
2727
) : CredentialsProvider {
2828

2929
private fun requireProperty(variable: String): String =
30-
getProperty(variable) ?: throw ProviderConfigurationException("Missing value for system property `$variable`")
30+
getProperty(variable)?.takeIf(String::isNotBlank) ?: throw ProviderConfigurationException("Missing value for system property `$variable`")
3131

3232
override suspend fun resolve(attributes: Attributes): Credentials {
3333
coroutineContext.trace<SystemPropertyCredentialsProvider> {

aws-runtime/aws-config/common/test/aws/sdk/kotlin/runtime/auth/credentials/EnvironmentCredentialsProviderTest.kt

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class EnvironmentCredentialsProviderTest {
1717
private fun provider(vararg vars: Pair<String, String>) = EnvironmentCredentialsProvider((vars.toMap())::get)
1818

1919
@Test
20-
fun `it should read from environment variables (incl session token)`() = runTest {
20+
fun testReadFromEnvironmentIncludingSessionToken() = runTest {
2121
val provider = provider(
2222
AwsSdkSetting.AwsAccessKeyId.envVar to "abc",
2323
AwsSdkSetting.AwsSecretAccessKey.envVar to "def",
@@ -27,7 +27,7 @@ class EnvironmentCredentialsProviderTest {
2727
}
2828

2929
@Test
30-
fun `it should read from environment variables (excl session token)`() = runTest {
30+
fun testReadFromEnvironmentExcludingSessionToken() = runTest {
3131
val provider = provider(
3232
AwsSdkSetting.AwsAccessKeyId.envVar to "abc",
3333
AwsSdkSetting.AwsSecretAccessKey.envVar to "def",
@@ -36,16 +36,36 @@ class EnvironmentCredentialsProviderTest {
3636
}
3737

3838
@Test
39-
fun `it should throw an exception on missing access key`() = runTest {
39+
fun testThrowsWhenMissingAccessKey() = runTest {
4040
assertFailsWith<ProviderConfigurationException> {
4141
provider(AwsSdkSetting.AwsSecretAccessKey.envVar to "def").resolve()
4242
}.message.shouldContain("Missing value for environment variable `AWS_ACCESS_KEY_ID`")
4343
}
4444

4545
@Test
46-
fun `it should throw an exception on missing secret key`() = runTest {
46+
fun testThrowsWhenMissingSecretKey() = runTest {
4747
assertFailsWith<ProviderConfigurationException> {
4848
provider(AwsSdkSetting.AwsAccessKeyId.envVar to "abc").resolve()
4949
}.message.shouldContain("Missing value for environment variable `AWS_SECRET_ACCESS_KEY`")
5050
}
51+
52+
@Test
53+
fun testIgnoresEmptyAccessKey() = runTest {
54+
assertFailsWith<ProviderConfigurationException> {
55+
provider(
56+
AwsSdkSetting.AwsAccessKeyId.envVar to "",
57+
AwsSdkSetting.AwsSecretAccessKey.envVar to "abc",
58+
).resolve()
59+
}.message.shouldContain("Missing value for environment variable `AWS_ACCESS_KEY_ID`")
60+
}
61+
62+
@Test
63+
fun testIgnoresEmptySecretKey() = runTest {
64+
assertFailsWith<ProviderConfigurationException> {
65+
provider(
66+
AwsSdkSetting.AwsAccessKeyId.envVar to "abc",
67+
AwsSdkSetting.AwsSecretAccessKey.envVar to "",
68+
).resolve()
69+
}.message.shouldContain("Missing value for environment variable `AWS_SECRET_ACCESS_KEY`")
70+
}
5171
}

aws-runtime/aws-config/common/test/aws/sdk/kotlin/runtime/auth/credentials/SystemPropertyCredentialsProviderTest.kt

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class SystemPropertyCredentialsProviderTest {
1717
private fun provider(vararg vars: Pair<String, String>) = SystemPropertyCredentialsProvider((vars.toMap())::get)
1818

1919
@Test
20-
fun readAllSystemProperties() = runTest {
20+
fun testReadAllSystemProperties() = runTest {
2121
val provider = provider(
2222
AwsSdkSetting.AwsAccessKeyId.sysProp to "abc",
2323
AwsSdkSetting.AwsSecretAccessKey.sysProp to "def",
@@ -27,7 +27,7 @@ class SystemPropertyCredentialsProviderTest {
2727
}
2828

2929
@Test
30-
fun readAllSystemPropertiesExceptSessionToken() = runTest {
30+
fun testReadAllSystemPropertiesExceptSessionToken() = runTest {
3131
val provider = provider(
3232
AwsSdkSetting.AwsAccessKeyId.sysProp to "abc",
3333
AwsSdkSetting.AwsSecretAccessKey.sysProp to "def",
@@ -36,16 +36,36 @@ class SystemPropertyCredentialsProviderTest {
3636
}
3737

3838
@Test
39-
fun throwsExceptionWhenMissingAccessKey() = runTest {
39+
fun testThrowsExceptionWhenMissingAccessKey() = runTest {
4040
assertFailsWith<ProviderConfigurationException> {
4141
provider(AwsSdkSetting.AwsSecretAccessKey.sysProp to "def").resolve()
4242
}.message.shouldContain("Missing value for system property `aws.accessKeyId`")
4343
}
4444

4545
@Test
46-
fun throwsExceptionWhenMissingSecretKey() = runTest {
46+
fun testThrowsExceptionWhenMissingSecretKey() = runTest {
4747
assertFailsWith<ProviderConfigurationException> {
4848
provider(AwsSdkSetting.AwsAccessKeyId.sysProp to "abc").resolve()
4949
}.message.shouldContain("Missing value for system property `aws.secretAccessKey`")
5050
}
51+
52+
@Test
53+
fun testIgnoresEmptyAccessKey() = runTest {
54+
assertFailsWith<ProviderConfigurationException> {
55+
provider(
56+
AwsSdkSetting.AwsAccessKeyId.sysProp to "",
57+
AwsSdkSetting.AwsSecretAccessKey.sysProp to "abc",
58+
).resolve()
59+
}.message.shouldContain("Missing value for system property `aws.accessKeyId`")
60+
}
61+
62+
@Test
63+
fun testIgnoresEmptySecretKey() = runTest {
64+
assertFailsWith<ProviderConfigurationException> {
65+
provider(
66+
AwsSdkSetting.AwsAccessKeyId.sysProp to "abc",
67+
AwsSdkSetting.AwsSecretAccessKey.sysProp to "",
68+
).resolve()
69+
}.message.shouldContain("Missing value for system property `aws.secretAccessKey`")
70+
}
5171
}

0 commit comments

Comments
 (0)