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 for ApiKeyIntegTests related to Expired API keys remover #43477

Merged
merged 1 commit into from
Oct 2, 2019

Conversation

bizybot
Copy link
Contributor

@bizybot bizybot commented Jun 21, 2019

When API key is invalidated we do two things first it tries to trigger ExpiredApiKeysRemover task
and second, we do index the invalidation for the API key. The index invalidation may happen
before the ExpiredApiKeysRemover task is run and in that case, the API key
invalidated will also get deleted. If the ExpiredApiKeysRemover runs before the
API key invalidation is indexed then the API key is not deleted and will be
deleted in the future run.
This behavior was not captured in the tests related to ExpiredApiKeysRemover
causing intermittent failures.
This commit fixes those tests by checking if the API key invalidated is reported
back when we get API keys after invalidation and perform the checks based on that.

Closes #41747

When API key is invalidated we do two things first it tries to trigger `ExpiredApiKeysRemover` task
and second, we do index the invalidation for the API key. The index invalidation may happen
before the `ExpiredApiKeysRemover` task is run and in that case, the API key
invalidated will also get deleted. If the `ExpiredApiKeysRemover` runs before the
API key invalidation is indexed then the API key is not deleted and will be
deleted in the future run.
This behavior was not captured in the tests related to `ExpiredApiKeysRemover`
causing intermittent failures.
This commit fixes those tests by checking if the API key invalidated is reported
back when we get API keys after invalidation and perform the checks based on that.

Closes elastic#41747
@bizybot bizybot added >test Issues or PRs that are addressing/adding tests :Security/Authentication Logging in, Usernames/passwords, Realms (Native/LDAP/AD/SAML/PKI/etc) v8.0.0 v6.7.3 v7.3.0 v6.8.2 labels Jun 21, 2019
@bizybot bizybot requested a review from albertzaharovits June 21, 2019 10:07
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-security

Copy link
Contributor

@albertzaharovits albertzaharovits left a comment

Choose a reason for hiding this comment

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

The index invalidation may happen
before the ExpiredApiKeysRemover task is run and in that case, the API key
invalidated will also get deleted.

I believe this is not true. And I see the changes in the tests reflect this thinking.

In ExpiredApiKeysRemover there is this condition
.should(QueryBuilders.rangeQuery("expiration_time").lte(now.minus(EXPIRED_API_KEYS_RETENTION_PERIOD).toEpochMilli()))
such that expired API keys will not immediately be removed.
And I believe the tests don't change this?

@bizybot
Copy link
Contributor Author

bizybot commented Jun 24, 2019

The index invalidation may happen
before the ExpiredApiKeysRemover task is run and in that case, the API key
invalidated will also get deleted.

I believe this is not true. And I see the changes in the tests reflect this thinking.

In ExpiredApiKeysRemover there is this condition
.should(QueryBuilders.rangeQuery("expiration_time").lte(now.minus(EXPIRED_API_KEYS_RETENTION_PERIOD).toEpochMilli()))
such that expired API keys will not immediately be removed.
And I believe the tests don't change this?

Hi @albertzaharovits Thank you for your comments.
The behavior is slightly different from what you see,

