hashicorp / vault-k8s

First-class support for Vault and Kubernetes.
Mozilla Public License 2.0
789 stars 170 forks source link

vault-agent container cannot read secrets from kubernetes secrets engine #444

Closed fizzamahdi closed 1 year ago

fizzamahdi commented 1 year ago

Unlike the AWS and Database Secrets Engines where the "creds/:name" endpoints support GET requests, the Kubernetes Secrets Engine's "creds/:name" endpoint only supports POST requests. That type of request is currently not supported by the vault-agent container that gets injected into pods by the Vault Agent Injector.

Will the Vault Agent be able to support this in the future?

Current setup

We have two EKS clusters and there is an application in cluster A that needs access to the Kubernetes API in cluster B. A workload in Cluster A authenticatea to Vault via a kubernetes auth method for Cluster A and the Vault role has a policy attached to it that gives it the ability to read credentials from a kubernetes secrets engine for Cluster B.

Here are the specific:

Error message

The vault-agent-init container in a vault-k8s-secrets-engine-example pod has the following error message:

2023-03-16T21:28:43.734Z [WARN] (view) vault.read(kubernetes/puas/us-east-1/maas-k8s/creds/k8s-secrets-engine-example): vault.read(kubernetes/puas/us-east-1/maas-k8s/creds/k8s-secrets-engine-example): Error making API request.

Namespace: PCAS/isbx/
URL: GET https://vault.infosec.ic1.statefarm:8200/v1/kubernetes/puas/us-east-1/maas-k8s/creds/k8s-secrets-engine-example
Code: 405. Errors:

* 1 error occurred:
    * unsupported operation

Additional info

We were using the vault agent to pull a long-lived static kubernetes service account token from a KV secrets engine, so we want to switch over to using the kubernetes secrets engine. However, since we are using EKS, we have an option of using AWS credentials to get a kubernetes token (i.e. aws eks get-token) as a workaround if needed.

tomhjp commented 1 year ago

The templating language supports POST, but it's implicit, similar to how curl will automatically switch to POST if you provide body data. I think this modification should make it work (filling in the appropriate k8s namespace that you want the creds to be created in):

     vault.hashicorp.com/agent-inject-template-token: |
       {{- with secret "kubernetes/puas/us-east-1/maas-k8s/creds/k8s-secrets-engine-example" kubernetes_namespace=foo -}}
       {{ index .Data.service_account_token }}
       {{- end }}
fizzamahdi commented 1 year ago

Thank you for the clarification! Just adding the body worked for me.