Skip to content

Commit

Permalink
refactor: Refactoring for gateway.filters classes (#2157)
Browse files Browse the repository at this point in the history
Signed-off-by: Amanda D'Errico <[email protected]>
  • Loading branch information
AmandaDErrico authored Mar 7, 2022
1 parent db52527 commit ab36819
Show file tree
Hide file tree
Showing 21 changed files with 125 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,69 +9,63 @@
*/
package org.zowe.apiml.gateway.filters.post;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.zowe.apiml.security.common.config.AuthConfigurationProperties;
import org.zowe.apiml.util.CookieUtil;

import javax.servlet.http.HttpServletResponse;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.POST_TYPE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SEND_RESPONSE_FILTER_ORDER;

/**
* Uses the authentication token provided as a query parameter and puts it to
* the expected place (cookie).
*/
public class ConvertAuthTokenInUriToCookieFilter extends ZuulFilter {
public class ConvertAuthTokenInUriToCookieFilter extends PostZuulFilter {
private final AuthConfigurationProperties authConfigurationProperties;

public ConvertAuthTokenInUriToCookieFilter(AuthConfigurationProperties authConfigurationProperties) {
this.authConfigurationProperties = authConfigurationProperties;
}

public String filterType() {
return POST_TYPE;
}

public int filterOrder() {
return SEND_RESPONSE_FILTER_ORDER - 1;
}

public boolean shouldFilter() {
return true;
RequestContext context = RequestContext.getCurrentContext();
AuthConfigurationProperties.CookieProperties cp = authConfigurationProperties.getCookieProperties();
return context.getRequestQueryParams() != null && context.getRequestQueryParams().containsKey(cp.getCookieName());
}

public Object run() {
RequestContext context = RequestContext.getCurrentContext();
AuthConfigurationProperties.CookieProperties cp = authConfigurationProperties.getCookieProperties();
if ((context.getRequestQueryParams() != null) && context.getRequestQueryParams().containsKey(cp.getCookieName())) {
HttpServletResponse servletResponse = context.getResponse();
HttpServletResponse servletResponse = context.getResponse();
AuthConfigurationProperties.CookieProperties cp = authConfigurationProperties.getCookieProperties();

// SameSite attribute is not supported in Cookie used in HttpServletResponse.addCookie,
// so specify Set-Cookie header directly
String cookieHeader = CookieUtil.setCookieHeader(
cp.getCookieName(),
context.getRequestQueryParams().get(cp.getCookieName()).get(0),
cp.getCookieComment(),
cp.getCookiePath(),
cp.getCookieSameSite().getValue(),
cp.getCookieMaxAge(),
true,
true
);
servletResponse.addHeader("Set-Cookie", cookieHeader);
// SameSite attribute is not supported in Cookie used in HttpServletResponse.addCookie,
// so specify Set-Cookie header directly
String cookieHeader = CookieUtil.setCookieHeader(
cp.getCookieName(),
context.getRequestQueryParams().get(cp.getCookieName()).get(0),
cp.getCookieComment(),
cp.getCookiePath(),
cp.getCookieSameSite().getValue(),
cp.getCookieMaxAge(),
true,
true
);
servletResponse.addHeader("Set-Cookie", cookieHeader);

String url = context.getRequest().getRequestURL().toString();
String newUrl;
if (url.endsWith("/apicatalog/")) {
newUrl = url + "#/dashboard";
} else {
newUrl = url;
}
context.addZuulResponseHeader("Location", newUrl);
context.setResponseStatusCode(HttpServletResponse.SC_MOVED_TEMPORARILY);
String url = context.getRequest().getRequestURL().toString();
String newUrl;
if (url.endsWith("/apicatalog/")) {
newUrl = url + "#/dashboard";
} else {
newUrl = url;
}
context.addZuulResponseHeader("Location", newUrl);
context.setResponseStatusCode(HttpServletResponse.SC_MOVED_TEMPORARILY);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
package org.zowe.apiml.gateway.filters.post;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.Debug;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -20,7 +19,6 @@
import java.util.List;
import java.util.stream.Collectors;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.POST_TYPE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SEND_RESPONSE_FILTER_ORDER;

/**
Expand All @@ -30,16 +28,11 @@
*/
@Component
@Slf4j
public class DebugHeaderFilter extends ZuulFilter {
public class DebugHeaderFilter extends PostZuulFilter {

@Value("${zuul.debug.request.debugHeaderLimit:4096}")
private int debugHeaderLimit;

@Override
public String filterType() {
return POST_TYPE;
}

@Override
public int filterOrder() {
return SEND_RESPONSE_FILTER_ORDER - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
package org.zowe.apiml.gateway.filters.post;

import com.netflix.util.Pair;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -37,7 +36,7 @@
* </ul>
*/
@RequiredArgsConstructor
public class PageRedirectionFilter extends ZuulFilter implements RoutedServicesUser {
public class PageRedirectionFilter extends PostZuulFilter implements RoutedServicesUser {
@Value("${server.attls.enabled:false}")
private boolean isAttlsEnabled;
private static final int MAX_ENTRIES = 1000;
Expand Down Expand Up @@ -66,11 +65,6 @@ public boolean shouldFilter() {
return HttpStatus.valueOf(status).is3xxRedirection();
}

@Override
public String filterType() {
return POST_TYPE;
}

@Override
public int filterOrder() {
return SEND_RESPONSE_FILTER_ORDER - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
*/
package org.zowe.apiml.gateway.filters.post;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;
import org.zowe.apiml.gateway.services.ServiceInstancesUtils;

import java.util.List;
import java.util.Map;
Expand All @@ -28,15 +27,10 @@
*/
@Component
@RequiredArgsConstructor
public class PerServiceAddResponseHeadersFilter extends ZuulFilter {
public class PerServiceAddResponseHeadersFilter extends PostZuulFilter {

private final DiscoveryClient discoveryClient;

@Override
public String filterType() {
return POST_TYPE;
}

@Override
public int filterOrder() {
return SEND_RESPONSE_FILTER_ORDER - 1;
Expand All @@ -48,15 +42,13 @@ public boolean shouldFilter() {
}

@Override
public Object run() throws ZuulException {
RequestContext context = RequestContext.getCurrentContext();
String serviceId = (String) context.get(SERVICE_ID_KEY);
List<ServiceInstance> serviceInstances = discoveryClient.getInstances(serviceId);

public Object run() {
List<ServiceInstance> serviceInstances = ServiceInstancesUtils.getServiceInstancesFromDiscoveryClient(discoveryClient);
if (serviceInstances != null && !serviceInstances.isEmpty()) {
ServiceInstance serviceInstance = serviceInstances.get(0);
Map<String, String> metadata = serviceInstance.getMetadata();

RequestContext context = RequestContext.getCurrentContext();
String headersToAdd = metadata.get("apiml.response.headers");
if (headersToAdd != null && !headersToAdd.trim().isEmpty()) {
String[] headerValuePairs = StringUtils.stripAll(headersToAdd.split(","));
Expand All @@ -70,7 +62,6 @@ public Object run() throws ZuulException {
}
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
package org.zowe.apiml.gateway.filters.post;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.RequiredArgsConstructor;
import org.zowe.apiml.gateway.cache.LoadBalancerCache;
Expand All @@ -30,16 +29,11 @@
* if the user is authenticated and there is no instance in the cache stores the selected instance in the cache.
*/
@RequiredArgsConstructor
public class PostStoreLoadBalancerCacheFilter extends ZuulFilter {
public class PostStoreLoadBalancerCacheFilter extends PostZuulFilter {

private final RequestAuthenticationService authenticationService;
private final LoadBalancerCache loadBalancerCache;

@Override
public String filterType() {
return POST_TYPE;
}

@Override
public int filterOrder() {
return SEND_RESPONSE_FILTER_ORDER - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
*
* Copyright Contributors to the Zowe Project.
*/
package org.zowe.apiml.gateway.filters.pre;
package org.zowe.apiml.gateway.filters.post;

import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.ZuulFilter;

public interface RequestContextProvider {
RequestContext context();
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.POST_TYPE;

public abstract class PostZuulFilter extends ZuulFilter {
@Override
public String filterType() {
return POST_TYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,13 @@

package org.zowe.apiml.gateway.filters.post;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.zowe.apiml.gateway.ribbon.RequestContextUtils;
import org.zowe.apiml.gateway.ribbon.loadbalancer.LoadBalancerConstants;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.POST_TYPE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SEND_RESPONSE_FILTER_ORDER;

public class RoutedInstanceIdFilter extends ZuulFilter {
@Override
public String filterType() {
return POST_TYPE;
}

public class RoutedInstanceIdFilter extends PostZuulFilter {
@Override
public int filterOrder() {
return SEND_RESPONSE_FILTER_ORDER - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.zowe.apiml.gateway.services.ServiceInstancesUtils;
import org.zowe.apiml.message.core.Message;
import org.zowe.apiml.message.core.MessageService;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.http.HttpStatus;

import java.util.*;
Expand All @@ -38,19 +37,14 @@

@RequiredArgsConstructor
@Slf4j
public class EncodedCharactersFilter extends ZuulFilter {
public class EncodedCharactersFilter extends PreZuulFilter {

private final DiscoveryClient discoveryClient;
private final MessageService messageService;
public static final String METADATA_KEY = "apiml.enableUrlEncodedCharacters";
private static final List<String> PROHIBITED_CHARACTERS =
Arrays.asList("%2e", "%2E", ";", "%3b", "%3B", "%2f", "%2F", "\\", "%5c", "%5C", "%25", "%");

@Override
public String filterType() {
return FilterConstants.PRE_TYPE;
}

@Override
public int filterOrder() {
return PRE_DECORATION_FILTER_ORDER + 2;
Expand All @@ -60,10 +54,7 @@ public int filterOrder() {
public boolean shouldFilter() {
boolean shouldFilter = true;

RequestContext context = RequestContext.getCurrentContext();
final String serviceId = (String) context.get(SERVICE_ID_KEY);

List<ServiceInstance> instanceList = discoveryClient.getInstances(serviceId);
List<ServiceInstance> instanceList = ServiceInstancesUtils.getServiceInstancesFromDiscoveryClient(discoveryClient);

List<Map<String, String>> enabledList = instanceList.stream()
.map(ServiceInstance::getMetadata)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
package org.zowe.apiml.gateway.filters.pre;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
Expand All @@ -19,7 +18,6 @@
import java.util.*;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_DECORATION_FILTER_ORDER;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;

/**
* Filter is used to clear values in request headers. Provide list of such headers in property source and they will
Expand All @@ -28,15 +26,10 @@
@Component
@NoArgsConstructor
@AllArgsConstructor
public class HeaderSanitizerFilter extends ZuulFilter {
public class HeaderSanitizerFilter extends PreZuulFilter {
@Value(value = "${apiml.security.headersToBeCleared:}")
private String[] headersToBeCleared;

@Override
public String filterType() {
return PRE_TYPE;
}

@Override
public int filterOrder() {
return PRE_DECORATION_FILTER_ORDER + 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
package org.zowe.apiml.gateway.filters.pre;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
import org.zowe.apiml.product.routing.RoutedService;
Expand All @@ -26,15 +25,10 @@
* Must be run after PreDecorationFilter. This will set Proxy, ServiceId and other variables in RequestContext
*/
@Slf4j
public class LocationFilter extends ZuulFilter implements RoutedServicesUser {
public class LocationFilter extends PreZuulFilter implements RoutedServicesUser {

private final Map<String, RoutedServices> routedServicesMap = new HashMap<>();

@Override
public String filterType() {
return "pre";
}

@Override
public int filterOrder() {
return PRE_DECORATION_FILTER_ORDER + 3;
Expand Down
Loading

0 comments on commit ab36819

Please sign in to comment.