Reading secrets thru Vault Agent with gzip encoding between Vault agent and Vault server
Describe the bug Configured the vault agent and when try accessing data thru it, the error is
2019-04-18T08:24:27.677Z [INFO] cache: received request: path=<redacted> method=GET
2019-04-18T08:24:27.677Z [DEBUG] cache.leasecache: forwarding request: path=<redacted> method=GET
2019-04-18T08:24:27.766Z [INFO] cache.apiproxy: forwarding request: path=<redacted> method=GET
2019-04-18T08:24:27.771Z [ERROR] cache.leasecache: failed to parse response as secret: error="invalid character '\x1f' looking for beginning of value"
Executing the query with Curl works, so it's clearly not a problem of permissions
To Reproduce Steps to reproduce the behavior:
- Run
vault kv put secret/test foo=bardirectly to vault without passing thru the agent - Run
vault read secret/data/testwithexport VAULT_AGENT_ADDR=http://127.0.0.1:8100 - See error
Expected behavior See the content of the secret
Environment:
- Vault Server Version (retrieve with
vault status):
Key Value
--- -----
Recovery Seal Type shamir
Initialized true
Sealed false
Total Recovery Shares 5
Threshold 3
Version 1.1.1
Cluster Name acceptance
Cluster ID ------
HA Enabled true
HA Cluster https://10.244.78.109:8201
HA Mode active
- Vault CLI Version (retrieve with
vault version):Vault v1.1.1 ('a3dcd63451cf6da1d04928b601bbe9748d53842e') - Server Operating System/Architecture: Kubernetes 1.12.4, CoreOS, Docker 18.06.
Additional context This is due to gzip encoding done by the ingress-nginx in front of Vault Server. If I send the vault agent directly to Vault server this issue is not present
This likely applies to all installation with LB with HTTPS termination in front of Vault
Found the problem with tcpdump. Vault Agent is trying to parse a secret (command/agent/lease_cache.go L239) as it was in plain test but the body is coming with gzip encoding. In the Vault cli this case is handled, but it's not in the proxier resp.Body. I'd like to fix this, I just need some pointer
In the Vault cli this case is handled, but it's not in the proxier resp.Body.
The Vault CLI also makes the same api.ParseSecret call so I am not sure why it would work in that case unless the VAULT_ADDR is pointing directly against the server address and not going through the LB. When dealing with gzip'ed responses via cURL, you would need to pipe the output to gunzip in order to read the decompressed response, so decompression is handled separately.
Can you post both the Vault CLI and cURL output (with any sensitive info scrubbed), and provide Vault's address as well as the LB's address to correlate where the request is being routed?
I can confirm vault agent isn't able to handle gzipped responses. I have nginx in front of the vault server. With gzip off it started working.
What is the status of this issue?
@calvn I'm trying the differences between what's happening in the vault-agent and what happens with a simple vault kv get secret/foo. I don't think the gzip part is the problem in the code itself because it should be handled in the client.RawRequestWithContext
In the CLI the code is simple: https://github.com/hashicorp/vault/blob/master/command/kv_helpers.go#L19
In agent, because of the caching I guess:
- here is the actual request https://github.com/hashicorp/vault/blob/master/command/agent/cache/api_proxy.go#L41
- then the body goes into here https://github.com/hashicorp/vault/blob/master/command/agent/cache/proxy.go#L50
- then then body gets read here https://github.com/hashicorp/vault/blob/master/command/agent/cache/lease_cache.go#L238
So to have the same setup we should change from
secret, err := api.ParseSecret(bytes.NewReader(resp.ResponseBody))
to
secret, err := api.ParseSecret(resp.Response.Body)
I'm going to experiment and if it works propose a PR
@kalafut this is the issue I was referring to yesterday
Is nginx adding encoding headers as well? If it is I believe it should work. My suspicion is that it's compressing but not adding/modifying headers indicating as such.
Wouldn’t this affect the vault CLI as well as the vault-agent?
The weird thing indeed is that with CLI everything works
On Sep 11, 2019, at 09:53, Jeff Mitchell [email protected] wrote:
Is nginx adding encoding headers as well? If it is I believe it should work. My suspicion is that it's compressing but not adding/modifying headers indicating as such.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/hashicorp/vault/issues/6606?email_source=notifications&email_token=AFFQIVESS4AT4QL24JXQIPTQJEPBZA5CNFSM4HG3LZ62YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6PE4PI#issuecomment-530468413, or mute the thread https://github.com/notifications/unsubscribe-auth/AFFQIVF6EJ47ZX6RS2FJD4LQJEPBZANCNFSM4HG3LZ6Q.
I believe the issue stems from: https://golang.org/src/net/http/transport.go?#L2351
When the proxy creates the real request, it copies over headers, including Accept-Encoding: gzip. Go treats setting that manually as a request to not automatically decompress gzipped responses.
I repro'd the original error by putting nginx in from of Vault, and with this change it works fine.