Polishing
Use VaultOperations in PropertySource. Create VaultEndpoint from URI.
This commit is contained in:
@@ -49,6 +49,46 @@ public class VaultEndpoint implements Serializable {
|
||||
*/
|
||||
private String scheme = "https";
|
||||
|
||||
/**
|
||||
* Create a {@link VaultEndpoint} given a {@code host} and {@code port}.
|
||||
*
|
||||
* @param host must not be empty or {@literal null}.
|
||||
* @param port must be a valid port in the range of 1-65535
|
||||
* @return a new {@link VaultEndpoint}.
|
||||
*/
|
||||
public static VaultEndpoint create(String host, int port) {
|
||||
|
||||
Assert.hasText(host, "Host must not be empty");
|
||||
|
||||
VaultEndpoint vaultEndpoint = new VaultEndpoint();
|
||||
|
||||
vaultEndpoint.setHost(host);
|
||||
vaultEndpoint.setPort(port);
|
||||
|
||||
return vaultEndpoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link VaultEndpoint} given a {@link URI}.
|
||||
*
|
||||
* @param uri must contain hostname, port and scheme, must not be empty or {@literal null}.
|
||||
* @return a new {@link VaultEndpoint}.
|
||||
*/
|
||||
public static VaultEndpoint from(URI uri) {
|
||||
|
||||
Assert.notNull(uri, "URI must not be null");
|
||||
Assert.hasText(uri.getScheme(), "Scheme must not be empty");
|
||||
Assert.hasText(uri.getHost(), "Host must not be empty");
|
||||
|
||||
VaultEndpoint vaultEndpoint = new VaultEndpoint();
|
||||
|
||||
vaultEndpoint.setHost(uri.getHost());
|
||||
vaultEndpoint.setPort(uri.getPort());
|
||||
vaultEndpoint.setScheme(uri.getScheme());
|
||||
|
||||
return vaultEndpoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the hostname.
|
||||
*/
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.springframework.core.env.EnumerablePropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.vault.client.VaultException;
|
||||
import org.springframework.vault.core.VaultOperations;
|
||||
import org.springframework.vault.core.VaultTemplate;
|
||||
import org.springframework.vault.support.VaultResponse;
|
||||
|
||||
@@ -35,7 +36,7 @@ import org.springframework.vault.support.VaultResponse;
|
||||
* @since 3.1
|
||||
* @see org.springframework.core.env.PropertiesPropertySource
|
||||
*/
|
||||
public class VaultPropertySource extends EnumerablePropertySource<VaultTemplate> {
|
||||
public class VaultPropertySource extends EnumerablePropertySource<VaultOperations> {
|
||||
|
||||
protected final static Logger logger = LoggerFactory.getLogger(VaultPropertySource.class);
|
||||
|
||||
@@ -47,11 +48,11 @@ public class VaultPropertySource extends EnumerablePropertySource<VaultTemplate>
|
||||
* Create a new {@link VaultPropertySource} given a {@link VaultTemplate} and {@code path} inside of Vault. This
|
||||
* property source loads properties upon construction.
|
||||
*
|
||||
* @param template must not be {@literal null}.
|
||||
* @param vaultOperations must not be {@literal null}.
|
||||
* @param path the path inside Vault (e.g. {@code secret/myapp/myproperties}. Must not be empty or {@literal null}.
|
||||
*/
|
||||
public VaultPropertySource(VaultTemplate template, String path) {
|
||||
this(path, template, path);
|
||||
public VaultPropertySource(VaultOperations vaultOperations, String path) {
|
||||
this(path, vaultOperations, path);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,12 +60,12 @@ public class VaultPropertySource extends EnumerablePropertySource<VaultTemplate>
|
||||
* Vault. This property source loads properties upon construction.
|
||||
*
|
||||
* @param name name of the property source, must not be {@literal null}.
|
||||
* @param template must not be {@literal null}.
|
||||
* @param vaultOperations must not be {@literal null}.
|
||||
* @param path the path inside Vault (e.g. {@code secret/myapp/myproperties}. Must not be empty or {@literal null}.
|
||||
*/
|
||||
public VaultPropertySource(String name, VaultTemplate template, String path) {
|
||||
public VaultPropertySource(String name, VaultOperations vaultOperations, String path) {
|
||||
|
||||
super(name, template);
|
||||
super(name, vaultOperations);
|
||||
|
||||
Assert.hasText(path, "Path name must contain at least one character");
|
||||
Assert.isTrue(!path.startsWith("/"), "Path name must not start with a slash (/)");
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2016 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.vault.client;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link VaultEndpoint}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class VaultEndpointUnitTests {
|
||||
|
||||
@Test
|
||||
public void shouldCreateEndpointFromHostAndPort() throws Exception {
|
||||
|
||||
VaultEndpoint endpoint = VaultEndpoint.create("host", 1234);
|
||||
|
||||
assertThat(endpoint.getScheme()).isEqualTo("https");
|
||||
assertThat(endpoint.getHost()).isEqualTo("host");
|
||||
assertThat(endpoint.getPort()).isEqualTo(1234);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateEndpointFromURI() throws Exception {
|
||||
|
||||
VaultEndpoint endpoint = VaultEndpoint.from(URI.create("http://127.0.0.1:443"));
|
||||
|
||||
assertThat(endpoint.getScheme()).isEqualTo("http");
|
||||
assertThat(endpoint.getHost()).isEqualTo("127.0.0.1");
|
||||
assertThat(endpoint.getPort()).isEqualTo(443);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user