hashicorp / nomad

Nomad is an easy-to-use, flexible, and performant workload orchestrator that can deploy a mix of microservice, batch, containerized, and non-containerized applications. Nomad is easy to operate and scale and has native Consul and Vault integrations.
https://www.nomadproject.io/
Other
14.81k stars 1.94k forks source link

Automatic Token Handling in `nomad login` #20478

Open thetoothpick opened 5 months ago

thetoothpick commented 5 months ago

Proposal

The current functionality of the nomad login command in HashiCorp Nomad only outputs the secret token to standard output. This feature request proposes enhancing the nomad login command to persist the secret token and automatically use it in subsequent commands without manual intervention. It would also be useful to handle token expiration, either by displaying a warning to the user or automatically requesting a new token after expiration.

Use-cases

Automatically handling secret tokens generated by nomad login would increase usability and make handling tokens easier.

Attempted Solutions

I have a shell script to export the NOMAD_TOKEN variable based on the output of `nomad login -t "{{.SecretID}}".

nomad-login () {
  export NOMAD_TOKEN=$(nomad login -t "{{.SecretID}}")
  if [[ "-v" == "$1" ]]; then
    nomad acl token self
  fi
}

However, using this on a team requires adding it to everyone's shell setup, and it's not always clear that there is a separate shell function.

tgross commented 4 months ago

Hi @thetoothpick! The tricky thing with setting the env var is that we can't do that in the parent shell, so the login can't persist outside that session without writing to disk somewhere. One approach we've talked about for this is described in https://github.com/hashicorp/nomad/issues/11043, where having some kind of persistent "context" would let you switch between logins while handling all the tokens as you'd like to do here.

Ideally we'd also be able to put tokens in the session keyring rather than having them sit around on disk, but then we get into cross-platform desktop details so that might be in a v2 of any such feature. In any case, thanks for opening the issue and I'll mark this for roadmapping.

thetoothpick commented 4 months ago

thanks @tgross! Even if it's somewhat crude, something as simple as Vault's method of writing a file (mentioned in some of the linked tickets) would be useful here (better than nothing 🙂).