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

fixes gh-857
This commit is contained in:
Spencer Gibb
2016-03-01 16:20:54 -07:00
parent 2808162037
commit b8cbf4e637
4 changed files with 95 additions and 5 deletions

View File

@@ -112,6 +112,7 @@ public class ZuulProxyConfiguration extends ZuulConfiguration {
helper.setTraces(this.traces);
}
helper.setIgnoredHeaders(this.zuulProperties.getIgnoredHeaders());
helper.setTraceRequestBody(this.zuulProperties.isTraceRequestBody());
return helper;
}

View File

@@ -34,6 +34,7 @@ import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.springframework.boot.actuate.trace.TraceRepository;
import org.springframework.cloud.netflix.zuul.util.RequestUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@@ -69,6 +70,8 @@ public class ProxyRequestHelper {
private Set<String> whitelistHosts = new LinkedHashSet<>();
private boolean traceRequestBody = true;
public void setWhitelistHosts(Set<String> whitelistHosts) {
this.whitelistHosts.addAll(whitelistHosts);
}
@@ -85,6 +88,10 @@ public class ProxyRequestHelper {
this.traces = traces;
}
public void setTraceRequestBody(boolean traceRequestBody) {
this.traceRequestBody = traceRequestBody;
}
public String buildZuulRequestURI(HttpServletRequest request) {
RequestContext context = RequestContext.getCurrentContext();
String uri = request.getRequestURI();
@@ -225,7 +232,7 @@ public class ProxyRequestHelper {
public Map<String, Object> debug(String verb, String uri,
MultiValueMap<String, String> headers, MultiValueMap<String, String> params,
InputStream requestEntity) throws IOException {
Map<String, Object> info = new LinkedHashMap<String, Object>();
Map<String, Object> info = new LinkedHashMap<>();
if (this.traces != null) {
RequestContext context = RequestContext.getCurrentContext();
info.put("method", verb);
@@ -233,8 +240,8 @@ public class ProxyRequestHelper {
info.put("query", getQueryString(params));
info.put("remote", true);
info.put("proxy", context.get("proxy"));
Map<String, Object> trace = new LinkedHashMap<String, Object>();
Map<String, Object> input = new LinkedHashMap<String, Object>();
Map<String, Object> trace = new LinkedHashMap<>();
Map<String, Object> input = new LinkedHashMap<>();
trace.put("request", input);
info.put("headers", trace);
for (Entry<String, List<String>> entry : headers.entrySet()) {
@@ -258,9 +265,9 @@ public class ProxyRequestHelper {
return info;
}
private boolean shouldDebugBody(RequestContext ctx) {
/* for tests */ boolean shouldDebugBody(RequestContext ctx) {
HttpServletRequest request = ctx.getRequest();
if (ctx.isChunkedRequestBody()) {
if (!this.traceRequestBody || ctx.isChunkedRequestBody() || RequestUtils.isZuulServletRequest()) {
return false;
}
if (request == null || request.getContentType() == null) {

View File

@@ -72,6 +72,8 @@ public class ZuulProperties {
private Host host = new Host();
private boolean traceRequestBody = true;
public Set<String> getIgnoredHeaders() {
Set<String> ignoredHeaders = new LinkedHashSet<>(this.ignoredHeaders);
if (ClassUtils.isPresent(

View File

@@ -26,6 +26,7 @@ import org.springframework.boot.actuate.trace.InMemoryTraceRepository;
import org.springframework.boot.actuate.trace.Trace;
import org.springframework.boot.actuate.trace.TraceRepository;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.util.LinkedMultiValueMap;
@@ -85,6 +86,85 @@ public class ProxyRequestHelperTests {
}
@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() {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");