Skip to content

Commit a0766f7

Browse files
committed
[Test] Fix superuser API key bwc test (elastic#82792)
The role descriptors became optional since version 7.3.0. For earlier versions, they must be specified. This PR specifies them conditionally based on the old cluster version. This also serves a variation of the test to show that dropping write access to system indices from the limiting role will prevent the key from writing to system indices as a whole. Resolves: elastic#82785
1 parent 0a92222 commit a0766f7

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/FullClusterRestartIT.java

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ public void testServiceAccountApiKey() throws IOException {
331331
}
332332
}
333333

334-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/82785")
335334
public void testApiKeySuperuser() throws IOException {
336335
if (isRunningAgainstOldCluster()) {
337336
final Request createUserRequest = new Request("PUT", "/_security/user/api_key_super_creator");
@@ -354,10 +353,29 @@ public void testApiKeySuperuser() throws IOException {
354353
)
355354
)
356355
);
357-
createApiKeyRequest.setJsonEntity("""
358-
{
359-
"name": "super_legacy_key"
360-
}""");
356+
if (getOldClusterVersion().onOrAfter(Version.V_7_3_0)) {
357+
createApiKeyRequest.setJsonEntity("""
358+
{
359+
"name": "super_legacy_key"
360+
}""");
361+
} else {
362+
createApiKeyRequest.setJsonEntity("""
363+
{
364+
"name": "super_legacy_key",
365+
"role_descriptors": {
366+
"super": {
367+
"cluster": [ "all" ],
368+
"indices": [
369+
{
370+
"names": [ "*" ],
371+
"privileges": [ "all" ],
372+
"allow_restricted_indices": true
373+
}
374+
]
375+
}
376+
}
377+
}""");
378+
}
361379
final Map<String, Object> createApiKeyResponse = entityAsMap(client().performRequest(createApiKeyRequest));
362380
final byte[] keyBytes = (createApiKeyResponse.get("id") + ":" + createApiKeyResponse.get("api_key")).getBytes(
363381
StandardCharsets.UTF_8
@@ -374,12 +392,16 @@ public void testApiKeySuperuser() throws IOException {
374392
{
375393
"doc_type": "foo"
376394
}""");
377-
indexRequest.setOptions(
378-
expectWarnings(
379-
"this request accesses system indices: [.security-7], but in a future major "
380-
+ "version, direct access to system indices will be prevented by default"
381-
).toBuilder().addHeader("Authorization", apiKeyAuthHeader)
382-
);
395+
if (getOldClusterVersion().onOrAfter(Version.V_7_10_0)) {
396+
indexRequest.setOptions(
397+
expectWarnings(
398+
"this request accesses system indices: [.security-7], but in a future major "
399+
+ "version, direct access to system indices will be prevented by default"
400+
).toBuilder().addHeader("Authorization", apiKeyAuthHeader)
401+
);
402+
} else {
403+
indexRequest.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", apiKeyAuthHeader));
404+
}
383405
assertOK(client().performRequest(indexRequest));
384406
}
385407
} else {
@@ -390,12 +412,17 @@ public void testApiKeySuperuser() throws IOException {
390412

391413
// read is ok
392414
final Request searchRequest = new Request("GET", ".security/_search");
393-
searchRequest.setOptions(
394-
expectWarnings(
395-
"this request accesses system indices: [.security-7], but in a future major "
396-
+ "version, direct access to system indices will be prevented by default"
397-
).toBuilder().addHeader("Authorization", apiKeyAuthHeader)
398-
);
415+
// TODO: change the warning expectation to be always once #82837 is fixed
416+
// Configure the warning to be optional due to #82837, it is ok since this test is for something else
417+
searchRequest.setOptions(RequestOptions.DEFAULT.toBuilder().setWarningsHandler(warnings -> {
418+
if (warnings.isEmpty()) {
419+
return false;
420+
} else if (warnings.size() == 1) {
421+
return false == warnings.get(0).startsWith("this request accesses system indices: [.security-7]");
422+
} else {
423+
return true;
424+
}
425+
}).addHeader("Authorization", apiKeyAuthHeader));
399426
assertOK(client().performRequest(searchRequest));
400427

401428
// write must not be allowed
@@ -404,12 +431,7 @@ public void testApiKeySuperuser() throws IOException {
404431
{
405432
"doc_type": "foo"
406433
}""");
407-
indexRequest.setOptions(
408-
expectWarnings(
409-
"this request accesses system indices: [.security-7], but in a future major "
410-
+ "version, direct access to system indices will be prevented by default"
411-
).toBuilder().addHeader("Authorization", apiKeyAuthHeader)
412-
);
434+
indexRequest.setOptions(RequestOptions.DEFAULT.toBuilder().addHeader("Authorization", apiKeyAuthHeader));
413435
final ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(indexRequest));
414436
assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(403));
415437
assertThat(e.getMessage(), containsString("is unauthorized"));

0 commit comments

Comments
 (0)