Skip to content

Commit 6ae725d

Browse files
authored
Fix bugs for unexpired API keys and id filtering (#76208)
This PR fixed an old bug and a new bug introduced #75335. Interestingly, the two bugs somewhat cancelled each other in tests. In addition, the test setup also contributed to the overall issue. The old bug is about filtering out expired API keys, but the relationship was wrong in the search query. The new bug is that _id field should be allowed in the index level for the new API key search API. Because of the old bug, the query always gets rewritten because the tests do not have any API keys that are expired before the query time. The query rewriting effectively bypasses the _id field check. Hence the new bug is not triggered.
1 parent 16189a9 commit 6ae725d

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/QueryApiKeyIT.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,15 @@ private Tuple<String, String> createApiKey(String name,
276276
XContentTestUtils.convertToXContent(roleDescriptors == null ? Map.of() : roleDescriptors, XContentType.JSON).utf8ToString();
277277
final String metadataString =
278278
XContentTestUtils.convertToXContent(metadata == null ? Map.of() : metadata, XContentType.JSON).utf8ToString();
279-
request.setJsonEntity("{\"name\":\"" + name
280-
+ "\", \"role_descriptors\":" + roleDescriptorsString
281-
+ ", \"metadata\":" + metadataString + "}");
279+
if (randomBoolean()) {
280+
request.setJsonEntity("{\"name\":\"" + name
281+
+ "\", \"role_descriptors\":" + roleDescriptorsString
282+
+ ", \"metadata\":" + metadataString + "}");
283+
} else {
284+
request.setJsonEntity("{\"name\":\"" + name
285+
+ "\", \"expiration\": \"10d\", \"role_descriptors\":" + roleDescriptorsString
286+
+ ", \"metadata\":" + metadataString + "}");
287+
}
282288
request.setOptions(request.getOptions().toBuilder().addHeader(HttpHeaders.AUTHORIZATION, authHeader));
283289
final Response response = client().performRequest(request);
284290
assertOK(response);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.security.authc.apikey;
9+
10+
import org.elasticsearch.common.settings.Settings;
11+
import org.elasticsearch.core.TimeValue;
12+
import org.elasticsearch.index.query.QueryBuilders;
13+
import org.elasticsearch.test.SecuritySingleNodeTestCase;
14+
import org.elasticsearch.xpack.core.XPackSettings;
15+
import org.elasticsearch.xpack.core.security.action.CreateApiKeyAction;
16+
import org.elasticsearch.xpack.core.security.action.CreateApiKeyRequest;
17+
import org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyAction;
18+
import org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyRequest;
19+
import org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyResponse;
20+
21+
import static org.hamcrest.Matchers.equalTo;
22+
23+
public class ApiKeySingleNodeTests extends SecuritySingleNodeTestCase {
24+
25+
@Override
26+
protected Settings nodeSettings() {
27+
Settings.Builder builder = Settings.builder().put(super.nodeSettings());
28+
builder.put(XPackSettings.API_KEY_SERVICE_ENABLED_SETTING.getKey(), true);
29+
return builder.build();
30+
}
31+
32+
public void testQueryWithExpiredKeys() throws InterruptedException {
33+
final String id1 = client().execute(CreateApiKeyAction.INSTANCE,
34+
new CreateApiKeyRequest("expired-shortly", null, TimeValue.timeValueMillis(1), null))
35+
.actionGet()
36+
.getId();
37+
final String id2 = client().execute(CreateApiKeyAction.INSTANCE,
38+
new CreateApiKeyRequest("long-lived", null, TimeValue.timeValueDays(1), null))
39+
.actionGet()
40+
.getId();
41+
Thread.sleep(10); // just to be 100% sure that the 1st key is expired when we search for it
42+
43+
final QueryApiKeyRequest queryApiKeyRequest = new QueryApiKeyRequest(QueryBuilders.idsQuery().addIds(id1, id2));
44+
final QueryApiKeyResponse queryApiKeyResponse = client().execute(QueryApiKeyAction.INSTANCE, queryApiKeyRequest).actionGet();
45+
assertThat(queryApiKeyResponse.getApiKeyInfos().length, equalTo(1));
46+
assertThat(queryApiKeyResponse.getApiKeyInfos()[0].getId(), equalTo(id2));
47+
}
48+
}

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
@@ -894,7 +894,7 @@ private void findApiKeys(final BoolQueryBuilder boolQuery, boolean filterOutInva
894894
}
895895
if (filterOutExpiredKeys) {
896896
final BoolQueryBuilder expiredQuery = QueryBuilders.boolQuery();
897-
expiredQuery.should(QueryBuilders.rangeQuery("expiration_time").lte(Instant.now().toEpochMilli()));
897+
expiredQuery.should(QueryBuilders.rangeQuery("expiration_time").gt(Instant.now().toEpochMilli()));
898898
expiredQuery.should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("expiration_time")));
899899
boolQuery.filter(expiredQuery);
900900
}

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/ApiKeyBoolQueryBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class ApiKeyBoolQueryBuilder extends BoolQueryBuilder {
3333

3434
// Field names allowed at the index level
3535
private static final Set<String> ALLOWED_EXACT_INDEX_FIELD_NAMES =
36-
Set.of("doc_type", "name", "api_key_invalidated", "creation_time", "expiration_time");
36+
Set.of("_id", "doc_type", "name", "api_key_invalidated", "creation_time", "expiration_time");
3737

3838
private ApiKeyBoolQueryBuilder() {}
3939

0 commit comments

Comments
 (0)