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:
Mark Paluch
2018-01-23 15:19:01 +01:00
parent 56ee82d6fb
commit bfa6923ccd

View File

@@ -24,7 +24,6 @@ import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
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.Configuration;
import org.springframework.context.annotation.Import;
@@ -46,55 +45,52 @@ import org.springframework.vault.client.VaultEndpointProvider;
@EnableConfigurationProperties(VaultProperties.class)
@Order(Ordered.LOWEST_PRECEDENCE - 2)
@EnableDiscoveryClient
@Import({ UtilAutoConfiguration.class, DiscoveryBootstrapConfiguration.class })
@Import(UtilAutoConfiguration.class)
public class DiscoveryClientVaultBootstrapConfiguration {
@ConditionalOnProperty(name = "spring.cloud.vault.enabled", matchIfMissing = true)
@Configuration
static class DiscoveryBootstrapConfiguration {
private final VaultProperties vaultProperties;
private final VaultProperties vaultProperties;
public DiscoveryBootstrapConfiguration(VaultProperties vaultProperties) {
this.vaultProperties = vaultProperties;
}
@Bean
@ConditionalOnMissingBean
public VaultServiceInstanceProvider vaultServerInstanceProvider(
DiscoveryClient discoveryClient) {
return new DiscoveryClientVaultServiceInstanceProvider(discoveryClient);
}
public DiscoveryClientVaultBootstrapConfiguration(VaultProperties vaultProperties) {
this.vaultProperties = vaultProperties;
}
@Bean
@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(
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())) {
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;
if (StringUtils.hasText(this.vaultProperties.getUri())) {
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;
}
}