Skip to content

Commit f4ff07d

Browse files
committed
Add api key metadata version check in ApiKeyService
1 parent a16e649 commit f4ff07d

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public void writeTo(StreamOutput out) throws IOException {
154154
if (metadata != null && false == metadata.isEmpty()) {
155155
if (out.getVersion().before(Version.V_7_13_0)) {
156156
throw new IllegalArgumentException(
157-
"api key metadata requires minimum node version to be [7.13.0], got: [" + out.getVersion() + "]");
157+
"api key metadata requires minimum node version to be [7.13], got: [" + out.getVersion() + "]");
158158
} else {
159159
out.writeMap(metadata);
160160
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ private void createApiKeyAndIndexIt(Authentication authentication, CreateApiKeyR
285285
indexResponse -> listener.onResponse(
286286
new CreateApiKeyResponse(request.getName(), indexResponse.getId(), apiKey, expiration)),
287287
listener::onFailure))));
288-
} catch (IOException e) {
288+
} catch (Exception e) {
289289
listener.onFailure(e);
290290
}
291291
}
@@ -334,8 +334,17 @@ XContentBuilder newDocument(SecureString apiKey, String name, Authentication aut
334334
builder.endObject();
335335

336336
builder.field("name", name)
337-
.field("version", version.id)
338-
.field("metadata_flattened", metadata)
337+
.field("version", version.id);
338+
final Version masterNodeVersion = clusterService.state().nodes().getMasterNode().getVersion();
339+
if (masterNodeVersion.onOrAfter(Version.V_7_13_0)) {
340+
builder.field("metadata_flattened", metadata);
341+
} else {
342+
if (metadata != null && false == metadata.isEmpty()) {
343+
throw new IllegalArgumentException(
344+
"api key metadata requires master node to be on version [7.13] or later, got [" + masterNodeVersion + "]");
345+
}
346+
}
347+
builder
339348
.startObject("creator")
340349
.field("principal", authentication.getUser().principal())
341350
.field("full_name", authentication.getUser().fullName())

0 commit comments

Comments
 (0)