move builder class into main package, deprecate interface
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
f783286909
commit
ce24de7347
@ -2,6 +2,7 @@
|
||||
|
||||
### Breaking
|
||||
* Requires Java 11 or later
|
||||
* Builder invocation has changed, use `HTTPVaultConnector.builder()....build()`
|
||||
|
||||
### Removal
|
||||
* Remove deprecated `VaultConnectorFactory` in favor of `VaultConnectorBuilder` with identical API
|
||||
|
@ -16,7 +16,6 @@
|
||||
|
||||
package de.stklcode.jvault.connector;
|
||||
|
||||
import de.stklcode.jvault.connector.builder.HTTPVaultConnectorBuilder;
|
||||
import de.stklcode.jvault.connector.exception.AuthorizationRequiredException;
|
||||
import de.stklcode.jvault.connector.exception.InvalidRequestException;
|
||||
import de.stklcode.jvault.connector.exception.VaultConnectorException;
|
||||
@ -72,6 +71,16 @@ public class HTTPVaultConnector implements VaultConnector {
|
||||
private String token; // Current token.
|
||||
private long tokenTTL = 0; // Expiration time for current token.
|
||||
|
||||
/**
|
||||
* Get a new builder for a connector.
|
||||
*
|
||||
* @return Builder instance.
|
||||
* @since 1.0
|
||||
*/
|
||||
public static HTTPVaultConnectorBuilder builder() {
|
||||
return new HTTPVaultConnectorBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create connector using hostname and schema.
|
||||
*
|
||||
|
@ -14,9 +14,9 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package de.stklcode.jvault.connector.builder;
|
||||
package de.stklcode.jvault.connector;
|
||||
|
||||
import de.stklcode.jvault.connector.HTTPVaultConnector;
|
||||
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;
|
||||
@ -64,7 +64,7 @@ public final class HTTPVaultConnectorBuilder implements VaultConnectorBuilder {
|
||||
* Default empty constructor.
|
||||
* Initializes factory with default values.
|
||||
*/
|
||||
public HTTPVaultConnectorBuilder() {
|
||||
HTTPVaultConnectorBuilder() {
|
||||
host = DEFAULT_HOST;
|
||||
port = DEFAULT_PORT;
|
||||
tls = DEFAULT_TLS;
|
||||
@ -89,7 +89,7 @@ public final class HTTPVaultConnectorBuilder implements VaultConnectorBuilder {
|
||||
*
|
||||
* @return Hostname or IP address
|
||||
*/
|
||||
public String getHost() {
|
||||
String getHost() {
|
||||
return this.host;
|
||||
}
|
||||
|
||||
@ -109,7 +109,7 @@ public final class HTTPVaultConnectorBuilder implements VaultConnectorBuilder {
|
||||
*
|
||||
* @return Vault TCP port
|
||||
*/
|
||||
public Integer getPort() {
|
||||
Integer getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
@ -129,7 +129,7 @@ public final class HTTPVaultConnectorBuilder implements VaultConnectorBuilder {
|
||||
*
|
||||
* @return use TLS or not
|
||||
*/
|
||||
public boolean isWithTLS() {
|
||||
boolean isWithTLS() {
|
||||
return this.tls;
|
||||
}
|
||||
|
||||
@ -138,7 +138,7 @@ public final class HTTPVaultConnectorBuilder implements VaultConnectorBuilder {
|
||||
*
|
||||
* @return TLS version.
|
||||
*/
|
||||
public String getTlsVersion() {
|
||||
String getTlsVersion() {
|
||||
return this.tlsVersion;
|
||||
}
|
||||
|
||||
@ -201,7 +201,7 @@ public final class HTTPVaultConnectorBuilder implements VaultConnectorBuilder {
|
||||
*
|
||||
* @return Vault API prefix.
|
||||
*/
|
||||
public String getPrefix() {
|
||||
String getPrefix() {
|
||||
return this.prefix;
|
||||
}
|
||||
|
||||
@ -239,7 +239,7 @@ public final class HTTPVaultConnectorBuilder implements VaultConnectorBuilder {
|
||||
*
|
||||
* @return path to certificate file, if specified.
|
||||
*/
|
||||
public X509Certificate getTrustedCA() {
|
||||
X509Certificate getTrustedCA() {
|
||||
return this.trustedCA;
|
||||
}
|
||||
|
||||
@ -311,7 +311,7 @@ public final class HTTPVaultConnectorBuilder implements VaultConnectorBuilder {
|
||||
*
|
||||
* @return The number of retries to attempt on 5xx errors (default: 0)
|
||||
*/
|
||||
public int getNumberOfRetries() {
|
||||
int getNumberOfRetries() {
|
||||
return this.numberOfRetries;
|
||||
}
|
||||
|
||||
@ -332,15 +332,27 @@ public final class HTTPVaultConnectorBuilder implements VaultConnectorBuilder {
|
||||
*
|
||||
* @return Timeout value in milliseconds.
|
||||
*/
|
||||
public Integer getTimeout() {
|
||||
Integer getTimeout() {
|
||||
return this.timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build command, produces connector after initialization.
|
||||
*
|
||||
* @return Vault Connector instance.
|
||||
*/
|
||||
@Override
|
||||
public HTTPVaultConnector build() {
|
||||
return new HTTPVaultConnector(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
@Override
|
||||
public HTTPVaultConnector buildAndAuth() throws VaultConnectorException {
|
||||
if (token == null) {
|
@ -16,6 +16,8 @@
|
||||
|
||||
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;
|
||||
|
||||
@ -25,15 +27,19 @@ import de.stklcode.jvault.connector.exception.VaultConnectorException;
|
||||
*
|
||||
* @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 new HTTPVaultConnectorBuilder();
|
||||
return HTTPVaultConnector.builder();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -18,4 +18,5 @@
|
||||
* 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;
|
||||
|
@ -14,10 +14,9 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package de.stklcode.jvault.connector.builder;
|
||||
package de.stklcode.jvault.connector;
|
||||
|
||||
import com.github.stefanbirkner.systemlambda.SystemLambda;
|
||||
import de.stklcode.jvault.connector.HTTPVaultConnector;
|
||||
import de.stklcode.jvault.connector.exception.ConnectionException;
|
||||
import de.stklcode.jvault.connector.exception.TlsException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -50,14 +49,14 @@ class HTTPVaultConnectorBuilderTest {
|
||||
@Test
|
||||
void builderTest() throws Exception {
|
||||
/* Minimal configuration */
|
||||
HTTPVaultConnector connector = VaultConnectorBuilder.http().withHost("vault.example.com").build();
|
||||
HTTPVaultConnector connector = HTTPVaultConnector.builder().withHost("vault.example.com").build();
|
||||
|
||||
assertEquals("https://vault.example.com:8200/v1/", getRequestHelperPrivate(connector, "baseURL"), "URL not set correctly");
|
||||
assertNull(getRequestHelperPrivate(connector, "trustedCaCert"), "Trusted CA cert set when no cert provided");
|
||||
assertEquals(0, getRequestHelperPrivate(connector, "retries"), "Number of retries unexpectedly set");
|
||||
|
||||
/* Specify all options */
|
||||
HTTPVaultConnectorBuilder builder = VaultConnectorBuilder.http()
|
||||
HTTPVaultConnectorBuilder builder = HTTPVaultConnector.builder()
|
||||
.withHost("vault2.example.com")
|
||||
.withoutTLS()
|
||||
.withPort(1234)
|
||||
@ -81,7 +80,7 @@ class HTTPVaultConnectorBuilderTest {
|
||||
/* Provide address only should be enough */
|
||||
withVaultEnv(VAULT_ADDR, null, null, null).execute(() -> {
|
||||
HTTPVaultConnectorBuilder builder = assertDoesNotThrow(
|
||||
() -> VaultConnectorBuilder.http().fromEnv(),
|
||||
() -> HTTPVaultConnector.builder().fromEnv(),
|
||||
"Factory creation from minimal environment failed"
|
||||
);
|
||||
HTTPVaultConnector connector = builder.build();
|
||||
@ -96,7 +95,7 @@ class HTTPVaultConnectorBuilderTest {
|
||||
/* Provide address and number of retries */
|
||||
withVaultEnv(VAULT_ADDR, null, VAULT_MAX_RETRIES.toString(), null).execute(() -> {
|
||||
HTTPVaultConnectorBuilder builder = assertDoesNotThrow(
|
||||
() -> VaultConnectorBuilder.http().fromEnv(),
|
||||
() -> HTTPVaultConnector.builder().fromEnv(),
|
||||
"Factory creation from environment failed"
|
||||
);
|
||||
HTTPVaultConnector connector = builder.build();
|
||||
@ -113,7 +112,7 @@ class HTTPVaultConnectorBuilderTest {
|
||||
withVaultEnv(VAULT_ADDR, VAULT_CACERT, VAULT_MAX_RETRIES.toString(), null).execute(() -> {
|
||||
TlsException e = assertThrows(
|
||||
TlsException.class,
|
||||
() -> VaultConnectorBuilder.http().fromEnv(),
|
||||
() -> HTTPVaultConnector.builder().fromEnv(),
|
||||
"Creation with unknown cert path failed."
|
||||
);
|
||||
assertTrue(e.getCause() instanceof NoSuchFileException);
|
||||
@ -125,7 +124,7 @@ class HTTPVaultConnectorBuilderTest {
|
||||
/* Automatic authentication */
|
||||
withVaultEnv(VAULT_ADDR, null, VAULT_MAX_RETRIES.toString(), VAULT_TOKEN).execute(() -> {
|
||||
HTTPVaultConnectorBuilder builder = assertDoesNotThrow(
|
||||
() -> VaultConnectorBuilder.http().fromEnv(),
|
||||
() -> HTTPVaultConnector.builder().fromEnv(),
|
||||
"Factory creation from minimal environment failed"
|
||||
);
|
||||
assertEquals(VAULT_TOKEN, getPrivate(builder, "token"), "Token not set correctly");
|
||||
@ -137,7 +136,7 @@ class HTTPVaultConnectorBuilderTest {
|
||||
withVaultEnv("This is not a valid URL!", null, VAULT_MAX_RETRIES.toString(), VAULT_TOKEN).execute(() -> {
|
||||
assertThrows(
|
||||
ConnectionException.class,
|
||||
() -> VaultConnectorBuilder.http().fromEnv(),
|
||||
() -> HTTPVaultConnector.builder().fromEnv(),
|
||||
"Invalid URL from environment should raise an exception"
|
||||
);
|
||||
|
@ -19,7 +19,6 @@ package de.stklcode.jvault.connector;
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
import com.github.tomakehurst.wiremock.client.WireMock;
|
||||
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
|
||||
import de.stklcode.jvault.connector.builder.VaultConnectorBuilder;
|
||||
import de.stklcode.jvault.connector.exception.*;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
|
@ -16,8 +16,6 @@
|
||||
|
||||
package de.stklcode.jvault.connector;
|
||||
|
||||
import de.stklcode.jvault.connector.builder.HTTPVaultConnectorBuilder;
|
||||
import de.stklcode.jvault.connector.builder.VaultConnectorBuilder;
|
||||
import de.stklcode.jvault.connector.exception.*;
|
||||
import de.stklcode.jvault.connector.model.AppRole;
|
||||
import de.stklcode.jvault.connector.model.AuthBackend;
|
||||
@ -92,7 +90,7 @@ class HTTPVaultConnectorTest {
|
||||
}
|
||||
|
||||
/* Initialize connector */
|
||||
HTTPVaultConnectorBuilder builder = VaultConnectorBuilder.http()
|
||||
HTTPVaultConnectorBuilder builder = HTTPVaultConnector.builder()
|
||||
.withHost(config.getHost())
|
||||
.withPort(config.getPort())
|
||||
.withTLS(isTls);
|
||||
|
Loading…
x
Reference in New Issue
Block a user