From 34b46554853d394a94e96d918bb67d7acccf6a4b Mon Sep 17 00:00:00 2001 From: MatejNedic Date: Sat, 9 Apr 2022 10:09:21 +0200 Subject: [PATCH 1/6] Improve validation prefix logging --- .../cloud/paramstore/AwsParamStoreProperties.java | 2 +- .../paramstore/AwsParamStorePropertiesTest.java | 13 +++++++++++++ .../AwsSecretsManagerProperties.java | 2 +- .../AwsSecretsManagerPropertiesTest.java | 14 ++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/spring-cloud-aws-parameter-store-config/src/main/java/io/awspring/cloud/paramstore/AwsParamStoreProperties.java b/spring-cloud-aws-parameter-store-config/src/main/java/io/awspring/cloud/paramstore/AwsParamStoreProperties.java index 7497cfa31..6c174622d 100644 --- a/spring-cloud-aws-parameter-store-config/src/main/java/io/awspring/cloud/paramstore/AwsParamStoreProperties.java +++ b/spring-cloud-aws-parameter-store-config/src/main/java/io/awspring/cloud/paramstore/AwsParamStoreProperties.java @@ -96,7 +96,7 @@ public void afterPropertiesSet() throws Exception { if (StringUtils.hasLength(prefix) && !PREFIX_PATTERN.matcher(prefix).matches()) { throw new ValidationException(CONFIG_PREFIX + ".prefix", - "The prefix must have pattern of: " + PREFIX_PATTERN.toString()); + "The prefix value: " + prefix + " must have pattern of: " + PREFIX_PATTERN.toString()); } if (!PROFILE_SEPARATOR_PATTERN.matcher(profileSeparator).matches()) { throw new ValidationException(CONFIG_PREFIX + ".profileSeparator", diff --git a/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java b/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java index 27d08ae3b..6ff5fe9a1 100644 --- a/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java +++ b/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java @@ -25,6 +25,7 @@ import static org.assertj.core.api.Assertions.assertThatNoException; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * Tests for {@link AwsParamStoreProperties}. @@ -78,6 +79,18 @@ void acceptsBackslashAsProfileSeparator() { assertThatNoException().isThrownBy(properties::afterPropertiesSet); } + @Test + void checkExceptionLoggingForPrefix() { + try { + AwsParamStoreProperties properties = new AwsParamStorePropertiesBuilder().withPrefix("!.").build(); + properties.afterPropertiesSet(); + } + catch (Exception e) { + assertEquals(e.getMessage(), + "The prefix value: !. must have pattern of: (/)?([a-zA-Z0-9.\\-]+)(?:/[a-zA-Z0-9]+)*"); + } + } + private static Stream invalidProperties() { return Stream.of( Arguments.of(new AwsParamStorePropertiesBuilder().withPrefix("!.").build(), "prefix", "Pattern"), diff --git a/spring-cloud-aws-secrets-manager-config/src/main/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerProperties.java b/spring-cloud-aws-secrets-manager-config/src/main/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerProperties.java index 20a1e09f2..c10fcfbf4 100644 --- a/spring-cloud-aws-secrets-manager-config/src/main/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerProperties.java +++ b/spring-cloud-aws-secrets-manager-config/src/main/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerProperties.java @@ -98,7 +98,7 @@ public void afterPropertiesSet() throws Exception { if (StringUtils.hasLength(prefix) && !PREFIX_PATTERN.matcher(prefix).matches()) { throw new ValidationException(CONFIG_PREFIX + ".prefix", - "The prefix must have pattern of: " + PREFIX_PATTERN.toString()); + "The prefix value: " + prefix + " must have pattern of: " + PREFIX_PATTERN.toString()); } if (!PROFILE_SEPARATOR_PATTERN.matcher(profileSeparator).matches()) { throw new ValidationException(CONFIG_PREFIX + ".profileSeparator", diff --git a/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java b/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java index e0b02c6fb..64991abea 100644 --- a/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java +++ b/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java @@ -18,12 +18,14 @@ import java.util.stream.Stream; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import static org.assertj.core.api.Assertions.assertThatNoException; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * Tests for {@link AwsSecretsManagerProperties}. @@ -45,6 +47,18 @@ void validationSucceeds(AwsSecretsManagerProperties properties) { assertThatNoException().isThrownBy(properties::afterPropertiesSet); } + @Test + void checkExceptionLoggingForPrefix() { + try { + AwsSecretsManagerProperties properties = new AwsSecretsManagerPropertiesBuilder().withPrefix("!.").build(); + properties.afterPropertiesSet(); + } + catch (Exception e) { + assertEquals(e.getMessage(), + "The prefix value: !. must have pattern of: (/)?([a-zA-Z0-9.\\-]+)(?:/[a-zA-Z0-9]+)*"); + } + } + private static Stream validProperties() { return Stream.of( Arguments.of(new AwsSecretsManagerPropertiesBuilder().withPrefix("").withDefaultContext("app") From 3d63c5536471a9d1cffefc6960698217c7873bd1 Mon Sep 17 00:00:00 2001 From: MatejNedic Date: Sat, 9 Apr 2022 10:24:25 +0200 Subject: [PATCH 2/6] Take care of comments polish and add a new test --- .../AwsParamStorePropertiesTest.java | 19 +++++++++++-------- .../AwsSecretsManagerPropertiesTest.java | 14 +++++--------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java b/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java index 6ff5fe9a1..0c69b4b93 100644 --- a/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java +++ b/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java @@ -49,6 +49,14 @@ void validationSucceeds() { assertThatNoException().isThrownBy(properties::afterPropertiesSet); } + @Test + void validationSucceedsForPrefix() { + AwsParamStoreProperties properties = new AwsParamStorePropertiesBuilder().withPrefix("/someRandomValue-dev01") + .withDefaultContext("app").withProfileSeparator("_").build(); + + assertThatNoException().isThrownBy(properties::afterPropertiesSet); + } + @Test void validationSucceedsPrefix() { AwsParamStoreProperties properties = new AwsParamStorePropertiesBuilder().withPrefix("/con/test/bla") @@ -81,14 +89,9 @@ void acceptsBackslashAsProfileSeparator() { @Test void checkExceptionLoggingForPrefix() { - try { - AwsParamStoreProperties properties = new AwsParamStorePropertiesBuilder().withPrefix("!.").build(); - properties.afterPropertiesSet(); - } - catch (Exception e) { - assertEquals(e.getMessage(), - "The prefix value: !. must have pattern of: (/)?([a-zA-Z0-9.\\-]+)(?:/[a-zA-Z0-9]+)*"); - } + AwsParamStoreProperties properties = new AwsParamStorePropertiesBuilder().withPrefix("!.").build(); + assertThatThrownBy(properties::afterPropertiesSet) + .hasMessage("The prefix value: !. must have pattern of: (/)?([a-zA-Z0-9.\\-]+)(?:/[a-zA-Z0-9]+)*"); } private static Stream invalidProperties() { diff --git a/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java b/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java index 64991abea..cff83e8e6 100644 --- a/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java +++ b/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java @@ -25,7 +25,6 @@ import static org.assertj.core.api.Assertions.assertThatNoException; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.jupiter.api.Assertions.assertEquals; /** * Tests for {@link AwsSecretsManagerProperties}. @@ -49,20 +48,17 @@ void validationSucceeds(AwsSecretsManagerProperties properties) { @Test void checkExceptionLoggingForPrefix() { - try { - AwsSecretsManagerProperties properties = new AwsSecretsManagerPropertiesBuilder().withPrefix("!.").build(); - properties.afterPropertiesSet(); - } - catch (Exception e) { - assertEquals(e.getMessage(), - "The prefix value: !. must have pattern of: (/)?([a-zA-Z0-9.\\-]+)(?:/[a-zA-Z0-9]+)*"); - } + AwsSecretsManagerProperties properties = new AwsSecretsManagerPropertiesBuilder().withPrefix("!.").build(); + assertThatThrownBy(properties::afterPropertiesSet) + .hasMessage("The prefix value: !. must have pattern of: (/)?([a-zA-Z0-9.\\-]+)(?:/[a-zA-Z0-9]+)*"); } private static Stream validProperties() { return Stream.of( Arguments.of(new AwsSecretsManagerPropertiesBuilder().withPrefix("").withDefaultContext("app") .withProfileSeparator("_").build()), + Arguments.of(new AwsSecretsManagerPropertiesBuilder().withPrefix("/someRandomValue-dev01") + .withDefaultContext("app").withProfileSeparator("_").build()), Arguments.of(new AwsSecretsManagerPropertiesBuilder().withPrefix("/sec").withDefaultContext("app") .withProfileSeparator("_").build()), Arguments.of(new AwsSecretsManagerPropertiesBuilder().withPrefix("/sec/test/var") From ae89f4fe6fe5c94821af3d75d54526269ef211b0 Mon Sep 17 00:00:00 2001 From: MatejNedic Date: Sat, 9 Apr 2022 10:35:46 +0200 Subject: [PATCH 3/6] remove unused property --- .../awspring/cloud/paramstore/AwsParamStorePropertiesTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java b/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java index 0c69b4b93..216161820 100644 --- a/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java +++ b/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java @@ -25,7 +25,6 @@ import static org.assertj.core.api.Assertions.assertThatNoException; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.jupiter.api.Assertions.assertEquals; /** * Tests for {@link AwsParamStoreProperties}. From e1612bbf51235d493524eaa92751dd1fa38ca388 Mon Sep 17 00:00:00 2001 From: MatejNedic Date: Sat, 9 Apr 2022 10:54:04 +0200 Subject: [PATCH 4/6] introduce missing regex part --- .../paramstore/AwsParamStoreProperties.java | 2 +- .../AwsParamStorePropertiesTest.java | 64 ++++++------------- .../AwsSecretsManagerProperties.java | 2 +- .../AwsSecretsManagerPropertiesTest.java | 4 +- 4 files changed, 24 insertions(+), 48 deletions(-) diff --git a/spring-cloud-aws-parameter-store-config/src/main/java/io/awspring/cloud/paramstore/AwsParamStoreProperties.java b/spring-cloud-aws-parameter-store-config/src/main/java/io/awspring/cloud/paramstore/AwsParamStoreProperties.java index 6c174622d..41361492b 100644 --- a/spring-cloud-aws-parameter-store-config/src/main/java/io/awspring/cloud/paramstore/AwsParamStoreProperties.java +++ b/spring-cloud-aws-parameter-store-config/src/main/java/io/awspring/cloud/paramstore/AwsParamStoreProperties.java @@ -42,7 +42,7 @@ public class AwsParamStoreProperties implements InitializingBean { /** * Pattern used for prefix validation. */ - private static final Pattern PREFIX_PATTERN = Pattern.compile("(/)?([a-zA-Z0-9.\\-]+)(?:/[a-zA-Z0-9]+)*"); + private static final Pattern PREFIX_PATTERN = Pattern.compile("(/)?([a-zA-Z0-9.\\-]+)(?:/[a-zA-Z0-9.\\-]+)*"); /** * Pattern used for profileSeparator validation. diff --git a/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java b/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java index 216161820..39202f35c 100644 --- a/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java +++ b/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java @@ -40,49 +40,9 @@ public void validationFails(AwsParamStoreProperties properties, String field, St assertThatThrownBy(properties::afterPropertiesSet).isInstanceOf(ValidationException.class); } - @Test - void validationSucceeds() { - AwsParamStoreProperties properties = new AwsParamStorePropertiesBuilder().withPrefix("/con") - .withDefaultContext("app").withProfileSeparator("_").build(); - - assertThatNoException().isThrownBy(properties::afterPropertiesSet); - } - - @Test - void validationSucceedsForPrefix() { - AwsParamStoreProperties properties = new AwsParamStorePropertiesBuilder().withPrefix("/someRandomValue-dev01") - .withDefaultContext("app").withProfileSeparator("_").build(); - - assertThatNoException().isThrownBy(properties::afterPropertiesSet); - } - - @Test - void validationSucceedsPrefix() { - AwsParamStoreProperties properties = new AwsParamStorePropertiesBuilder().withPrefix("/con/test/bla") - .withDefaultContext("app").withProfileSeparator("_").build(); - - assertThatNoException().isThrownBy(properties::afterPropertiesSet); - } - - @Test - void validationSucceedsNoPrefix() { - AwsParamStoreProperties properties = new AwsParamStorePropertiesBuilder().withPrefix("") - .withDefaultContext("app").withProfileSeparator("_").build(); - - assertThatNoException().isThrownBy(properties::afterPropertiesSet); - } - - @Test - void acceptsForwardSlashAsProfileSeparator() { - AwsParamStoreProperties properties = new AwsParamStoreProperties(); - properties.setProfileSeparator("/"); - assertThatNoException().isThrownBy(properties::afterPropertiesSet); - } - - @Test - void acceptsBackslashAsProfileSeparator() { - AwsParamStoreProperties properties = new AwsParamStoreProperties(); - properties.setProfileSeparator("\\"); + @ParameterizedTest + @MethodSource("validProperties") + void validationSucceeds(AwsParamStoreProperties properties) { assertThatNoException().isThrownBy(properties::afterPropertiesSet); } @@ -90,7 +50,23 @@ void acceptsBackslashAsProfileSeparator() { void checkExceptionLoggingForPrefix() { AwsParamStoreProperties properties = new AwsParamStorePropertiesBuilder().withPrefix("!.").build(); assertThatThrownBy(properties::afterPropertiesSet) - .hasMessage("The prefix value: !. must have pattern of: (/)?([a-zA-Z0-9.\\-]+)(?:/[a-zA-Z0-9]+)*"); + .hasMessage("The prefix value: !. must have pattern of: (/)?([a-zA-Z0-9.\\-]+)(?:/[a-zA-Z0-9.\\-]+)*"); + } + + private static Stream validProperties() { + return Stream.of( + Arguments.of(new AwsParamStorePropertiesBuilder().withPrefix("/con").withDefaultContext("app") + .withProfileSeparator("_").build()), + Arguments.of(new AwsParamStorePropertiesBuilder().withPrefix("/config/someRandomValue-dev01") + .withDefaultContext("app").withProfileSeparator("_").build()), + Arguments.of(new AwsParamStorePropertiesBuilder().withPrefix("/con/test/bla").withDefaultContext("app") + .withProfileSeparator("_").build()), + Arguments.of(new AwsParamStorePropertiesBuilder().withPrefix("").withDefaultContext("app") + .withProfileSeparator("_").build()), + Arguments.of(new AwsParamStorePropertiesBuilder().withPrefix("/config").withDefaultContext("app") + .withProfileSeparator("/").build()), + Arguments.of(new AwsParamStorePropertiesBuilder().withPrefix("/config").withDefaultContext("app") + .withProfileSeparator("\\").build())); } private static Stream invalidProperties() { diff --git a/spring-cloud-aws-secrets-manager-config/src/main/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerProperties.java b/spring-cloud-aws-secrets-manager-config/src/main/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerProperties.java index c10fcfbf4..0dac25568 100644 --- a/spring-cloud-aws-secrets-manager-config/src/main/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerProperties.java +++ b/spring-cloud-aws-secrets-manager-config/src/main/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerProperties.java @@ -43,7 +43,7 @@ public class AwsSecretsManagerProperties implements InitializingBean { /** * Pattern used for prefix validation. */ - private static final Pattern PREFIX_PATTERN = Pattern.compile("(/)?([a-zA-Z0-9.\\-]+)(?:/[a-zA-Z0-9]+)*"); + private static final Pattern PREFIX_PATTERN = Pattern.compile("(/)?([a-zA-Z0-9.\\-]+)(?:/[a-zA-Z0-9.\\-]+)*"); /** * Pattern used for profileSeparator validation. diff --git a/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java b/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java index cff83e8e6..1d1c275c1 100644 --- a/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java +++ b/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java @@ -50,14 +50,14 @@ void validationSucceeds(AwsSecretsManagerProperties properties) { void checkExceptionLoggingForPrefix() { AwsSecretsManagerProperties properties = new AwsSecretsManagerPropertiesBuilder().withPrefix("!.").build(); assertThatThrownBy(properties::afterPropertiesSet) - .hasMessage("The prefix value: !. must have pattern of: (/)?([a-zA-Z0-9.\\-]+)(?:/[a-zA-Z0-9]+)*"); + .hasMessage("The prefix value: !. must have pattern of: (/)?([a-zA-Z0-9.\\-]+)(?:/[a-zA-Z0-9.\\-]+)*"); } private static Stream validProperties() { return Stream.of( Arguments.of(new AwsSecretsManagerPropertiesBuilder().withPrefix("").withDefaultContext("app") .withProfileSeparator("_").build()), - Arguments.of(new AwsSecretsManagerPropertiesBuilder().withPrefix("/someRandomValue-dev01") + Arguments.of(new AwsSecretsManagerPropertiesBuilder().withPrefix("/secret/someRandomValue-dev01") .withDefaultContext("app").withProfileSeparator("_").build()), Arguments.of(new AwsSecretsManagerPropertiesBuilder().withPrefix("/sec").withDefaultContext("app") .withProfileSeparator("_").build()), From 5243854d2425ddc9b4cbf1124cd45624a46472ab Mon Sep 17 00:00:00 2001 From: Maciej Walkowiak Date: Sun, 10 Apr 2022 07:47:53 +0200 Subject: [PATCH 5/6] Relax validation --- .../paramstore/AwsParamStoreProperties.java | 6 ++--- .../AwsParamStorePropertiesTest.java | 25 +++++++++++-------- .../AwsSecretsManagerProperties.java | 6 ++--- .../AwsSecretsManagerPropertiesTest.java | 24 +++++++++++------- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/spring-cloud-aws-parameter-store-config/src/main/java/io/awspring/cloud/paramstore/AwsParamStoreProperties.java b/spring-cloud-aws-parameter-store-config/src/main/java/io/awspring/cloud/paramstore/AwsParamStoreProperties.java index 41361492b..3f48dd6e6 100644 --- a/spring-cloud-aws-parameter-store-config/src/main/java/io/awspring/cloud/paramstore/AwsParamStoreProperties.java +++ b/spring-cloud-aws-parameter-store-config/src/main/java/io/awspring/cloud/paramstore/AwsParamStoreProperties.java @@ -42,7 +42,7 @@ public class AwsParamStoreProperties implements InitializingBean { /** * Pattern used for prefix validation. */ - private static final Pattern PREFIX_PATTERN = Pattern.compile("(/)?([a-zA-Z0-9.\\-]+)(?:/[a-zA-Z0-9.\\-]+)*"); + private static final Pattern PREFIX_PATTERN = Pattern.compile("[a-zA-Z0-9.\\-/]+"); /** * Pattern used for profileSeparator validation. @@ -96,11 +96,11 @@ public void afterPropertiesSet() throws Exception { if (StringUtils.hasLength(prefix) && !PREFIX_PATTERN.matcher(prefix).matches()) { throw new ValidationException(CONFIG_PREFIX + ".prefix", - "The prefix value: " + prefix + " must have pattern of: " + PREFIX_PATTERN.toString()); + "The prefix value: " + prefix + " must have pattern of: " + PREFIX_PATTERN.toString()); } if (!PROFILE_SEPARATOR_PATTERN.matcher(profileSeparator).matches()) { throw new ValidationException(CONFIG_PREFIX + ".profileSeparator", - "The profileSeparator must have pattern of: " + PROFILE_SEPARATOR_PATTERN.toString()); + "The profileSeparator must have pattern of: " + PROFILE_SEPARATOR_PATTERN.toString()); } } diff --git a/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java b/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java index 39202f35c..8baffabe0 100644 --- a/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java +++ b/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java @@ -25,6 +25,8 @@ import static org.assertj.core.api.Assertions.assertThatNoException; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; /** * Tests for {@link AwsParamStoreProperties}. @@ -36,8 +38,14 @@ class AwsParamStorePropertiesTest { @ParameterizedTest @MethodSource("invalidProperties") - public void validationFails(AwsParamStoreProperties properties, String field, String errorCode) { - assertThatThrownBy(properties::afterPropertiesSet).isInstanceOf(ValidationException.class); + public void validationFails(AwsParamStoreProperties properties, String field) throws Exception { + try { + properties.afterPropertiesSet(); + fail("validation should fail"); + } + catch (ValidationException e) { + assertThat(e.getField()).endsWith(field); + } } @ParameterizedTest @@ -50,7 +58,7 @@ void validationSucceeds(AwsParamStoreProperties properties) { void checkExceptionLoggingForPrefix() { AwsParamStoreProperties properties = new AwsParamStorePropertiesBuilder().withPrefix("!.").build(); assertThatThrownBy(properties::afterPropertiesSet) - .hasMessage("The prefix value: !. must have pattern of: (/)?([a-zA-Z0-9.\\-]+)(?:/[a-zA-Z0-9.\\-]+)*"); + .hasMessage("The prefix value: !. must have pattern of: [a-zA-Z0-9.\\-/]+"); } private static Stream validProperties() { @@ -70,14 +78,11 @@ private static Stream validProperties() { } private static Stream invalidProperties() { - return Stream.of( - Arguments.of(new AwsParamStorePropertiesBuilder().withPrefix("!.").build(), "prefix", "Pattern"), - Arguments.of(new AwsParamStorePropertiesBuilder().withDefaultContext("").build(), "defaultContext", - "NotEmpty"), - Arguments.of(new AwsParamStorePropertiesBuilder().withProfileSeparator("").build(), "profileSeparator", - "NotEmpty"), + return Stream.of(Arguments.of(new AwsParamStorePropertiesBuilder().withPrefix("!.").build(), "prefix"), + Arguments.of(new AwsParamStorePropertiesBuilder().withDefaultContext("").build(), "defaultContext"), + Arguments.of(new AwsParamStorePropertiesBuilder().withProfileSeparator("").build(), "profileSeparator"), Arguments.of(new AwsParamStorePropertiesBuilder().withProfileSeparator("!_").build(), - "profileSeparator", "Pattern")); + "profileSeparator")); } private static class AwsParamStorePropertiesBuilder { diff --git a/spring-cloud-aws-secrets-manager-config/src/main/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerProperties.java b/spring-cloud-aws-secrets-manager-config/src/main/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerProperties.java index 0dac25568..11220a982 100644 --- a/spring-cloud-aws-secrets-manager-config/src/main/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerProperties.java +++ b/spring-cloud-aws-secrets-manager-config/src/main/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerProperties.java @@ -43,7 +43,7 @@ public class AwsSecretsManagerProperties implements InitializingBean { /** * Pattern used for prefix validation. */ - private static final Pattern PREFIX_PATTERN = Pattern.compile("(/)?([a-zA-Z0-9.\\-]+)(?:/[a-zA-Z0-9.\\-]+)*"); + private static final Pattern PREFIX_PATTERN = Pattern.compile("[a-zA-Z0-9.\\-/]+"); /** * Pattern used for profileSeparator validation. @@ -98,11 +98,11 @@ public void afterPropertiesSet() throws Exception { if (StringUtils.hasLength(prefix) && !PREFIX_PATTERN.matcher(prefix).matches()) { throw new ValidationException(CONFIG_PREFIX + ".prefix", - "The prefix value: " + prefix + " must have pattern of: " + PREFIX_PATTERN.toString()); + "The prefix value: " + prefix + " must have pattern of: " + PREFIX_PATTERN.toString()); } if (!PROFILE_SEPARATOR_PATTERN.matcher(profileSeparator).matches()) { throw new ValidationException(CONFIG_PREFIX + ".profileSeparator", - "The profileSeparator must have pattern of: " + PROFILE_SEPARATOR_PATTERN.toString()); + "The profileSeparator must have pattern of: " + PROFILE_SEPARATOR_PATTERN.toString()); } } diff --git a/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java b/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java index 1d1c275c1..28fb00849 100644 --- a/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java +++ b/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java @@ -25,6 +25,8 @@ import static org.assertj.core.api.Assertions.assertThatNoException; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.fail; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link AwsSecretsManagerProperties}. @@ -36,8 +38,14 @@ class AwsSecretsManagerPropertiesTest { @ParameterizedTest @MethodSource("invalidProperties") - public void validationFails(AwsSecretsManagerProperties properties, String field, String errorCode) { - assertThatThrownBy(properties::afterPropertiesSet).isInstanceOf(ValidationException.class); + public void validationFails(AwsSecretsManagerProperties properties, String field) throws Exception { + try { + properties.afterPropertiesSet(); + fail("validation should fail"); + } + catch (ValidationException e) { + assertThat(e.getField()).endsWith(field); + } } @ParameterizedTest @@ -50,7 +58,7 @@ void validationSucceeds(AwsSecretsManagerProperties properties) { void checkExceptionLoggingForPrefix() { AwsSecretsManagerProperties properties = new AwsSecretsManagerPropertiesBuilder().withPrefix("!.").build(); assertThatThrownBy(properties::afterPropertiesSet) - .hasMessage("The prefix value: !. must have pattern of: (/)?([a-zA-Z0-9.\\-]+)(?:/[a-zA-Z0-9.\\-]+)*"); + .hasMessage("The prefix value: !. must have pattern of: [a-zA-Z0-9.\\-/]+"); } private static Stream validProperties() { @@ -72,14 +80,12 @@ private static Stream validProperties() { } private static Stream invalidProperties() { - return Stream.of( - Arguments.of(new AwsSecretsManagerPropertiesBuilder().withPrefix("!.").build(), "prefix", "Pattern"), - Arguments.of(new AwsSecretsManagerPropertiesBuilder().withDefaultContext("").build(), "defaultContext", - "NotEmpty"), + return Stream.of(Arguments.of(new AwsSecretsManagerPropertiesBuilder().withPrefix("!.").build(), "prefix"), + Arguments.of(new AwsSecretsManagerPropertiesBuilder().withDefaultContext("").build(), "defaultContext"), Arguments.of(new AwsSecretsManagerPropertiesBuilder().withProfileSeparator("").build(), - "profileSeparator", "NotEmpty"), + "profileSeparator"), Arguments.of(new AwsSecretsManagerPropertiesBuilder().withProfileSeparator("!_").build(), - "profileSeparator", "Pattern")); + "profileSeparator")); } private static class AwsSecretsManagerPropertiesBuilder { From 55aca4718aa539c2317ef16ec90c2d8f3ca8fa06 Mon Sep 17 00:00:00 2001 From: Maciej Walkowiak Date: Sun, 10 Apr 2022 08:01:14 +0200 Subject: [PATCH 6/6] Polish. --- .../awspring/cloud/paramstore/AwsParamStorePropertiesTest.java | 2 +- .../cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java b/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java index 8baffabe0..a0e5b9773 100644 --- a/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java +++ b/spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertiesTest.java @@ -23,9 +23,9 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatNoException; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; /** diff --git a/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java b/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java index 28fb00849..4cf3d8164 100644 --- a/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java +++ b/spring-cloud-aws-secrets-manager-config/src/test/java/io/awspring/cloud/secretsmanager/AwsSecretsManagerPropertiesTest.java @@ -23,10 +23,10 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatNoException; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.fail; -import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link AwsSecretsManagerProperties}.