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

Handle access tokens that expire after authentication stage. #122155

Merged
merged 3 commits into from
Jan 25, 2022

Conversation

azasypkin
Copy link
Member

@azasypkin azasypkin commented Dec 30, 2021

Summary

In this PR we introduce a new Authenticator.reauthenticate method that is invoked in a Core's setUnauthorizedErrorHandler handler to refresh user's access token if it's expired after authentication stage.

Fixes: #104893


Release note: Previously long-running requests could lead to sporadic logouts in certain cases even though user's session was still active. It shouldn't be the case now.

@azasypkin azasypkin added release_note:fix Team:Security Team focused on: Auth, Users, Roles, Spaces, Audit Logging, and more! Feature:Security/Authentication Platform Security - Authentication v8.0.0 auto-backport Deprecated - use backport:version if exact versions are needed v8.1.0 skip-ci v7.17.0 labels Dec 30, 2021
@azasypkin azasypkin force-pushed the issue-104893-acess-token-refresh branch 2 times, most recently from 306ab6d to b84d178 Compare January 4, 2022 13:32
@azasypkin azasypkin force-pushed the issue-104893-acess-token-refresh branch 2 times, most recently from f9c8b6d to 7b31989 Compare January 19, 2022 14:30
@azasypkin azasypkin force-pushed the issue-104893-acess-token-refresh branch from 7b31989 to 3cd3617 Compare January 19, 2022 14:32
@azasypkin azasypkin marked this pull request as ready for review January 19, 2022 15:52
@azasypkin azasypkin requested a review from a team as a code owner January 19, 2022 15:52
@elasticmachine
Copy link
Contributor

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

Copy link
Contributor

@jportner jportner left a comment

Choose a reason for hiding this comment

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

I have just a couple of nits, really scraping the bottom of the barrel here.
Nice job!


const existingSessionValue = await this.getSessionValue(request);
if (!existingSessionValue) {
this.logger.warn('It is not possible to extend session since it is no longer available.');
Copy link
Contributor

Choose a reason for hiding this comment

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

I understand that a successful re-authentication always attempts to extend the session. However, if I was reading through the logs, I think I would find the log messages a bit confusing:

WARN: Re-authenticating request due to error: blah
WARN: It is not possible to extend session since it is no longer available.

Perhaps we could reword this to make it a bit clearer what's happening. WDYT about this?

Suggested change
this.logger.warn('It is not possible to extend session since it is no longer available.');
this.logger.warn('Session is no longer available and cannot be re-authenticated.');

Copy link
Member Author

Choose a reason for hiding this comment

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

Perhaps we could reword this to make it a bit clearer what's happening. WDYT about this?

Yeah, sounds better, thanks

Comment on lines +422 to +423
// We can ignore `undefined` value here since it's ruled out on the previous step, if provider isn't
// available then `getSessionValue` should have returned `null`.
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 I appreciate the comment here

return toolkit.notHandled();
}

this.logger.warn(`Re-authenticating request due to error: ${getDetailedErrorMessage(error)}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

We're seeing this behavior pretty regularly for some users, and AFAIK there's nothing "wrong" with their configuration. So maybe this should be a debug log level instead of warn?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that's good point - it was useful during development, but can be quite confusing in production.

expect(reauthenticate).not.toHaveBeenCalled();
});

it('does not handle error when security is disabled in elasticsearch.', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: this is just defensive coding, we don't really ever expect a 401 error if security is disabled in ES, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct, just covering if code branches in the hook, but we shouldn't get 401 here. Unless security was disabled when request reached Kibana, then application handler was busy with something, in the meantime ES enabled security, and only then application handler decided to query Elasticsearch and obviously got 401 - 0.00001% chance 🙈

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

✅ unchanged

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@azasypkin azasypkin merged commit 0491214 into elastic:main Jan 25, 2022
@azasypkin azasypkin deleted the issue-104893-acess-token-refresh branch January 25, 2022 11:15
@kibanamachine
Copy link
Contributor

💔 All backports failed

Status Branch Result
8.0 Backport failed because of merge conflicts

How to fix

Re-run the backport manually:

node scripts/backport --pr 122155

Questions ?

Please refer to the Backport tool documentation

azasypkin added a commit that referenced this pull request Jan 25, 2022
#123703)

(cherry picked from commit 0491214)

# Conflicts:
#	x-pack/test/security_api_integration/tests/saml/saml_login.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Deprecated - use backport:version if exact versions are needed Feature:Security/Authentication Platform Security - Authentication release_note:fix Team:Security Team focused on: Auth, Users, Roles, Spaces, Audit Logging, and more! v8.0.0 v8.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle access tokens that expire after authentication stage
5 participants