大网数据广告活动
服务器运维问答

Login to HashiCorp Vault with Kubernetes Auth from Pod with Vault CLI

· 52CE 我爱测

TL;DR: What is the proper way to login from Vault CLI in a Kubernetes Pod using the Kubernetes Auth Method . I want to create regular snapshots from my HashiCorp Vault raft storage. So I created a Kubernetes CronJob running the s…

问题原文

原帖链接 · Max N. · CC BY-SA 4.0

TL;DR: What is the proper way to login from Vault CLI in a Kubernetes Pod using the Kubernetes Auth Method.

I want to create regular snapshots from my HashiCorp Vault raft storage. So I created a Kubernetes CronJob running the same image as my Vault cluster, that executes the following command on a schedule:

vault operator raft snapshot save /backups/daily-$(date +"%Y-%m-%d-%H-%M").snap

/backups is a persistent volume mounted to the Pod.

Of course the Pod needs to be authenticated to Vault. I have the Kubernetes Auth Method enabled. The Pods created by the CronJob/Job are running with a service account vault-backups. So I added a role vault_backups to Vault, that is bound to the service account vault-backups in the vault namespace and assigned it a new policy raft_snapshots_read with the following content:

path "/sys/storage/raft/snapshot"
{
  capabilities = ["read"]
}

For the actual login I'm currently doing the following:

export VAULT_TOKEN=$(vault write auth/kubernetes/login \
    role=vault_backups \
    jwt=$(cat /run/secrets/kubernetes.io/serviceaccount/token) |\
  grep -w token |\
  awk '{print $2}')

This command now uses the JWT token of the service account vault-backups to login with the role vault_backup. It returns a formatted output (by default table) containing the token. I grep and parse the line with the token to save it into VAULT_TOKEN.

Afterwards the raft snapshot save command executes successfully and I have my snapshot saved to the volume.

The solution works, but I'm not sure if this is the proper way to do this. First of all parsing the output feels kind of strange. Especially since there is the vault login command. But for vault login -method kubernetes it fails with:

Unknown auth method: kubernetes. Use "vault auth list" to see the complete list of auth methods. Additionally, some auth methods are only available via the HTTP API.

vault auth list includes an entry with type kubernetes, so I assume it is only available via the HTTP API.

I know that there is the option to get the output of vault write auth/kubernetes/login as JSON, but a) there is nothing installed in the container to parse JSON (like jq) and b) this still is a two step procedure, that includes parsing the result. It's just more structured then.

So is there a better way to achieve the login?

Thanks in advance!

已采纳解答

原帖链接 · Max N. · CC BY-SA 4.0

While looking through other tutorials I saw, that there is the parameter -field=<FIELD NAME>, that can be used to filter the output of vault write for specific fields.

So the login now looks like this:

export VAULT_TOKEN=$(vault write -field=token auth/kubernetes/login \
    role=vault_backups \
    jwt=$(cat /run/secrets/kubernetes.io/serviceaccount/token))

Thanks anyways.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-07-18)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。