-
-
Notifications
You must be signed in to change notification settings - Fork 782
Make 'st2 login' actually work #3219
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
Conversation
In st2client/base.py, there's a conditional that detects if the username and password is provided, and if so, try to retrieve a token and update the token attribute of the client before returning. However, in a traditional `st2 login` scenario, only the 'username' field is populated, so this code will never run. Previous tests were done on systems that effectively had auth disabled (doh!), so while the config file and token files were being changed/generated correctly, API requests did not carry the 'X-Auth-Token' header. Since auth was disabled, this was no problem. However, on a real installation, this feature just plainly did not work. In short, this allows the 'st2 login' feature to work, and it also provides unit tests to help prevent this kind of thing Signed-off-by: Matt Oswalt <oswaltm@brocade.com>
Signed-off-by: Matt Oswalt <oswaltm@brocade.com>
Signed-off-by: Matt Oswalt <oswaltm@brocade.com>
Codecov Report
@@ Coverage Diff @@
## master #3219 +/- ##
=========================================
Coverage ? 77.81%
=========================================
Files ? 433
Lines ? 22416
Branches ? 0
=========================================
Hits ? 17443
Misses ? 4973
Partials ? 0
Continue to review full report at Codecov.
|
|
Good catch - thanks. Will test and confirm locally it works (with auth enabled :)). |
|
|
||
| SKIP_AUTH_CLASSES = [ | ||
| TokenCreateCommand.__name__, | ||
| LoginCommand.__name__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LoginCommand needs to be here otherwise it won't work - I confirmed it locally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On a related note - we need a test case to catch this.
If LoginCommand class is not here, st2 login won't work so we should have a test which catches this :)
|
I confirmed it's working locally with this change - 4b8a146. |
We really just want to read a username from config, etc. and don't want to perform any additional auth requests.
|
I pushed another change to skip auth api call when running Imo (please correct me if I'm wrong), there is no need to try to authenticate and hit the auth API when running |
st2client/tests/unit/test_auth.py
Outdated
| # TODO(mierdin): This tests that this particular command sends X-Auth-Token but you should | ||
| # also test other commands after this token has been installed | ||
| kwargs = { | ||
| 'headers': {'X-Auth-Token': self.TOKEN['token'], 'content-type': 'application/json'}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test was correct since this request is hitting auth api (and not regular API) to authenticate to retrieve the token and as such, only username and password should be sent (and token returned in the response and used on subsequent requests to the st2 api).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
username and password and token is returned back.
X-Auth-Token is indeed sent in the header in subsequent requests after st2 login to st2 API.
|
I believe this should do it as far as current state of st2 tests goes - https://github.com/StackStorm/st2/pull/3219/files#r101242702 Some additional st2tests (aka integration tests) would also be nice at some point. |
|
Same story for e2e tests here - #3215 (comment) |
| cache_token = rc_config.get('cli', {}).get('cache_token', False) | ||
|
|
||
| if username and password: | ||
| if credentials: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing I realized is if password is not written to config file and token expires, the code doesn't handle the case correctly. So if password is not present, we have to throw an exception that user needs to re-login with credentials or write password to config.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as "not handling that case correctly" - that's an expect behavior, but yes, as discussed on Slack, we should improve on it and print a message if user doesn't use -w option that token will expire in X hours and user will need to re-login.
And yeah, we can perhaps do the same on "token expired error" (advise to re-login), but we don't really know if the exception is related to expired token or it's simply an invalid token so we need to keep that in mind so the message needs to be more advisory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's simply an invalid token so we need to keep that in mind so the message needs to be more advisory
Yes, this is what I want. Sorry to confuse with the word "exception". Right now the message we get when a token expires with this change is not ideal. See https://gist.github.com/lakshmi-kannan/31c4f5cbc10f81f382ae76380ae1994c#file-gistfile1-txt-L9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the code to print a message on successful login in 3418ffe.
st2 login testu -p testp
Logged in as testu
Note: You didn't use --write-password option so the password hasn't been stored in the client config and you will need to login again in 24 hours when the auth token expires.
As an alternative you can run st2 login command with the "--write-password" flag, but keep it mind this will cause it to store the password in plain-text in the client config file.--write-password flag.
|
@Kami @lakshmi-kannan was CI finished when this was merged? |
|
I think we should enable in GitHub setting which will block merging if some tests are still red/unfinished. Bug introduced after merging this PR was actually caught in /bin/sh -c st2\ run\ core.local\ --\ hostname
Failed to authenticate with credentials provided in the config.
ERROR: 401 Client Error: Unauthorized
MESSAGE: Invalid or missing credentials for url: http://127.0.0.1:9100/tokenswhen there is no |
|
@armab just for my own sanity - after circle runs basic testing like unit and integration, things eventually go back to circle for actual package build right? Just curious we have checks in place for this or if this would have only shown up post-merge. |
In
st2client/base.py, there's a conditional that detects if the username and passwordis provided, and if so, try to retrieve a token and update the token attribute of the
client before returning. However, in a traditional
st2 loginscenario, only theusernamefield is populated, so this code will never run.Previous tests were done on systems that effectively had auth disabled (doh!), so while
the config file and token files were being changed/generated correctly, API requests
did not carry the
X-Auth-Tokenheader. Since auth was disabled, this was no problem.However, on a real installation, this feature just plainly did not work.
In short, this allows the 'st2 login' feature to work, and it also provides unit tests
to help prevent this kind of thing
st2 login