Skip to content

Salt-api token cherrypy/tornado inconsistency #38139

Closed

Description

@jeanpralo

Hi guys,

I was wondering why do we send a session_id back in the rest_cherrypy netapi module (https://github.com/saltstack/salt/blob/develop/salt/netapi/rest_cherrypy/app.py#L1712) when we send directly a salt token back in the tornado one (https://github.com/saltstack/salt/blob/develop/salt/netapi/rest_tornado/saltnado.py#L765).

In some instance it can be quite useful to send back the salt token. Let's say you want to reuse that token somewhere else (an extra api service on the salt master), we can just use that token to make sure the person is actually authorised ( using salt.auth.resolver).

So options are either replace that session_id token by the real token or add an extra key to the returned dictionary (salt_token). What do you guys think? Personally I'd rather fix cherrypy to match tornado.

Activity

  1. changed the title [-]Salt-api cherrypy token[/-] [+]Salt-api token cherrypy/tornado inconsistency[/+] on Dec 7, 2016
  2. whiteinge commented on Dec 7, 2016

    @whiteinge
    Contributor

    Hi, thanks for the questions.

    salt-api does not enforce authorization, Salt's eauth system does that. Local/Runner/WheelClient all accept username/password/eauth or token kwargs that are then checked by the eauth system. rest_cherrypy's session system just pairs a session to a Salt eauth token and passes that in for you.

    If you have a Salt eauth token already there is no need for cookies or session handling and rest_cherrypy has taken that into account with endpoints that skip CherryPy's session handling and accept raw Salt eauth credentials. The /run and /events endpoints specifically. Looking again the docs could do a better job of explaining that; I'll send a pull request to update them.

    There was no publicly exposed method to create or retrieve a Salt eauth token until #20363. (Sure you could grab one from the file system but that leaves much to be desired.) Now that PR is in salt-api should probably gain an endpoint to expose that function directly.

    As for your question about Saltnado, rest_cherrypy is the reference implementation (#26505). At the time it was written, Saltnado had to do some tricks around Salt's Python API since it wasn't directly compatible with a coroutine-based workflow. As part of that work it missed the line between salt-api and eauth (a recent PR made that line a tad sharper but more work is needed).

  3. added
    Question
    on Dec 7, 2016
  4. added this to the Approved milestone on Dec 7, 2016
  5. jeanpralo commented on Dec 7, 2016

    @jeanpralo
    ContributorAuthor

    Thanks for the answer @whiteinge, I guess the PR I have proposed is probably not something we want to look at doing then.

    The problem I am facing is that I use a nginx proxy in front of the salt-api as I have multiple endpoints. Some goes directly to salt-api and some other do various things. I would like those other endpoints to benefit from the token as well, at the moment there is no way for me of doing it as the token is stored in a RamSession object and I have no way of retrieving that info anywhere else but salt-api.

  6. jeanpralo commented on Dec 7, 2016

    @jeanpralo
    ContributorAuthor

    Ok I am sorry I think I just got it:

    1- Query a token via the /run endpoint
    2- Do the calls with the retrieved token

    Correct ?

  7. whiteinge commented on Dec 7, 2016

    @whiteinge
    Contributor

    @jeanpralo do the changes in #38145 answer your workflow question?

    The auth runner I mentioned above is available in the 2016.11 release. If you're not running that release yet I suggest you continue to retrieve Salt eauth tokens however you're currently doing it since they are not publicly exposed any other way. If Saltnado is currently returning them on login perhaps you could have Nginx send all requests to /login to Saltnado instead and retrieve the token manually from there?

  8. jeanpralo commented on Dec 8, 2016

    @jeanpralo
    ContributorAuthor

    @whiteinge I believe this is clear enough.

    Thanks for the info around the auth runner I will have a play with soon when I got time and probably upgrade ASAP.

  9. whiteinge commented on Dec 8, 2016

    @whiteinge
    Contributor

    Great. Thanks for raising the question in the first place. The docs on this are not great and I did not know Saltnado was returning eauth tokens.

  10. jeanpralo commented on Mar 21, 2017

    @jeanpralo
    ContributorAuthor

    Hey @whiteinge ,

    I am now looking into it and I have an empty return when executing: salt-run auth.mk_token

    I am using 2016.11.3 version./

  11. whiteinge commented on Mar 21, 2017

    @whiteinge
    Contributor

    Looks like we need some better error handling in that function. I'll get that added.

    I expect you have an eauth error in your master log file. Make sure you pass full eauth credentials as arguments to that function and make sure those credentials are working (salt -a <eauth backend> '*' test.ping is a quick way to check them).

  12. jeanpralo commented on Mar 21, 2017

    @jeanpralo
    ContributorAuthor

    @whiteinge alrighty double mistake ! Truncated the password stupidly and I had to explicitly set eauth to pam.

    If I want to do it over the api would that be correct ?

     curl -k -XPOST https://localhost:8000/run -H 'Content-Type: application/json' -d '[{"client": "runner", "fun": "auth.mk_token", "eauth": "pam", "username": "salt", "password": "saltdev"}]'
    

    Because I still get no response, but when I do a lookup jid I can't seem to see my fun_args username, password and eauth.

  13. jeanpralo commented on Mar 22, 2017

    @jeanpralo
    ContributorAuthor

    @whiteinge Ok after debugging a bit this is because of https://github.com/saltstack/salt/blob/develop/salt/runner.py#L61

    So I got it running with this:

    curl -k -XPOST https://127.0.0.1:8001/run -H 'Content-Type: application/json' -d '[{"client": "runner", "fun": "auth.mk_token", "eauth": "pam", "username": "salt", "password": "saltdev", "kwarg": {"eauth": "pam", "username": "salt", "password": "saltdev"}}]'
    

    Are we happy with this, or should we change it somehow ? At the end it makes sense it is just a bit confusing is all, maybe some clearer documentation around this ?

    Also should we be looking at allowing the use of salt-token across all endpoints ?

  14. whiteinge commented on Mar 23, 2017

    @whiteinge
    Contributor

    I hadn't seen that call yet via the /run endpoint. You're right that's pretty gnarly. ^_^

    My guess is the common use-case is people will generate a token at the CLI to then use for the REST API. But for those who want to generate it from the API, you'll need to pass creds to authenticate the call and also creds for the token so I think that's about the best we can do here. Good work figuring out the right syntax. 👍

    One thought is we could add a /token endpoint that works similarly to /login but just returns the token directly rather than authenticating a session.

  15. jeanpralo commented on Mar 23, 2017

    @jeanpralo
    ContributorAuthor

    @whiteinge I guess we could have a /token probably a tad nicer than this syntax.

    My guess is the common use-case is people will generate a token at the CLI to then use for the REST API.

    Well so far unless I am mistaken only the /events endpoints allow the use of such token. It would be really nice if we can use it across all endpoints. What are your thoughts on that ?

  16. whiteinge commented on Mar 24, 2017

    @whiteinge
    Contributor

    Any of Salt's client interfaces that accept the username/password/eauth trio can be replaced by a token kwarg that sends an eauth token. So that will work for /run for example. That's not specific to salt-api but is just how Salt's Python API works.

  17. jeanpralo commented on Mar 24, 2017

    @jeanpralo
    ContributorAuthor

    Alright fair enough :)

    Thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionSalt-API

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions