Commit b8cbf4e6 by Spencer Gibb

Don't debug body if zuul servlet or if disabled.

fixes gh-857
parent 28081620
...@@ -112,6 +112,7 @@ public class ZuulProxyConfiguration extends ZuulConfiguration { ...@@ -112,6 +112,7 @@ public class ZuulProxyConfiguration extends ZuulConfiguration {
helper.setTraces(this.traces); helper.setTraces(this.traces);
} }
helper.setIgnoredHeaders(this.zuulProperties.getIgnoredHeaders()); helper.setIgnoredHeaders(this.zuulProperties.getIgnoredHeaders());
helper.setTraceRequestBody(this.zuulProperties.isTraceRequestBody());
return helper; return helper;
} }
......
...@@ -34,6 +34,7 @@ import java.util.Set; ...@@ -34,6 +34,7 @@ import java.util.Set;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.springframework.boot.actuate.trace.TraceRepository; import org.springframework.boot.actuate.trace.TraceRepository;
import org.springframework.cloud.netflix.zuul.util.RequestUtils;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
...@@ -69,6 +70,8 @@ public class ProxyRequestHelper { ...@@ -69,6 +70,8 @@ public class ProxyRequestHelper {
private Set<String> whitelistHosts = new LinkedHashSet<>(); private Set<String> whitelistHosts = new LinkedHashSet<>();
private boolean traceRequestBody = true;
public void setWhitelistHosts(Set<String> whitelistHosts) { public void setWhitelistHosts(Set<String> whitelistHosts) {
this.whitelistHosts.addAll(whitelistHosts); this.whitelistHosts.addAll(whitelistHosts);
} }
...@@ -85,6 +88,10 @@ public class ProxyRequestHelper { ...@@ -85,6 +88,10 @@ public class ProxyRequestHelper {
this.traces = traces; this.traces = traces;
} }
public void setTraceRequestBody(boolean traceRequestBody) {
this.traceRequestBody = traceRequestBody;
}
public String buildZuulRequestURI(HttpServletRequest request) { public String buildZuulRequestURI(HttpServletRequest request) {
RequestContext context = RequestContext.getCurrentContext(); RequestContext context = RequestContext.getCurrentContext();
String uri = request.getRequestURI(); String uri = request.getRequestURI();
...@@ -225,7 +232,7 @@ public class ProxyRequestHelper { ...@@ -225,7 +232,7 @@ public class ProxyRequestHelper {
public Map<String, Object> debug(String verb, String uri, public Map<String, Object> debug(String verb, String uri,
MultiValueMap<String, String> headers, MultiValueMap<String, String> params, MultiValueMap<String, String> headers, MultiValueMap<String, String> params,
InputStream requestEntity) throws IOException { InputStream requestEntity) throws IOException {
Map<String, Object> info = new LinkedHashMap<String, Object>(); Map<String, Object> info = new LinkedHashMap<>();
if (this.traces != null) { if (this.traces != null) {
RequestContext context = RequestContext.getCurrentContext(); RequestContext context = RequestContext.getCurrentContext();
info.put("method", verb); info.put("method", verb);
...@@ -233,8 +240,8 @@ public class ProxyRequestHelper { ...@@ -233,8 +240,8 @@ public class ProxyRequestHelper {
info.put("query", getQueryString(params)); info.put("query", getQueryString(params));
info.put("remote", true); info.put("remote", true);
info.put("proxy", context.get("proxy")); info.put("proxy", context.get("proxy"));
Map<String, Object> trace = new LinkedHashMap<String, Object>(); Map<String, Object> trace = new LinkedHashMap<>();
Map<String, Object> input = new LinkedHashMap<String, Object>(); Map<String, Object> input = new LinkedHashMap<>();
trace.put("request", input); trace.put("request", input);
info.put("headers", trace); info.put("headers", trace);
for (Entry<String, List<String>> entry : headers.entrySet()) { for (Entry<String, List<String>> entry : headers.entrySet()) {
...@@ -258,9 +265,9 @@ public class ProxyRequestHelper { ...@@ -258,9 +265,9 @@ public class ProxyRequestHelper {
return info; return info;
} }
private boolean shouldDebugBody(RequestContext ctx) { /* for tests */ boolean shouldDebugBody(RequestContext ctx) {
HttpServletRequest request = ctx.getRequest(); HttpServletRequest request = ctx.getRequest();
if (ctx.isChunkedRequestBody()) { if (!this.traceRequestBody || ctx.isChunkedRequestBody() || RequestUtils.isZuulServletRequest()) {
return false; return false;
} }
if (request == null || request.getContentType() == null) { if (request == null || request.getContentType() == null) {
......
...@@ -72,6 +72,8 @@ public class ZuulProperties { ...@@ -72,6 +72,8 @@ public class ZuulProperties {
private Host host = new Host(); private Host host = new Host();
private boolean traceRequestBody = true;
public Set<String> getIgnoredHeaders() { public Set<String> getIgnoredHeaders() {
Set<String> ignoredHeaders = new LinkedHashSet<>(this.ignoredHeaders); Set<String> ignoredHeaders = new LinkedHashSet<>(this.ignoredHeaders);
if (ClassUtils.isPresent( if (ClassUtils.isPresent(
......
...@@ -26,6 +26,7 @@ import org.springframework.boot.actuate.trace.InMemoryTraceRepository; ...@@ -26,6 +26,7 @@ import org.springframework.boot.actuate.trace.InMemoryTraceRepository;
import org.springframework.boot.actuate.trace.Trace; import org.springframework.boot.actuate.trace.Trace;
import org.springframework.boot.actuate.trace.TraceRepository; import org.springframework.boot.actuate.trace.TraceRepository;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
...@@ -86,6 +87,85 @@ public class ProxyRequestHelperTests { ...@@ -86,6 +87,85 @@ public class ProxyRequestHelperTests {
} }
@Test @Test
public void shouldDebugBodyDisabled() throws Exception {
RequestContext context = RequestContext.getCurrentContext();
ProxyRequestHelper helper = new ProxyRequestHelper();
helper.setTraceRequestBody(false);
assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(false));
}
@Test
public void shouldDebugBodyChunked() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/");
RequestContext context = RequestContext.getCurrentContext();
context.setChunkedRequestBody();
context.setRequest(request);
ProxyRequestHelper helper = new ProxyRequestHelper();
assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(false));
}
@Test
public void shouldDebugBodyServlet() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/");
RequestContext context = RequestContext.getCurrentContext();
context.setZuulEngineRan();
context.setRequest(request);
ProxyRequestHelper helper = new ProxyRequestHelper();
assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(false));
}
@Test
public void shouldDebugBodyNullContentType() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/");
request.setContentType(null);
RequestContext context = RequestContext.getCurrentContext();
context.setRequest(request);
ProxyRequestHelper helper = new ProxyRequestHelper();
assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(true));
}
@Test
public void shouldDebugBodyNullRequest() throws Exception {
RequestContext context = RequestContext.getCurrentContext();
ProxyRequestHelper helper = new ProxyRequestHelper();
assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(true));
}
@Test
public void shouldDebugBodyNotMultitypeContentType() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/");
request.setContentType(MediaType.APPLICATION_JSON_VALUE);
RequestContext context = RequestContext.getCurrentContext();
context.setRequest(request);
ProxyRequestHelper helper = new ProxyRequestHelper();
assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(true));
}
@Test
public void shouldDebugBodyMultitypeContentType() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/");
request.setContentType(MediaType.MULTIPART_FORM_DATA_VALUE);
RequestContext context = RequestContext.getCurrentContext();
context.setRequest(request);
ProxyRequestHelper helper = new ProxyRequestHelper();
assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(false));
}
@Test
public void buildZuulRequestHeadersWork() { public void buildZuulRequestHeadersWork() {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
request.addHeader("singleName", "singleValue"); request.addHeader("singleName", "singleValue");
......
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