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: don't drop active_user on expired tokens [MLG-1494] #8653

Merged
merged 1 commit into from
Jan 10, 2024

Conversation

rb-determined-ai
Copy link
Member

The authentication logic would drop expired tokens, and then ask for a password and, if that worked, add the fresh token back to the token store.

However, the initial token drop had a side-effect of dropping the active user, which was not being restored.

After this fix, the TokenStore doesn't try to be so smart. Instead, the logout-related CLI commands will explicitly clear the active user, but tokens dropped due to expiration are unaffected.

Test Plan

  • automated test coverage

Copy link

netlify bot commented Jan 5, 2024

Deploy Preview for determined-ui ready!

Name Link
🔨 Latest commit 641977d
🔍 Latest deploy log https://app.netlify.com/sites/determined-ui/deploys/659ddce98490b400085af0b3
😎 Deploy Preview https://deploy-preview-8653--determined-ui.netlify.app/
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

codecov bot commented Jan 5, 2024

Codecov Report

Attention: 4 lines in your changes are missing coverage. Please review.

Comparison is base (ab08345) 51.45% compared to head (641977d) 51.46%.
Report is 13 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #8653   +/-   ##
=======================================
  Coverage   51.45%   51.46%           
=======================================
  Files         882      884    +2     
  Lines      156065   156101   +36     
  Branches     2085     2085           
=======================================
+ Hits        80304    80336   +32     
- Misses      74270    74273    +3     
- Partials     1491     1492    +1     
Flag Coverage Δ
backend 36.09% <ø> (-0.01%) ⬇️
harness 64.32% <95.00%> (+0.02%) ⬆️
web 53.76% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
harness/tests/cli/test_auth.py 93.29% <100.00%> (-1.29%) ⬇️
harness/tests/common/api/test_authentication.py 100.00% <100.00%> (ø)
harness/tests/common/api/test_certs.py 100.00% <100.00%> (ø)
harness/tests/cli/util.py 87.03% <85.71%> (-0.22%) ⬇️
harness/determined/common/api/authentication.py 78.13% <50.00%> (-0.06%) ⬇️

... and 2 files with indirect coverage changes

Copy link
Contributor

@wes-turner wes-turner left a comment

Choose a reason for hiding this comment

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

Better 1 / better 2, this is clearly better. It's also a nice, small fix.

Extra credit if you want extra credit is embedded in comments.

This fix would have taken me, personally, a long time because I'd first need to figure these things out for myself. This PR didn't cause those problems, but it does make those existing problems marginally worse.

It took me a long time (and your help) to understand how this fix fixes the problem. Not a comment on the fix you provided, but in part a symptom of the complexity in this file.

@@ -422,6 +423,30 @@ def test_logout(scenario_set: Logout) -> None:
cli.main(cmd)


@pytest.mark.parametrize("active_user", ["alice", "bob", None])
Copy link
Contributor

Choose a reason for hiding this comment

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

Request, here, to move those tests that exercise functionality from just one file (and that don't rely on an actual running master) to harness/tests/determined/... per this doc.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll move it to harness/tests/... and change the doc? But the extra /determined/ layer is an unhelpful layer of hierarchy I think.

Copy link
Member Author

Choose a reason for hiding this comment

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

Looking at the other tests in this file, these other tests are actually are testing cli logic (at least, as-written). These tests also happen to be the only real coverage of common/api/authentication.py. I'll admit that seems wrong but I don't think moving it is actually any better, unless test_logout() and test_login_scenarios() are re-written to be specific to the authentication module, and separate cli-specific unit tests are written to make sure the cli faithfully passes certain variables into the authentication module.

I think that's a good idea, but I'd prefer to put that off until after my big auth refactor pr, since that pr already had to refactor these tests a bit. Rewriting them in this PR would mean I have to rewrite them again in that PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you'd like to change the doc from harness/tests/determined/... to harness/tests/..., I support that change. Just when doing it please also move the files currently in harness/tests/determined along with.

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 that's a good idea...

Your logic has the right shape to me even if I don't fully get the details. The scenarios strike me as tests of auth, not of the CLI. But you know this better than I do.

Anyway, if this is work you'd like the team get done later, maybe worth a ticket? Otherwise good chance we forget.

@@ -215,6 +215,9 @@ def logout(
if session_user is None:
return

if session_user == token_store.get_active_user():
Copy link
Contributor

Choose a reason for hiding this comment

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

Extra credit 1:logout is too complicated to not have a docstring that describes its semantics.

Copy link
Member Author

Choose a reason for hiding this comment

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

ok I extended the docstring.

@@ -215,6 +215,9 @@ def logout(
if session_user is None:
return

if session_user == token_store.get_active_user():
token_store.clear_active()
Copy link
Contributor

Choose a reason for hiding this comment

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

Extra credit 2: The docstring for TokenStore isn't totally right in that it maintains both a token store and an "active user" string. Which... makes its name kind of misleading. Is a refactor wanted?

Copy link
Member Author

Choose a reason for hiding this comment

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

updated the docstring. No a refactor is not wanted, because there is only one auth.json file where this is stored, and so it makes sense that there is only one object to control it.

The authentication logic would drop expired tokens, and then ask for a
password and, if that worked, add the fresh token back to the token
store.

However, the initial token drop had a side-effect of dropping the active
user, which was not being restored.

After this fix, the TokenStore doesn't try to be so smart.  Instead, the
logout-related CLI commands will explicitly clear the active user, but
tokens dropped due to expiration are unaffected.
@rb-determined-ai rb-determined-ai enabled auto-merge (squash) January 10, 2024 00:07
@rb-determined-ai rb-determined-ai merged commit fd4c1a0 into main Jan 10, 2024
69 of 83 checks passed
@rb-determined-ai rb-determined-ai deleted the rb/dropped-user branch January 10, 2024 00:10
@dannysauer dannysauer modified the milestone: 0.27.1 Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants