4848import java .util .stream .Collectors ;
4949
5050import static org .hamcrest .Matchers .containsInAnyOrder ;
51+ import static org .hamcrest .Matchers .containsString ;
5152import static org .hamcrest .Matchers .equalTo ;
5253import static org .hamcrest .Matchers .hasItem ;
5354import static org .hamcrest .Matchers .is ;
@@ -877,62 +878,56 @@ public void testHasPrivilegesWithApiKeys() throws IOException {
877878
878879 public void testRoleWithSelectorInIndexPattern () throws Exception {
879880 setupDataStream ();
880-
881881 createUser ("user" , PASSWORD , "role" );
882- upsertRole ("""
883- {
884- "cluster": ["all"],
885- "indices": [
886- {
887- "names": ["*::failures"],
888- "privileges": ["read"]
889- }
890- ]
891- }""" , "role" );
892- createAndStoreApiKey ("user" , null );
893-
894- expectThrows ("user" , new Search ("test1::failures" ), 403 );
895- expectSearch ("user" , new Search ("*::failures" ));
896-
897- upsertRole ("""
898- {
899- "cluster": ["all"],
900- "indices": [
901- {
902- "names": ["test1::failures"],
903- "privileges": ["read"]
904- }
905- ]
906- }""" , "role" );
882+ expectThrowsSelectorsNotAllowed (
883+ () -> upsertRole (
884+ Strings .format ("""
885+ {
886+ "cluster": ["all"],
887+ "indices": [
888+ {
889+ "names": ["%s"],
890+ "privileges": ["%s"]
891+ }
892+ ]
893+ }""" , randomFrom ("*::failures" , "test1::failures" , "test1::data" , "*::data" ), randomFrom ("read" , "read_failure_store" )),
894+ "role" ,
895+ false
896+ )
897+ );
907898
908- expectThrows ("user" , new Search ("test1::failures" ), 403 );
909- expectSearch ("user" , new Search ("*::failures" ));
899+ AssertionError bulkFailedError = expectThrows (
900+ AssertionError .class ,
901+ () -> upsertRole (
902+ Strings .format ("""
903+ {
904+ "cluster": ["all"],
905+ "indices": [
906+ {
907+ "names": ["%s"],
908+ "privileges": ["%s"]
909+ }
910+ ]
911+ }""" , randomFrom ("*::failures" , "test1::failures" , "test1::data" , "*::data" ), randomFrom ("read" , "read_failure_store" )),
912+ "role" ,
913+ true
914+ )
915+ );
916+ assertThat (bulkFailedError .getMessage (), containsString ("selectors [::] are not allowed in the index name expression" ));
910917
911- upsertRole ("""
918+ expectThrowsSelectorsNotAllowed (() -> createApiKey ( "user" , Strings . format ("""
912919 {
913- "cluster": ["all"],
914- "indices": [
915- {
916- "names": ["*::failures"],
917- "privileges": ["read_failure_store"]
920+ "role": {
921+ "cluster": ["all"],
922+ "indices": [
923+ {
924+ "names": ["%s"],
925+ "privileges": ["%s"]
926+ }
927+ ]
918928 }
919- ]
920- }""" , "role" );
921- expectThrows ("user" , new Search ("test1::failures" ), 403 );
922- expectSearch ("user" , new Search ("*::failures" ));
929+ }""" , randomFrom ("*::failures" , "test1::failures" , "test1::data" , "*::data" ), randomFrom ("read" , "read_failure_store" ))));
923930
924- upsertRole ("""
925- {
926- "cluster": ["all"],
927- "indices": [
928- {
929- "names": ["test1::failures"],
930- "privileges": ["read_failure_store"]
931- }
932- ]
933- }""" , "role" );
934- expectThrows ("user" , new Search ("test1::failures" ), 403 );
935- expectSearch ("user" , new Search ("*::failures" ));
936931 }
937932
938933 public void testFailureStoreAccess () throws Exception {
@@ -2489,7 +2484,7 @@ protected void createUser(String username, SecureString password, String... role
24892484 protected String createAndStoreApiKey (String username , @ Nullable String roleDescriptors ) throws IOException {
24902485 assertThat ("API key already registered for user: " + username , apiKeys .containsKey (username ), is (false ));
24912486 apiKeys .put (username , createApiKey (username , roleDescriptors ));
2492- return createApiKey (username , roleDescriptors );
2487+ return apiKeys . get (username );
24932488 }
24942489
24952490 private String createApiKey (String username , String roleDescriptors ) throws IOException {
@@ -2514,22 +2509,35 @@ private String createApiKey(String username, String roleDescriptors) throws IOEx
25142509 return (String ) responseAsMap .get ("encoded" );
25152510 }
25162511
2517- protected void upsertRole (String roleDescriptor , String roleName ) throws IOException {
2518- Request createRoleRequest = roleRequest (roleDescriptor , roleName );
2512+ protected Response upsertRole (String roleDescriptor , String roleName ) throws IOException {
2513+ return upsertRole (roleDescriptor , roleName , randomBoolean ());
2514+ }
2515+
2516+ protected Response upsertRole (String roleDescriptor , String roleName , boolean bulk ) throws IOException {
2517+ Request createRoleRequest = roleRequest (roleDescriptor , roleName , bulk );
25192518 Response createRoleResponse = adminClient ().performRequest (createRoleRequest );
25202519 assertOK (createRoleResponse );
2520+ if (bulk ) {
2521+ Map <String , Object > flattenedResponse = Maps .flatten (responseAsMap (createRoleResponse ), true , true );
2522+ if (flattenedResponse .containsKey ("errors.count" ) && (int ) flattenedResponse .get ("errors.count" ) > 0 ) {
2523+ throw new AssertionError (
2524+ "Failed to create role [" + roleName + "], reason: " + flattenedResponse .get ("errors.details." + roleName + ".reason" )
2525+ );
2526+ }
2527+ }
2528+ return createRoleResponse ;
25212529 }
25222530
2523- protected Request roleRequest (String roleDescriptor , String roleName ) {
2531+ protected Request roleRequest (String roleDescriptor , String roleName , boolean bulk ) {
25242532 Request createRoleRequest ;
2525- if (randomBoolean ()) {
2526- createRoleRequest = new Request (randomFrom (HttpPut .METHOD_NAME , HttpPost .METHOD_NAME ), "/_security/role/" + roleName );
2527- createRoleRequest .setJsonEntity (roleDescriptor );
2528- } else {
2533+ if (bulk ) {
25292534 createRoleRequest = new Request (HttpPost .METHOD_NAME , "/_security/role" );
25302535 createRoleRequest .setJsonEntity (org .elasticsearch .core .Strings .format ("""
25312536 {"roles": {"%s": %s}}
25322537 """ , roleName , roleDescriptor ));
2538+ } else {
2539+ createRoleRequest = new Request (randomFrom (HttpPut .METHOD_NAME , HttpPost .METHOD_NAME ), "/_security/role/" + roleName );
2540+ createRoleRequest .setJsonEntity (roleDescriptor );
25332541 }
25342542 return createRoleRequest ;
25352543 }
@@ -2592,4 +2600,10 @@ private void expectHasPrivilegesWithApiKey(String apiKey, String requestBody, St
25922600 Response response = performRequestWithApiKey (apiKey , req );
25932601 assertThat (responseAsMap (response ), equalTo (mapFromJson (expectedResponse )));
25942602 }
2603+
2604+ private static void expectThrowsSelectorsNotAllowed (ThrowingRunnable runnable ) {
2605+ ResponseException exception = expectThrows (ResponseException .class , runnable );
2606+ assertThat (exception .getResponse ().getStatusLine ().getStatusCode (), equalTo (400 ));
2607+ assertThat (exception .getMessage (), containsString ("selectors [::] are not allowed in the index name expression" ));
2608+ }
25952609}
0 commit comments