机密管理与安全配置存储
学习如何安全地存储、轮换和访问机密信息,避免错误配置泄露凭据,并掌握环境变量、保管库和机密扫描的相关模式。
机密管理与安全配置存储 是 CoddyKit 上的免费 Secure Coding & OWASP Top 10 for Backend 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Secure Coding & OWASP Top 10 for Backend 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Secure Coding & OWASP Top 10 for Backend 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
The Secrets Problem
Hardcoded passwords, API keys, and tokens are one of the most common security misconfigurations. Once a secret lands in source control, it must be considered compromised forever.
This lesson covers how to keep secrets out of code and store configuration safely.
Never Commit Secrets
The first rule: secrets never live in your repository. Use a .gitignore to exclude files like .env, and prefer injected configuration over baked-in values.
- No passwords in source code
- No keys in config files committed to git
- No secrets in container images
Environment Variables
Environment variables are the simplest way to inject secrets at runtime. The application reads them from the process environment instead of a tracked file.
import os
db_password = os.environ.get('DB_PASSWORD')
if not db_password:
raise RuntimeError('DB_PASSWORD is not set')
print('Loaded secret of length', len(db_password))Limits of Env Vars
Env vars are better than hardcoding but have weaknesses: they can leak through crash dumps, child processes, debug endpoints, and logging of the whole environment.
For high-value secrets, prefer a dedicated secrets manager.
Secret Vaults
Tools like HashiCorp Vault, AWS Secrets Manager, and Azure Key Vault store secrets encrypted at rest, control access via fine-grained policies, and provide an audit trail of every read.
- Centralized storage with access control
- Automatic encryption at rest and in transit
- Audit logs of who fetched what and when
Fetching from a Vault
Applications request secrets at startup using a short-lived identity token instead of a static key.
def get_secret(client, path):
# client is authenticated via a short-lived role token
response = client.read(path)
if response is None:
raise RuntimeError('Secret not found: ' + path)
return response['data']['value']
# usage: get_secret(vault, 'secret/data/db')Secret Rotation
Rotation means changing secrets regularly and immediately after suspected exposure. Short-lived, automatically rotated credentials limit the window an attacker can use a stolen key.
Design apps to reload credentials without a full restart so rotation is painless.
Least Privilege for Secrets
Each service should only be able to read the secrets it needs. Scope vault policies and cloud IAM roles tightly so a compromised service cannot harvest unrelated credentials.
Detecting Leaked Secrets
Use secret-scanning tools in CI to block commits that contain credential patterns. Catching a leak before it merges is far cheaper than rotating after exposure.
import re
patterns = [r'AKIA[0-9A-Z]{16}', r'(?i)password\s*=\s*[\'\"]\S+']
line = 'aws_key = AKIAIOSFODNN7EXAMPLE'
for p in patterns:
if re.search(p, line):
print('Possible secret detected!')Encrypting Config at Rest
When config must be stored as files, encrypt them. Tools like SOPS or sealed-secrets let you commit encrypted values safely, decrypting only at deploy time with a managed key.
- Encrypt before storing
- Keep the decryption key in a managed KMS
- Never store the key alongside the data
Auditing Access
Log and review every secret access. Anomalies, like a service reading a secret it never used before, are strong indicators of compromise and feed your monitoring pipeline.
Quick Check
Test your understanding of secrets management.
Recap
You learned to keep secrets out of code, inject them via env vars or a vault, apply rotation and least privilege, scan for leaks in CI, and audit every access. Proper secrets management closes one of the biggest misconfiguration gaps in backend systems.
常见问题解答
「机密管理与安全配置存储」课时是免费的吗?
是的 — 「机密管理与安全配置存储」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Secure Coding & OWASP Top 10 for Backend 课程的其余内容,请升级到 CoddyKit PRO。 Secure Coding & OWASP Top 10 for Backend 课程共包含 4 节课。
「机密管理与安全配置存储」这节课中我会学到什么?
学习如何安全地存储、轮换和访问机密信息,避免错误配置泄露凭据,并掌握环境变量、保管库和机密扫描的相关模式。 你通过在浏览器中直接运行的动手代码来练习 Secure Coding & OWASP Top 10 for Backend,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Secure Coding & OWASP Top 10 for Backend 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Secure Coding & OWASP Top 10 for Backend 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「机密管理与安全配置存储」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Secure Coding & OWASP Top 10 for Backend 课中编写并运行代码吗?
能。每节 Secure Coding & OWASP Top 10 for Backend 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 加固服务器与应用程序配置
- 安全地管理依赖项与库
- 补丁管理与软件更新
- 机密管理与安全配置存储