diff --git a/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/DiscoveryClientVaultBootstrapConfiguration.java b/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/DiscoveryClientVaultBootstrapConfiguration.java index 252b57ce..b1a6f8fa 100644 --- a/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/DiscoveryClientVaultBootstrapConfiguration.java +++ b/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/DiscoveryClientVaultBootstrapConfiguration.java @@ -50,18 +50,21 @@ public class DiscoveryClientVaultBootstrapConfiguration { private final VaultProperties vaultProperties; - private final VaultServerInstanceProvider instanceProvider; - - public DiscoveryClientVaultBootstrapConfiguration(VaultProperties vaultProperties, - DiscoveryClient discoveryClient) { - + public DiscoveryClientVaultBootstrapConfiguration(VaultProperties vaultProperties) { this.vaultProperties = vaultProperties; - this.instanceProvider = new VaultServerInstanceProvider(discoveryClient); } @Bean @ConditionalOnMissingBean - public VaultEndpointProvider vaultEndpointProvider() { + public VaultServiceInstanceProvider vaultServerInstanceProvider( + DiscoveryClient discoveryClient) { + return new DiscoveryClientVaultServiceInstanceProvider(discoveryClient); + } + + @Bean + @ConditionalOnMissingBean + public VaultEndpointProvider vaultEndpointProvider( + VaultServiceInstanceProvider instanceProvider) { final String serviceId = this.vaultProperties.getDiscovery().getServiceId(); @@ -86,12 +89,6 @@ public class DiscoveryClientVaultBootstrapConfiguration { vaultEndpoint.setScheme(server.isSecure() ? "https" : fallbackScheme); } - return new VaultEndpointProvider() { - - @Override - public VaultEndpoint getVaultEndpoint() { - return vaultEndpoint; - } - }; + return () -> vaultEndpoint; } } diff --git a/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultServerInstanceProvider.java b/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/DiscoveryClientVaultServiceInstanceProvider.java similarity index 86% rename from spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultServerInstanceProvider.java rename to spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/DiscoveryClientVaultServiceInstanceProvider.java index 5b953826..b8b0091c 100644 --- a/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultServerInstanceProvider.java +++ b/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/DiscoveryClientVaultServiceInstanceProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2018 the original author or authors. + * Copyright 2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,11 +31,13 @@ import org.springframework.cloud.client.discovery.DiscoveryClient; */ @CommonsLog @RequiredArgsConstructor -class VaultServerInstanceProvider { +public class DiscoveryClientVaultServiceInstanceProvider implements + VaultServiceInstanceProvider { private final DiscoveryClient client; - ServiceInstance getVaultServerInstance(String serviceId) { + @Override + public ServiceInstance getVaultServerInstance(String serviceId) { log.debug("Locating Vault server (" + serviceId + ") via discovery"); diff --git a/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultServiceInstanceProvider.java b/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultServiceInstanceProvider.java new file mode 100644 index 00000000..f0b51ef0 --- /dev/null +++ b/spring-cloud-vault-config/src/main/java/org/springframework/cloud/vault/config/VaultServiceInstanceProvider.java @@ -0,0 +1,37 @@ +/* + * Copyright 2017-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cloud.vault.config; + +import org.springframework.cloud.client.ServiceInstance; + +/** + * Provider interface to obtain a {@link ServiceInstance} to look up the Vault service. + * + * @author Mark Paluch + * @since 1.1 + */ +@FunctionalInterface +public interface VaultServiceInstanceProvider { + + /** + * Lookup {@link ServiceInstance} by {@code serviceId}. + * + * @param serviceId the service Id. + * @return {@link ServiceInstance} for the given {@code serviceId}. + * @throws IllegalStateException if no service with {@code serviceId} was found. + */ + ServiceInstance getVaultServerInstance(String serviceId); +}