Skip to content

Commit

Permalink
KNOX-2123 - Setting requestURI using the given servletRequest in case…
Browse files Browse the repository at this point in the history
… the service is unavailable and logging it with the appropriate action outcome
  • Loading branch information
smolnar82 committed Dec 16, 2019
1 parent 4be5227 commit f33a589
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 15 deletions.
Expand Up @@ -32,6 +32,7 @@
import org.apache.knox.gateway.i18n.messages.MessagesFactory;
import org.apache.knox.gateway.i18n.resources.ResourcesFactory;
import org.apache.knox.gateway.topology.Topology;
import org.apache.knox.gateway.util.ServletRequestUtils;
import org.apache.knox.gateway.util.urltemplate.Matcher;
import org.apache.knox.gateway.util.urltemplate.Parser;
import org.apache.knox.gateway.util.urltemplate.Template;
Expand Down Expand Up @@ -101,19 +102,17 @@ public void doFilter( ServletRequest servletRequest, ServletResponse servletResp
HttpServletResponse httpResponse = (HttpServletResponse)servletResponse;

//TODO: The resulting pathInfo + query needs to be added to the servlet context somehow so that filters don't need to rebuild it. This is done in HttpClientDispatch right now for example.
String servlet = httpRequest.getServletPath();
String path = httpRequest.getPathInfo();
String query = httpRequest.getQueryString();
String requestPath = ( servlet == null ? "" : servlet ) + ( path == null ? "" : path );
String requestPathWithQuery = requestPath + ( query == null ? "" : "?" + query );
String requestPath = ServletRequestUtils.getRequestPath(httpRequest);
String requestPathWithQuery = ServletRequestUtils.getRequestPathWithQuery(httpRequest);

Template pathWithQueryTemplate;
try {
pathWithQueryTemplate = Parser.parseLiteral( requestPathWithQuery );
} catch( URISyntaxException e ) {
throw new ServletException( e );
}
String contextWithPathAndQuery = httpRequest.getContextPath() + requestPathWithQuery;
String contextWithPathAndQuery = ServletRequestUtils.getContextPathWithQuery(httpRequest);
LOG.receivedRequest( httpRequest.getMethod(), requestPath );

servletRequest.setAttribute(
Expand Down
Expand Up @@ -33,6 +33,7 @@
import org.apache.knox.gateway.services.ServiceType;
import org.apache.knox.gateway.services.GatewayServices;
import org.apache.knox.gateway.services.metrics.MetricsService;
import org.apache.knox.gateway.util.ServletRequestUtils;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
Expand All @@ -58,10 +59,9 @@ public class GatewayServlet implements Servlet, Filter {
private static final GatewayResources res = ResourcesFactory.get( GatewayResources.class );
private static final GatewayMessages LOG = MessagesFactory.get( GatewayMessages.class );

private static AuditService auditService = AuditServiceFactory.getAuditService();
private static Auditor auditor = AuditServiceFactory.getAuditService()
.getAuditor( AuditConstants.DEFAULT_AUDITOR_NAME,
AuditConstants.KNOX_SERVICE_NAME, AuditConstants.KNOX_COMPONENT_NAME );
private static final AuditService auditService = AuditServiceFactory.getAuditService();
private static final Auditor auditor = AuditServiceFactory.getAuditService().getAuditor(AuditConstants.DEFAULT_AUDITOR_NAME, AuditConstants.KNOX_SERVICE_NAME,
AuditConstants.KNOX_COMPONENT_NAME);

private FilterConfigAdapter filterConfig;
private GatewayFilter filter;
Expand Down Expand Up @@ -140,9 +140,7 @@ public void service( ServletRequest servletRequest, ServletResponse servletRespo
} else {
((HttpServletResponse)servletResponse).setStatus( HttpServletResponse.SC_SERVICE_UNAVAILABLE );
}
String requestUri = (String)servletRequest.getAttribute( AbstractGatewayFilter.SOURCE_REQUEST_CONTEXT_URL_ATTRIBUTE_NAME );
int status = ((HttpServletResponse)servletResponse).getStatus();
auditor.audit( Action.ACCESS, requestUri, ResourceType.URI, ActionOutcome.SUCCESS, res.responseStatus( status ) );
auditLog(servletRequest, servletResponse);
} finally {
auditService.detachContext();
}
Expand Down Expand Up @@ -170,14 +168,22 @@ public void doFilter( ServletRequest servletRequest, ServletResponse servletResp
} else {
((HttpServletResponse)servletResponse).setStatus( HttpServletResponse.SC_SERVICE_UNAVAILABLE );
}
String requestUri = (String)servletRequest.getAttribute( AbstractGatewayFilter.SOURCE_REQUEST_CONTEXT_URL_ATTRIBUTE_NAME );
int status = ((HttpServletResponse)servletResponse).getStatus();
auditor.audit( Action.ACCESS, requestUri, ResourceType.URI, ActionOutcome.SUCCESS, res.responseStatus( status ) );
auditLog(servletRequest, servletResponse);
} finally {
auditService.detachContext();
}
}

private void auditLog(ServletRequest servletRequest, ServletResponse servletResponse) {
final int status = ((HttpServletResponse) servletResponse).getStatus();
if (HttpServletResponse.SC_SERVICE_UNAVAILABLE == status) {
auditor.audit(Action.ACCESS, ServletRequestUtils.getContextPathWithQuery(servletRequest), ResourceType.URI, ActionOutcome.UNAVAILABLE, res.responseStatus(status));
} else {
String requestUri = (String) servletRequest.getAttribute(AbstractGatewayFilter.SOURCE_REQUEST_CONTEXT_URL_ATTRIBUTE_NAME);
auditor.audit(Action.ACCESS, requestUri, ResourceType.URI, ActionOutcome.SUCCESS, res.responseStatus(status));
}
}

@Override
public String getServletInfo() {
return res.gatewayServletInfo();
Expand Down
@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.knox.gateway.util;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

/**
* Provides useful methods to fetch different parts from {@link ServletRequest} and {@link HttpServletRequest} interfaces.
*/
public class ServletRequestUtils {

public static String getRequestPath(ServletRequest servletRequest) {
return getRequestPath((HttpServletRequest) servletRequest);
}

public static String getRequestPath(HttpServletRequest httpServletRequest) {
return emptyOrValue(httpServletRequest.getServletPath()) + emptyOrValue(httpServletRequest.getPathInfo());
}

public static String getRequestPathWithQuery(ServletRequest servletRequest) {
return getRequestPathWithQuery((HttpServletRequest) servletRequest);
}

public static String getRequestPathWithQuery(HttpServletRequest httpServletRequest) {
return getRequestPath(httpServletRequest) + emptyOrValue(httpServletRequest.getQueryString(), "?");
}

public static String getContextPathWithQuery(ServletRequest servletRequest) {
return getContextPathWithQuery((HttpServletRequest) servletRequest);
}

public static String getContextPathWithQuery(HttpServletRequest httpServletRequest) {
return httpServletRequest.getContextPath() + getRequestPathWithQuery(httpServletRequest);
}

private static String emptyOrValue(String toTest) {
return emptyOrValue(toTest, null);
}

private static String emptyOrValue(String toTest, String prefix) {
if (toTest == null) {
return "";
} else {
return prefix == null ? toTest : prefix + toTest;
}
}

}

0 comments on commit f33a589

Please sign in to comment.