enforce use of builder to create a new HTTPVaultConnector (#54)
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Remove constructors of HTTPVaultConnector and make the builder constructor package-private to enforce use of .builder()....build() For convenience we add direct builder constructors with a full URI argument to allow a one-line initialization if necessary.
This commit is contained in:
@ -24,7 +24,8 @@ import de.stklcode.jvault.connector.model.*;
|
||||
import de.stklcode.jvault.connector.model.response.*;
|
||||
import de.stklcode.jvault.connector.model.response.embedded.AuthMethod;
|
||||
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -40,7 +41,6 @@ import static java.util.Collections.singletonMap;
|
||||
* @since 0.1
|
||||
*/
|
||||
public class HTTPVaultConnector implements VaultConnector {
|
||||
private static final String PATH_PREFIX = "/v1/";
|
||||
private static final String PATH_SEAL_STATUS = "sys/seal-status";
|
||||
private static final String PATH_SEAL = "sys/seal";
|
||||
private static final String PATH_UNSEAL = "sys/unseal";
|
||||
@ -82,71 +82,26 @@ public class HTTPVaultConnector implements VaultConnector {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create connector using hostname and schema.
|
||||
* Get a new builder for a connector.
|
||||
*
|
||||
* @param hostname The hostname
|
||||
* @param useTLS If TRUE, use HTTPS, otherwise HTTP
|
||||
* @param baseURL Base URL.
|
||||
* @return Builder instance.
|
||||
* @throws URISyntaxException Invalid URI syntax.
|
||||
* @since 1.0
|
||||
*/
|
||||
public HTTPVaultConnector(final String hostname, final boolean useTLS) {
|
||||
this(hostname, useTLS, null);
|
||||
public static HTTPVaultConnectorBuilder builder(String baseURL) throws URISyntaxException {
|
||||
return new HTTPVaultConnectorBuilder().withBaseURL(baseURL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create connector using hostname, schema and port.
|
||||
* Get a new builder for a connector.
|
||||
*
|
||||
* @param hostname The hostname
|
||||
* @param useTLS If TRUE, use HTTPS, otherwise HTTP
|
||||
* @param port The port
|
||||
* @param baseURL Base URL.
|
||||
* @return Builder instance.
|
||||
* @since 1.0
|
||||
*/
|
||||
public HTTPVaultConnector(final String hostname, final boolean useTLS, final Integer port) {
|
||||
this(hostname, useTLS, port, PATH_PREFIX);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create connector using hostname, schema, port and path.
|
||||
*
|
||||
* @param hostname The hostname
|
||||
* @param useTLS If TRUE, use HTTPS, otherwise HTTP
|
||||
* @param port The port
|
||||
* @param prefix HTTP API prefix (default: /v1/)
|
||||
*/
|
||||
public HTTPVaultConnector(final String hostname, final boolean useTLS, final Integer port, final String prefix) {
|
||||
this(((useTLS) ? "https" : "http")
|
||||
+ "://" + hostname
|
||||
+ ((port != null) ? ":" + port : "")
|
||||
+ prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create connector using hostname, schema, port, path and trusted certificate.
|
||||
*
|
||||
* @param hostname The hostname
|
||||
* @param useTLS If TRUE, use HTTPS, otherwise HTTP
|
||||
* @param tlsVersion TLS version
|
||||
* @param port The port
|
||||
* @param prefix HTTP API prefix (default: /v1/)
|
||||
* @param trustedCaCert Trusted CA certificate
|
||||
* @param numberOfRetries Number of retries on 5xx errors
|
||||
* @param timeout Timeout for HTTP requests (milliseconds)
|
||||
*/
|
||||
public HTTPVaultConnector(final String hostname,
|
||||
final boolean useTLS,
|
||||
final String tlsVersion,
|
||||
final Integer port,
|
||||
final String prefix,
|
||||
final X509Certificate trustedCaCert,
|
||||
final int numberOfRetries,
|
||||
final Integer timeout) {
|
||||
this(
|
||||
((useTLS) ? "https" : "http")
|
||||
+ "://" + hostname
|
||||
+ ((port != null) ? ":" + port : "")
|
||||
+ prefix,
|
||||
trustedCaCert,
|
||||
numberOfRetries,
|
||||
timeout,
|
||||
tlsVersion
|
||||
);
|
||||
public static HTTPVaultConnectorBuilder builder(URI baseURL) {
|
||||
return new HTTPVaultConnectorBuilder().withBaseURL(baseURL);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -154,7 +109,7 @@ public class HTTPVaultConnector implements VaultConnector {
|
||||
*
|
||||
* @param builder The builder.
|
||||
*/
|
||||
public HTTPVaultConnector(final HTTPVaultConnectorBuilder builder) {
|
||||
HTTPVaultConnector(final HTTPVaultConnectorBuilder builder) {
|
||||
this.request = new RequestHelper(
|
||||
((builder.isWithTLS()) ? "https" : "http") + "://" +
|
||||
builder.getHost() +
|
||||
@ -167,68 +122,6 @@ public class HTTPVaultConnector implements VaultConnector {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create connector using full URL.
|
||||
*
|
||||
* @param baseURL The URL
|
||||
*/
|
||||
public HTTPVaultConnector(final String baseURL) {
|
||||
this(baseURL, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create connector using full URL and trusted certificate.
|
||||
*
|
||||
* @param baseURL The URL
|
||||
* @param trustedCaCert Trusted CA certificate
|
||||
*/
|
||||
public HTTPVaultConnector(final String baseURL, final X509Certificate trustedCaCert) {
|
||||
this(baseURL, trustedCaCert, 0, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create connector using full URL and trusted certificate.
|
||||
*
|
||||
* @param baseURL The URL
|
||||
* @param trustedCaCert Trusted CA certificate
|
||||
* @param numberOfRetries Number of retries on 5xx errors
|
||||
*/
|
||||
public HTTPVaultConnector(final String baseURL, final X509Certificate trustedCaCert, final int numberOfRetries) {
|
||||
this(baseURL, trustedCaCert, numberOfRetries, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create connector using full URL and trusted certificate.
|
||||
*
|
||||
* @param baseURL The URL
|
||||
* @param trustedCaCert Trusted CA certificate
|
||||
* @param numberOfRetries Number of retries on 5xx errors
|
||||
* @param timeout Timeout for HTTP requests (milliseconds)
|
||||
*/
|
||||
public HTTPVaultConnector(final String baseURL,
|
||||
final X509Certificate trustedCaCert,
|
||||
final int numberOfRetries,
|
||||
final Integer timeout) {
|
||||
this(baseURL, trustedCaCert, numberOfRetries, timeout, DEFAULT_TLS_VERSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create connector using full URL and trusted certificate.
|
||||
*
|
||||
* @param baseURL The URL
|
||||
* @param trustedCaCert Trusted CA certificate
|
||||
* @param numberOfRetries Number of retries on 5xx errors
|
||||
* @param timeout Timeout for HTTP requests (milliseconds)
|
||||
* @param tlsVersion TLS Version.
|
||||
*/
|
||||
public HTTPVaultConnector(final String baseURL,
|
||||
final X509Certificate trustedCaCert,
|
||||
final int numberOfRetries,
|
||||
final Integer timeout,
|
||||
final String tlsVersion) {
|
||||
this.request = new RequestHelper(baseURL, numberOfRetries, timeout, tlsVersion, trustedCaCert);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void resetAuth() {
|
||||
token = null;
|
||||
|
@ -16,13 +16,14 @@
|
||||
|
||||
package de.stklcode.jvault.connector;
|
||||
|
||||
import de.stklcode.jvault.connector.builder.VaultConnectorBuilder;
|
||||
import de.stklcode.jvault.connector.exception.ConnectionException;
|
||||
import de.stklcode.jvault.connector.exception.TlsException;
|
||||
import de.stklcode.jvault.connector.exception.VaultConnectorException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@ -30,6 +31,7 @@ import java.nio.file.Paths;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.CertificateFactory;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Vault Connector Builder implementation for HTTP Vault connectors.
|
||||
@ -37,7 +39,7 @@ import java.security.cert.X509Certificate;
|
||||
* @author Stefan Kalscheuer
|
||||
* @since 0.8.0
|
||||
*/
|
||||
public final class HTTPVaultConnectorBuilder implements VaultConnectorBuilder {
|
||||
public final class HTTPVaultConnectorBuilder {
|
||||
private static final String ENV_VAULT_ADDR = "VAULT_ADDR";
|
||||
private static final String ENV_VAULT_CACERT = "VAULT_CACERT";
|
||||
private static final String ENV_VAULT_TOKEN = "VAULT_TOKEN";
|
||||
@ -73,6 +75,32 @@ public final class HTTPVaultConnectorBuilder implements VaultConnectorBuilder {
|
||||
numberOfRetries = DEFAULT_NUMBER_OF_RETRIES;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set base URL, e.g. "protocol://host:port/prefix".
|
||||
*
|
||||
* @param baseURL Base URL
|
||||
* @return self
|
||||
* @throws URISyntaxException Invalid URI syntax.
|
||||
* @since 1.0
|
||||
*/
|
||||
public HTTPVaultConnectorBuilder withBaseURL(final String baseURL) throws URISyntaxException {
|
||||
return withBaseURL(new URI(baseURL));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set base URL, e.g. "protocol://host:port/prefix".
|
||||
*
|
||||
* @param baseURL Base URL
|
||||
* @return self
|
||||
* @since 1.0
|
||||
*/
|
||||
public HTTPVaultConnectorBuilder withBaseURL(final URI baseURL) {
|
||||
return withTLS(!("http".equalsIgnoreCase(Objects.requireNonNullElse(baseURL.getScheme(), ""))))
|
||||
.withHost(baseURL.getHost())
|
||||
.withPort(baseURL.getPort())
|
||||
.withPrefix(baseURL.getPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set hostname (default: 127.0.0.1).
|
||||
*
|
||||
@ -95,12 +123,20 @@ public final class HTTPVaultConnectorBuilder implements VaultConnectorBuilder {
|
||||
|
||||
/**
|
||||
* Set port (default: 8200).
|
||||
* A value of {@code null} or {@code -1} indicates that no port is specified, i.e. the protocol default is used.
|
||||
* Otherwise a valid port number bwetween 1 and 65535 is expected.
|
||||
*
|
||||
* @param port Vault TCP port
|
||||
* @return self
|
||||
*/
|
||||
public HTTPVaultConnectorBuilder withPort(final Integer port) {
|
||||
this.port = port;
|
||||
if (port < 0) {
|
||||
this.port = null;
|
||||
} else if(port < 1 || port > 65535) {
|
||||
throw new IllegalArgumentException("Port number " + port + " out of range");
|
||||
} else {
|
||||
this.port = port;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -341,7 +377,6 @@ public final class HTTPVaultConnectorBuilder implements VaultConnectorBuilder {
|
||||
*
|
||||
* @return Vault Connector instance.
|
||||
*/
|
||||
@Override
|
||||
public HTTPVaultConnector build() {
|
||||
return new HTTPVaultConnector(this);
|
||||
}
|
||||
@ -353,7 +388,6 @@ public final class HTTPVaultConnectorBuilder implements VaultConnectorBuilder {
|
||||
* @throws VaultConnectorException if authentication failed
|
||||
* @since 0.6.0
|
||||
*/
|
||||
@Override
|
||||
public HTTPVaultConnector buildAndAuth() throws VaultConnectorException {
|
||||
if (token == null) {
|
||||
throw new ConnectionException("No vault token provided, unable to authenticate.");
|
||||
|
@ -1,60 +0,0 @@
|
||||
/*
|
||||
* Copyright 2016-2021 Stefan Kalscheuer
|
||||
*
|
||||
* 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 de.stklcode.jvault.connector.builder;
|
||||
|
||||
import de.stklcode.jvault.connector.HTTPVaultConnector;
|
||||
import de.stklcode.jvault.connector.HTTPVaultConnectorBuilder;
|
||||
import de.stklcode.jvault.connector.VaultConnector;
|
||||
import de.stklcode.jvault.connector.exception.VaultConnectorException;
|
||||
|
||||
/**
|
||||
* Abstract Vault Connector Builder interface.
|
||||
* Provides builder style for Vault connectors.
|
||||
*
|
||||
* @author Stefan Kalscheuer
|
||||
* @since 0.8.0
|
||||
* @deprecated Builder interface is no longer maintained. Use {@link HTTPVaultConnector#builder()} for instantiation.
|
||||
*/
|
||||
@Deprecated(forRemoval = true)
|
||||
public interface VaultConnectorBuilder {
|
||||
/**
|
||||
* Get Factory implementation for HTTP Vault Connector.
|
||||
*
|
||||
* @return HTTP Connector Factory
|
||||
* @deprecated use {@link HTTPVaultConnector#builder()} instead.
|
||||
*/
|
||||
@Deprecated(forRemoval = true)
|
||||
static HTTPVaultConnectorBuilder http() {
|
||||
return HTTPVaultConnector.builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Build command, produces connector after initialization.
|
||||
*
|
||||
* @return Vault Connector instance.
|
||||
*/
|
||||
VaultConnector build();
|
||||
|
||||
/**
|
||||
* Build connector and authenticate with token set in factory or from environment.
|
||||
*
|
||||
* @return Authenticated Vault connector instance.
|
||||
* @throws VaultConnectorException if authentication failed
|
||||
* @since 0.6.0
|
||||
*/
|
||||
VaultConnector buildAndAuth() throws VaultConnectorException;
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
/*
|
||||
* Copyright 2016-2021 Stefan Kalscheuer
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This package contains the {@link de.stklcode.jvault.connector.builder.VaultConnectorBuilder} to initialize a
|
||||
* connector instance.
|
||||
*/
|
||||
@Deprecated(forRemoval = true)
|
||||
package de.stklcode.jvault.connector.builder;
|
@ -57,7 +57,7 @@ public final class RequestHelper implements Serializable {
|
||||
final Integer timeout,
|
||||
final String tlsVersion,
|
||||
final X509Certificate trustedCaCert) {
|
||||
this.baseURL = baseURL;
|
||||
this.baseURL = baseURL + (baseURL.endsWith("/") ? "" : "/");
|
||||
this.retries = retries;
|
||||
this.timeout = timeout;
|
||||
this.tlsVersion = tlsVersion;
|
||||
|
Reference in New Issue
Block a user