Skip to content

Commit d2cae33

Browse files
authored
Add v7 restCompat for invalidating API key with the id field (#78664)
This PR restore the id field for InvaliateApiKey API so it can be used if the request explicitly requires v7 compatibility. Relates: #66671
1 parent cb983a9 commit d2cae33

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

x-pack/plugin/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ tasks.named("yamlRestTestV7CompatTransform").configure{ task ->
9898
task.skipTest("rollup/put_job/Test basic put_job", "rollup was an experimental feature, also see #41227")
9999
task.skipTest("rollup/start_job/Test start job twice", "rollup was an experimental feature, also see #41227")
100100
task.skipTest("ml/trained_model_cat_apis/Test cat trained models", "A type field was added to cat.ml_trained_models #73660, this is a backwards compatible change. Still this is a cat api, and we don't support them with rest api compatibility. (the test would be very hard to transform too)")
101-
task.skipTest("api_key/10_basic/Test invalidate api keys with single id", "waiting for https://github.com/elastic/elasticsearch/pull/78664")
102101

103102
task.replaceKeyInDo("license.delete", "xpack-license.delete")
104103
task.replaceKeyInDo("license.get", "xpack-license.get")

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/apikey/RestInvalidateApiKeyAction.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
package org.elasticsearch.xpack.security.rest.action.apikey;
99

1010
import org.elasticsearch.client.node.NodeClient;
11-
import org.elasticsearch.xcontent.ParseField;
11+
import org.elasticsearch.common.Strings;
1212
import org.elasticsearch.common.settings.Settings;
13+
import org.elasticsearch.core.RestApiVersion;
14+
import org.elasticsearch.xcontent.ParseField;
1315
import org.elasticsearch.xcontent.ConstructingObjectParser;
1416
import org.elasticsearch.xcontent.XContentBuilder;
1517
import org.elasticsearch.xcontent.XContentParser;
@@ -42,11 +44,7 @@ public final class RestInvalidateApiKeyAction extends SecurityBaseRestHandler {
4244
});
4345

4446
static {
45-
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField("realm_name"));
46-
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField("username"));
47-
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField("name"));
48-
PARSER.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), new ParseField("owner"));
49-
PARSER.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), new ParseField("ids"));
47+
initObjectParser(PARSER, false);
5048
}
5149

5250
public RestInvalidateApiKeyAction(Settings settings, XPackLicenseState licenseState) {
@@ -61,7 +59,7 @@ public List<Route> routes() {
6159
@Override
6260
protected RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException {
6361
try (XContentParser parser = request.contentParser()) {
64-
final InvalidateApiKeyRequest invalidateApiKeyRequest = PARSER.parse(parser, null);
62+
final InvalidateApiKeyRequest invalidateApiKeyRequest = getObjectParser(request).parse(parser, null);
6563
return channel -> client.execute(InvalidateApiKeyAction.INSTANCE, invalidateApiKeyRequest,
6664
new RestBuilderListener<InvalidateApiKeyResponse>(channel) {
6765
@Override
@@ -78,4 +76,43 @@ public RestResponse buildResponse(InvalidateApiKeyResponse invalidateResp,
7876
public String getName() {
7977
return "xpack_security_invalidate_api_key";
8078
}
79+
80+
private ConstructingObjectParser<InvalidateApiKeyRequest, Void> getObjectParser(RestRequest request) {
81+
if (request.getRestApiVersion() == RestApiVersion.V_7) {
82+
final ConstructingObjectParser<InvalidateApiKeyRequest, Void> objectParser = new ConstructingObjectParser<>(
83+
"invalidate_api_key_v7", a -> {
84+
final String id = (String) a[5];
85+
@SuppressWarnings("unchecked")
86+
final List<String> ids = (List<String>) a[4];
87+
if (id != null && ids != null) {
88+
throw new IllegalArgumentException("Must use either [id] or [ids], not both at the same time");
89+
}
90+
final String[] idsArray;
91+
if (Strings.hasText(id)) {
92+
idsArray = new String[] { id };
93+
} else if (ids != null) {
94+
idsArray = ids.toArray(String[]::new);
95+
} else {
96+
idsArray = null;
97+
}
98+
return new InvalidateApiKeyRequest((String) a[0], (String) a[1], (String) a[2],
99+
(a[3] == null) ? false : (Boolean) a[3], idsArray);
100+
});
101+
initObjectParser(objectParser, true);
102+
return objectParser;
103+
} else {
104+
return PARSER;
105+
}
106+
}
107+
108+
private static void initObjectParser(ConstructingObjectParser<InvalidateApiKeyRequest, Void> objectParser, boolean restCompatMode) {
109+
objectParser.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField("realm_name"));
110+
objectParser.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField("username"));
111+
objectParser.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField("name"));
112+
objectParser.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), new ParseField("owner"));
113+
objectParser.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), new ParseField("ids"));
114+
if (restCompatMode) {
115+
objectParser.declareString(ConstructingObjectParser.optionalConstructorArg(), new ParseField("id").withAllDeprecated("ids"));
116+
}
117+
}
81118
}

0 commit comments

Comments
 (0)