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.
This commit is contained in:
Mark Paluch
2021-06-18 08:59:44 +02:00
parent 6e03afbed0
commit d4de4b6416
3 changed files with 40 additions and 1 deletions

View File

@@ -44,7 +44,8 @@ public class VaultConfigLocation extends ConfigDataResource {
super(optional);
Assert.hasText(contextPath, "Context path must not be empty");
Assert.hasText(contextPath, "Location must not be empty");
validatePath(contextPath);
this.secretBackendMetadata = KeyValueSecretBackendMetadata.create(contextPath);
this.optional = optional;
@@ -54,6 +55,7 @@ public class VaultConfigLocation extends ConfigDataResource {
Assert.notNull(secretBackendMetadata, "SecretBackendMetadata must not be null");
validatePath(secretBackendMetadata.getPath());
this.secretBackendMetadata = secretBackendMetadata;
this.optional = optional;
}
@@ -101,4 +103,9 @@ public class VaultConfigLocation extends ConfigDataResource {
return sb.toString();
}
private static void validatePath(String contextPath) {
Assert.isTrue(!contextPath.endsWith("/"),
() -> String.format("Location 'vault://%s' must not end with a trailing slash", contextPath));
}
}

View File

@@ -30,6 +30,7 @@ import org.springframework.cloud.vault.util.Settings;
import org.springframework.context.ConfigurableApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
/**
* Integration tests for {@link VaultConfigDataLoader}.
@@ -62,6 +63,25 @@ public class VaultConfigDataLoaderIntegrationTests extends IntegrationTestSuppor
}
}
@Test
public void vaultLocationEndingWithSlashShouldFail() {
SpringApplication application = new SpringApplication(Config.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.setAdditionalProfiles("cloud");
try (ConfigurableApplicationContext context = application.run("--spring.application.name=my-config-loader",
"--spring.config.import=vault://secret/my-config-loader/cloud/",
"--spring.cloud.vault.token=" + Settings.token().getToken())) {
fail("expected exception");
}
catch (IllegalArgumentException e) {
assertThat(e).hasMessageContaining(
"Location 'vault://secret/my-config-loader/cloud/' must not end with a trailing slash");
}
}
@Test
public void shouldConsiderDisabledVault() {

View File

@@ -29,6 +29,7 @@ import org.springframework.boot.context.config.Profiles;
import org.springframework.boot.context.properties.bind.Binder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -67,6 +68,17 @@ public class VaultConfigDataLocationResolverUnitTests {
.hasSize(3);
}
@Test
public void shouldRejectLocationWithTrailingSlash() {
VaultConfigDataLocationResolver resolver = new VaultConfigDataLocationResolver();
assertThatIllegalArgumentException()
.isThrownBy(() -> resolver.resolveProfileSpecific(this.contextMock,
ConfigDataLocation.of("vault://foo/"), this.profilesMock))
.withMessage("Location 'vault://foo/' must not end with a trailing slash");
}
@Test
public void shouldDiscoverContextualLocations() {