Fallback to default port if Endpoint URI does not declare a port.
We now use the default port (scheme-specific) if a URI does not declare an explicit port number. Fixes gh-99.
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2016 the original author or authors.
|
* Copyright 2016-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -16,6 +16,7 @@
|
|||||||
package org.springframework.vault.client;
|
package org.springframework.vault.client;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
@@ -27,7 +28,7 @@ import org.springframework.util.Assert;
|
|||||||
* <p>
|
* <p>
|
||||||
* A {@link VaultEndpoint} defines the hostname, TCP port and the protocol scheme (HTTP or
|
* A {@link VaultEndpoint} defines the hostname, TCP port and the protocol scheme (HTTP or
|
||||||
* HTTPS).
|
* HTTPS).
|
||||||
*
|
*
|
||||||
* @author Mark Paluch
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
@EqualsAndHashCode
|
@EqualsAndHashCode
|
||||||
@@ -53,7 +54,7 @@ public class VaultEndpoint implements Serializable {
|
|||||||
/**
|
/**
|
||||||
* Create a secure {@link VaultEndpoint} given a {@code host} and {@code port} using
|
* Create a secure {@link VaultEndpoint} given a {@code host} and {@code port} using
|
||||||
* {@code https}.
|
* {@code https}.
|
||||||
*
|
*
|
||||||
* @param host must not be empty or {@literal null}.
|
* @param host must not be empty or {@literal null}.
|
||||||
* @param port must be a valid port in the range of 1-65535
|
* @param port must be a valid port in the range of 1-65535
|
||||||
* @return a new {@link VaultEndpoint}.
|
* @return a new {@link VaultEndpoint}.
|
||||||
@@ -86,7 +87,14 @@ public class VaultEndpoint implements Serializable {
|
|||||||
VaultEndpoint vaultEndpoint = new VaultEndpoint();
|
VaultEndpoint vaultEndpoint = new VaultEndpoint();
|
||||||
|
|
||||||
vaultEndpoint.setHost(uri.getHost());
|
vaultEndpoint.setHost(uri.getHost());
|
||||||
vaultEndpoint.setPort(uri.getPort());
|
try {
|
||||||
|
vaultEndpoint.setPort(uri.getPort() == -1 ? uri.toURL().getDefaultPort()
|
||||||
|
: uri.getPort());
|
||||||
|
}
|
||||||
|
catch (MalformedURLException e) {
|
||||||
|
throw new IllegalArgumentException(String.format(
|
||||||
|
"Can't retrieve default port from %s", uri), e);
|
||||||
|
}
|
||||||
vaultEndpoint.setScheme(uri.getScheme());
|
vaultEndpoint.setScheme(uri.getScheme());
|
||||||
|
|
||||||
return vaultEndpoint;
|
return vaultEndpoint;
|
||||||
@@ -101,7 +109,7 @@ public class VaultEndpoint implements Serializable {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the hostname.
|
* Sets the hostname.
|
||||||
*
|
*
|
||||||
* @param host must not be empty or {@literal null}.
|
* @param host must not be empty or {@literal null}.
|
||||||
*/
|
*/
|
||||||
public void setHost(String host) {
|
public void setHost(String host) {
|
||||||
@@ -156,7 +164,7 @@ public class VaultEndpoint implements Serializable {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Build the Vault URI string based on the given {@code path}.
|
* Build the Vault URI string based on the given {@code path}.
|
||||||
*
|
*
|
||||||
* @param path must not be empty or {@literal null}.
|
* @param path must not be empty or {@literal null}.
|
||||||
* @return constructed URI String.
|
* @return constructed URI String.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2016 the original author or authors.
|
* Copyright 2016-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -23,13 +23,13 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for {@link VaultEndpoint}.
|
* Unit tests for {@link VaultEndpoint}.
|
||||||
*
|
*
|
||||||
* @author Mark Paluch
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
public class VaultEndpointUnitTests {
|
public class VaultEndpointUnitTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateEndpointFromHostAndPort() throws Exception {
|
public void shouldCreateEndpointFromHostAndPort() {
|
||||||
|
|
||||||
VaultEndpoint endpoint = VaultEndpoint.create("host", 1234);
|
VaultEndpoint endpoint = VaultEndpoint.create("host", 1234);
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ public class VaultEndpointUnitTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateEndpointFromURI() throws Exception {
|
public void shouldCreateEndpointFromURI() {
|
||||||
|
|
||||||
VaultEndpoint endpoint = VaultEndpoint.from(URI.create("http://127.0.0.1:443"));
|
VaultEndpoint endpoint = VaultEndpoint.from(URI.create("http://127.0.0.1:443"));
|
||||||
|
|
||||||
@@ -47,4 +47,14 @@ public class VaultEndpointUnitTests {
|
|||||||
assertThat(endpoint.getHost()).isEqualTo("127.0.0.1");
|
assertThat(endpoint.getHost()).isEqualTo("127.0.0.1");
|
||||||
assertThat(endpoint.getPort()).isEqualTo(443);
|
assertThat(endpoint.getPort()).isEqualTo(443);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCreateEndpointFromURIWithoutPort() {
|
||||||
|
|
||||||
|
VaultEndpoint endpoint = VaultEndpoint.from(URI.create("http://127.0.0.1"));
|
||||||
|
|
||||||
|
assertThat(endpoint.getScheme()).isEqualTo("http");
|
||||||
|
assertThat(endpoint.getHost()).isEqualTo("127.0.0.1");
|
||||||
|
assertThat(endpoint.getPort()).isEqualTo(80);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user