|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2026 Eclipse Dirigible contributors |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v2.0 which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v20.html |
| 7 | + * |
| 8 | + * SPDX-FileCopyrightText: Eclipse Dirigible contributors SPDX-License-Identifier: EPL-2.0 |
| 9 | + */ |
| 10 | +package org.eclipse.dirigible.integration.tests.api; |
| 11 | + |
| 12 | +import static io.restassured.RestAssured.given; |
| 13 | + |
| 14 | +import java.nio.charset.StandardCharsets; |
| 15 | + |
| 16 | +import org.eclipse.dirigible.components.base.http.roles.Roles; |
| 17 | +import org.eclipse.dirigible.components.initializers.synchronizer.SynchronizationProcessor; |
| 18 | +import org.eclipse.dirigible.repository.api.IRepository; |
| 19 | +import org.eclipse.dirigible.repository.api.IRepositoryStructure; |
| 20 | +import org.eclipse.dirigible.tests.base.IntegrationTest; |
| 21 | +import org.eclipse.dirigible.tests.framework.restassured.RestAssuredExecutor; |
| 22 | +import org.eclipse.dirigible.tests.framework.security.SecurityUtil; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | + |
| 26 | +/** |
| 27 | + * End-to-end test for HTTP access constraints ({@code *.access}) on client Java endpoints served |
| 28 | + * under {@code /services/java/...}: a role constraint synchronized from the registry must deny a |
| 29 | + * user without the role (403) and admit a user with it (200) as soon as the synchronization round |
| 30 | + * completes. |
| 31 | + */ |
| 32 | +class JavaAccessConstraintsIT extends IntegrationTest { |
| 33 | + |
| 34 | + /** Project segment used for all sources in this IT. */ |
| 35 | + private static final String PROJECT = "java-access-it"; |
| 36 | + |
| 37 | + /** Registry path of the client Java handler. */ |
| 38 | + private static final String SOURCE_REGISTRY_PATH = IRepositoryStructure.PATH_REGISTRY_PUBLIC + "/" + PROJECT + "/demo/Hello.java"; |
| 39 | + |
| 40 | + /** Registry path of the access constraint artefact. */ |
| 41 | + private static final String ACCESS_REGISTRY_PATH = IRepositoryStructure.PATH_REGISTRY_PUBLIC + "/" + PROJECT + "/security/demo.access"; |
| 42 | + |
| 43 | + /** Endpoint where the {@code demo.Hello} handler answers. */ |
| 44 | + private static final String ENDPOINT = "/services/java/" + PROJECT + "/demo/Hello"; |
| 45 | + |
| 46 | + private static final String ADMIN_USER = "java-access-it-admin"; |
| 47 | + private static final String NON_ADMIN_USER = "java-access-it-developer"; |
| 48 | + private static final String PASSWORD = "java-access-it-password"; |
| 49 | + |
| 50 | + /** |
| 51 | + * Wait cap for the first request after the handler is synchronized - the in-process compile + |
| 52 | + * class-define cycle can finish shortly after {@code forceProcessSynchronizers()} returns. |
| 53 | + */ |
| 54 | + private static final long ASSERTION_TIMEOUT_SECONDS = 30; |
| 55 | + |
| 56 | + private static final String HANDLER_SOURCE = """ |
| 57 | + package demo; |
| 58 | + import jakarta.servlet.http.HttpServletRequest; |
| 59 | + import jakarta.servlet.http.HttpServletResponse; |
| 60 | + import org.eclipse.dirigible.engine.java.handler.JavaHandler; |
| 61 | + public class Hello implements JavaHandler { |
| 62 | + @Override |
| 63 | + public void handle(HttpServletRequest request, HttpServletResponse response) throws Exception { |
| 64 | + response.setContentType("application/json"); |
| 65 | + response.getWriter().write("{\\"message\\": \\"hello\\"}"); |
| 66 | + } |
| 67 | + } |
| 68 | + """; |
| 69 | + |
| 70 | + /** The constraint path is registry-relative - the filter strips the /services/java prefix. */ |
| 71 | + private static final String ACCESS_CONSTRAINT = """ |
| 72 | + { |
| 73 | + "constraints": [ |
| 74 | + { |
| 75 | + "path": "/%s/**", |
| 76 | + "method": "*", |
| 77 | + "scope": "HTTP", |
| 78 | + "roles": [ |
| 79 | + "%s" |
| 80 | + ] |
| 81 | + } |
| 82 | + ] |
| 83 | + } |
| 84 | + """.formatted(PROJECT, Roles.RoleNames.ADMINISTRATOR); |
| 85 | + |
| 86 | + @Autowired |
| 87 | + private IRepository repository; |
| 88 | + |
| 89 | + @Autowired |
| 90 | + private SynchronizationProcessor synchronizationProcessor; |
| 91 | + |
| 92 | + @Autowired |
| 93 | + private RestAssuredExecutor restAssuredExecutor; |
| 94 | + |
| 95 | + @Autowired |
| 96 | + private SecurityUtil securityUtil; |
| 97 | + |
| 98 | + @Test |
| 99 | + void access_constraints_are_enforced_for_java_endpoints() { |
| 100 | + securityUtil.createUserInDefaultTenant(ADMIN_USER, PASSWORD, Roles.RoleNames.ADMINISTRATOR); |
| 101 | + securityUtil.createUserInDefaultTenant(NON_ADMIN_USER, PASSWORD, Roles.RoleNames.DEVELOPER); |
| 102 | + |
| 103 | + // without a constraint any authenticated user can call the handler |
| 104 | + repository.createResource(SOURCE_REGISTRY_PATH, HANDLER_SOURCE.getBytes(StandardCharsets.UTF_8), false, "text/x-java", true); |
| 105 | + synchronizationProcessor.forceProcessSynchronizers(); |
| 106 | + assertEndpointStatusEventually(NON_ADMIN_USER, 200); |
| 107 | + |
| 108 | + // the constraint restricts the project to ADMINISTRATOR; it must be enforced as soon as the |
| 109 | + // forced synchronization returns - no retry window for the 403 |
| 110 | + repository.createResource(ACCESS_REGISTRY_PATH, ACCESS_CONSTRAINT.getBytes(StandardCharsets.UTF_8), false, "application/json", |
| 111 | + true); |
| 112 | + synchronizationProcessor.forceProcessSynchronizers(); |
| 113 | + assertEndpointStatus(NON_ADMIN_USER, 403); |
| 114 | + assertEndpointStatus(ADMIN_USER, 200); |
| 115 | + } |
| 116 | + |
| 117 | + private void assertEndpointStatusEventually(String user, int expectedStatus) { |
| 118 | + restAssuredExecutor.execute(() -> given().when() |
| 119 | + .get(ENDPOINT) |
| 120 | + .then() |
| 121 | + .statusCode(expectedStatus), |
| 122 | + user, PASSWORD, ASSERTION_TIMEOUT_SECONDS); |
| 123 | + } |
| 124 | + |
| 125 | + private void assertEndpointStatus(String user, int expectedStatus) { |
| 126 | + restAssuredExecutor.execute(() -> given().when() |
| 127 | + .get(ENDPOINT) |
| 128 | + .then() |
| 129 | + .statusCode(expectedStatus), |
| 130 | + user, PASSWORD); |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments