Allow isSecure property of LoadBalancer ServiceInstance to determine the scheme of the URL. Fixes #641

This commit is contained in:
Ryan Baxter
2018-12-18 15:51:19 -05:00
parent f06edbaf19
commit 077b647ec4
5 changed files with 70 additions and 7 deletions

View File

@@ -876,6 +876,13 @@ spring:
- Path=/service/**
----
NOTE: The `isSecure` value of the `ServiceInstance` returned from the `LoadBalancer` will override
the scheme specified in the request made to the Gateway. For example, if the request comes into the Gateway over `HTTPS`
but the `ServiceInstance` indicates it is not secure, then the downstream request will be made over
`HTTP`. The opposite situation can also apply. However if `GATEWAY_SCHEME_PREFIX_ATTR` is specified for the
route in the Gateway configuration, the prefix will be stripped and the resulting scheme from the
route URL will override the `ServiceInstance` configuration.
=== Netty Routing Filter
The Netty Routing Filter runs if the url located in the `ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR` exchange attribute has a `http` or `https` scheme. It uses the Netty `HttpClient` to make the downstream proxy request. The response is put in the `ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR` exchange attribute for use in a later filter. (There is an experimental `WebClientHttpRoutingFilter` that performs the same function, but does not require netty)

View File

@@ -76,7 +76,7 @@ public class LoadBalancerClientFilter implements GlobalFilter, Ordered {
// if the `lb:<scheme>` mechanism was used, use `<scheme>` as the default,
// if the loadbalancer doesn't provide one.
String overrideScheme = null;
String overrideScheme = instance.isSecure() ? "https" : "http";
if (schemePrefix != null) {
overrideScheme = url.getScheme();
}

View File

@@ -123,6 +123,58 @@ public class LoadBalancerClientFilterTests {
verifyNoMoreInteractions(chain);
}
@Test
public void instanceOverrideNonSecureScheme() {
MockServerHttpRequest request = MockServerHttpRequest
.get("https://localhost")
.build();
URI lbUri = URI.create("lb://service1");
ServerWebExchange webExchange = testFilter(request, lbUri);
URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
assertThat(uri).hasScheme("http").hasHost("service1-host1");
}
@Test
public void instanceOverrideSecureScheme() {
MockServerHttpRequest request = MockServerHttpRequest
.get("http://localhost")
.build();
URI lbUri = URI.create("lb://service1");
ServerWebExchange webExchange = testFilter(request, lbUri, 443);
URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
assertThat(uri).hasScheme("https").hasHost("service1-host1");
}
@Test
public void instanceOverrideSecureSchemePrefix() {
MockServerHttpRequest request = MockServerHttpRequest
.get("https://localhost")
.build();
URI lbUri = URI.create("http://service1");
ServerWebExchange exchange = MockServerWebExchange.from(request);
exchange.getAttributes().put(GATEWAY_SCHEME_PREFIX_ATTR, "lb");
ServerWebExchange webExchange = testFilter(exchange, lbUri, 443);
URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
assertThat(uri).hasScheme("http").hasHost("service1-host1");
}
@Test
public void instanceOverrideNonSecureSchemePrefix() {
MockServerHttpRequest request = MockServerHttpRequest
.get("http://localhost")
.build();
URI lbUri = URI.create("https://service1");
ServerWebExchange exchange = MockServerWebExchange.from(request);
exchange.getAttributes().put(GATEWAY_SCHEME_PREFIX_ATTR, "lb");
ServerWebExchange webExchange = testFilter(exchange, lbUri, 8081);
URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
assertThat(uri).hasScheme("https").hasHost("service1-host1");
}
@Test
public void happyPath() {
@@ -208,7 +260,7 @@ public class LoadBalancerClientFilterTests {
exchange = MockServerWebExchange.from(request);
exchange.getAttributes().put(GATEWAY_SCHEME_PREFIX_ATTR, "lb");
ServerWebExchange webExchange = testFilter(exchange, lbUri);
ServerWebExchange webExchange = testFilter(exchange, lbUri, 8081);
URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
assertThat(uri).hasScheme("ws").hasHost("service1-host1")
.hasParameter("a", "b");
@@ -274,10 +326,14 @@ public class LoadBalancerClientFilterTests {
}
private ServerWebExchange testFilter(MockServerHttpRequest request, URI uri) {
return testFilter(MockServerWebExchange.from(request), uri);
return testFilter(MockServerWebExchange.from(request), uri, 8081);
}
private ServerWebExchange testFilter(ServerWebExchange exchange, URI uri) {
private ServerWebExchange testFilter(MockServerHttpRequest request, URI uri, int port) {
return testFilter(MockServerWebExchange.from(request), uri, port);
}
private ServerWebExchange testFilter(ServerWebExchange exchange, URI uri, int port) {
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, uri);
ArgumentCaptor<ServerWebExchange> captor = ArgumentCaptor.forClass(ServerWebExchange.class);
@@ -288,7 +344,7 @@ public class LoadBalancerClientFilterTests {
when(clientFactory.getLoadBalancerContext("service1")).thenReturn(new RibbonLoadBalancerContext(loadBalancer));
when(clientFactory.getLoadBalancer("service1")).thenReturn(loadBalancer);
when(loadBalancer.chooseServer(any())).thenReturn(new Server("service1-host1", 8081));
when(loadBalancer.chooseServer(any())).thenReturn(new Server("service1-host1", port));
RibbonLoadBalancerClient client = new RibbonLoadBalancerClient(clientFactory);

View File

@@ -1,6 +1,6 @@
test:
hostport: httpbin.org:80
uri: lb://testservice
uri: lb:https://testservice
server:
ssl:

View File

@@ -1,6 +1,6 @@
test:
hostport: httpbin.org:80
uri: lb://testservice
uri: lb:https://testservice
server:
ssl: