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

Add projected service account support #288

Merged
merged 17 commits into from
Sep 3, 2021
Merged

Conversation

tvoran
Copy link
Member

@tvoran tvoran commented Aug 27, 2021

Continues #79. Fixes #78.

Adds more unit tests for serviceaccount(), and reads the tokenPath from the volumeMount for projected tokens.

Description

This PR adds the ability to specify a projected service token for vault-agent to use with auto-auth against the vault kubernetes auth method.

It adds a new annotation, vault.hashicorp.com/agent-service-account-token-volume-name, which is the name of the projected volume holding the token. If the volume is mounted to another container in the deployment, the token volume will be mounted to the same location in the vault-agent containers. Otherwise it will be mounted at the default location of /var/run/secrets/vault.hashicorp.com/serviceaccount/.

Example

Take the example nginx Pod from here, and add these annotations:

  annotations:
    # AGENT INJECTOR SETTINGS
    vault.hashicorp.com/agent-inject: "true"
    vault.hashicorp.com/agent-inject-status: "update"
    vault.hashicorp.com/agent-inject-secret-kv-secret: "internal/nginx"
    vault.hashicorp.com/agent-inject-template-kv-secret: |
      {{ with secret "internal/nginx" }}{{ .Data | toJSONPretty }}{{ end }}
    # VAULT SETTINGS
    vault.hashicorp.com/role: "app"
    vault.hashicorp.com/agent-service-account-token-volume-name: "vault-token"

Configure vault to accept the token with a role something like this:

vault write auth/kubernetes/role/app \
      bound_service_account_names=build-robot \
      bound_service_account_namespaces=default \
      audience=vault \
      policies=internal-app \
      ttl=24h

Once the Pod is deployed, vault-agent will be configured to use the token /var/run/secrets/tokens/vault-token for auto-auth. That token is periodically refreshed by kubernetes, and vault-agent will read the new token and use it whenever its refreshed.

One nice thing about this setup is that since the vault role is configured to only allow tokens that have an audience of vault, only the projected token will work, since the normal service token will have an audience of something like https://kubernetes.default.svc.cluster.local.

And in this example, to mount the projected token only on the vault-agent containers, remove the volumeMounts block from the nginx container. Then vault-agent will be configured to use the token at /var/run/secrets/vault.hashicorp.com/serviceaccount/vault-token for auto-auth.

tvoran added 3 commits August 31, 2021 17:15
Check for nil ServiceAccountTokenVolume and move error reporting into
the get token functions.
Copy link
Contributor

@benashz benashz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@tvoran tvoran deleted the add-projected-sa-support-fork branch September 3, 2021 23:36
RemcoBuddelmeijer pushed a commit to RemcoBuddelmeijer/vault-k8s that referenced this pull request Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support projected ServiceAccount volumes
4 participants