-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Not allow access to supervision endpoints (#118)
Signed-off-by: achour94 <[email protected]>
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/main/java/org/gridsuite/gateway/filters/SupervisionAccessControlFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package org.gridsuite.gateway.filters; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.cloud.gateway.filter.GatewayFilterChain; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* A global pre-filter that controls access to supervision endpoints in the API gateway. | ||
* | ||
* This filter inspects the incoming request path and blocks access to specific supervision | ||
* endpoints based on a predefined pattern. The filter is designed to enhance security by | ||
* restricting access to potentially sensitive supervision functionalities. | ||
* | ||
* The filter blocks access to paths matching the following pattern: | ||
* {@code /v<number>/supervision} or {@code /v<number>/supervision/<any-sub-path>} | ||
* where {@code <number>} can be any positive integer representing the API version. | ||
* | ||
* Examples of blocked paths: | ||
* - /v1/supervision | ||
* - /v2/supervision/ | ||
* - /v10/supervision/health | ||
* - /v999/supervision/metrics | ||
* | ||
* When a matching path is detected, the filter responds with a 403 Forbidden status. | ||
* | ||
* @author Achour BERRAHMA <achour.berrahma at rte-france.com> | ||
*/ | ||
@Component | ||
public class SupervisionAccessControlFilter extends AbstractGlobalPreFilter { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionAccessControlFilter.class); | ||
private static final Pattern SUPERVISION_PATTERN = Pattern.compile("^/v\\d+/supervision(/.*)?$"); | ||
public static final String ACCESS_TO_SUPERVISION_ENDPOINT_IS_NOT_ALLOWED = "{}: 403 Forbidden, Access to supervision endpoint is not allowed"; | ||
|
||
@Override | ||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { | ||
String path = exchange.getRequest().getURI().getPath(); | ||
if (SUPERVISION_PATTERN.matcher(path).matches()) { | ||
LOGGER.info(ACCESS_TO_SUPERVISION_ENDPOINT_IS_NOT_ALLOWED, | ||
exchange.getRequest().getPath()); | ||
return completeWithCode(exchange, HttpStatus.FORBIDDEN); | ||
} | ||
|
||
return chain.filter(exchange); | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
// Execute after TokenValidatorGlobalPreFilter and UserAdminControlGlobalPreFilter | ||
return Ordered.LOWEST_PRECEDENCE - 2; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters