Skip to content

Commit d4de4b6

Browse files
committed
Validate vault location path.
Vault locations must not end with a trailing slash as that location points to a directory and not a valid secret. Closes gh-601.
1 parent 6e03afb commit d4de4b6

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultConfigLocation.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public VaultConfigLocation(String contextPath, boolean optional) {
4444

4545
super(optional);
4646

47-
Assert.hasText(contextPath, "Context path must not be empty");
47+
Assert.hasText(contextPath, "Location must not be empty");
48+
validatePath(contextPath);
4849

4950
this.secretBackendMetadata = KeyValueSecretBackendMetadata.create(contextPath);
5051
this.optional = optional;
@@ -54,6 +55,7 @@ public VaultConfigLocation(SecretBackendMetadata secretBackendMetadata, boolean
5455

5556
Assert.notNull(secretBackendMetadata, "SecretBackendMetadata must not be null");
5657

58+
validatePath(secretBackendMetadata.getPath());
5759
this.secretBackendMetadata = secretBackendMetadata;
5860
this.optional = optional;
5961
}
@@ -101,4 +103,9 @@ public String toString() {
101103
return sb.toString();
102104
}
103105

106+
private static void validatePath(String contextPath) {
107+
Assert.isTrue(!contextPath.endsWith("/"),
108+
() -> String.format("Location 'vault://%s' must not end with a trailing slash", contextPath));
109+
}
110+
104111
}

spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultConfigDataLoaderIntegrationTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.context.ConfigurableApplicationContext;
3131

3232
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.assertj.core.api.Assertions.fail;
3334

3435
/**
3536
* Integration tests for {@link VaultConfigDataLoader}.
@@ -62,6 +63,25 @@ public void shouldConsiderProfiles() {
6263
}
6364
}
6465

66+
@Test
67+
public void vaultLocationEndingWithSlashShouldFail() {
68+
69+
SpringApplication application = new SpringApplication(Config.class);
70+
application.setWebApplicationType(WebApplicationType.NONE);
71+
application.setAdditionalProfiles("cloud");
72+
73+
try (ConfigurableApplicationContext context = application.run("--spring.application.name=my-config-loader",
74+
"--spring.config.import=vault://secret/my-config-loader/cloud/",
75+
"--spring.cloud.vault.token=" + Settings.token().getToken())) {
76+
77+
fail("expected exception");
78+
}
79+
catch (IllegalArgumentException e) {
80+
assertThat(e).hasMessageContaining(
81+
"Location 'vault://secret/my-config-loader/cloud/' must not end with a trailing slash");
82+
}
83+
}
84+
6585
@Test
6686
public void shouldConsiderDisabledVault() {
6787

spring-cloud-vault-config/src/test/java/org/springframework/cloud/vault/config/VaultConfigDataLocationResolverUnitTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.boot.context.properties.bind.Binder;
3030

3131
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3233
import static org.mockito.Mockito.mock;
3334
import static org.mockito.Mockito.when;
3435

@@ -67,6 +68,17 @@ public void shouldDiscoverDefaultLocations() {
6768
.hasSize(3);
6869
}
6970

71+
@Test
72+
public void shouldRejectLocationWithTrailingSlash() {
73+
74+
VaultConfigDataLocationResolver resolver = new VaultConfigDataLocationResolver();
75+
76+
assertThatIllegalArgumentException()
77+
.isThrownBy(() -> resolver.resolveProfileSpecific(this.contextMock,
78+
ConfigDataLocation.of("vault://foo/"), this.profilesMock))
79+
.withMessage("Location 'vault://foo/' must not end with a trailing slash");
80+
}
81+
7082
@Test
7183
public void shouldDiscoverContextualLocations() {
7284

0 commit comments

Comments
 (0)