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

consul kv command does not pass CONSUL_HTTP_TOKEN #2566

Closed
daveadams opened this issue Dec 2, 2016 · 6 comments
Closed

consul kv command does not pass CONSUL_HTTP_TOKEN #2566

daveadams opened this issue Dec 2, 2016 · 6 comments
Labels
type/bug Feature does not function as expected
Milestone

Comments

@daveadams
Copy link
Contributor

consul version

Client: 0.7.1
Server: n/a

Operating system and Environment details

Ubuntu 16.04

Description of the Issue (and unexpected/desired result)

From consul kv get --help:

  -token=<value>          ACL token to use in the request. This can also be
                          specified via the CONSUL_HTTP_TOKEN environment
                          variable. If unspecified, the query will default to
                          the token of the Consul agent at the HTTP address.

So, I expect the following two commands to have equivalent behavior:

$ consul kv get -token xyz config/test
$ CONSUL_HTTP_TOKEN=xyz consul kv get config/test

However, the first command with -token xyz specified as a CLI option, passes the token to the API correctly, whereas the second command with the environment variable only does not pass the token to the API.

Reproduction steps

Use netcat to verify, first, on a machine not running Consul, set netcat to listen to port 8500:

$ nc -l 8500

Then in another window, run the first command above. It will hang, but in the netcat terminal you can see the HTTP request that is sent:

GET /v1/kv/config/test HTTP/1.1
Host: 127.0.0.1:8500
User-Agent: Go-http-client/1.1
X-Consul-Token: xyz
Accept-Encoding: gzip

Hit Ctrl-C on the Consul command. This will also kill the netcat process. Restart netcat, and run the second command using the environment variable, and in the netcat window you will see:

GET /v1/kv/config/test HTTP/1.1
Host: 127.0.0.1:8500
User-Agent: Go-http-client/1.1
Accept-Encoding: gzip

In other words, consul kv command does not pass through the CONSUL_HTTP_TOKEN environment variable to the X-Consul-Token header as the documentation suggests.

@slackpad slackpad added the type/bug Feature does not function as expected label Dec 2, 2016
@slackpad slackpad added this to the 0.7.2 milestone Dec 2, 2016
@arunkumar-m
Copy link

Note: This issue will also appear for CONSUL_HTTP_ADDR env variable as well.

@daveadams
Copy link
Contributor Author

For me, CONSUL_HTTP_ADDR works fine. I'm helping some developers get access to our Consul system from their workstations, and I have them pull a temporary Consul token from Vault, then set their environment to talk to a reverse proxy I've set up to allow VPN access to the Consul HTTP API, so the actual workflow I get is:

$ vault auth -method=ldap username=myuser
[...omitted...]
$ consul kv get config/myapp/myvalue
Error querying Consul agent: Get http://127.0.0.1:8500/v1/kv/config/myapp/myvalue: dial tcp 127.0.0.1:8500: getsockopt: connection refused
$ export CONSUL_HTTP_ADDR=gateway.consul.mycorp.com:443
$ export CONSUL_HTTP_SSL=true
$ export CONSUL_HTTP_TOKEN=$( vault read -field token consul/creds/developer )
$ consul kv get config/myapp/myvalue
Error! No key exists at: config/myapp/myvalue
$ consul kv get -token=$CONSUL_HTTP_TOKEN config/myapp/myvalue
correct-config-value

There's no Consul agent running on my machine, as you can see in the first attempt to run the command without the CONSUL_HTTP_ADDR environment variable set.

@arunkumar-m
Copy link

arunkumar-m commented Dec 3, 2016

Yes, you are right, it is honoring CONSUL_HTTP_ADDR.
Submitted a pull request for the CONSUL_HTTP_TOKEN bug. #2569
#2610

@ashald
Copy link
Contributor

ashald commented Jan 12, 2017

Same issue with snapshot command in v0.7.2...

asobrien pushed a commit to asobrien/consul that referenced this issue Jan 16, 2017
@asobrien
Copy link

asobrien commented Jan 16, 2017

For completeness, the following commands don't currently respect the CONSUL_HTTP_TOKEN environment variable:

  • consul kv delete
  • consul kv export
  • consul kv get
  • consul kv import
  • consul kv put
  • consul snapshot restore
  • consul snapshot save

These commands always set the token value to the value passed via the cli (which defaults to "" when -token is not called), the following files are affected by this pattern:

  • command/kv_delete.go
  • command/kv_export.go
  • command/kv_get.go
  • command/kv_import.go
  • command/kv_put.go
  • command/snapshot_restore.go
  • command/snapshot_save.go

The complete set of changes required are reflected here.

@slackpad
Copy link
Contributor

slackpad commented Feb 7, 2017

These be fixed by #2717, which will centralize handling of the CLI options and environment variable handling.

@slackpad slackpad modified the milestones: 0.8.0, Triaged Feb 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Feature does not function as expected
Projects
None yet
Development

No branches or pull requests

5 participants