Skip to content

Commit 4c97cba

Browse files
committed
Add simple roles mapping integ test to test mapping of backend role to role
Signed-off-by: Craig Perkins <cwperx@amazon.com>
1 parent a731e62 commit 4c97cba

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.opensearch.security.http;
2+
3+
import java.util.List;
4+
5+
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;
6+
import org.junit.ClassRule;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import org.opensearch.test.framework.RolesMapping;
11+
import org.opensearch.test.framework.TestSecurityConfig;
12+
import org.opensearch.test.framework.cluster.ClusterManager;
13+
import org.opensearch.test.framework.cluster.LocalCluster;
14+
import org.opensearch.test.framework.cluster.TestRestClient;
15+
16+
import static org.apache.http.HttpStatus.SC_OK;
17+
import static org.hamcrest.MatcherAssert.assertThat;
18+
import static org.hamcrest.Matchers.contains;
19+
import static org.hamcrest.Matchers.is;
20+
import static org.hamcrest.Matchers.notNullValue;
21+
22+
@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class)
23+
@ThreadLeakScope(ThreadLeakScope.Scope.NONE)
24+
public class RolesMappingTests {
25+
static final TestSecurityConfig.User USER_A = new TestSecurityConfig.User("userA").password("s3cret").backendRoles("mapsToRoleA");
26+
static final TestSecurityConfig.User USER_B = new TestSecurityConfig.User("userB").password("P@ssw0rd").backendRoles("mapsToRoleB");
27+
28+
private static final TestSecurityConfig.Role ROLE_A = new TestSecurityConfig.Role("roleA").clusterPermissions("cluster_all");
29+
30+
private static final TestSecurityConfig.Role ROLE_B = new TestSecurityConfig.Role("roleB").clusterPermissions("cluster_all");
31+
32+
public static final TestSecurityConfig.AuthcDomain AUTHC_DOMAIN = new TestSecurityConfig.AuthcDomain("basic", 0)
33+
.httpAuthenticatorWithChallenge("basic")
34+
.backend("internal");
35+
36+
@ClassRule
37+
public static final LocalCluster cluster = new LocalCluster.Builder().clusterManager(ClusterManager.SINGLENODE)
38+
.anonymousAuth(false)
39+
.authc(AUTHC_DOMAIN)
40+
.roles(ROLE_A, ROLE_B)
41+
.rolesMapping(new RolesMapping(ROLE_A).backendRoles("mapsToRoleA"), new RolesMapping(ROLE_B).backendRoles("mapsToRoleB"))
42+
.users(USER_A, USER_B)
43+
.build();
44+
45+
@Test
46+
public void testBackendRoleToRoleMapping() {
47+
try (TestRestClient client = cluster.getRestClient(USER_A)) {
48+
49+
TestRestClient.HttpResponse response = client.getAuthInfo();
50+
51+
assertThat(response, is(notNullValue()));
52+
List<String> roles = response.getTextArrayFromJsonBody("/roles");
53+
List<String> backendRoles = response.getTextArrayFromJsonBody("/backend_roles");
54+
assertThat(roles, contains(ROLE_A.getName()));
55+
assertThat(backendRoles, contains("mapsToRoleA"));
56+
response.assertStatusCode(SC_OK);
57+
}
58+
59+
try (TestRestClient client = cluster.getRestClient(USER_B)) {
60+
61+
TestRestClient.HttpResponse response = client.getAuthInfo();
62+
63+
assertThat(response, is(notNullValue()));
64+
List<String> roles = response.getTextArrayFromJsonBody("/roles");
65+
List<String> backendRoles = response.getTextArrayFromJsonBody("/backend_roles");
66+
assertThat(roles, contains(ROLE_B.getName()));
67+
assertThat(backendRoles, contains("mapsToRoleB"));
68+
response.assertStatusCode(SC_OK);
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)