Skip to content

Commit f6e9680

Browse files
Add plain text secret support in Secrets Manager integration (#338)
Co-authored-by: Maciej Walkowiak <walkowiak.maciej@yahoo.com>
1 parent bb9992b commit f6e9680

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

spring-cloud-aws-autoconfigure/src/test/java/io/awspring/cloud/autoconfigure/config/secretsmanager/SecretsManagerConfigDataLoaderIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void outputsDebugLogs(CapturedOutput output) {
142142
try (ConfigurableApplicationContext context = runApplication(application,
143143
"aws-secretsmanager:/config/spring;/config/second")) {
144144
context.getEnvironment().getProperty("message");
145-
assertThat(output.getAll()).contains("Populating property retrieved from AWS Parameter Store: message");
145+
assertThat(output.getAll()).contains("Populating property retrieved from AWS Secrets Manager: message");
146146
}
147147
}
148148

spring-cloud-aws-secrets-manager/src/main/java/io/awspring/cloud/secretsmanager/SecretsManagerPropertySource.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.awspring.cloud.secretsmanager;
1717

18+
import com.fasterxml.jackson.core.JsonParseException;
1819
import com.fasterxml.jackson.core.JsonProcessingException;
1920
import com.fasterxml.jackson.core.type.TypeReference;
2021
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -75,17 +76,22 @@ public Object getProperty(String name) {
7576
}
7677

7778
private void readSecretValue(GetSecretValueRequest secretValueRequest) {
79+
GetSecretValueResponse secretValueResponse = source.getSecretValue(secretValueRequest);
7880
try {
79-
GetSecretValueResponse secretValueResponse = source.getSecretValue(secretValueRequest);
8081
Map<String, Object> secretMap = jsonMapper.readValue(secretValueResponse.secretString(),
8182
new TypeReference<Map<String, Object>>() {
8283
});
8384

8485
for (Map.Entry<String, Object> secretEntry : secretMap.entrySet()) {
85-
LOG.debug("Populating property retrieved from AWS Parameter Store: " + secretEntry.getKey());
86+
LOG.debug("Populating property retrieved from AWS Secrets Manager: " + secretEntry.getKey());
8687
properties.put(secretEntry.getKey(), secretEntry.getValue());
8788
}
8889
}
90+
catch (JsonParseException e) {
91+
// If the secret is not a JSON string, then it is a simple "plain text" string
92+
LOG.debug("Populating property retrieved from AWS Secrets Manager: " + secretValueResponse.name());
93+
properties.put(secretValueResponse.name(), secretValueResponse.secretString());
94+
}
8995
catch (JsonProcessingException e) {
9096
throw new RuntimeException(e);
9197
}

spring-cloud-aws-secrets-manager/src/test/java/io/awspring/cloud/secretsmanager/SecretsManagerPropertySourceTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ void shouldParseSecretValue() {
5252
assertThat(propertySource.getProperty("key2")).isEqualTo("value2");
5353
}
5454

55+
@Test
56+
void shouldParsePlainTextSecretValue() {
57+
GetSecretValueResponse secretValueResult = GetSecretValueResponse.builder().secretString("my secret")
58+
.name("secret name").build();
59+
60+
when(client.getSecretValue(any(GetSecretValueRequest.class))).thenReturn(secretValueResult);
61+
62+
propertySource.init();
63+
64+
assertThat(propertySource.getPropertyNames()).containsExactly("secret name");
65+
assertThat(propertySource.getProperty("secret name")).isEqualTo("my secret");
66+
}
67+
5568
@Test
5669
void throwsExceptionWhenSecretNotFound() {
5770
when(client.getSecretValue(any(GetSecretValueRequest.class)))

0 commit comments

Comments
 (0)