Skip to content

Commit cbddb5a

Browse files
committed
Fix bugs for unexpired API keys and id filtering (elastic#76208)
This PR fixed an old bug and a new bug introduced elastic#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 cee82e9 commit cbddb5a

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
@@ -279,9 +279,15 @@ private Tuple<String, String> createApiKey(String name,
279279
roleDescriptors == null ? org.elasticsearch.core.Map.of() : roleDescriptors, XContentType.JSON).utf8ToString();
280280
final String metadataString = XContentTestUtils.convertToXContent(
281281
metadata == null ? org.elasticsearch.core.Map.of() : metadata, XContentType.JSON).utf8ToString();
282-
request.setJsonEntity("{\"name\":\"" + name
283-
+ "\", \"role_descriptors\":" + roleDescriptorsString
284-
+ ", \"metadata\":" + metadataString + "}");
282+
if (randomBoolean()) {
283+
request.setJsonEntity("{\"name\":\"" + name
284+
+ "\", \"role_descriptors\":" + roleDescriptorsString
285+
+ ", \"metadata\":" + metadataString + "}");
286+
} else {
287+
request.setJsonEntity("{\"name\":\"" + name
288+
+ "\", \"expiration\": \"10d\", \"role_descriptors\":" + roleDescriptorsString
289+
+ ", \"metadata\":" + metadataString + "}");
290+
}
285291
request.setOptions(request.getOptions().toBuilder().addHeader(HttpHeaders.AUTHORIZATION, authHeader));
286292
final Response response = client().performRequest(request);
287293
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
@@ -928,7 +928,7 @@ private void findApiKeys(final BoolQueryBuilder boolQuery, boolean filterOutInva
928928
}
929929
if (filterOutExpiredKeys) {
930930
final BoolQueryBuilder expiredQuery = QueryBuilders.boolQuery();
931-
expiredQuery.should(QueryBuilders.rangeQuery("expiration_time").lte(Instant.now().toEpochMilli()));
931+
expiredQuery.should(QueryBuilders.rangeQuery("expiration_time").gt(Instant.now().toEpochMilli()));
932932
expiredQuery.should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("expiration_time")));
933933
boolQuery.filter(expiredQuery);
934934
}

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-
org.elasticsearch.core.Set.of("doc_type", "name", "api_key_invalidated", "creation_time", "expiration_time");
36+
org.elasticsearch.core.Set.of("_id", "doc_type", "name", "api_key_invalidated", "creation_time", "expiration_time");
3737

3838
private ApiKeyBoolQueryBuilder() {}
3939

0 commit comments

Comments
 (0)