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

Conversation

ywangd
Copy link
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
Collaborator

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

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.

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.

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.

@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
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