From ec4fbc5d3f2ce385c2689161ffce20f8861b0713 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Thu, 3 Jun 2021 11:46:24 +0200 Subject: [PATCH] remove deprecated factory classes (#46) VaultConnectorFactory and its implementation have been deprecated since 0.8 in favor of VaultConnectorBuilder. Finally remove the old classes. --- CHANGELOG.md | 3 + .../factory/HTTPVaultConnectorFactory.java | 204 ------------------ .../factory/VaultConnectorFactory.java | 42 ---- .../connector/factory/package-info.java | 23 -- .../HTTPVaultConnectorFactoryTest.java | 131 ----------- 5 files changed, 3 insertions(+), 400 deletions(-) delete mode 100644 src/main/java/de/stklcode/jvault/connector/factory/HTTPVaultConnectorFactory.java delete mode 100644 src/main/java/de/stklcode/jvault/connector/factory/VaultConnectorFactory.java delete mode 100644 src/main/java/de/stklcode/jvault/connector/factory/package-info.java delete mode 100644 src/test/java/de/stklcode/jvault/connector/factory/HTTPVaultConnectorFactoryTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 08a6504..7addcc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ ### Breaking * Requires Java 11 or later +### Removal +* Remove deprecated `VaultConnectorFactory` in favor of `VaultConnectorBuilder` with identical API + ### Improvements * Use pre-sized map objects for fixed-size payloads * Remove Apache HTTP Client dependency in favor of Java 11 HTTP diff --git a/src/main/java/de/stklcode/jvault/connector/factory/HTTPVaultConnectorFactory.java b/src/main/java/de/stklcode/jvault/connector/factory/HTTPVaultConnectorFactory.java deleted file mode 100644 index 0d22143..0000000 --- a/src/main/java/de/stklcode/jvault/connector/factory/HTTPVaultConnectorFactory.java +++ /dev/null @@ -1,204 +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.factory; - -import de.stklcode.jvault.connector.HTTPVaultConnector; -import de.stklcode.jvault.connector.builder.HTTPVaultConnectorBuilder; -import de.stklcode.jvault.connector.exception.VaultConnectorException; - -import javax.net.ssl.SSLContext; -import java.nio.file.Path; -import java.security.cert.X509Certificate; - -/** - * Vault Connector Factory implementation for HTTP Vault connectors. - * - * @author Stefan Kalscheuer - * @since 0.1 - * @deprecated As of 0.8.0 please refer to {@link de.stklcode.jvault.connector.builder.HTTPVaultConnectorBuilder} with identical API. - */ -@Deprecated -public final class HTTPVaultConnectorFactory extends VaultConnectorFactory { - - private final HTTPVaultConnectorBuilder delegate; - - /** - * Default empty constructor. - * Initializes factory with default values. - */ - public HTTPVaultConnectorFactory() { - delegate = new HTTPVaultConnectorBuilder(); - } - - /** - * Set hostname (default: 127.0.0.1). - * - * @param host Hostname or IP address - * @return self - */ - public HTTPVaultConnectorFactory withHost(final String host) { - delegate.withHost(host); - return this; - } - - /** - * Set port (default: 8200). - * - * @param port Vault TCP port - * @return self - */ - public HTTPVaultConnectorFactory withPort(final Integer port) { - delegate.withPort(port); - return this; - } - - /** - * Set TLS usage (default: TRUE). - * - * @param useTLS use TLS or not - * @return self - */ - public HTTPVaultConnectorFactory withTLS(final boolean useTLS) { - delegate.withTLS(useTLS); - return this; - } - - /** - * Convenience Method for TLS usage (enabled by default). - * - * @return self - */ - public HTTPVaultConnectorFactory withTLS() { - return withTLS(true); - } - - /** - * Convenience Method for NOT using TLS. - * - * @return self - */ - public HTTPVaultConnectorFactory withoutTLS() { - return withTLS(false); - } - - /** - * Set API prefix. Default is "/v1/" and changes should not be necessary for current state of development. - * - * @param prefix Vault API prefix (default: "/v1/" - * @return self - */ - public HTTPVaultConnectorFactory withPrefix(final String prefix) { - delegate.withPrefix(prefix); - return this; - } - - /** - * Add a trusted CA certificate for HTTPS connections. - * - * @param cert path to certificate file - * @return self - * @throws VaultConnectorException on error - * @since 0.4.0 - */ - public HTTPVaultConnectorFactory withTrustedCA(final Path cert) throws VaultConnectorException { - delegate.withTrustedCA(cert); - return this; - } - - /** - * Add a trusted CA certificate for HTTPS connections. - * - * @param cert path to certificate file - * @return self - * @since 0.8.0 - */ - public HTTPVaultConnectorFactory withTrustedCA(final X509Certificate cert) { - delegate.withTrustedCA(cert); - return this; - } - - /** - * Add a custom SSL context. - * Overwrites certificates set by {@link #withTrustedCA}. - * - * @param sslContext the SSL context - * @return self - * @since 0.4.0 - * @deprecated As of 0.8.0 this is no longer supported, please use {@link #withTrustedCA(Path)} or {@link #withTrustedCA(X509Certificate)}. - */ - public HTTPVaultConnectorFactory withSslContext(final SSLContext sslContext) { - throw new UnsupportedOperationException("Use of deprecated method, please switch to withTrustedCA()"); - } - - /** - * Set token for automatic authentication, using {@link #buildAndAuth()}. - * - * @param token Vault token - * @return self - * @since 0.6.0 - */ - public HTTPVaultConnectorFactory withToken(final String token) { - delegate.withToken(token); - return this; - } - - /** - * Build connector based on the {@code }VAULT_ADDR} and {@code VAULT_CACERT} (optional) environment variables. - * - * @return self - * @throws VaultConnectorException if Vault address from environment variables is malformed - * @since 0.6.0 - */ - public HTTPVaultConnectorFactory fromEnv() throws VaultConnectorException { - delegate.fromEnv(); - return this; - } - - /** - * Define the number of retries to attempt on 5xx errors. - * - * @param numberOfRetries The number of retries to attempt on 5xx errors (default: 0) - * @return self - * @since 0.6.0 - */ - public HTTPVaultConnectorFactory withNumberOfRetries(final int numberOfRetries) { - delegate.withNumberOfRetries(numberOfRetries); - return this; - } - - /** - * Define a custom timeout for the HTTP connection. - * - * @param milliseconds Timeout value in milliseconds. - * @return self - * @since 0.6.0 - */ - public HTTPVaultConnectorFactory withTimeout(final int milliseconds) { - delegate.withTimeout(milliseconds); - return this; - } - - @Override - public HTTPVaultConnector build() { - return delegate.build(); - } - - @Override - public HTTPVaultConnector buildAndAuth() throws VaultConnectorException { - return delegate.buildAndAuth(); - } -} diff --git a/src/main/java/de/stklcode/jvault/connector/factory/VaultConnectorFactory.java b/src/main/java/de/stklcode/jvault/connector/factory/VaultConnectorFactory.java deleted file mode 100644 index c94021a..0000000 --- a/src/main/java/de/stklcode/jvault/connector/factory/VaultConnectorFactory.java +++ /dev/null @@ -1,42 +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.factory; - -import de.stklcode.jvault.connector.builder.VaultConnectorBuilder; - -/** - * Abstract Vault Connector Factory interface. - * Provides builder pattern style factory for Vault connectors. - * - * @author Stefan Kalscheuer - * @since 0.1 - * @deprecated As of 0.8.0 please refer to {@link VaultConnectorBuilder} with identical API. - */ -@Deprecated -public abstract class VaultConnectorFactory implements VaultConnectorBuilder { - /** - * Get Factory implementation for HTTP Vault Connector. - * - * @return HTTP Connector Factory - * @deprecated As of 0.8.0 please refer to {@link VaultConnectorBuilder#http()}. - */ - @Deprecated - public static HTTPVaultConnectorFactory httpFactory() { - return new HTTPVaultConnectorFactory(); - } - -} diff --git a/src/main/java/de/stklcode/jvault/connector/factory/package-info.java b/src/main/java/de/stklcode/jvault/connector/factory/package-info.java deleted file mode 100644 index 3cef0ec..0000000 --- a/src/main/java/de/stklcode/jvault/connector/factory/package-info.java +++ /dev/null @@ -1,23 +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.factory.VaultConnectorFactory} to initialize a - * connector instance. - * - * @deprecated As of v0.8.0 please refer to {@link de.stklcode.jvault.connector.builder}. - */ -package de.stklcode.jvault.connector.factory; diff --git a/src/test/java/de/stklcode/jvault/connector/factory/HTTPVaultConnectorFactoryTest.java b/src/test/java/de/stklcode/jvault/connector/factory/HTTPVaultConnectorFactoryTest.java deleted file mode 100644 index dc3df2e..0000000 --- a/src/test/java/de/stklcode/jvault/connector/factory/HTTPVaultConnectorFactoryTest.java +++ /dev/null @@ -1,131 +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.factory; - -import com.github.stefanbirkner.systemlambda.SystemLambda; -import de.stklcode.jvault.connector.HTTPVaultConnector; -import de.stklcode.jvault.connector.exception.TlsException; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -import java.io.File; -import java.lang.reflect.Field; -import java.nio.file.NoSuchFileException; - -import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable; -import static org.hamcrest.CoreMatchers.*; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertThrows; - -/** - * JUnit test for HTTP Vault connector factory - * - * @author Stefan Kalscheuer - * @since 0.6.0 - */ -class HTTPVaultConnectorFactoryTest { - private static String VAULT_ADDR = "https://localhost:8201"; - private static Integer VAULT_MAX_RETRIES = 13; - private static String VAULT_TOKEN = "00001111-2222-3333-4444-555566667777"; - - @TempDir - File tempDir; - - /** - * Test building from environment variables - */ - @Test - void testFromEnv() throws Exception { - /* Provide address only should be enough */ - withVaultEnv(VAULT_ADDR, null, null, null).execute(() -> { - HTTPVaultConnectorFactory factory = assertDoesNotThrow( - () -> VaultConnectorFactory.httpFactory().fromEnv(), - "Factory creation from minimal environment failed" - ); - HTTPVaultConnector connector = factory.build(); - - assertThat("URL nor set correctly", getRequestHelperPrivate(connector, "baseURL"), is(equalTo(VAULT_ADDR + "/v1/"))); - assertThat("Trusted CA cert set when no cert provided", getRequestHelperPrivate(connector, "trustedCaCert"), is(nullValue())); - assertThat("Non-default number of retries, when none set", getRequestHelperPrivate(connector, "retries"), is(0)); - - return null; - }); - - /* Provide address and number of retries */ - withVaultEnv(VAULT_ADDR, null, VAULT_MAX_RETRIES.toString(), null).execute(() -> { - HTTPVaultConnectorFactory factory = assertDoesNotThrow( - () -> VaultConnectorFactory.httpFactory().fromEnv(), - "Factory creation from environment failed" - ); - HTTPVaultConnector connector = factory.build(); - - assertThat("URL nor set correctly", getRequestHelperPrivate(connector, "baseURL"), is(equalTo(VAULT_ADDR + "/v1/"))); - assertThat("Trusted CA cert set when no cert provided", getRequestHelperPrivate(connector, "trustedCaCert"), is(nullValue())); - assertThat("Number of retries not set correctly", getRequestHelperPrivate(connector, "retries"), is(VAULT_MAX_RETRIES)); - - return null; - }); - - /* Provide CA certificate */ - String VAULT_CACERT = tempDir.toString() + "/doesnotexist"; - withVaultEnv(VAULT_ADDR, VAULT_CACERT, VAULT_MAX_RETRIES.toString(), null).execute(() -> { - TlsException e = assertThrows( - TlsException.class, - () -> VaultConnectorFactory.httpFactory().fromEnv(), - "Creation with unknown cert path failed." - ); - assertThat(e.getCause(), is(instanceOf(NoSuchFileException.class))); - assertThat(((NoSuchFileException) e.getCause()).getFile(), is(VAULT_CACERT)); - - return null; - }); - - /* Automatic authentication */ - withVaultEnv(VAULT_ADDR, null, VAULT_MAX_RETRIES.toString(), VAULT_TOKEN).execute(() -> { - HTTPVaultConnectorFactory factory = assertDoesNotThrow( - () -> VaultConnectorFactory.httpFactory().fromEnv(), - "Factory creation from minimal environment failed" - ); - assertThat("Token nor set correctly", getPrivate(getPrivate(factory, "delegate"), "token"), is(equalTo(VAULT_TOKEN))); - - return null; - }); - } - - private SystemLambda.WithEnvironmentVariables withVaultEnv(String vault_addr, String vault_cacert, String vault_max_retries, String vault_token) { - return withEnvironmentVariable("VAULT_ADDR", vault_addr) - .and("VAULT_CACERT", vault_cacert) - .and("VAULT_MAX_RETRIES", vault_max_retries) - .and("VAULT_TOKEN", vault_token); - } - - private Object getRequestHelperPrivate(HTTPVaultConnector connector, String fieldName) throws NoSuchFieldException, IllegalAccessException { - return getPrivate(getPrivate(connector, "request"), fieldName); - } - - private Object getPrivate(Object target, String fieldName) throws NoSuchFieldException, IllegalAccessException { - Field field = target.getClass().getDeclaredField(fieldName); - if (field.isAccessible()) { - return field.get(target); - } - field.setAccessible(true); - Object value = field.get(target); - field.setAccessible(false); - return value; - } -}