From f22c8a669c4b3f5b684ff2e88d3b7163114ee381 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 9 Sep 2016 19:05:37 +0200 Subject: [PATCH] Polishing Use VaultOperations in PropertySource. Create VaultEndpoint from URI. --- .../vault/client/VaultEndpoint.java | 40 +++++++++++++++ .../vault/core/env/VaultPropertySource.java | 15 +++--- .../vault/client/VaultEndpointUnitTests.java | 50 +++++++++++++++++++ 3 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 spring-vault-core/src/test/java/org/springframework/vault/client/VaultEndpointUnitTests.java diff --git a/spring-vault-core/src/main/java/org/springframework/vault/client/VaultEndpoint.java b/spring-vault-core/src/main/java/org/springframework/vault/client/VaultEndpoint.java index 1faf2a0a..da3d3b56 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/client/VaultEndpoint.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/client/VaultEndpoint.java @@ -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. */ diff --git a/spring-vault-core/src/main/java/org/springframework/vault/core/env/VaultPropertySource.java b/spring-vault-core/src/main/java/org/springframework/vault/core/env/VaultPropertySource.java index 61f9ac57..1aac08ba 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/core/env/VaultPropertySource.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/core/env/VaultPropertySource.java @@ -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 { +public class VaultPropertySource extends EnumerablePropertySource { protected final static Logger logger = LoggerFactory.getLogger(VaultPropertySource.class); @@ -47,11 +48,11 @@ public class VaultPropertySource extends EnumerablePropertySource * 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 * 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 (/)"); diff --git a/spring-vault-core/src/test/java/org/springframework/vault/client/VaultEndpointUnitTests.java b/spring-vault-core/src/test/java/org/springframework/vault/client/VaultEndpointUnitTests.java new file mode 100644 index 00000000..64b49bdb --- /dev/null +++ b/spring-vault-core/src/test/java/org/springframework/vault/client/VaultEndpointUnitTests.java @@ -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); + } +}