Skip to content

Commit

Permalink
Allow prefix for Secrets Manager to be empty (#233)
Browse files Browse the repository at this point in the history
Fixes #232
  • Loading branch information
MatejNedic authored Feb 9, 2022
1 parent 77dff95 commit ed7b43f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ public class AwsSecretsManagerProperties implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {

if (!StringUtils.hasLength(prefix)) {
throw new ValidationException(CONFIG_PREFIX + ".prefix", "prefix should not be empty or null.");
}

if (!StringUtils.hasLength(defaultContext)) {
throw new ValidationException(CONFIG_PREFIX + ".defaultContext",
"defaultContext should not be empty or null.");
Expand All @@ -100,7 +96,7 @@ public void afterPropertiesSet() throws Exception {
"profileSeparator should not be empty or null.");
}

if (!PREFIX_PATTERN.matcher(prefix).matches()) {
if (StringUtils.hasLength(prefix) && !PREFIX_PATTERN.matcher(prefix).matches()) {
throw new ValidationException(CONFIG_PREFIX + ".prefix",
"The prefix must have pattern of: " + PREFIX_PATTERN.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import com.amazonaws.services.secretsmanager.AWSSecretsManager;
import org.apache.commons.logging.Log;

import org.springframework.util.StringUtils;

/**
* Is responsible for creating {@link AwsSecretsManagerPropertySource} and determining
* automatic contexts.
Expand Down Expand Up @@ -60,7 +58,7 @@ public List<String> getAutomaticContexts(List<String> profiles) {
}

private String getContext(String prefix, String context) {
if (StringUtils.hasLength(prefix)) {
if (prefix != null) {
return prefix + "/" + context;
}
return context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ void validationSucceeds(AwsSecretsManagerProperties properties) {

private static Stream<Arguments> validProperties() {
return Stream.of(
Arguments.of(new AwsSecretsManagerPropertiesBuilder().withPrefix("").withDefaultContext("app")
.withProfileSeparator("_").build()),
Arguments.of(new AwsSecretsManagerPropertiesBuilder().withPrefix("/sec").withDefaultContext("app")
.withProfileSeparator("_").build()),
Arguments.of(new AwsSecretsManagerPropertiesBuilder().withPrefix("/sec/test/var")
Expand All @@ -61,7 +63,6 @@ private static Stream<Arguments> validProperties() {

private static Stream<Arguments> invalidProperties() {
return Stream.of(
Arguments.of(new AwsSecretsManagerPropertiesBuilder().withPrefix("").build(), "prefix", "NotEmpty"),
Arguments.of(new AwsSecretsManagerPropertiesBuilder().withPrefix("!.").build(), "prefix", "Pattern"),
Arguments.of(new AwsSecretsManagerPropertiesBuilder().withDefaultContext("").build(), "defaultContext",
"NotEmpty"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ void contextSpecificOrderExpected() {
assertThat(contextToBeTested.get(3)).isEqualTo("/secret/application");
}

@Test
void contextExpectSpecificOrderAndEmptyPrefix() {
AwsSecretsManagerProperties properties = new AwsSecretsManagerPropertiesBuilder()
.withDefaultContext("application").withPrefix("").withName("messaging-service").build();

GetSecretValueResult secretValueResult = new GetSecretValueResult();
secretValueResult.setSecretString("{\"key1\": \"value1\", \"key2\": \"value2\"}");
when(smClient.getSecretValue(any(GetSecretValueRequest.class))).thenReturn(secretValueResult);

AwsSecretsManagerPropertySourceLocator locator = new AwsSecretsManagerPropertySourceLocator(smClient,
properties);
env.setActiveProfiles("test");
locator.locate(env);

List<String> contextToBeTested = new ArrayList<>(locator.getContexts());

assertThat(contextToBeTested.get(0)).isEqualTo("/messaging-service_test");
assertThat(contextToBeTested.get(1)).isEqualTo("/messaging-service");
assertThat(contextToBeTested.get(2)).isEqualTo("/application_test");
assertThat(contextToBeTested.get(3)).isEqualTo("/application");
}

@Test
void whenFailFastIsTrueAndSecretDoesNotExistThrowsException() {
AwsSecretsManagerProperties properties = new AwsSecretsManagerProperties();
Expand Down Expand Up @@ -174,6 +196,11 @@ public AwsSecretsManagerPropertiesBuilder withDefaultContext(String defaultConte
return this;
}

public AwsSecretsManagerPropertiesBuilder withPrefix(String prefix) {
this.properties.setPrefix(prefix);
return this;
}

public AwsSecretsManagerPropertiesBuilder withName(String name) {
this.properties.setName(name);
return this;
Expand Down

0 comments on commit ed7b43f

Please sign in to comment.