Skip to content

Commit eb0a12e

Browse files
committed
reuse builder methods when initializing from environment
We can just pass the environment variable to other pre-existing methods instead of parsing the URL twice. This also fixes URLs without explicit ports where we should not store "-1" in this case.
1 parent dad3502 commit eb0a12e

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
### Improvements
1111
* Replace deprecated `java.net.URL` usage with `java.net.URI` (#94)
1212

13+
### Fix
14+
* Fix initialization from environment without explicit port
15+
1316
### Dependencies
1417
* Updated Jackson to 2.18.3 (#90)
1518

src/main/java/de/stklcode/jvault/connector/HTTPVaultConnectorBuilder.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.security.cert.CertificateException;
3232
import java.security.cert.CertificateFactory;
3333
import java.security.cert.X509Certificate;
34-
import java.util.Objects;
3534

3635
/**
3736
* Vault Connector Builder implementation for HTTP Vault connectors.
@@ -96,10 +95,14 @@ public HTTPVaultConnectorBuilder withBaseURL(final String baseURL) throws URISyn
9695
* @since 1.0
9796
*/
9897
public HTTPVaultConnectorBuilder withBaseURL(final URI baseURL) {
99-
return withTLS(!("http".equalsIgnoreCase(Objects.requireNonNullElse(baseURL.getScheme(), ""))))
98+
String prefix = baseURL.getPath();
99+
if (prefix == null || prefix.isBlank()) {
100+
prefix = DEFAULT_PREFIX;
101+
}
102+
return withTLS(!("http".equalsIgnoreCase(baseURL.getScheme())))
100103
.withHost(baseURL.getHost())
101104
.withPort(baseURL.getPort())
102-
.withPrefix(baseURL.getPath());
105+
.withPrefix(prefix);
103106
}
104107

105108
/**
@@ -303,10 +306,7 @@ public HTTPVaultConnectorBuilder fromEnv() throws VaultConnectorException {
303306
/* Parse URL from environment variable */
304307
if (System.getenv(ENV_VAULT_ADDR) != null && !System.getenv(ENV_VAULT_ADDR).isBlank()) {
305308
try {
306-
var uri = new URI(System.getenv(ENV_VAULT_ADDR));
307-
this.host = uri.getHost();
308-
this.port = uri.getPort();
309-
this.tls = uri.getScheme().equalsIgnoreCase("https");
309+
withBaseURL(System.getenv(ENV_VAULT_ADDR));
310310
} catch (URISyntaxException e) {
311311
throw new ConnectionException("URL provided in environment variable malformed", e);
312312
}
@@ -315,7 +315,7 @@ public HTTPVaultConnectorBuilder fromEnv() throws VaultConnectorException {
315315
/* Read number of retries */
316316
if (System.getenv(ENV_VAULT_MAX_RETRIES) != null) {
317317
try {
318-
numberOfRetries = Integer.parseInt(System.getenv(ENV_VAULT_MAX_RETRIES));
318+
withNumberOfRetries(Integer.parseInt(System.getenv(ENV_VAULT_MAX_RETRIES)));
319319
} catch (NumberFormatException ignored) {
320320
/* Ignore malformed values. */
321321
}

src/test/java/de/stklcode/jvault/connector/HTTPVaultConnectorBuilderTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
*/
4242
class HTTPVaultConnectorBuilderTest {
4343
private static final String VAULT_ADDR = "https://localhost:8201";
44+
private static final String VAULT_ADDR_2 = "http://localhost";
45+
private static final String VAULT_ADDR_3 = "https://localhost/vault/";
4446
private static final Integer VAULT_MAX_RETRIES = 13;
4547
private static final String VAULT_TOKEN = "00001111-2222-3333-4444-555566667777";
4648

@@ -115,6 +117,22 @@ void testFromEnv() throws Exception {
115117

116118
return null;
117119
});
120+
withVaultEnv(VAULT_ADDR_2, null, null, null).execute(() -> {
121+
HTTPVaultConnectorBuilder builder = assertDoesNotThrow(
122+
() -> HTTPVaultConnector.builder().fromEnv(),
123+
"Factory creation from minimal environment failed"
124+
);
125+
assertEquals(VAULT_ADDR_2 + "/v1/", getRequestHelperPrivate(builder.build(), "baseURL"), "URL without port not set correctly");
126+
return null;
127+
});
128+
withVaultEnv(VAULT_ADDR_3, null, null, null).execute(() -> {
129+
HTTPVaultConnectorBuilder builder = assertDoesNotThrow(
130+
() -> HTTPVaultConnector.builder().fromEnv(),
131+
"Factory creation from minimal environment failed"
132+
);
133+
assertEquals(VAULT_ADDR_3, getRequestHelperPrivate(builder.build(), "baseURL"), "URL with custom path not set correctly");
134+
return null;
135+
});
118136

119137
// Provide address and number of retries.
120138
withVaultEnv(VAULT_ADDR, null, VAULT_MAX_RETRIES.toString(), null).execute(() -> {

0 commit comments

Comments
 (0)