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

UI: fix token expiry banner for batch tokens #27479

Merged
merged 11 commits into from
Jun 28, 2024

Conversation

andaley
Copy link
Contributor

@andaley andaley commented Jun 13, 2024

Description

Ensures the "token expired" banner displays in the UI when logged in via a batch token, regardless of whether that token was generated via an auth method or via vault token create directly.

Previously the banner would not display for users logged in with a batch token that was generated by an auth method.

Screenshot

Batch tokens show token expiry banner

image

Service tokens show token expiring soon banner

image

Service tokens show token expiry banner

TODO only if you're a HashiCorp employee

  • Labels: If this PR is the CE portion of an ENT change, and that ENT change is
    getting backported to N-2, use the new style backport/ent/x.x.x+ent labels
    instead of the old style backport/x.x.x labels.
  • Labels: If this PR is a CE only change, it can only be backported to N, so use
    the normal backport/x.x.x label (there should be only 1).
  • ENT Breakage: If this PR either 1) removes a public function OR 2) changes the signature
    of a public function, even if that change is in a CE file, double check that
    applying the patch for this PR to the ENT repo and running tests doesn't
    break any tests. Sometimes ENT only tests rely on public functions in CE
    files.
  • Jira: If this change has an associated Jira, it's referenced either
    in the PR description, commit message, or branch name.
  • RFC: If this change has an associated RFC, please link it in the description.
  • ENT PR: If this change has an associated ENT PR, please link it in the
    description. Also, make sure the changelog is in this PR, not in your ENT PR.

@github-actions github-actions bot added the hashicorp-contributed-pr If the PR is HashiCorp (i.e. not-community) contributed label Jun 13, 2024
Comment on lines 458 to 457
// this is the problem -- the auth/userpass/login/ endpoint returns a diff response than self-lookup. it doesn't include anything about expiration for batch tokens

console.log('authenticate resp: ', resp);

return this.authSuccess(options, resp.auth || resp.data);
Copy link
Contributor Author

@andaley andaley Jun 13, 2024

Choose a reason for hiding this comment

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

i believe this is the root of the problem. depending on what login method the user is using (token, userpass, LDAP etc), we hit a different API endpoint, and those apis have a different response shape. note the difference between the various responses:

  1. a GET /auth/token/lookup-self (using a batch token created via vault token create -type=batch -policy=test -ttl=20m) responds with type: "batch" and expiry_time properties. API docs confirm this.
  2. a POST to /auth/userpass/login (using a username and password with a batch token) responds with a property called token_type: "batch" and lease_duration but doesn't include an explicit expiry_time. API docs confirm this, too
endpointresponse
/auth/token/lookup-self Screenshot 2024-06-12 at 7 16 15 PM Screenshot 2024-06-12 at 7 16 27 PM
/auth/userpass/login Screenshot 2024-06-12 at 7 14 07 PM Screenshot 2024-06-12 at 7 14 16 PM

the frontend code on line 308 above suggests to me that we expect the backend responses to be the same regardless of whether the batch token was created via an auth method or via vault token create -type=batch -policy=test.

Copy link

github-actions bot commented Jun 13, 2024

CI Results:
All Go tests succeeded! ✅

Comment on lines -299 to -300
if (resp.renewable) {
Object.assign(data, this.calculateExpiration(resp));
Copy link
Contributor Author

@andaley andaley Jun 13, 2024

Choose a reason for hiding this comment

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

the this.calculateExpiration function is already set up to handle batch tokens returned by /auth/[backend]/login methods since those APIs return a lease_duration, which is accounted for on line 220 above.

as a result, i didn't see a reason why we wouldn't couldn't call calculateExpiration regardless of whether or not resp.renewable or not.

so you'll notice i modified this.calculateExpiration to handle the case where resp.type === 'batch' (which happens when logging in via a token directly). this way calculateExpiration is used universally, regardless of the authentication method (token/userpass/ldap, batch token, service token etc).

Copy link
Contributor

@hashishaw hashishaw left a comment

Choose a reason for hiding this comment

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

This looks like a great approach! I think we just need to do some manual testing for various login expiry flows (unfortunately those are difficult to test automatically)

ui/app/services/auth.js Show resolved Hide resolved
ui/app/services/auth.js Outdated Show resolved Hide resolved
@andaley andaley force-pushed the ui/vault-27813/fix-token-banner branch from 602a557 to 26f3a3f Compare June 18, 2024 01:12
@andaley andaley marked this pull request as ready for review June 18, 2024 01:12
@andaley andaley requested a review from a team as a code owner June 18, 2024 01:12
@andaley andaley changed the title WIP UI: fix token expiry banner for batch tokens UI: fix token expiry banner for batch tokens Jun 18, 2024
@andaley andaley added the ui label Jun 18, 2024
@andaley andaley added this to the 1.17.1 milestone Jun 18, 2024
@andaley andaley added backport/ent/1.15.x+ent backport/ent/1.16.x+ent Changes are backported to 1.16.x+ent labels Jun 18, 2024
Copy link

Build Results:
All builds succeeded! ✅

@andaley andaley force-pushed the ui/vault-27813/fix-token-banner branch from 26f3a3f to 8acb2b2 Compare June 18, 2024 01:21
Copy link
Contributor

@hashishaw hashishaw left a comment

Choose a reason for hiding this comment

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

Elegant solution and beautiful test coverage! ✨ thank you!

changelog/27479.txt Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/ent/1.16.x+ent Changes are backported to 1.16.x+ent hashicorp-contributed-pr If the PR is HashiCorp (i.e. not-community) contributed ui
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants