12
12
import org .elasticsearch .client .Response ;
13
13
import org .elasticsearch .client .ResponseException ;
14
14
import org .elasticsearch .common .settings .SecureString ;
15
+ import org .elasticsearch .core .Strings ;
15
16
import org .elasticsearch .test .rest .ObjectPath ;
16
17
import org .elasticsearch .xpack .core .security .authc .support .UsernamePasswordToken ;
17
18
import org .elasticsearch .xpack .security .SecurityOnTrialLicenseRestTestCase ;
@@ -49,11 +50,32 @@ public void setup() throws IOException {
49
50
}""" );
50
51
assertOK (adminClient ().performRequest (putRoleRequest ));
51
52
52
- final Request indexDocRequestA = new Request ("POST" , "/index-a/_doc/doc1?refresh=true" );
53
- indexDocRequestA .setJsonEntity ("{\" foo\" : \" bar\" }" );
54
- assertOK (adminClient ().performRequest (indexDocRequestA ));
53
+ // Create the index with 2 shards so each node has 1 shard
54
+ final Request createIndexRequest = new Request ("PUT" , "index-a" );
55
+ createIndexRequest .setJsonEntity ("""
56
+ {
57
+ "settings": {
58
+ "number_of_shards": 2,
59
+ "number_of_replicas": 0
60
+ }
61
+ }""" );
62
+ assertOK (adminClient ().performRequest (createIndexRequest ));
63
+ final Request bulkRequest = new Request ("POST" , "/_bulk?refresh=true" );
64
+ bulkRequest .setJsonEntity (Strings .format ("""
65
+ { "index": { "_index": "index-a", "_id": "doc1" } }
66
+ { "foo": "bar", "value": 1 }
67
+ { "index": { "_index": "index-a", "_id": "doc2" } }
68
+ { "foo": "bar", "value": 2 }
69
+ { "index": { "_index": "index-a", "_id": "doc3" } }
70
+ { "foo": "bar", "value": 3 }
71
+ { "index": { "_index": "index-a", "_id": "doc4" } }
72
+ { "foo": "bar", "value": 4 }
73
+ { "index": { "_index": "index-a", "_id": "doc5" } }
74
+ { "foo": "foo", "value": 5 }
75
+ """ ));
76
+ assertOK (adminClient ().performRequest (bulkRequest ));
55
77
56
- final Request indexDocRequestB = new Request ("POST" , "/index-b/_doc/doc2 ?refresh=true" );
78
+ final Request indexDocRequestB = new Request ("POST" , "/index-b/_doc/doc20 ?refresh=true" );
57
79
indexDocRequestB .setJsonEntity ("{\" baz\" : \" qux\" }" );
58
80
assertOK (adminClient ().performRequest (indexDocRequestB ));
59
81
}
@@ -103,7 +125,8 @@ public void testWorkflowsRestrictionAllowsAccess() throws IOException {
103
125
"term": {
104
126
"{{field_name}}": "{{field_value}}"
105
127
}
106
- }
128
+ },
129
+ "sort": [ "value" ]
107
130
},
108
131
"params": {
109
132
"field_name": "foo",
@@ -117,8 +140,11 @@ public void testWorkflowsRestrictionAllowsAccess() throws IOException {
117
140
ObjectPath queryResponseA = assertOKAndCreateObjectPath (
118
141
performRequestWithApiKey (new Request ("GET" , "_application/search_application/my-app-a/_search" ), apiKeyEncoded )
119
142
);
120
- assertThat (queryResponseA .evaluate ("hits.total.value" ), equalTo (1 ));
143
+ assertThat (queryResponseA .evaluate ("hits.total.value" ), equalTo (4 ));
121
144
assertThat (queryResponseA .evaluate ("hits.hits.0._id" ), equalTo ("doc1" ));
145
+ assertThat (queryResponseA .evaluate ("hits.hits.1._id" ), equalTo ("doc2" ));
146
+ assertThat (queryResponseA .evaluate ("hits.hits.2._id" ), equalTo ("doc3" ));
147
+ assertThat (queryResponseA .evaluate ("hits.hits.3._id" ), equalTo ("doc4" ));
122
148
123
149
// Check that access is rejected by workflow restriction after successful search application query call.
124
150
// This test additionally proves that the permission check works correctly even after the API key's role is cached.
0 commit comments