Commit f4681606 by Dave Syer

Add auth.route.* for controlling OAuth2 token relay

If user adds auth.route.<service>.scheme: passthru then instead of adding the bearer token the filter will not do anything (and the normal authorization header will be passed through to the backend) See gh-19
parent 16c25a28
sudo: false
language: java
before_install:
- git config user.name "$GIT_NAME"
......
......@@ -72,11 +72,13 @@ public class ProxyRouteLocator {
public ProxyRouteSpec getMatchingRoute(String path) {
String location = null;
String targetPath = null;
String id = null;
String prefix = properties.getPrefix();
for (Entry<String, ZuulRoute> entry : routes.get().entrySet()) {
String pattern = entry.getKey();
if (pathMatcher.match(pattern, path)) {
ZuulRoute route = entry.getValue();
id = route.getId();
location = route.getLocation();
targetPath = path;
if (path.startsWith(prefix) && properties.isStripPrefix()) {
......@@ -93,7 +95,7 @@ public class ProxyRouteLocator {
break;
}
}
return location == null ? null : new ProxyRouteSpec(targetPath, location, prefix);
return location == null ? null : new ProxyRouteSpec(id, targetPath, location, prefix);
}
// Package access so ZuulHandlerMapping can reset it's mappings
......@@ -178,6 +180,7 @@ public class ProxyRouteLocator {
@Data
@AllArgsConstructor
public static class ProxyRouteSpec {
private String id;
private String path;
private String location;
private String prefix;
......
......@@ -32,6 +32,12 @@ public class ZuulProperties {
if (!StringUtils.hasText(value.getLocation())) {
value.serviceId = entry.getKey();
}
if (!StringUtils.hasText(value.getId())) {
value.id = entry.getKey();
}
if (!StringUtils.hasText(value.getPath())) {
value.path = "/" + entry.getKey() + "/**";
}
}
return this.routes;
}
......@@ -40,6 +46,7 @@ public class ZuulProperties {
@AllArgsConstructor
@NoArgsConstructor
public static class ZuulRoute {
private String id;
private String path;
private String serviceId;
private String url;
......@@ -54,6 +61,7 @@ public class ZuulProperties {
location = values[1];
path = values[0];
}
this.id = extractId(path);
if (!path.startsWith("/")) {
path = "/" + path;
}
......@@ -62,6 +70,7 @@ public class ZuulProperties {
}
public ZuulRoute(String path, String location) {
this.id = extractId(path);
this.path = path;
setLocation(location);
}
......@@ -82,6 +91,12 @@ public class ZuulProperties {
serviceId = location;
}
}
private String extractId(String path) {
path = path.startsWith("/") ? path.substring(1) : path;
path = path.replace("/*","").replace("*", "");
return path;
}
}
}
......@@ -57,7 +57,7 @@ public class PreDecorationFilter extends ZuulFilter {
if (location != null) {
ctx.put("requestURI", route.getPath());
ctx.put("proxy", true);
ctx.put("proxy", route.getId());
if (location.startsWith("http:") || location.startsWith("https:")) {
ctx.setRouteHost(getUrl(location));
......
......@@ -315,7 +315,7 @@ public class RibbonRoutingFilter extends ZuulFilter {
}
for (String key : resp.getHeaders().keySet()) {
boolean isValidHeader = isValidHeader(key);
boolean isValidHeader = isIncludedHeader(key);
Collection<java.lang.String> list = resp.getHeaders().get(key);
for (String header : list) {
context.addOriginResponseHeader(key, header);
......@@ -331,7 +331,7 @@ public class RibbonRoutingFilter extends ZuulFilter {
}
boolean isValidHeader(String headerName) {
private boolean isIncludedHeader(String headerName) {
switch (headerName.toLowerCase()) {
case "connection":
case "content-length":
......
......@@ -299,7 +299,7 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
return requestEntity;
}
boolean isValidHeader(String name) {
private boolean isIncludedHeader(String name) {
if (name.toLowerCase().contains("content-length"))
return false;
if (!RequestContext.getCurrentContext().getResponseGZipped()) {
......@@ -316,7 +316,7 @@ public class SimpleHostRoutingFilter extends ZuulFilter {
while (headerNames.hasMoreElements()) {
String name = (String) headerNames.nextElement();
String value = request.getHeader(name);
if (isValidHeader(name))
if (isIncludedHeader(name))
headers.add(new BasicHeader(name, value));
}
......
......@@ -49,6 +49,7 @@ public class ProxyRouteLocatorTests {
routeLocator.getRoutes(); // force refresh
ProxyRouteSpec route = routeLocator.getMatchingRoute("/foo/1");
assertEquals("foo", route.getLocation());
assertEquals("foo", route.getId());
}
@Test
......@@ -65,7 +66,7 @@ public class ProxyRouteLocatorTests {
@Test
public void testGetMatchingPathWithNoPrefixStripping() throws Exception {
ProxyRouteLocator routeLocator = new ProxyRouteLocator(this.discovery, this.properties);
this.properties.getRoutes().put("foo", new ZuulRoute("/foo/**", "foo", null, false));
this.properties.getRoutes().put("foo", new ZuulRoute("foo", "/foo/**", "foo", null, false));
this.properties.setStripPrefix(false);
this.properties.setPrefix("/proxy");
routeLocator.getRoutes(); // force refresh
......@@ -89,7 +90,7 @@ public class ProxyRouteLocatorTests {
@Test
public void testGetMatchingPathWithGlobalPrefixStripping() throws Exception {
ProxyRouteLocator routeLocator = new ProxyRouteLocator(this.discovery, this.properties);
this.properties.getRoutes().put("foo", new ZuulRoute("/foo/**", "foo", null, false));
this.properties.getRoutes().put("foo", new ZuulRoute("foo", "/foo/**", "foo", null, false));
this.properties.setPrefix("/proxy");
routeLocator.getRoutes(); // force refresh
ProxyRouteSpec route = routeLocator.getMatchingRoute("/proxy/foo/1");
......
......@@ -55,7 +55,7 @@ public class PreDecorationFilterTests {
properties.setPrefix("/api");
properties.setStripPrefix(true);
request.setRequestURI("/api/foo/1");
routeLocator.addRoute(new ZuulRoute("/foo/**", "foo", null, false));
routeLocator.addRoute(new ZuulRoute("foo", "/foo/**", "foo", null, false));
filter.run();
RequestContext ctx = RequestContext.getCurrentContext();
assertEquals("/foo/1", ctx.get("requestURI"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment