Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs for unexpired API keys and id filtering #76208

Merged
merged 2 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,15 @@ private Tuple<String, String> createApiKey(String name,
XContentTestUtils.convertToXContent(roleDescriptors == null ? Map.of() : roleDescriptors, XContentType.JSON).utf8ToString();
final String metadataString =
XContentTestUtils.convertToXContent(metadata == null ? Map.of() : metadata, XContentType.JSON).utf8ToString();
request.setJsonEntity("{\"name\":\"" + name
+ "\", \"role_descriptors\":" + roleDescriptorsString
+ ", \"metadata\":" + metadataString + "}");
if (randomBoolean()) {
request.setJsonEntity("{\"name\":\"" + name
+ "\", \"role_descriptors\":" + roleDescriptorsString
+ ", \"metadata\":" + metadataString + "}");
} else {
request.setJsonEntity("{\"name\":\"" + name
+ "\", \"expiration\": \"10d\", \"role_descriptors\":" + roleDescriptorsString
+ ", \"metadata\":" + metadataString + "}");
}
request.setOptions(request.getOptions().toBuilder().addHeader(HttpHeaders.AUTHORIZATION, authHeader));
final Response response = client().performRequest(request);
assertOK(response);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.security.authc.apikey;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.test.SecuritySingleNodeTestCase;
import org.elasticsearch.xpack.core.XPackSettings;
import org.elasticsearch.xpack.core.security.action.CreateApiKeyAction;
import org.elasticsearch.xpack.core.security.action.CreateApiKeyRequest;
import org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyAction;
import org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyRequest;
import org.elasticsearch.xpack.core.security.action.apikey.QueryApiKeyResponse;

import static org.hamcrest.Matchers.equalTo;

public class ApiKeySingleNodeTests extends SecuritySingleNodeTestCase {

@Override
protected Settings nodeSettings() {
Settings.Builder builder = Settings.builder().put(super.nodeSettings());
builder.put(XPackSettings.API_KEY_SERVICE_ENABLED_SETTING.getKey(), true);
return builder.build();
}

public void testQueryWithExpiredKeys() {
final String id1 = client().execute(CreateApiKeyAction.INSTANCE,
new CreateApiKeyRequest("expired-shortly", null, TimeValue.timeValueMillis(1), null))
.actionGet()
.getId();
final String id2 = client().execute(CreateApiKeyAction.INSTANCE,
new CreateApiKeyRequest("long-lived", null, TimeValue.timeValueDays(1), null))
.actionGet()
.getId();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we ensure the clock has ticked here?
It should but eventually someone will run this on a machine that is so fast that the key hasn't expired by the time we search.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the 1st key expires in just 1ms, the machine needs to be super fast and lucky to finish creating the 2nd key (hashing and wait_for) within that time frame. That said, it does not hurt to add a sleep for 10ms so I did it.

final QueryApiKeyRequest queryApiKeyRequest = new QueryApiKeyRequest(QueryBuilders.idsQuery().addIds(id1, id2));
final QueryApiKeyResponse queryApiKeyResponse = client().execute(QueryApiKeyAction.INSTANCE, queryApiKeyRequest).actionGet();
assertThat(queryApiKeyResponse.getApiKeyInfos().length, equalTo(1));
assertThat(queryApiKeyResponse.getApiKeyInfos()[0].getId(), equalTo(id2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ private void findApiKeys(final BoolQueryBuilder boolQuery, boolean filterOutInva
}
if (filterOutExpiredKeys) {
final BoolQueryBuilder expiredQuery = QueryBuilders.boolQuery();
expiredQuery.should(QueryBuilders.rangeQuery("expiration_time").lte(Instant.now().toEpochMilli()));
expiredQuery.should(QueryBuilders.rangeQuery("expiration_time").gt(Instant.now().toEpochMilli()));
Copy link
Member Author

@ywangd ywangd Aug 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the old bug. But this piece of code was never exercised till now.

expiredQuery.should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("expiration_time")));
boolQuery.filter(expiredQuery);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class ApiKeyBoolQueryBuilder extends BoolQueryBuilder {

// Field names allowed at the index level
private static final Set<String> ALLOWED_EXACT_INDEX_FIELD_NAMES =
Set.of("doc_type", "name", "api_key_invalidated", "creation_time", "expiration_time");
Set.of("_id", "doc_type", "name", "api_key_invalidated", "creation_time", "expiration_time");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new bug.


private ApiKeyBoolQueryBuilder() {}

Expand Down