Skip to content

Commit 7f10e3b

Browse files
committed
Reenable testWhenUserLimitedByOnlyAliasOfIndexCanWriteToIndexWhichWasRolledoverByILMPolicy
This also adds `profile: true` to the search request and executes an additional explain query request when the test fails.
1 parent d20d90a commit 7f10e3b

File tree

1 file changed

+47
-23
lines changed
  • x-pack/plugin/ilm/qa/with-security/src/test/java/org/elasticsearch/xpack/security

1 file changed

+47
-23
lines changed

x-pack/plugin/ilm/qa/with-security/src/test/java/org/elasticsearch/xpack/security/PermissionsIT.java

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,10 @@ public void testSLMWithPermissions() throws Exception {
168168
final HighLevelClient hlAdminClient = new HighLevelClient(adminClient());
169169

170170
// Build two high level clients, each using a different user
171-
final RestClientBuilder adminBuilder = RestClient.builder(adminClient().getNodes().toArray(new Node[0]));
172-
final String adminToken = basicAuthHeaderValue("slm_admin", new SecureString("slm-pass".toCharArray()));
173-
configureClient(adminBuilder, Settings.builder()
174-
.put(ThreadContext.PREFIX + ".Authorization", adminToken)
175-
.build());
176-
adminBuilder.setStrictDeprecationMode(true);
171+
final RestClientBuilder adminBuilder = getRestClientBuilder("slm_admin", "slm-pass");
177172
final RestHighLevelClient adminHLRC = new RestHighLevelClient(adminBuilder);
178173

179-
final RestClientBuilder userBuilder = RestClient.builder(adminClient().getNodes().toArray(new Node[0]));
180-
final String userToken = basicAuthHeaderValue("slm_user", new SecureString("slm-user-pass".toCharArray()));
181-
configureClient(userBuilder, Settings.builder()
182-
.put(ThreadContext.PREFIX + ".Authorization", userToken)
183-
.build());
184-
userBuilder.setStrictDeprecationMode(true);
174+
final RestClientBuilder userBuilder = getRestClientBuilder("slm_user", "slm-user-pass");
185175
final RestHighLevelClient readHlrc = new RestHighLevelClient(userBuilder);
186176

187177
PutRepositoryRequest repoRequest = new PutRepositoryRequest();
@@ -273,7 +263,6 @@ public void testCanViewExplainOnUnmanagedIndex() throws Exception {
273263
* Tests when the user is limited by alias of an index is able to write to index
274264
* which was rolled over by an ILM policy.
275265
*/
276-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/41440")
277266
@TestIssueLogging(value = "org.elasticsearch:DEBUG", issueUrl = "https://github.com/elastic/elasticsearch/issues/41440")
278267
public void testWhenUserLimitedByOnlyAliasOfIndexCanWriteToIndexWhichWasRolledoverByILMPolicy() throws Exception {
279268
/*
@@ -301,19 +290,41 @@ public void testWhenUserLimitedByOnlyAliasOfIndexCanWriteToIndexWhichWasRolledov
301290
});
302291

303292
// test_user: index docs using alias, now should be able write to new index
304-
indexDocs("test_user", "x-pack-test-password", "foo_alias", 1);
293+
indexDoc("test_user", "x-pack-test-password", "foo_alias", "documentID");
305294
refresh("foo_alias");
306295

307296
// verify that the doc has been indexed into new write index
308-
assertBusy(() -> {
309-
Request request = new Request("GET", "/foo-logs-000002/_search");
297+
try {
298+
assertBusy(() -> {
299+
Request request = new Request("GET", "/foo-logs-000002/_search");
300+
request.setJsonEntity("{\n" +
301+
"\"profile\": true,\n" +
302+
"\"query\": {\n" +
303+
" \"match_all\": {}\n" +
304+
" }\n" +
305+
"}");
306+
Response response = adminClient().performRequest(request);
307+
try (InputStream content = response.getEntity().getContent()) {
308+
Map<String, Object> map = XContentHelper.convertToMap(JsonXContent.jsonXContent, content, false);
309+
logger.info("Search response payload is {}", map);
310+
Integer totalHits = (Integer) XContentMapValues.extractValue("hits.total.value", map);
311+
assertThat(totalHits, equalTo(1));
312+
}
313+
});
314+
} catch (AssertionError e) {
315+
Request request = new Request("GET", "/foo-logs-000002/_explain/documentID");
316+
request.setJsonEntity("{\n" +
317+
"\"query\": {\n" +
318+
" \"match_all\": {}\n" +
319+
" }\n" +
320+
"}");
310321
Response response = adminClient().performRequest(request);
311322
try (InputStream content = response.getEntity().getContent()) {
312323
Map<String, Object> map = XContentHelper.convertToMap(JsonXContent.jsonXContent, content, false);
313-
Integer totalHits = (Integer) XContentMapValues.extractValue("hits.total.value", map);
314-
assertThat(totalHits, equalTo(1));
324+
logger.info("Explain search for document with id documentID {}", map);
315325
}
316-
});
326+
throw e;
327+
}
317328
}
318329

319330
private void createNewSingletonPolicy(RestClient client, String policy, String phaseName, LifecycleAction action) throws IOException {
@@ -368,15 +379,19 @@ private void createRole(String name, String alias) throws IOException {
368379
assertOK(adminClient().performRequest(request));
369380
}
370381

371-
private void indexDocs(String user, String passwd, String index, int noOfDocs) throws IOException {
382+
private RestClientBuilder getRestClientBuilder(String user, String passwd) throws IOException {
372383
RestClientBuilder builder = RestClient.builder(adminClient().getNodes().toArray(new Node[0]));
373384
String token = basicAuthHeaderValue(user, new SecureString(passwd.toCharArray()));
374385
configureClient(builder, Settings.builder()
375-
.put(ThreadContext.PREFIX + ".Authorization", token)
376-
.build());
386+
.put(ThreadContext.PREFIX + ".Authorization", token)
387+
.build());
377388
builder.setStrictDeprecationMode(true);
378-
try (RestClient userClient = builder.build();) {
389+
return builder;
390+
}
379391

392+
private void indexDocs(String user, String passwd, String index, int noOfDocs) throws IOException {
393+
RestClientBuilder builder = getRestClientBuilder(user, passwd);
394+
try (RestClient userClient = builder.build()) {
380395
for (int cnt = 0; cnt < noOfDocs; cnt++) {
381396
Request request = new Request("POST", "/" + index + "/_doc");
382397
request.setJsonEntity(jsonDoc);
@@ -385,6 +400,15 @@ private void indexDocs(String user, String passwd, String index, int noOfDocs) t
385400
}
386401
}
387402

403+
private void indexDoc(String user, String passwd, String index, String docId) throws IOException {
404+
RestClientBuilder builder = getRestClientBuilder(user, passwd);
405+
try (RestClient userClient = builder.build()) {
406+
Request request = new Request("POST", "/" + index + "/_doc/" + docId);
407+
request.setJsonEntity(jsonDoc);
408+
assertOK(userClient.performRequest(request));
409+
}
410+
}
411+
388412
private void refresh(String index) throws IOException {
389413
Request request = new Request("POST", "/" + index + "/_refresh");
390414
assertOK(adminClient().performRequest(request));

0 commit comments

Comments
 (0)