هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
Step by step
- Point the CLI to Vault (quick answer) — with a private CA also
export VAULT_CACERT=/etc/ssl/certs/company-ca.pem. People log in with OIDC/LDAP, not with the root token. - Store the secret — KV version 2 keeps old versions:
vault kv metadata get -mount=secret shop/dblists them,vault kv rollback -mount=secret -version=2 shop/dbgoes back. - A read-only policy for the app — note the
data/part in the path for KV v2:
path "secret/data/shop/*" {
capabilities = ["read"]
}vault policy write shop-read shop-read.hcl- A login for the app: AppRole — a role ID (like a user name) and a secret ID (like a password, short-lived):
vault auth enable approle
vault write auth/approle/role/shop token_policies=shop-read token_ttl=1h token_max_ttl=4h secret_id_ttl=24h
vault read -field=role_id auth/approle/role/shop/role-id
vault write -f -field=secret_id auth/approle/role/shop/secret-id- Read it from a script — log in, get a short token, read one field:
export VAULT_TOKEN=$(vault write -field=token auth/approle/login role_id="$ROLE_ID" secret_id="$SECRET_ID")
DB_PASS=$(vault kv get -mount=secret -field=password shop/db)Keep it in a variable or a file with mode 600 — never print it or pass it on the command line of another program.
- Better for apps — Vault Agent logs in and writes the secret to a file (and renews it); in Kubernetes use the Kubernetes auth method with the Agent injector or the Vault Secrets Operator. In CI, use JWT/OIDC login from GitHub Actions or GitLab instead of stored tokens.
- Even better: dynamic secrets — the database secrets engine creates a new database user per app login that expires by itself; nothing long-lived to leak.
- Audit — turn on an audit device (
vault audit enable file file_path=/var/log/vault/audit.log) so every read is logged.
Check that it worked
With the AppRole token, vault kv get -mount=secret shop/db works, but vault kv put -mount=secret shop/db password=x and reading another path (secret/billing/db) fail with permission denied; vault token lookup shows policies [default shop-read] and a TTL of 1 hour.
Common problems
| Problem | Cause | Fix |
|---|---|---|
permission denied although the policy looks right | KV v2 needs secret/data/… in the policy path. | Add data/ (and metadata/ for listing). |
x509: certificate signed by unknown authority | Private CA. | VAULT_CACERT or the system trust store. |
Vault is sealed | Vault restarted and was not unsealed. | Unseal with the key shares, or use auto-unseal (cloud KMS). |
invalid secret id | Secret ID expired or already used. | Create a new one; check secret_id_ttl / secret_id_num_uses. |
| Token expired in a long job | token_ttl too short and no renewal. | Vault Agent renews; or renew with vault token renew. |
Secret visible in ps or shell history | Passed as an argument. | Use environment variables or files; HISTCONTROL=ignorespace. |
Go further
- Command pages: vault.
- Next how-tos: Encrypt Kubernetes secrets in Git with SOPS, Keep secrets out of Terraform, Use secrets safely in GitHub Actions, Rotate AWS access keys.
- Learn it: Secrets services — Vault, CKS — External secret stores. Full guide: Vault.
1Why does a KV v2 policy path contain data/?
KV version 2 stores the secret values under secret/data/…, so the policy must name that path.
2What is the difference between a role ID and a secret ID in AppRole?
The role ID is like a user name and can be public; the secret ID is like a short-lived password and must be delivered securely.