7. Secrets Management in CI/CD
Secrets management is critical for CI/CD security. Secrets include API keys, passwords, tokens, and certificates. Exposing secrets in CI/CD pipelines is a security risk. This module covers best practices for managing secrets securely in CI/CD workflows.
The key insight is that secrets should never be hardcoded or stored in source control. Secrets should be stored in secure vaults, injected at runtime, and rotated regularly. Understanding secrets management is essential for secure CI/CD implementations.
This module covers secret storage, injection, rotation, and best practices for GitHub Actions, GitLab CI, and Jenkins.
Secret Storage Solutions
Several solutions exist for storing secrets securely. HashiCorp Vault is a popular open-source secrets management tool. Cloud providers offer their own solutions: AWS Secrets Manager, Azure Key Vault, and GCP Secret Manager. CI/CD platforms also provide built-in secret management: GitHub Secrets, GitLab CI/CD Variables, and Jenkins Credentials. Choosing the right solution depends on your infrastructure and requirements.
For small teams, built-in CI/CD secrets are sufficient. For larger organizations, dedicated secrets management tools provide additional features like rotation, audit logs, and fine-grained access control. Understanding the trade-offs is essential for choosing the right solution.
Step-by-Step: Managing Secrets
Follow these steps to manage secrets securely in your CI/CD pipeline.
GitHub Actions Secrets:
1. Go to repository Settings → Secrets and variables → Actions.
2. Click New repository secret.
3. Enter the name (e.g., DEPLOY_TOKEN) and value.
4. Access in workflow: ${{ secrets.DEPLOY_TOKEN }}
GitLab CI/CD Variables:
1. Go to project Settings → CI/CD → Variables.
2. Click Add variable.
3. Enter the key (e.g., DEPLOY_TOKEN) and value.
4. Access in pipeline: $DEPLOY_TOKEN
Jenkins Credentials:
1. Go to Dashboard → Manage Jenkins → Manage Credentials.
2. Click Global and Add Credentials.
3. Select kind (e.g., Secret text).
4. Enter the secret and ID.
5. Access in pipeline: withCredentials([string(credentialsId: <strong><code>deploy-token, variable: TOKEN)])
HashiCorp Vault:
1. Install Vault: vault server -dev
2. Store a secret: vault kv put secret/myapp DEPLOY_TOKEN=xyz123
3. Access in pipeline: vault kv get secret/myapp
Try It Yourself: Create a workflow that uses a secret to authenticate with an external service. Practice rotating secrets and updating workflows.
Knowledge Check
Ready to test your understanding of 7. Secrets Management in CI/CD?