Skip to content

Commit 6627e41

Browse files
authored
Remove gateway control. (#107)
Signed-off-by: AAJELLAL <[email protected]>
1 parent 433b074 commit 6627e41

File tree

2 files changed

+49
-134
lines changed

2 files changed

+49
-134
lines changed

src/main/java/org/gridsuite/gateway/filters/ElementAccessControllerGlobalPreFilter.java

Lines changed: 2 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,13 @@
88
package org.gridsuite.gateway.filters;
99

1010
import lombok.NonNull;
11-
import org.gridsuite.gateway.ServiceURIsConfig;
12-
import org.gridsuite.gateway.dto.AccessControlInfos;
13-
import org.gridsuite.gateway.endpoints.EndPointElementServer;
14-
import org.gridsuite.gateway.endpoints.EndPointServer;
1511
import org.slf4j.Logger;
1612
import org.slf4j.LoggerFactory;
1713
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
18-
import org.springframework.cloud.gateway.route.Route;
19-
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
20-
import org.springframework.context.ApplicationContext;
2114
import org.springframework.core.Ordered;
22-
import org.springframework.http.HttpHeaders;
23-
import org.springframework.http.HttpStatusCode;
24-
import org.springframework.http.server.RequestPath;
25-
import org.springframework.http.server.reactive.ServerHttpRequest;
2615
import org.springframework.stereotype.Component;
27-
import org.springframework.web.reactive.function.client.WebClient;
2816
import org.springframework.web.server.ServerWebExchange;
2917
import reactor.core.publisher.Mono;
30-
import reactor.core.scheduler.Schedulers;
31-
32-
import java.util.Objects;
33-
import java.util.Optional;
34-
import java.util.logging.Level;
35-
import java.util.regex.Pattern;
36-
37-
import static org.gridsuite.gateway.GatewayConfig.END_POINT_SERVICE_NAME;
38-
import static org.gridsuite.gateway.GatewayConfig.HEADER_USER_ID;
39-
import static org.gridsuite.gateway.endpoints.EndPointElementServer.QUERY_PARAM_IDS;
40-
import static org.springframework.http.HttpStatus.*;
4118

4219
/**
4320
* @author Slimane Amar <slimane.amar at rte-france.com>
@@ -47,19 +24,6 @@ public class ElementAccessControllerGlobalPreFilter extends AbstractGlobalPreFil
4724

4825
private static final Logger LOGGER = LoggerFactory.getLogger(ElementAccessControllerGlobalPreFilter.class);
4926

50-
private static final String ROOT_CATEGORY_REACTOR = "reactor.";
51-
52-
private static final String ELEMENTS_ROOT_PATH = "elements";
53-
54-
private final WebClient webClient;
55-
56-
private final ApplicationContext applicationContext;
57-
58-
public ElementAccessControllerGlobalPreFilter(ApplicationContext context, ServiceURIsConfig servicesURIsConfig, WebClient.Builder webClientBuilder) {
59-
this.webClient = webClientBuilder.baseUrl(servicesURIsConfig.getDirectoryServerBaseUri()).build();
60-
this.applicationContext = context;
61-
}
62-
6327
@Override
6428
public int getOrder() {
6529
// Before WebsocketRoutingFilter to control access
@@ -69,61 +33,8 @@ public int getOrder() {
6933
@Override
7034
public Mono<Void> filter(@NonNull ServerWebExchange exchange, @NonNull GatewayFilterChain chain) {
7135
LOGGER.debug("Filter : {}", getClass().getSimpleName());
72-
73-
RequestPath path = exchange.getRequest().getPath();
74-
75-
// Filter only requests to the endpoint servers with this pattern : /v<number>/<appli_root_path>
76-
if (!Pattern.matches("/v(\\d)+/.*", path.value())) {
77-
return chain.filter(exchange);
78-
}
79-
80-
// Is an elements' endpoint with a controlled access ?
81-
String endPointServiceName = Objects.requireNonNull((String) (Objects.requireNonNull((Route) exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR)).getMetadata()).get(END_POINT_SERVICE_NAME));
82-
EndPointServer endPointServer = applicationContext.containsBean(endPointServiceName) ? (EndPointServer) applicationContext.getBean(endPointServiceName) : null;
83-
if (endPointServer == null || !endPointServer.hasElementsAccessControl()) {
84-
return chain.filter(exchange);
85-
}
86-
87-
// Is a root path with a controlled access ?
88-
EndPointElementServer endPointElementServer = (EndPointElementServer) endPointServer;
89-
if (endPointElementServer.isNotControlledRootPath(path.elements().get(3).value())) {
90-
return chain.filter(exchange);
91-
}
92-
93-
// Is a method allowed ?
94-
if (!endPointElementServer.isAllowedMethod(exchange.getRequest().getMethod())) {
95-
return completeWithCode(exchange, FORBIDDEN);
96-
}
97-
98-
Optional<AccessControlInfos> accessControlInfos = endPointElementServer.getAccessControlInfos(exchange.getRequest());
99-
return accessControlInfos.isEmpty() ? completeWithCode(exchange, FORBIDDEN) : isAccessAllowed(exchange, chain, accessControlInfos.get());
100-
}
101-
102-
private Mono<Void> isAccessAllowed(ServerWebExchange exchange, GatewayFilterChain chain, AccessControlInfos accessControlInfos) {
103-
ServerHttpRequest httpRequest = exchange.getRequest();
104-
HttpHeaders httpHeaders = exchange.getRequest().getHeaders();
105-
return webClient
106-
.head()
107-
.uri(uriBuilder -> uriBuilder
108-
.path(httpRequest.getPath().subPath(0, 3).value()) // version
109-
.path(ELEMENTS_ROOT_PATH)
110-
.queryParam(QUERY_PARAM_IDS, accessControlInfos.getElementUuids())
111-
.build()
112-
)
113-
.header(HEADER_USER_ID, Objects.requireNonNull(httpHeaders.get(HEADER_USER_ID)).get(0))
114-
.exchangeToMono(response -> {
115-
HttpStatusCode httpStatusCode = response.statusCode();
116-
if (httpStatusCode.equals(OK)) {
117-
return chain.filter(exchange);
118-
} else if (httpStatusCode.equals(NOT_FOUND)) {
119-
return completeWithCode(exchange, NOT_FOUND);
120-
} else if (httpStatusCode.equals(FORBIDDEN)) {
121-
return completeWithCode(exchange, FORBIDDEN);
122-
}
123-
return response.createException().flatMap(Mono::error);
124-
})
125-
.publishOn(Schedulers.boundedElastic())
126-
.log(ROOT_CATEGORY_REACTOR, Level.FINE);
36+
//TODO: the control is disabled for the moment, it will be processed in another US. For more details contact slimane
37+
return chain.filter(exchange);
12738
}
12839
}
12940

src/test/java/org/gridsuite/gateway/ElementAccessControlTest.java

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.springframework.beans.factory.annotation.Value;
2525
import org.springframework.boot.test.context.SpringBootTest;
2626
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
27-
import org.springframework.http.HttpStatus;
2827
import org.springframework.test.context.junit4.SpringRunner;
2928
import org.springframework.test.web.reactive.server.WebTestClient;
3029

@@ -186,40 +185,45 @@ public void testGetElements() {
186185
stubFor(head(urlEqualTo(String.format("/v1/elements?ids=%s", uuid))).withPort(port).withHeader("userId", equalTo("user1"))
187186
.willReturn(aResponse()));
188187

189-
// user2 not allowed
188+
// user2 allowed
190189
stubFor(head(urlEqualTo(String.format("/v1/elements?ids=%s", uuid))).withPort(port).withHeader("userId", equalTo("user2"))
191-
.willReturn(aResponse().withStatus(HttpStatus.FORBIDDEN.value())));
190+
.willReturn(aResponse()));
192191

193192
stubFor(get(urlEqualTo(String.format("/v1/studies/%s", uuid))).withHeader("userId", equalTo("user1"))
194193
.willReturn(aResponse()));
195194

195+
stubFor(get(urlEqualTo(String.format("/v1/studies/%s", uuid))).withHeader("userId", equalTo("user2"))
196+
.willReturn(aResponse()));
197+
196198
stubFor(get(urlEqualTo(String.format("/v1/studies/metadata?ids=%s", uuid))).withHeader("userId", equalTo("user1"))
197199
.willReturn(aResponse()));
198200

201+
stubFor(get(urlEqualTo(String.format("/v1/studies/metadata?ids=%s", uuid))).withHeader("userId", equalTo("user2"))
202+
.willReturn(aResponse()));
203+
199204
stubFor(get(urlEqualTo(String.format("/v1/filters/%s", uuid))).withHeader("userId", equalTo("user1"))
200205
.willReturn(aResponse()));
201206

202207
stubFor(get(urlEqualTo(String.format("/v1/contingency-lists/%s", uuid))).withHeader("userId", equalTo("user1"))
203208
.willReturn(aResponse()));
204209

205-
// No uuid element forbidden
206210
webClient
207211
.get().uri("study/v1/studies")
208212
.header("Authorization", "Bearer " + tokenUser1)
209213
.exchange()
210-
.expectStatus().isForbidden();
214+
.expectStatus().isNotFound();
211215

212-
// Bad uuid forbidden
216+
// Bad uuid
213217
webClient
214218
.get().uri(String.format("study/v1/studies/%s", "badUuid"))
215219
.header("Authorization", "Bearer " + tokenUser1)
216220
.exchange()
217-
.expectStatus().isForbidden();
221+
.expectStatus().isNotFound();
218222
webClient
219223
.get().uri(String.format("study/v1/studies/%s", (UUID) null))
220224
.header("Authorization", "Bearer " + tokenUser1)
221225
.exchange()
222-
.expectStatus().isForbidden();
226+
.expectStatus().isNotFound();
223227

224228
webClient
225229
.get().uri(String.format("study/v1/studies/%s", uuid))
@@ -249,25 +253,25 @@ public void testGetElements() {
249253
.get().uri(String.format("study/v1/studies/%s", uuid))
250254
.header("Authorization", "Bearer " + tokenUser2)
251255
.exchange()
252-
.expectStatus().isForbidden();
256+
.expectStatus().isOk();
253257

254258
webClient
255259
.get().uri(String.format("study/v1/studies/metadata?ids=%s", uuid))
256260
.header("Authorization", "Bearer " + tokenUser2)
257261
.exchange()
258-
.expectStatus().isForbidden();
262+
.expectStatus().isOk();
259263

260264
webClient
261265
.get().uri(String.format("actions/v1/contingency-lists/%s", uuid))
262266
.header("Authorization", "Bearer " + tokenUser2)
263267
.exchange()
264-
.expectStatus().isForbidden();
268+
.expectStatus().isNotFound();
265269

266270
webClient
267271
.get().uri(String.format("filter/v1/filters/%s", uuid))
268272
.header("Authorization", "Bearer " + tokenUser2)
269273
.exchange()
270-
.expectStatus().isForbidden();
274+
.expectStatus().isNotFound();
271275
}
272276

273277
@Test
@@ -282,11 +286,11 @@ public void testCreateElements() {
282286
stubFor(head(urlEqualTo(String.format("/v1/elements?ids=%s", uuid))).withPort(port).withHeader("userId", equalTo("user1"))
283287
.willReturn(aResponse()));
284288

285-
// user2 not allowed
289+
// user2 is also allowed
286290
stubFor(head(urlEqualTo(String.format("/v1/directories?ids=%s", uuid))).withPort(port).withHeader("userId", equalTo("user2"))
287-
.willReturn(aResponse().withStatus(HttpStatus.FORBIDDEN.value())));
291+
.willReturn(aResponse()));
288292
stubFor(head(urlEqualTo(String.format("/v1/elements?ids=%s", uuid))).withPort(port).withHeader("userId", equalTo("user2"))
289-
.willReturn(aResponse().withStatus(HttpStatus.FORBIDDEN.value())));
293+
.willReturn(aResponse()));
290294

291295
stubFor(post(urlEqualTo(String.format("/v1/explore/studies?%s=%s", ExploreServer.QUERY_PARAM_PARENT_DIRECTORY_ID, uuid))).withHeader("userId", equalTo("user1"))
292296
.willReturn(aResponse()));
@@ -297,55 +301,55 @@ public void testCreateElements() {
297301
stubFor(post(urlEqualTo(String.format("/v1/explore/filters?%s=%s", ExploreServer.QUERY_PARAM_PARENT_DIRECTORY_ID, uuid))).withHeader("userId", equalTo("user1"))
298302
.willReturn(aResponse()));
299303

300-
// Direct creation of elements without going through the explor server is forbidden
304+
// Direct creation of elements without going through the explore server
301305
webClient
302306
.post().uri("study/v1/studies")
303307
.header("Authorization", "Bearer " + tokenUser1)
304308
.exchange()
305-
.expectStatus().isForbidden();
309+
.expectStatus().isNotFound();
306310
webClient
307311
.post().uri("actions/v1/script-contingency-lists")
308312
.header("Authorization", "Bearer " + tokenUser1)
309313
.exchange()
310-
.expectStatus().isForbidden();
314+
.expectStatus().isNotFound();
311315
webClient
312316
.post().uri("filter/v1/filters")
313317
.header("Authorization", "Bearer " + tokenUser1)
314318
.exchange()
315-
.expectStatus().isForbidden();
319+
.expectStatus().isNotFound();
316320

317-
// Creation of elements without directory parent is forbidden
321+
// Creation of elements without directory parent
318322
webClient
319323
.post().uri(String.format("explore/v1/explore/studies"))
320324
.header("Authorization", "Bearer " + tokenUser1)
321325
.exchange()
322-
.expectStatus().isForbidden();
326+
.expectStatus().isNotFound();
323327

324-
// Creation of elements with bad parameter for directory parent uuid is forbidden
328+
// Creation of elements with bad parameter for directory parent uuid
325329
webClient
326330
.post().uri(String.format("explore/v1/explore/studies?%s=%s", ExploreServer.QUERY_PARAM_PARENT_DIRECTORY_ID + "bad", uuid))
327331
.header("Authorization", "Bearer " + tokenUser1)
328332
.exchange()
329-
.expectStatus().isForbidden();
333+
.expectStatus().isNotFound();
330334

331-
// Creation of elements with bad directory parent uuid is forbidden
335+
// Creation of elements with bad directory parent uuid
332336
webClient
333337
.post().uri(String.format("explore/v1/explore/studies?%s=%s", ExploreServer.QUERY_PARAM_PARENT_DIRECTORY_ID, "badUuid"))
334338
.header("Authorization", "Bearer " + tokenUser1)
335339
.exchange()
336-
.expectStatus().isForbidden();
340+
.expectStatus().isNotFound();
337341
webClient
338342
.post().uri(String.format("explore/v1/explore/studies?%s=%s", ExploreServer.QUERY_PARAM_PARENT_DIRECTORY_ID, null))
339343
.header("Authorization", "Bearer " + tokenUser1)
340344
.exchange()
341-
.expectStatus().isForbidden();
345+
.expectStatus().isNotFound();
342346

343-
// Creation of elements with multiple directory parent uuids is forbidden
347+
// Creation of elements with multiple directory parent uuids
344348
webClient
345349
.post().uri(String.format("explore/v1/explore/studies?%s=%s,%s", ExploreServer.QUERY_PARAM_PARENT_DIRECTORY_ID, uuid, uuid))
346350
.header("Authorization", "Bearer " + tokenUser1)
347351
.exchange()
348-
.expectStatus().isForbidden();
352+
.expectStatus().isNotFound();
349353

350354
webClient
351355
.post().uri(String.format("explore/v1/explore/studies?%s=%s", ExploreServer.QUERY_PARAM_PARENT_DIRECTORY_ID, uuid))
@@ -384,7 +388,7 @@ public void testCreateSubElements() {
384388
.post().uri("study/v1/studies")
385389
.header("Authorization", "Bearer " + tokenUser1)
386390
.exchange()
387-
.expectStatus().isForbidden();
391+
.expectStatus().isNotFound();
388392

389393
webClient
390394
.post().uri(String.format("study/v1/studies/%s/tree/nodes", uuid))
@@ -403,9 +407,9 @@ public void testUpdateElements() {
403407
stubFor(head(urlEqualTo(String.format("/v1/elements?ids=%s", uuid))).withPort(port).withHeader("userId", equalTo("user1"))
404408
.willReturn(aResponse()));
405409

406-
// user2 not allowed
410+
// user2 allowed
407411
stubFor(head(urlEqualTo(String.format("/v1/elements?ids=%s", uuid))).withPort(port).withHeader("userId", equalTo("user2"))
408-
.willReturn(aResponse().withStatus(HttpStatus.FORBIDDEN.value())));
412+
.willReturn(aResponse()));
409413

410414
stubFor(put(urlEqualTo(String.format("/v1/studies/%s/nodes/idNode", uuid))).withHeader("userId", equalTo("user1"))
411415
.willReturn(aResponse()));
@@ -416,22 +420,22 @@ public void testUpdateElements() {
416420
stubFor(put(urlEqualTo(String.format("/v1/filters/%s", uuid))).withHeader("userId", equalTo("user1"))
417421
.willReturn(aResponse()));
418422

419-
// Put with no or bad uuid is forbidden
423+
// Put with no or bad uuid
420424
webClient
421425
.put().uri("study/v1/studies/nodes/idNode")
422426
.header("Authorization", "Bearer " + tokenUser1)
423427
.exchange()
424-
.expectStatus().isForbidden();
428+
.expectStatus().isNotFound();
425429
webClient
426430
.put().uri(String.format("study/v1/studies/%s/nodes/idNode", (UUID) null))
427431
.header("Authorization", "Bearer " + tokenUser1)
428432
.exchange()
429-
.expectStatus().isForbidden();
433+
.expectStatus().isNotFound();
430434
webClient
431435
.put().uri(String.format("study/v1/studies/%s/nodes/idNode", "badUuid"))
432436
.header("Authorization", "Bearer " + tokenUser1)
433437
.exchange()
434-
.expectStatus().isForbidden();
438+
.expectStatus().isNotFound();
435439

436440
webClient
437441
.put().uri(String.format("study/v1/studies/%s/nodes/idNode", uuid))
@@ -462,9 +466,9 @@ public void testDeleteElements() {
462466
stubFor(head(urlEqualTo(String.format("/v1/elements?ids=%s", uuid))).withPort(port).withHeader("userId", equalTo("user1"))
463467
.willReturn(aResponse()));
464468

465-
// user2 not allowed
469+
// user2 allowed
466470
stubFor(head(urlEqualTo(String.format("/v1/elements?ids=%s", uuid))).withPort(port).withHeader("userId", equalTo("user2"))
467-
.willReturn(aResponse().withStatus(HttpStatus.FORBIDDEN.value())));
471+
.willReturn(aResponse()));
468472

469473
stubFor(delete(urlEqualTo(String.format("/v1/explore/elements/%s", uuid))).withHeader("userId", equalTo("user1"))
470474
.willReturn(aResponse()));
@@ -478,28 +482,28 @@ public void testDeleteElements() {
478482
stubFor(delete(urlEqualTo(String.format("/v1/filters/%s", uuid))).withHeader("userId", equalTo("user1"))
479483
.willReturn(aResponse()));
480484

481-
// Delete elements with no or bad uuid is forbidden
485+
// Delete elements with no or bad uuid
482486
webClient
483487
.delete().uri("explore/v1/explore/elements")
484488
.header("Authorization", "Bearer " + tokenUser1)
485489
.exchange()
486-
.expectStatus().isForbidden();
490+
.expectStatus().isNotFound();
487491
webClient
488492
.delete().uri(String.format("explore/v1/explore/elements/%s", (UUID) null))
489493
.header("Authorization", "Bearer " + tokenUser1)
490494
.exchange()
491-
.expectStatus().isForbidden();
495+
.expectStatus().isNotFound();
492496
webClient
493497
.delete().uri(String.format("explore/v1/explore/elements/%s", "badUuid"))
494498
.header("Authorization", "Bearer " + tokenUser1)
495499
.exchange()
496-
.expectStatus().isForbidden();
500+
.expectStatus().isNotFound();
497501

498502
webClient
499503
.delete().uri(String.format("explore/v1/explore/elements/%s", uuid))
500504
.header("Authorization", "Bearer " + tokenUser2)
501505
.exchange()
502-
.expectStatus().isForbidden();
506+
.expectStatus().isNotFound();
503507

504508
webClient
505509
.delete().uri(String.format("explore/v1/explore/elements/%s", uuid))
@@ -572,7 +576,7 @@ public void testDuplicateElements() {
572576
.post().uri("study/v1/studies")
573577
.header("Authorization", "Bearer " + tokenUser1)
574578
.exchange()
575-
.expectStatus().isForbidden();
579+
.expectStatus().isNotFound();
576580

577581
webClient
578582
.post().uri(String.format("explore/v1/explore/studies?%s=%s", ExploreServer.QUERY_PARAM_DUPLICATE_FROM_ID, uuid))

0 commit comments

Comments
 (0)