متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 207Security · Vault

How to read secrets from HashiCorp Vault

Database passwords in .env files, CI variables and wikis are copied everywhere and never rotated. Vault keeps secrets in one place: people and apps log in, a policy decides which paths they may read, every access is audited, and a secret can be changed in one place. This how-to stores a secret in the KV store, writes a read-only policy, gives an app its own login with AppRole, and reads the secret from a script.

  • 5 الدروس
  • 3 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

207.1

The quick answer

export VAULT_ADDR=https://vault.example.com:8200
vault login -method=oidc
vault kv put -mount=secret shop/db username=shop password='LONG-RANDOM-PASSWORD'
vault kv get -mount=secret -field=password shop/db

207.2

Step by step

  1. 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.
  2. Store the secret — KV version 2 keeps old versions: vault kv metadata get -mount=secret shop/db lists them, vault kv rollback -mount=secret -version=2 shop/db goes back.
  3. A read-only policy for the app — note the data/ part in the path for KV v2:
shop-read.hcl
path "secret/data/shop/*" {
  capabilities = ["read"]
}
vault policy write shop-read shop-read.hcl
  1. 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
  1. 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.

  1. 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.
  2. 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.
  3. Audit — turn on an audit device (vault audit enable file file_path=/var/log/vault/audit.log) so every read is logged.

207.3

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.

الأوامر في هذا الدرس

207.4

Common problems

ProblemCauseFix
permission denied although the policy looks rightKV v2 needs secret/data/… in the policy path.Add data/ (and metadata/ for listing).
x509: certificate signed by unknown authorityPrivate CA.VAULT_CACERT or the system trust store.
Vault is sealedVault restarted and was not unsealed.Unseal with the key shares, or use auto-unseal (cloud KMS).
invalid secret idSecret ID expired or already used.Create a new one; check secret_id_ttl / secret_id_num_uses.
Token expired in a long jobtoken_ttl too short and no renewal.Vault Agent renews; or renew with vault token renew.
Secret visible in ps or shell historyPassed as an argument.Use environment variables or files; HISTCONTROL=ignorespace.

الأوامر في هذا الدرس

207.5

Go further

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.

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل