Refactor VaultServiceInstanceProvider into interface.
We now provide a VaultServerInstanceProvider interface with a default implementation based on DiscoveryClient as bean. See gh-186.
This commit is contained in:
@@ -24,7 +24,6 @@ import org.springframework.cloud.client.ServiceInstance;
|
|||||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
import org.springframework.cloud.commons.util.UtilAutoConfiguration;
|
import org.springframework.cloud.commons.util.UtilAutoConfiguration;
|
||||||
import org.springframework.cloud.vault.config.DiscoveryClientVaultBootstrapConfiguration.DiscoveryBootstrapConfiguration;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
@@ -46,55 +45,52 @@ import org.springframework.vault.client.VaultEndpointProvider;
|
|||||||
@EnableConfigurationProperties(VaultProperties.class)
|
@EnableConfigurationProperties(VaultProperties.class)
|
||||||
@Order(Ordered.LOWEST_PRECEDENCE - 2)
|
@Order(Ordered.LOWEST_PRECEDENCE - 2)
|
||||||
@EnableDiscoveryClient
|
@EnableDiscoveryClient
|
||||||
@Import({ UtilAutoConfiguration.class, DiscoveryBootstrapConfiguration.class })
|
@Import(UtilAutoConfiguration.class)
|
||||||
public class DiscoveryClientVaultBootstrapConfiguration {
|
public class DiscoveryClientVaultBootstrapConfiguration {
|
||||||
|
|
||||||
@ConditionalOnProperty(name = "spring.cloud.vault.enabled", matchIfMissing = true)
|
private final VaultProperties vaultProperties;
|
||||||
@Configuration
|
|
||||||
static class DiscoveryBootstrapConfiguration {
|
|
||||||
|
|
||||||
private final VaultProperties vaultProperties;
|
public DiscoveryClientVaultBootstrapConfiguration(VaultProperties vaultProperties) {
|
||||||
|
this.vaultProperties = vaultProperties;
|
||||||
public DiscoveryBootstrapConfiguration(VaultProperties vaultProperties) {
|
}
|
||||||
this.vaultProperties = vaultProperties;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean
|
|
||||||
public VaultServiceInstanceProvider vaultServerInstanceProvider(
|
|
||||||
DiscoveryClient discoveryClient) {
|
|
||||||
return new DiscoveryClientVaultServiceInstanceProvider(discoveryClient);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
|
@ConditionalOnProperty(name = "spring.cloud.vault.enabled", matchIfMissing = true)
|
||||||
|
public VaultServiceInstanceProvider vaultServerInstanceProvider(
|
||||||
|
DiscoveryClient discoveryClient) {
|
||||||
|
return new DiscoveryClientVaultServiceInstanceProvider(discoveryClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
@ConditionalOnProperty(name = "spring.cloud.vault.enabled", matchIfMissing = true)
|
||||||
public VaultEndpointProvider vaultEndpointProvider(
|
public VaultEndpointProvider vaultEndpointProvider(
|
||||||
VaultServiceInstanceProvider instanceProvider) {
|
VaultServiceInstanceProvider instanceProvider) {
|
||||||
|
|
||||||
final String serviceId = this.vaultProperties.getDiscovery().getServiceId();
|
final String serviceId = this.vaultProperties.getDiscovery().getServiceId();
|
||||||
|
|
||||||
final String fallbackScheme;
|
final String fallbackScheme;
|
||||||
|
|
||||||
if (StringUtils.hasText(this.vaultProperties.getUri())) {
|
if (StringUtils.hasText(this.vaultProperties.getUri())) {
|
||||||
fallbackScheme = URI.create(this.vaultProperties.getUri()).getScheme();
|
fallbackScheme = URI.create(this.vaultProperties.getUri()).getScheme();
|
||||||
}
|
|
||||||
else {
|
|
||||||
fallbackScheme = this.vaultProperties.getScheme();
|
|
||||||
}
|
|
||||||
|
|
||||||
ServiceInstance server = instanceProvider.getVaultServerInstance(serviceId);
|
|
||||||
|
|
||||||
final VaultEndpoint vaultEndpoint = VaultEndpoint.create(server.getHost(),
|
|
||||||
server.getPort());
|
|
||||||
|
|
||||||
if (server.getMetadata().containsKey("scheme")) {
|
|
||||||
vaultEndpoint.setScheme(server.getMetadata().get("scheme"));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
vaultEndpoint.setScheme(server.isSecure() ? "https" : fallbackScheme);
|
|
||||||
}
|
|
||||||
|
|
||||||
return () -> vaultEndpoint;
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
fallbackScheme = this.vaultProperties.getScheme();
|
||||||
|
}
|
||||||
|
|
||||||
|
ServiceInstance server = instanceProvider.getVaultServerInstance(serviceId);
|
||||||
|
|
||||||
|
final VaultEndpoint vaultEndpoint = VaultEndpoint.create(server.getHost(),
|
||||||
|
server.getPort());
|
||||||
|
|
||||||
|
if (server.getMetadata().containsKey("scheme")) {
|
||||||
|
vaultEndpoint.setScheme(server.getMetadata().get("scheme"));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
vaultEndpoint.setScheme(server.isSecure() ? "https" : fallbackScheme);
|
||||||
|
}
|
||||||
|
|
||||||
|
return () -> vaultEndpoint;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user