Skip to content

Commit d8b64d2

Browse files
committed
Fix ApiKeyBackwardsCompatibilityIT, fix update error message check
1 parent 4e5b362 commit d8b64d2

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/apikey/ApiKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ public String toString() {
435435
+ roleDescriptors
436436
+ ", limited_by="
437437
+ limitedBy
438-
+ ", certificate_identity"
438+
+ ", certificate_identity="
439439
+ certificateIdentity
440440
+ "]";
441441
}

x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/apikey/ApiKeyRestIT.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1695,7 +1695,10 @@ public void testUpdateFailureCases() throws IOException {
16951695
updateRequest.setJsonEntity("{}");
16961696
final ResponseException e2 = expectThrows(ResponseException.class, () -> client().performRequest(updateRequest));
16971697
assertThat(e2.getResponse().getStatusLine().getStatusCode(), equalTo(400));
1698-
assertThat(e2.getMessage(), containsString("must update either [access] or [metadata] for cross-cluster API keys"));
1698+
assertThat(
1699+
e2.getMessage(),
1700+
containsString("must update [access] or [metadata] or [certificate_identity] for cross-cluster API keys")
1701+
);
16991702

17001703
// Access cannot be empty
17011704
updateRequest.setJsonEntity("{\"access\":{}}");

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ private String getCertificateIdentityFromRequest(AbstractCreateApiKeyRequest req
640640
);
641641
throw new ElasticsearchException(
642642
"API key creation failed. The cluster is in a mixed-version state and does not yet "
643-
+ "support the certificate_identity field. Please retry after the upgrade is complete."
643+
+ "support the [certificate_identity] field. Please retry after the upgrade is complete."
644644
);
645645
}
646646
}

x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/ApiKeyBackwardsCompatibilityIT.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,35 @@ public void testCertificateIdentityBackwardsCompatibility() throws Exception {
217217
);
218218
}
219219
case MIXED -> {
220-
// Mixed cluster should reject certificate identity due to feature gating
221-
var exception = expectThrows(Exception.class, () -> createCrossClusterApiKeyWithCertIdentity("CN=test-.*"));
222-
assertThat(
223-
exception.getMessage(),
224-
containsString("cluster is in a mixed-version state and does not yet support the certificate_identity field")
225-
);
220+
try {
221+
this.createClientsByVersion();
222+
223+
Exception oldNodeException = expectThrows(
224+
Exception.class,
225+
() -> createCrossClusterApiKeyWithCertIdentity(oldVersionClient, "CN=test-.*")
226+
);
227+
assertThat(
228+
oldNodeException.getMessage(),
229+
anyOf(containsString("unknown field [certificate_identity]"), containsString("certificate_identity not supported"))
230+
);
231+
232+
// Test against new node - should get mixed-version error
233+
Exception newNodeException = expectThrows(
234+
Exception.class,
235+
() -> createCrossClusterApiKeyWithCertIdentity(newVersionClient, "CN=test-.*")
236+
);
237+
assertThat(
238+
newNodeException.getMessage(),
239+
containsString(
240+
"API key creation failed. The cluster is in a mixed-version state and does not yet "
241+
+ "support the [certificate_identity] field. Please retry after the upgrade is complete."
242+
)
243+
);
244+
} finally {
245+
this.closeClientsByVersion();
246+
}
226247
}
248+
227249
case UPGRADED -> {
228250
// Fully upgraded cluster should support certificate identity
229251
final Tuple<String, String> apiKey = createCrossClusterApiKeyWithCertIdentity("CN=test-.*");
@@ -463,6 +485,11 @@ private void assertQuery(RestClient restClient, String body, Consumer<List<Map<S
463485
}
464486

465487
private Tuple<String, String> createCrossClusterApiKeyWithCertIdentity(String certificateIdentity) throws IOException {
488+
return createCrossClusterApiKeyWithCertIdentity(client(), certificateIdentity);
489+
}
490+
491+
private Tuple<String, String> createCrossClusterApiKeyWithCertIdentity(RestClient client, String certificateIdentity)
492+
throws IOException {
466493
final String name = "test-cc-api-key-" + randomAlphaOfLengthBetween(3, 5);
467494
final Request createApiKeyRequest = new Request("POST", "/_security/cross_cluster/api_key");
468495
createApiKeyRequest.setJsonEntity(Strings.format("""
@@ -478,7 +505,7 @@ private Tuple<String, String> createCrossClusterApiKeyWithCertIdentity(String ce
478505
}
479506
}""", name, certificateIdentity));
480507

481-
final Response createResponse = client().performRequest(createApiKeyRequest);
508+
final Response createResponse = client.performRequest(createApiKeyRequest);
482509
assertOK(createResponse);
483510
final ObjectPath path = ObjectPath.createFromResponse(createResponse);
484511
final String id = path.evaluate("id");
@@ -487,5 +514,4 @@ private Tuple<String, String> createCrossClusterApiKeyWithCertIdentity(String ce
487514
assertThat(key, notNullValue());
488515
return Tuple.tuple(id, key);
489516
}
490-
491517
}

0 commit comments

Comments
 (0)