Skip to content

Secrets Management

存储 and manage secrets securely 使用 vaults, KMS, and 环境-based injection.

你将学到

  • 为什么 hardcoded secrets 是 dangerous
  • Secret management tools: Vault, AWS Secrets Manager
  • Injecting secrets 到 applications safely
  • Rotating and auditing secrets

概念讲解

硬编码密钥的问题

Every application needs secrets — database passwords, API keys, TLS certificates. The worst approach is hardcoding them in source code or config files committed to git. Secrets in git are effectively public — even in a private repo, they end up in CI logs, Docker images, and developer machines. Once a secret is committed, it must be considered compromised and rotated. Never commit secrets to version control.

Secret Management Tools

HashiCorp Vault is a popular open-source tool that centralizes secret storage, with encryption, access policies, and dynamic secrets (it can generate temporary database credentials). AWS Secrets Manager and Parameter Store are cloud-native options for AWS. Azure Key Vault and Google Secret Manager are their equivalents. Doppler and Infisical are newer developer-friendly options. These tools encrypt secrets at rest, control access with policies, and audit every access.

Injecting Secrets

Applications should receive secrets at runtime, not have them baked in. In Docker, pass secrets with -e or --环境-file. In Kubernetes, use Secrets and mount them as files or environment variables. In cloud platforms, use the SDK to fetch secrets at startup. For local development, use .环境 files that are gitignored. The key principle is that secrets live outside the code and are injected at the moment they're needed.

Rotation and Auditing

Secrets should be rotated regularly — even if they haven't been leaked. Set a rotation schedule (e.g., every 90 days) and automate it where possible. Dynamic secrets (short-lived credentials generated on demand) are even better. Audit who accessed which secret and when — all secret management tools provide audit logs. Have an incident response plan for secret leaks: rotate immediately, investigate how it leaked, and fix the root cause.

Secret Scanning

Integrate secret scanning into your CI pipeline to catch leaked secrets before they reach production. Tools like GitGuardian, TruffleHog, and GitHub's built-in secret scanning scan commits for known secret patterns (AWS keys, database URLs, API tokens). If a secret is detected, the pipeline fails and the team is notified. Enable push protection to block commits containing secrets at the Git level. This automated detection layer complements your manual practices and provides a safety net for human error.

示例代码

              # Never do this — hardcoded secrets in code:
#   const DB_PASSWORD = "supersecret123";  // BAD!

# Instead, read from environment or a secret manager:
#   const password = process.env.DB_PASSWORD;

# HashiCorp Vault: store and retrieve a secret
vault kv put secret/myapp/db password="s3cur3p@ss" user="admin"
vault kv get secret/myapp/db          # retrieve the secret

# AWS Secrets Manager (CLI)
aws secretsmanager create-secret \
  --name "myapp/db-password" \
  --secret-string '{"password":"s3cur3p@ss"}'

aws secretsmanager get-secret-value \
  --secret-id "myapp/db-password" \
  --query SecretString --output text

# Kubernetes: create a secret from literals
kubectl create secret generic db-secret \
  --from-literal=password=s3cur3p@ss \
  --from-literal=user=admin
            

Vault, AWS Secrets Manager, and Kubernetes Secrets 所有 provide secure storage. Secrets 是 injected at 运行时通过环境变量 or mounted files, 从不 hardcoded.

动手试一试

常见错误

错误写法

存储 secrets in 环境变量 in production

正确做法

环境变量能 是 leaked 通过处理 dumps, 错误 reports, and child 处理继承. Use secret manager 那个 injects secrets at 运行时 or mounts 它们 as files.

错误写法

不 rotating secrets regularly

正确做法

设置 automated rotation schedule. Stale secrets 是 security risk — 如果 one leaks, longer 它 goes unrotated, 更多 damage attacker 能做.

练习 Secrets Management

1 个练习

练习 Secrets Management

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误