-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
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.
- Loading branch information
Showing
4 changed files
with
59 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...ClusterTest/java/org/elasticsearch/xpack/security/authc/apikey/ApiKeySingleNodeTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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() throws InterruptedException { | ||
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(); | ||
Thread.sleep(10); // just to be 100% sure that the 1st key is expired when we search for 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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters