Skip to content

Commit

Permalink
Replace blacklistHeader to excludeHeaderList
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaho12 committed Sep 13, 2024
1 parent c03df85 commit b85acc6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
6 changes: 3 additions & 3 deletions docs/routing-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ routingRules:
rulesConfigPath: "app/config/routing_rules.yml" # replace with actual path to your rules config file
rulesExternalConfiguration:
urlPath: https://router.example.com/gateway-rules # replace with your own API path
blacklistHeaders:
excludeHeaderList:
- 'Authorization'
```
* Redirect URLs are not supported
* Optionally add headers to the `BlacklistHeaders` list to exclude requests with corresponding header values
* Optionally add headers to the `excludeHeaderList` list to exclude requests with corresponding header values
from being sent in the POST request.

If there is error parsing the routing rules configuration file, an error is logged,
Expand All @@ -48,7 +48,7 @@ You can use an external service for processing your routing by setting the
`rulesType` to `EXTERNAL` and configuring the `rulesExternalConfiguration`.

Trino Gateway then sends all headers as a map in the body of a POST request to the external service.
Headers specified in `blacklistHeaders` are excluded. If `requestAnalyzerConfig.isAnalyzeRequest` is set to `true`,
Headers specified in `excludeHeaderList` are excluded. If `requestAnalyzerConfig.isAnalyzeRequest` is set to `true`,
`TrinoRequestUser` and `TrinoQueryProperties` are also included.

Additionally, the following HTTP information is included:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class RulesExternalConfiguration
{
private String urlPath;
private List<String> blackListHeaders;
private List<String> excludeHeaderList;

public String getUrlPath()
{
Expand All @@ -30,13 +30,13 @@ public void setUrlPath(String urlPath)
this.urlPath = urlPath;
}

public List<String> getBlackListHeaders()
public List<String> getExcludeHeaderList()
{
return this.blackListHeaders;
return this.excludeHeaderList;
}

public void setBlackListHeaders(List<String> blackListHeaders)
public void setExcludeHeaderList(List<String> excludeHeaderList)
{
this.blackListHeaders = blackListHeaders;
this.excludeHeaderList = excludeHeaderList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class ExternalRoutingGroupSelector
implements RoutingGroupSelector
{
private static final Logger log = Logger.get(ExternalRoutingGroupSelector.class);
private final Set<String> blacklistHeaders;
private final Set<String> excludeHeaderList;
private final URI uri;
private final HttpClient httpClient;
private final RequestAnalyzerConfig requestAnalyzerConfig;
Expand All @@ -61,10 +61,10 @@ public class ExternalRoutingGroupSelector
@VisibleForTesting
ExternalRoutingGroupSelector(RulesExternalConfiguration rulesExternalConfiguration, RequestAnalyzerConfig requestAnalyzerConfig)
{
Set<String> defaultBlacklistHeaders = ImmutableSet.of("Content-Length");
this.blacklistHeaders = ImmutableSet.<String>builder()
.addAll(defaultBlacklistHeaders)
.addAll(rulesExternalConfiguration.getBlackListHeaders())
Set<String> defaultExcludeHeaderList = ImmutableSet.of("Content-Length");
this.excludeHeaderList = ImmutableSet.<String>builder()
.addAll(defaultExcludeHeaderList)
.addAll(rulesExternalConfiguration.getExcludeHeaderList())
.build();

this.requestAnalyzerConfig = requestAnalyzerConfig;
Expand Down Expand Up @@ -142,8 +142,8 @@ private Multimap<String, String> getValidHeaders(HttpServletRequest servletReque
Multimap<String, String> headers = ArrayListMultimap.create();
for (String name : list(servletRequest.getHeaderNames())) {
for (String value : list(servletRequest.getHeaders(name))) {
// Add all headers to ListMultimap except those in blacklist
if (!blacklistHeaders.contains(name)) {
// Add all headers to ListMultimap except those in excludeHeaderList
if (!excludeHeaderList.contains(name)) {
headers.put(name, value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static Stream<RulesExternalConfiguration> provideRoutingRuleExternalConfig()
{
RulesExternalConfiguration restConfig = new RulesExternalConfiguration();
restConfig.setUrlPath("http://localhost:8080/api/public/gateway_rules");
restConfig.setBlackListHeaders(new ArrayList<>(List.of("Authorization")));
restConfig.setExcludeHeaderList(new ArrayList<>(List.of("Authorization")));
return Stream.of(restConfig);
}

Expand Down Expand Up @@ -163,7 +163,7 @@ void testApiFailure(RulesExternalConfiguration rulesExternalConfiguration)
void testNullUri()
{
RulesExternalConfiguration restConfig = new RulesExternalConfiguration();
restConfig.setBlackListHeaders(new ArrayList<>(List.of("Authorization")));
restConfig.setExcludeHeaderList(new ArrayList<>(List.of("Authorization")));

// Assert that a RuntimeException is thrown with message
assertThatThrownBy(() -> RoutingGroupSelector.byRoutingExternal(restConfig, requestAnalyzerConfig))
Expand All @@ -172,28 +172,28 @@ void testNullUri()
}

@Test
void testBlackListHeader()
void testExcludeHeader()
throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
{
// set custom RulesExternalConfiguration config
RulesExternalConfiguration restConfig = new RulesExternalConfiguration();
restConfig.setUrlPath("http://localhost:8080/api/public/gateway_rules");
restConfig.setBlackListHeaders(new ArrayList<>(List.of("test-blackList-header")));
restConfig.setExcludeHeaderList(new ArrayList<>(List.of("test-exclude-header")));

RoutingGroupSelector routingGroupSelector =
RoutingGroupSelector.byRoutingExternal(restConfig, requestAnalyzerConfig);

// Mock headers to be read by mockRequest
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
List<String> customHeaderNames = List.of("test-blackList-header", "not-blacklisted-header");
List<String> customBlackListHeaderValues = List.of("test-blacklist-value");
List<String> customValidHeaderValues = List.of("not-blacklist-value");
List<String> customHeaderNames = List.of("test-exclude-header", "not-excluded-header");
List<String> customExcludeHeaderValues = List.of("test-excludeHeader-value");
List<String> customValidHeaderValues = List.of("not-excludeHeader-value");
Enumeration<String> headerNamesEnumeration = Collections.enumeration(customHeaderNames);
when(mockRequest.getHeaderNames()).thenReturn(headerNamesEnumeration);
when(mockRequest.getHeaders("test-blackList-header")).thenReturn(Collections.enumeration(customBlackListHeaderValues));
when(mockRequest.getHeaders("not-blacklisted-header")).thenReturn(Collections.enumeration(customValidHeaderValues));
when(mockRequest.getHeaders("test-exclude-header")).thenReturn(Collections.enumeration(customExcludeHeaderValues));
when(mockRequest.getHeaders("not-excluded-header")).thenReturn(Collections.enumeration(customValidHeaderValues));

// Use reflection to get valid headers after removing blacklist headers
// Use reflection to get valid headers after removing excludeHeaderList headers
Method getValidHeaders = ExternalRoutingGroupSelector.class.getDeclaredMethod("getValidHeaders", HttpServletRequest.class);
getValidHeaders.setAccessible(true);

Expand Down

0 comments on commit b85acc6

Please sign in to comment.