You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@knox.apache.org by mo...@apache.org on 2022/01/26 20:39:27 UTC

[knox] branch master updated: KNOX-2704 - Honor request id (correlation id) set by upstream loadbalancer (#534)

This is an automated email from the ASF dual-hosted git repository.

more pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/knox.git


The following commit(s) were added to refs/heads/master by this push:
     new 4073660  KNOX-2704 - Honor request id (correlation id) set by upstream loadbalancer (#534)
4073660 is described below

commit 4073660cc307fe3f769ddd3f521d15a3b50293ec
Author: Sandeep Moré <mo...@gmail.com>
AuthorDate: Wed Jan 26 15:39:21 2022 -0500

    KNOX-2704 - Honor request id (correlation id) set by upstream loadbalancer (#534)
    
    * KNOX-2704 - Honor request id (correlation id) set by upstream loadbalancer
---
 .../org/apache/knox/gateway/GatewayFilter.java     | 15 ++++-
 .../knox/gateway/filter/CorrelationHandler.java    |  7 ++-
 .../org/apache/knox/gateway/GatewayFilterTest.java | 71 ++++++++++++++++++++++
 3 files changed, 89 insertions(+), 4 deletions(-)

diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/GatewayFilter.java b/gateway-server/src/main/java/org/apache/knox/gateway/GatewayFilter.java
index e8f0b49..0667c5a 100644
--- a/gateway-server/src/main/java/org/apache/knox/gateway/GatewayFilter.java
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/GatewayFilter.java
@@ -38,6 +38,7 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletRequestWrapper;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.knox.gateway.audit.api.Action;
 import org.apache.knox.gateway.audit.api.ActionOutcome;
 import org.apache.knox.gateway.audit.api.AuditContext;
@@ -60,6 +61,8 @@ import org.apache.knox.gateway.util.urltemplate.Matcher;
 import org.apache.knox.gateway.util.urltemplate.Parser;
 import org.apache.knox.gateway.util.urltemplate.Template;
 
+import static org.apache.knox.gateway.filter.CorrelationHandler.REQUEST_ID_HEADER_NAME;
+
 public class GatewayFilter implements Filter {
 
   private static final FilterChain EMPTY_CHAIN = new FilterChain() {
@@ -150,7 +153,13 @@ public class GatewayFilter implements Filter {
       }
     }
 
-    assignCorrelationRequestId();
+    /* If request contains X-Request-Id header use it else use random uuid as correlation id */
+    final String reqID = ((HttpServletRequest) servletRequest).getHeader(REQUEST_ID_HEADER_NAME);
+    if (StringUtils.isBlank(reqID)) {
+      assignCorrelationRequestId(UUID.randomUUID().toString());
+    } else {
+      assignCorrelationRequestId(reqID);
+    }
     // Populate Audit/correlation parameters
     AuditContext auditContext = auditService.getContext();
     if(auditContext == null) {
@@ -236,11 +245,11 @@ public class GatewayFilter implements Filter {
   }
 
   // Now creating the correlation context only if required since it may be created upstream in the CorrelationHandler.
-  private void assignCorrelationRequestId() {
+  private void assignCorrelationRequestId(final String requestID) {
     CorrelationService correlationService = CorrelationServiceFactory.getCorrelationService();
     CorrelationContext correlationContext = correlationService.getContext();
     if( correlationContext == null ) {
-      correlationService.attachContext(new Log4jCorrelationContext(UUID.randomUUID().toString(), null, null));
+      correlationService.attachContext(new Log4jCorrelationContext(requestID, null, null));
     }
   }
 
diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/filter/CorrelationHandler.java b/gateway-server/src/main/java/org/apache/knox/gateway/filter/CorrelationHandler.java
index 7b6dab2..ba5b1ff 100644
--- a/gateway-server/src/main/java/org/apache/knox/gateway/filter/CorrelationHandler.java
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/filter/CorrelationHandler.java
@@ -23,6 +23,7 @@ import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.knox.gateway.audit.api.CorrelationService;
 import org.apache.knox.gateway.audit.api.CorrelationServiceFactory;
 import org.apache.knox.gateway.audit.log4j.correlation.Log4jCorrelationContext;
@@ -30,13 +31,17 @@ import org.eclipse.jetty.server.Request;
 import org.eclipse.jetty.server.handler.HandlerWrapper;
 
 public class CorrelationHandler extends HandlerWrapper {
+  public static final String REQUEST_ID_HEADER_NAME = "X-Request-Id";
 
   @Override
   public void handle( String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response )
       throws IOException, ServletException {
     CorrelationService correlationService = CorrelationServiceFactory.getCorrelationService();
+    /* If request id is specified use it */
+    final String req_id = request.getHeader(REQUEST_ID_HEADER_NAME);
     correlationService.attachContext(
-            new Log4jCorrelationContext(UUID.randomUUID().toString(), null, null));
+            new Log4jCorrelationContext(StringUtils.isBlank(req_id) ? UUID.randomUUID().toString() : req_id,
+                null, null));
     try {
       super.handle( target, baseRequest, request, response );
     } finally {
diff --git a/gateway-server/src/test/java/org/apache/knox/gateway/GatewayFilterTest.java b/gateway-server/src/test/java/org/apache/knox/gateway/GatewayFilterTest.java
index 5e0d844..a349819 100644
--- a/gateway-server/src/test/java/org/apache/knox/gateway/GatewayFilterTest.java
+++ b/gateway-server/src/test/java/org/apache/knox/gateway/GatewayFilterTest.java
@@ -18,6 +18,9 @@
 package org.apache.knox.gateway;
 
 import org.apache.knox.gateway.audit.api.AuditServiceFactory;
+import org.apache.knox.gateway.audit.api.CorrelationContext;
+import org.apache.knox.gateway.audit.api.CorrelationService;
+import org.apache.knox.gateway.audit.api.CorrelationServiceFactory;
 import org.apache.knox.gateway.config.GatewayConfig;
 import org.apache.knox.gateway.filter.AbstractGatewayFilter;
 import org.apache.knox.gateway.topology.Topology;
@@ -39,7 +42,11 @@ import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.net.URISyntaxException;
 
+import static org.apache.knox.gateway.filter.CorrelationHandler.REQUEST_ID_HEADER_NAME;
 import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.CoreMatchers.notNullValue;
 import static org.hamcrest.MatcherAssert.assertThat;
 
 @Category( { UnitTests.class, FastTests.class } )
@@ -140,6 +147,18 @@ public class GatewayFilterTest {
 
   }
 
+  public static class TestCorrelationFilter extends AbstractGatewayFilter {
+    public String correlation_id;
+    public String request_id;
+    @Override
+    protected void doFilter( HttpServletRequest request, HttpServletResponse response, FilterChain chain ) throws IOException, ServletException {
+      this.request_id = request.getHeader( REQUEST_ID_HEADER_NAME );
+      CorrelationService correlationService = CorrelationServiceFactory.getCorrelationService();
+      CorrelationContext correlationContext = correlationService.getContext();
+      correlation_id = correlationContext.getRequestId();
+    }
+  }
+
   @Test
   public void testTargetServiceRoleRequestAttribute() throws Exception {
 
@@ -177,6 +196,58 @@ public class GatewayFilterTest {
 
   }
 
+  /**
+   * make sure request id passed by request to knox is picked up as a correlation id
+   * @throws Exception
+   */
+  @Test
+  public void testLoadBalancerCorrelationID() throws Exception {
+
+    final String TEST_REQ_ID = "7365dfbc1028ad7e4501dad3454a34c3";
+    FilterConfig config = EasyMock.createNiceMock( FilterConfig.class );
+    EasyMock.replay( config );
+
+    HttpServletRequest request = EasyMock.createNiceMock( HttpServletRequest.class );
+    HttpServletRequest requestNoID = EasyMock.createNiceMock( HttpServletRequest.class );
+
+    ServletContext context = EasyMock.createNiceMock( ServletContext.class );
+    GatewayConfig gatewayConfig = EasyMock.createNiceMock( GatewayConfig.class );
+    EasyMock.expect( context.getAttribute(
+    GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE)).andReturn(gatewayConfig).anyTimes();
+    EasyMock.expect(gatewayConfig.getHeaderNameForRemoteAddress()).andReturn(
+        "Custom-Forwarded-For").anyTimes();
+    request.setAttribute( AbstractGatewayFilter.TARGET_SERVICE_ROLE, "test-role" );
+    EasyMock.expectLastCall().anyTimes();
+
+    EasyMock.expect( request.getPathInfo() ).andReturn( "test-path/test-resource" ).anyTimes();
+    EasyMock.expect( request.getServletContext() ).andReturn( context ).anyTimes();
+    EasyMock.expect( request.getHeader( REQUEST_ID_HEADER_NAME ) ).andReturn( TEST_REQ_ID ).anyTimes();
+    HttpServletResponse response = EasyMock.createNiceMock( HttpServletResponse.class );
+
+    EasyMock.expect( requestNoID.getPathInfo() ).andReturn( "test-path/test-resource" ).anyTimes();
+    EasyMock.expect( requestNoID.getServletContext() ).andReturn( context ).anyTimes();
+
+    EasyMock.replay(response,request,requestNoID,context,gatewayConfig);
+
+
+    TestCorrelationFilter filter = new TestCorrelationFilter();
+
+    /* test a case where request coming into knox has request id header */
+    GatewayFilter gateway = new GatewayFilter();
+    gateway.addFilter( "test-path/**", "test-filter", filter, null, "test-role" );
+    gateway.init( config );
+    gateway.doFilter( request, response );
+    assertThat(filter.request_id, is( TEST_REQ_ID ) );
+    assertThat(filter.correlation_id, is( TEST_REQ_ID ) );
+
+    /* test the case where request id for request coming to knox is absent */
+    gateway.doFilter( requestNoID, response );
+    assertThat(filter.request_id, nullValue() );
+    assertThat(filter.correlation_id, notNullValue() );
+    assertThat(filter.correlation_id, not( TEST_REQ_ID ) );
+    gateway.destroy();
+  }
+
   @Test
   public void testDefaultServicePathTopologyRequestAttribute() throws Exception {