Do not inject base URI components to absolute URIs.

PrefixAwareUriBuilderFactory now no longer injects components from a base URI into a URI that is considered an absolute one (URI starting with https: or http: protocol schemes).

Resolves gh-377.
This commit is contained in:
Mark Paluch
2019-02-19 10:42:45 +01:00
parent 646d93eaf7
commit 122f1c72e7
2 changed files with 32 additions and 2 deletions

View File

@@ -191,6 +191,10 @@ public class VaultClients {
@Override
public UriBuilder uriString(String uriTemplate) {
if (uriTemplate.startsWith("http:") || uriTemplate.startsWith("https:")) {
return UriComponentsBuilder.fromUriString(uriTemplate);
}
VaultEndpoint endpoint = endpointProvider.getVaultEndpoint();
String baseUri = toBaseUri(endpoint);

View File

@@ -19,6 +19,7 @@ import java.net.URI;
import org.junit.Test;
import org.springframework.vault.client.VaultClients.PrefixAwareUriBuilderFactory;
import org.springframework.vault.client.VaultClients.PrefixAwareUriTemplateHandler;
import static org.assertj.core.api.Assertions.assertThat;
@@ -32,7 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class VaultClientsUnitTests {
@Test
public void shouldPrefixRelativeUrl() {
public void uriHandlerShouldPrefixRelativeUrl() {
VaultEndpoint localhost = VaultEndpoint.create("localhost", 8200);
PrefixAwareUriTemplateHandler handler = new PrefixAwareUriTemplateHandler(
@@ -44,7 +45,7 @@ public class VaultClientsUnitTests {
}
@Test
public void shouldNotPrefixAbsoluteUrl() {
public void uriHandlerShouldNotPrefixAbsoluteUrl() {
VaultEndpoint localhost = VaultEndpoint.create("localhost", 8200);
PrefixAwareUriTemplateHandler handler = new PrefixAwareUriTemplateHandler(
@@ -55,4 +56,29 @@ public class VaultClientsUnitTests {
assertThat(uri).hasScheme("https").hasHost("foo").hasPort(-1)
.hasPath("/path/bar");
}
@Test
public void uriBuilderShouldPrefixRelativeUrl() {
VaultEndpoint localhost = VaultEndpoint.create("localhost", 8200);
PrefixAwareUriBuilderFactory handler = new PrefixAwareUriBuilderFactory(
() -> localhost);
URI uri = handler.expand("/path/{bar}", "bar");
assertThat(uri).hasHost("localhost").hasPort(8200).hasPath("/v1/path/bar");
}
@Test
public void uriBuilderShouldNotPrefixAbsoluteUrl() {
VaultEndpoint localhost = VaultEndpoint.create("localhost", 8200);
PrefixAwareUriBuilderFactory handler = new PrefixAwareUriBuilderFactory(
() -> localhost);
URI uri = handler.expand("https://foo/path/{bar}", "bar");
assertThat(uri).hasScheme("https").hasHost("foo").hasPort(-1)
.hasPath("/path/bar");
}
}