Skip to content

Fix bugs for unexpired API keys and id filtering#76208

Merged
ywangd merged 2 commits intoelastic:masterfrom
ywangd:filter-out-expired-keys-bug
Aug 9, 2021
Merged

Fix bugs for unexpired API keys and id filtering#76208
ywangd merged 2 commits intoelastic:masterfrom
ywangd:filter-out-expired-keys-bug

Conversation

@ywangd
Copy link
Copy Markdown
Member

@ywangd ywangd commented Aug 6, 2021

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.

I am tagging this PR as >non-issue because the code having the old bug was never used till now and the new bug has not been released yet.

@ywangd ywangd added >non-issue :Security/Security Security issues without another label v7.15.0 v8.0.0-alpha1 labels Aug 6, 2021
@ywangd ywangd requested a review from tvernum August 6, 2021 12:02
@elasticmachine elasticmachine added the Team:Security Meta label for security team label Aug 6, 2021
@elasticmachine
Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-security (Team:Security)

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
Copy Markdown
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.

// 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
Copy Markdown
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.

new CreateApiKeyRequest("long-lived", null, TimeValue.timeValueDays(1), null))
.actionGet()
.getId();

Copy link
Copy Markdown
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
Copy Markdown
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.

@ywangd ywangd added the auto-backport Automatically create backport pull requests when merged label Aug 9, 2021
@ywangd ywangd merged commit 6ae725d into elastic:master Aug 9, 2021
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

💔 Backport failed

Status Branch Result
7.x Commit could not be cherrypicked due to conflicts

To backport manually run:
backport --pr 76208

ywangd added a commit to ywangd/elasticsearch that referenced this pull request Aug 9, 2021
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.
ywangd added a commit that referenced this pull request Aug 9, 2021
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-backport Automatically create backport pull requests when merged >non-issue :Security/Security Security issues without another label Team:Security Meta label for security team v7.15.0 v8.0.0-alpha2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants