| 
 | 1 | +/*  | 
 | 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one  | 
 | 3 | + * or more contributor license agreements. Licensed under the Elastic License  | 
 | 4 | + * 2.0; you may not use this file except in compliance with the Elastic License  | 
 | 5 | + * 2.0.  | 
 | 6 | + */  | 
 | 7 | +package org.elasticsearch.integration;  | 
 | 8 | + | 
 | 9 | +import org.elasticsearch.action.support.PlainActionFuture;  | 
 | 10 | +import org.elasticsearch.test.NativeRealmIntegTestCase;  | 
 | 11 | +import org.elasticsearch.test.TestSecurityClient;  | 
 | 12 | +import org.elasticsearch.xpack.core.security.authz.RoleDescriptor;  | 
 | 13 | +import org.elasticsearch.xpack.core.security.authz.store.ReservedRolesStore;  | 
 | 14 | +import org.elasticsearch.xpack.core.security.authz.store.RoleRetrievalResult;  | 
 | 15 | +import org.elasticsearch.xpack.security.authz.store.NativeRolesStore;  | 
 | 16 | +import org.elasticsearch.xpack.security.support.SecuritySystemIndices;  | 
 | 17 | +import org.junit.Before;  | 
 | 18 | +import org.junit.BeforeClass;  | 
 | 19 | + | 
 | 20 | +import java.io.IOException;  | 
 | 21 | +import java.util.HashSet;  | 
 | 22 | +import java.util.Set;  | 
 | 23 | + | 
 | 24 | +import static org.elasticsearch.test.SecuritySettingsSource.SECURITY_REQUEST_OPTIONS;  | 
 | 25 | +import static org.hamcrest.Matchers.containsInAnyOrder;  | 
 | 26 | +import static org.hamcrest.Matchers.empty;  | 
 | 27 | +import static org.hamcrest.Matchers.is;  | 
 | 28 | +import static org.hamcrest.Matchers.notNullValue;  | 
 | 29 | + | 
 | 30 | +/**  | 
 | 31 | + * Test for the {@link NativeRolesStore#getRoleDescriptors} method.  | 
 | 32 | + */  | 
 | 33 | +public class GeRoleDescriptorsTests extends NativeRealmIntegTestCase {  | 
 | 34 | + | 
 | 35 | +    private static Set<String> customRoles;  | 
 | 36 | + | 
 | 37 | +    @BeforeClass  | 
 | 38 | +    public static void init() throws Exception {  | 
 | 39 | +        new ReservedRolesStore();  | 
 | 40 | + | 
 | 41 | +        final int numOfRoles = randomIntBetween(5, 10);  | 
 | 42 | +        customRoles = new HashSet<>(numOfRoles);  | 
 | 43 | +        for (int i = 0; i < numOfRoles; i++) {  | 
 | 44 | +            customRoles.add("custom_role_" + randomAlphaOfLength(10) + "_" + i);  | 
 | 45 | +        }  | 
 | 46 | +    }  | 
 | 47 | + | 
 | 48 | +    @Before  | 
 | 49 | +    public void setup() throws IOException {  | 
 | 50 | +        final TestSecurityClient securityClient = new TestSecurityClient(getRestClient(), SECURITY_REQUEST_OPTIONS);  | 
 | 51 | +        for (String role : customRoles) {  | 
 | 52 | +            final RoleDescriptor descriptor = new RoleDescriptor(  | 
 | 53 | +                role,  | 
 | 54 | +                new String[0],  | 
 | 55 | +                new RoleDescriptor.IndicesPrivileges[] {  | 
 | 56 | +                    RoleDescriptor.IndicesPrivileges.builder()  | 
 | 57 | +                        .indices("*")  | 
 | 58 | +                        .privileges("ALL")  | 
 | 59 | +                        .allowRestrictedIndices(randomBoolean())  | 
 | 60 | +                        .build() },  | 
 | 61 | +                new String[0]  | 
 | 62 | +            );  | 
 | 63 | +            securityClient.putRole(descriptor);  | 
 | 64 | +            logger.info("--> created role [{}]", role);  | 
 | 65 | +        }  | 
 | 66 | + | 
 | 67 | +        ensureGreen(SecuritySystemIndices.SECURITY_MAIN_ALIAS);  | 
 | 68 | +    }  | 
 | 69 | + | 
 | 70 | +    public void testGetCustomRoles() {  | 
 | 71 | +        for (NativeRolesStore rolesStore : internalCluster().getInstances(NativeRolesStore.class)) {  | 
 | 72 | +            PlainActionFuture<RoleRetrievalResult> future = new PlainActionFuture<>();  | 
 | 73 | +            rolesStore.getRoleDescriptors(customRoles, future);  | 
 | 74 | +            RoleRetrievalResult result = future.actionGet();  | 
 | 75 | +            assertThat(result, notNullValue());  | 
 | 76 | +            assertTrue(result.isSuccess());  | 
 | 77 | +            assertThat(result.getDescriptors().stream().map(RoleDescriptor::getName).toList(), containsInAnyOrder(customRoles.toArray()));  | 
 | 78 | +        }  | 
 | 79 | +    }  | 
 | 80 | + | 
 | 81 | +    public void testGetReservedRoles() {  | 
 | 82 | +        for (NativeRolesStore rolesStore : internalCluster().getInstances(NativeRolesStore.class)) {  | 
 | 83 | +            PlainActionFuture<RoleRetrievalResult> future = new PlainActionFuture<>();  | 
 | 84 | +            Set<String> reservedRoles = randomUnique(() -> randomFrom(ReservedRolesStore.names()), randomIntBetween(1, 5));  | 
 | 85 | +            rolesStore.getRoleDescriptors(reservedRoles, future);  | 
 | 86 | +            RoleRetrievalResult result = future.actionGet();  | 
 | 87 | +            assertThat(result, notNullValue());  | 
 | 88 | +            assertTrue(result.isSuccess());  | 
 | 89 | +            assertThat(result.getDescriptors(), is(empty()));  | 
 | 90 | +        }  | 
 | 91 | +    }  | 
 | 92 | + | 
 | 93 | +    public void testGetAllRoles() {  | 
 | 94 | +        for (NativeRolesStore rolesStore : internalCluster().getInstances(NativeRolesStore.class)) {  | 
 | 95 | +            PlainActionFuture<RoleRetrievalResult> future = new PlainActionFuture<>();  | 
 | 96 | +            rolesStore.getRoleDescriptors(randomBoolean() ? null : Set.of(), future);  | 
 | 97 | +            RoleRetrievalResult result = future.actionGet();  | 
 | 98 | +            assertThat(result, notNullValue());  | 
 | 99 | +            assertTrue(result.isSuccess());  | 
 | 100 | +            assertThat(result.getDescriptors().stream().map(RoleDescriptor::getName).toList(), containsInAnyOrder(customRoles.toArray()));  | 
 | 101 | +        }  | 
 | 102 | +    }  | 
 | 103 | +}  | 
0 commit comments