.setQuery(QueryBuilders.boolQuery()
    .filter(QueryBuilders.termsQuery("doc_type", "api_key"))
    .should(QueryBuilders.termsQuery("api_key_invalidated", true))
    .should(QueryBuilders.rangeQuery("expiration_time")
                  .lte(now.minus(EXPIRED_API_KEYS_RETENTION_PERIOD).toEpochMilli()))
    .minimumShouldMatch(1)

The expiration_time can be null (for API keys with no expiration) and the minimumShouldMatch
means at least one out of the 2 should clause must match. So if the indexing of the invalidate happens before the task is run and if the API key does not expire then it will be deleted by the ExpiredApiKeysRemover as api_key_invalidated would be true. The task may run before the indexing of invalidating happens and in that case, the API key would not be deleted.
As we do not know or there is no way to identify what happened, we have changed the test such that if the returned results contain or miss the recent invalidated API key. Hope this helps. Thank you.

@albertzaharovits
Copy link
Contributor

The expiration_time can be null (for API keys with no expiration) and the minimumShouldMatch
means at least one out of the 2 should clause must match.

Ooops! You're right! I completely misunderstood the condition there. Good catch in the tests!

So, invalidated keys are removed as soon as possible but expired ones have a 7 days (configurable) grace period before they are deleted. I believe the reason for the grace period is for audit & diagnostic purposes (a service stops working some day, with its API key non-existent; did the API key existed and and was deleted automatically, or it never existed and the client must have messed up its credentials).

Should invalidated API keys also have the same grace period? I believe this might have been discussed before? Invalidation is an explicit action that we audit so this is a strong reason no to; but we don't audit or otherwise trace specifically which API key got invalidated.

Alternatively, instead of marking them as invalidated and then having a periodic job do the removal, could we just remove them?

I am proposing this only because the changes required in tests look unnecessary complicated (but correct).

@jpountz jpountz added v7.4.0 and removed v7.3.0 labels Jul 3, 2019
@bizybot
Copy link
Contributor Author

bizybot commented Jul 11, 2019

Hi, @albertzaharovits,

Thank you for your comments. I see your point and to do that we will need to modify the existing behavior which we would not be able to do for older released versions where the test is also failing.
On the other channel, we discussed another issue that we found where we would want to trigger the API keys/ Token remover on every API instead of only being done on invalidation.
I think as we would not be able to backport these changes (as they are bug fix/enhancement) to released versions, I think we should fix the test behavior for now and backport them.

I can create another issue for tracking the other changes that we discussed, wdyt?

@jakelandis jakelandis added v6.8.3 and removed v6.8.2 labels Jul 24, 2019
@bizybot bizybot removed the v7.4.0 label Aug 28, 2019
@pcsanwald pcsanwald added v6.8.4 and removed v6.8.3 labels Aug 29, 2019
@bizybot bizybot added the v7.5.0 label Sep 10, 2019
@jkakavas
Copy link
Member

@bizybot I think we're good to merge this and you can open the follow up issue to discuss the invalidation grace period ?

Copy link
Contributor

@albertzaharovits albertzaharovits left a comment

Choose a reason for hiding this comment

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

LGTM

@bizybot bizybot merged commit e15d2e0 into elastic:master Oct 2, 2019
bizybot added a commit to bizybot/elasticsearch that referenced this pull request Oct 4, 2019
…#43477)

When API key is invalidated we do two things first it tries to trigger `ExpiredApiKeysRemover` task
and second, we do index the invalidation for the API key. The index invalidation may happen
before the `ExpiredApiKeysRemover` task is run and in that case, the API key
invalidated will also get deleted. If the `ExpiredApiKeysRemover` runs before the
API key invalidation is indexed then the API key is not deleted and will be
deleted in the future run.
This behavior was not captured in the tests related to `ExpiredApiKeysRemover`
causing intermittent failures.
This commit fixes those tests by checking if the API key invalidated is reported
back when we get API keys after invalidation and perform the checks based on that.

Closes elastic#41747
bizybot added a commit to bizybot/elasticsearch that referenced this pull request Oct 4, 2019
…#43477)

When API key is invalidated we do two things first it tries to trigger `ExpiredApiKeysRemover` task
and second, we do index the invalidation for the API key. The index invalidation may happen
before the `ExpiredApiKeysRemover` task is run and in that case, the API key
invalidated will also get deleted. If the `ExpiredApiKeysRemover` runs before the
API key invalidation is indexed then the API key is not deleted and will be
deleted in the future run.
This behavior was not captured in the tests related to `ExpiredApiKeysRemover`
causing intermittent failures.
This commit fixes those tests by checking if the API key invalidated is reported
back when we get API keys after invalidation and perform the checks based on that.

Closes elastic#41747
bizybot added a commit that referenced this pull request Oct 4, 2019
…#47546)

When API key is invalidated we do two things first it tries to trigger `ExpiredApiKeysRemover` task
and second, we do index the invalidation for the API key. The index invalidation may happen
before the `ExpiredApiKeysRemover` task is run and in that case, the API key
invalidated will also get deleted. If the `ExpiredApiKeysRemover` runs before the
API key invalidation is indexed then the API key is not deleted and will be
deleted in the future run.
This behavior was not captured in the tests related to `ExpiredApiKeysRemover`
causing intermittent failures.
This commit fixes those tests by checking if the API key invalidated is reported
back when we get API keys after invalidation and perform the checks based on that.

Closes #41747
bizybot added a commit that referenced this pull request Oct 4, 2019
…#47547)

When API key is invalidated we do two things first it tries to trigger `ExpiredApiKeysRemover` task
and second, we do index the invalidation for the API key. The index invalidation may happen
before the `ExpiredApiKeysRemover` task is run and in that case, the API key
invalidated will also get deleted. If the `ExpiredApiKeysRemover` runs before the
API key invalidation is indexed then the API key is not deleted and will be
deleted in the future run.
This behavior was not captured in the tests related to `ExpiredApiKeysRemover`
causing intermittent failures.
This commit fixes those tests by checking if the API key invalidated is reported
back when we get API keys after invalidation and perform the checks based on that.

Closes #41747
bizybot added a commit to bizybot/elasticsearch that referenced this pull request Oct 6, 2019
…#43477) (elastic#47547)

When API key is invalidated we do two things first it tries to trigger `ExpiredApiKeysRemover` task
and second, we do index the invalidation for the API key. The index invalidation may happen
before the `ExpiredApiKeysRemover` task is run and in that case, the API key
invalidated will also get deleted. If the `ExpiredApiKeysRemover` runs before the
API key invalidation is indexed then the API key is not deleted and will be
deleted in the future run.
This behavior was not captured in the tests related to `ExpiredApiKeysRemover`
causing intermittent failures.
This commit fixes those tests by checking if the API key invalidated is reported
back when we get API keys after invalidation and perform the checks based on that.

Closes elastic#41747
bizybot added a commit that referenced this pull request Oct 6, 2019
…#47547) (#47628)

When API key is invalidated we do two things first it tries to trigger `ExpiredApiKeysRemover` task
and second, we do index the invalidation for the API key. The index invalidation may happen
before the `ExpiredApiKeysRemover` task is run and in that case, the API key
invalidated will also get deleted. If the `ExpiredApiKeysRemover` runs before the
API key invalidation is indexed then the API key is not deleted and will be
deleted in the future run.
This behavior was not captured in the tests related to `ExpiredApiKeysRemover`
causing intermittent failures.
This commit fixes those tests by checking if the API key invalidated is reported
back when we get API keys after invalidation and perform the checks based on that.

Closes #41747
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Security/Authentication Logging in, Usernames/passwords, Realms (Native/LDAP/AD/SAML/PKI/etc) >test Issues or PRs that are addressing/adding tests v6.7.3 v6.8.4 v7.5.0 v8.0.0-alpha1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CI] ApiKeyIntegTests#testExpiredApiKeysBehaviorWhenKeysExpired1WeekBeforeAnd1DayBefore fails
7 participants