Fixing compilation errors from ConfigDataLocation changes in Boot

This commit is contained in:
Ryan Baxter
2020-10-29 14:41:16 -04:00
parent d0a7d96b60
commit 8f74490a8f
3 changed files with 23 additions and 15 deletions

View File

@@ -24,9 +24,11 @@ import java.util.stream.Collectors;
import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.context.config.ConfigDataLocation;
import org.springframework.boot.context.config.ConfigDataLocationNotFoundException;
import org.springframework.boot.context.config.ConfigDataLocationResolver;
import org.springframework.boot.context.config.ConfigDataLocationResolverContext;
import org.springframework.boot.context.config.ConfigDataResourceNotFoundException;
import org.springframework.boot.context.config.Profiles;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.Binder;
@@ -78,25 +80,24 @@ import org.springframework.util.ReflectionUtils;
public class VaultConfigDataLocationResolver implements ConfigDataLocationResolver<VaultConfigLocation> {
@Override
public boolean isResolvable(ConfigDataLocationResolverContext context, String location) {
public boolean isResolvable(ConfigDataLocationResolverContext context, ConfigDataLocation location) {
boolean vaultEnabled = context.getBinder().bind(VaultProperties.PREFIX + ".enabled", Boolean.class)
.orElse(true);
return location.startsWith(VaultConfigLocation.VAULT_PREFIX) && vaultEnabled;
return location.getValue().startsWith(VaultConfigLocation.VAULT_PREFIX) && vaultEnabled;
}
@Override
public List<VaultConfigLocation> resolve(ConfigDataLocationResolverContext context, String location,
boolean optional) throws ConfigDataLocationNotFoundException {
public List<VaultConfigLocation> resolve(ConfigDataLocationResolverContext context, ConfigDataLocation location)
throws ConfigDataLocationNotFoundException, ConfigDataResourceNotFoundException {
return Collections.emptyList();
}
@Override
public List<VaultConfigLocation> resolveProfileSpecific(ConfigDataLocationResolverContext context, String location,
boolean optional, Profiles profiles) throws ConfigDataLocationNotFoundException {
public List<VaultConfigLocation> resolveProfileSpecific(ConfigDataLocationResolverContext context,
ConfigDataLocation location, Profiles profiles) throws ConfigDataLocationNotFoundException {
if (!location.startsWith(VaultConfigLocation.VAULT_PREFIX)) {
if (!location.getValue().startsWith(VaultConfigLocation.VAULT_PREFIX)) {
return Collections.emptyList();
}
@@ -105,16 +106,17 @@ public class VaultConfigDataLocationResolver implements ConfigDataLocationResolv
if (location.equals(VaultConfigLocation.VAULT_PREFIX)
|| location.equals(VaultConfigLocation.VAULT_PREFIX + "//")) {
List<SecretBackendMetadata> sorted = getSecretBackends(context, profiles);
return sorted.stream().map(it -> new VaultConfigLocation(it, optional)).collect(Collectors.toList());
return sorted.stream().map(it -> new VaultConfigLocation(it, location.isOptional()))
.collect(Collectors.toList());
}
String contextPath = location.substring(VaultConfigLocation.VAULT_PREFIX.length());
String contextPath = location.getValue().substring(VaultConfigLocation.VAULT_PREFIX.length());
while (contextPath.startsWith("/")) {
contextPath = contextPath.substring(1);
}
return Collections.singletonList(new VaultConfigLocation(contextPath, optional));
return Collections.singletonList(new VaultConfigLocation(contextPath, location.isOptional()));
}
private static void registerVaultProperties(ConfigDataLocationResolverContext context) {

View File

@@ -17,6 +17,7 @@
package org.springframework.cloud.vault.config;
import org.springframework.boot.context.config.ConfigDataLocation;
import org.springframework.boot.context.config.ConfigDataResource;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
@@ -28,7 +29,7 @@ import org.springframework.util.ObjectUtils;
* @since 3.0
* @see SecretBackendMetadata
*/
public class VaultConfigLocation extends ConfigDataLocation {
public class VaultConfigLocation extends ConfigDataResource {
/**
* Prefix used to indicate a {@link VaultConfigLocation}.

View File

@@ -23,6 +23,7 @@ import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.DefaultBootstrapContext;
import org.springframework.boot.context.config.ConfigDataLocation;
import org.springframework.boot.context.config.ConfigDataLocationResolverContext;
import org.springframework.boot.context.config.Profiles;
import org.springframework.boot.context.properties.bind.Binder;
@@ -57,9 +58,13 @@ public class VaultConfigDataLocationResolverUnitTests {
when(this.profilesMock.getActive()).thenReturn(Arrays.asList("a", "b"));
assertThat(resolver.resolveProfileSpecific(this.contextMock, "vault:", false, this.profilesMock)).hasSize(3);
assertThat(
resolver.resolveProfileSpecific(this.contextMock, ConfigDataLocation.of("vault:"), this.profilesMock))
.hasSize(3);
assertThat(resolver.resolveProfileSpecific(this.contextMock, "vault://", false, this.profilesMock)).hasSize(3);
assertThat(
resolver.resolveProfileSpecific(this.contextMock, ConfigDataLocation.of("vault://"), this.profilesMock))
.hasSize(3);
}
@Test
@@ -68,7 +73,7 @@ public class VaultConfigDataLocationResolverUnitTests {
VaultConfigDataLocationResolver resolver = new VaultConfigDataLocationResolver();
List<VaultConfigLocation> locations = resolver.resolveProfileSpecific(this.contextMock,
"vault://my/context/path", false, this.profilesMock);
ConfigDataLocation.of("vault://my/context/path"), this.profilesMock);
assertThat(locations).hasSize(1);
assertThat(locations.get(0)).hasToString("VaultConfigLocation [path='my/context/path', optional=false]");