Skip to content

Conversation

@azasypkin
Copy link
Member

@azasypkin azasypkin commented Mar 25, 2019

Always refresh access token if authentication fails with 401 (and temporarily with 500: 'token document is missing and must be present), and always re-initiate authentication if refresh token fails with 400, no matter what the underlying reason is.

Blocked by #33774

Fixes #33646 and #22905

"Release Note: Kibana now automatically re-initiates login when session access/refresh token pair used for Token and SAML authentication is removed from Elasticsearch (typically after 24 hours of user inactivity)."

@azasypkin azasypkin added blocked Team:Security Platform Security: Auth, Users, Roles, Spaces, Audit Logging, etc t// enhancement New value added to drive a business result v7.2.0 labels Mar 25, 2019
@azasypkin azasypkin requested a review from a team as a code owner March 25, 2019 13:13
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-security

@elasticmachine

This comment has been minimized.

@azasypkin azasypkin force-pushed the issue-33646-400-and-401 branch from b1e6272 to a62a9b2 Compare March 25, 2019 14:29
@elasticmachine

This comment has been minimized.

Copy link
Contributor

@spalger spalger Mar 25, 2019

Choose a reason for hiding this comment

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

Need to merge master and rewrite this import before merging: #33761

Copy link
Member Author

Choose a reason for hiding this comment

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

Will do, thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

If I'm following along correctly, the following tests should be updated as well:

  • fails if state contains invalid credentials. line 179
  • fails when session contains a rejected token line 285

When submitting a request to _security/_authenticate with an invalid Bearer header, we're getting back a 401 as well, which will match the new isAccessTokenExpiredError and attempt to use the refresh token to get a new access token. Previously, we weren't trying to use the refresh tokens in this situation.

Copy link
Member Author

Choose a reason for hiding this comment

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

fails if state contains invalid credentials. line 179
fails when session contains a rejected token line 285

Hmm, right, but If we change these tests, then they will be exactly the same as succeeds with valid session even if requiring a token refresh, line 134 or redirects non-AJAX requests to /login and clears session if token refresh fails with 400 error' line 360, depending on what happens during refreshing.

It feels like we can either remove these tests completely or change them to test non-401 errors (e.g. 500), what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

I like the idea of adding tests for the 500 situation

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, will do then!

Copy link
Contributor

Choose a reason for hiding this comment

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

Save question as above...

Is it possible that when we do the access token logic, we get back the 401 because the token document exists and it's expired, and then before we get here the refresh token is deleted and we get back a 500 with the message body denoting the token being deleted?

Copy link
Member Author

Choose a reason for hiding this comment

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

See reply above, tl;dr it shouldn't be possible.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for looking into this!

Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as above about logging the error message.

Copy link
Member Author

Choose a reason for hiding this comment

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

We log all error messages right at the beginning of the catch :) Or you think just logging message isn't enough? Potentially the error can have body.error.reason or body.error.error_description, but I'm not sure if we want to somehow extract and log these....

Copy link
Contributor

Choose a reason for hiding this comment

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

I think just err.message is fine, I completely missed that we did so above. Please ignore!

… re-initiate SAML handshake for any refresh token error with 400 status code.
@azasypkin azasypkin force-pushed the issue-33646-400-and-401 branch from a62a9b2 to 8830007 Compare March 26, 2019 12:31
@azasypkin azasypkin removed the blocked label Mar 26, 2019
@azasypkin
Copy link
Member Author

Had to force-push after rebase on master, but nothing has changed and won't do it for this PR anymore.

@elasticmachine

This comment has been minimized.

expect(authenticationResult.notHandled()).to.be(true);
});

it('fails if state contains invalid credentials.', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

note: we had a similar one below, that I've repurposed to test 500 errors.

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@azasypkin azasypkin requested a review from kobelb March 27, 2019 10:41
@azasypkin azasypkin added the Feature:Security/Authentication Platform Security - Authentication label Mar 27, 2019
@azasypkin azasypkin merged commit 411a3fb into elastic:master Mar 27, 2019
@azasypkin azasypkin deleted the issue-33646-400-and-401 branch March 27, 2019 14:09
azasypkin added a commit to azasypkin/kibana that referenced this pull request Mar 27, 2019
… re-authentication user for any refresh token error with 400 status code. (elastic#33777)

* Use refresh token for any access token error with 401 status code and re-initiate SAML handshake for any refresh token error with 400 status code.

* Switch from `expect.js` to `@kbn/expect`.

* Review#1: repurpose redundant tests to test token failures with 500 code.
@azasypkin
Copy link
Member Author

azasypkin commented Mar 27, 2019

6.8/6.8.8: 34147e8
7.x/7.1.0: 6011692

joelgriffith pushed a commit that referenced this pull request Mar 27, 2019
… re-authentication user for any refresh token error with 400 status code. (#33777)

* Use refresh token for any access token error with 401 status code and re-initiate SAML handshake for any refresh token error with 400 status code.

* Switch from `expect.js` to `@kbn/expect`.

* Review#1: repurpose redundant tests to test token failures with 500 code.
@LeeDr LeeDr mentioned this pull request Mar 18, 2020
1 task
@kobelb kobelb removed the enhancement New value added to drive a business result label Mar 18, 2020
@azasypkin azasypkin changed the title Use refresh token for any access token error with 401 status code and re-authentication user for any refresh token error with 400 status code. Use refresh token for any access token error with 401 status code and re-authenticate user for any refresh token error with 400 status code. Mar 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backported Feature:Security/Authentication Platform Security - Authentication release_note:fix review Team:Security Platform Security: Auth, Users, Roles, Spaces, Audit Logging, etc t// v6.8.8 v7.2.0

Projects

None yet

4 participants