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 d74e094ef4
commit 583a685f05
2 changed files with 32 additions and 2 deletions

View File

@@ -219,6 +219,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

@@ -23,6 +23,7 @@ import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.vault.client.VaultClients.PrefixAwareUriBuilderFactory;
import org.springframework.vault.client.VaultClients.PrefixAwareUriTemplateHandler;
import org.springframework.web.client.RestTemplate;
@@ -40,7 +41,7 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
public class VaultClientsUnitTests {
@Test
public void shouldPrefixRelativeUrl() {
public void uriHandlerShouldPrefixRelativeUrl() {
VaultEndpoint localhost = VaultEndpoint.create("localhost", 8200);
PrefixAwareUriTemplateHandler handler = new PrefixAwareUriTemplateHandler(
@@ -52,7 +53,7 @@ public class VaultClientsUnitTests {
}
@Test
public void shouldNotPrefixAbsoluteUrl() {
public void uriHandlerShouldNotPrefixAbsoluteUrl() {
VaultEndpoint localhost = VaultEndpoint.create("localhost", 8200);
PrefixAwareUriTemplateHandler handler = new PrefixAwareUriTemplateHandler(
@@ -64,6 +65,31 @@ public class VaultClientsUnitTests {
.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");
}
@Test
public void shouldApplyNamespace() {