1
---
2
name: secrets-management
3
description: Implement secure secrets management for CI/CD pipelines using Vault, AWS Secrets Manager, or native platform solutions. Use when handling sensitive credentials, rotating secrets, or securing CI/CD environments.
4
---
5
6
# Secrets Management
7
8
Secure secrets management practices for CI/CD pipelines using Vault, AWS Secrets Manager, and other tools.
9
10
## Purpose
11
12
Implement secure secrets management in CI/CD pipelines without hardcoding sensitive information.
13
14
## When to Use
15
16
- Store API keys and credentials
17
- Manage database passwords
18
- Handle TLS certificates
19
- Rotate secrets automatically
20
- Implement least-privilege access
21
22
## Secrets Management Tools
23
24
### HashiCorp Vault
25
26
- Centralized secrets management
27
- Dynamic secrets generation
28
- Secret rotation
29
- Audit logging
30
- Fine-grained access control
31
32
### AWS Secrets Manager
33
34
- AWS-native solution
35
- Automatic rotation
36
- Integration with RDS
37
- CloudFormation support
38
39
### Azure Key Vault
40
41
- Azure-native solution
42
- HSM-backed keys
43
- Certificate management
44
- RBAC integration
45
46
### Google Secret Manager
47
48
- GCP-native solution
49
- Versioning
50
- IAM integration
51
52
## HashiCorp Vault Integration
53
54
### Setup Vault
55
56
```bash
57
# Start Vault dev server
58
vault server -dev
59
60
# Set environment
61
export VAULT_ADDR='http://127.0.0.1:8200'
62
export VAULT_TOKEN='root'
63
64
# Enable secrets engine
65
vault secrets enable -path=secret kv-v2
66
67
# Store secret
68
vault kv put secret/database/config username=admin password=secret
69
```
70
71
### GitHub Actions with Vault
72
73
```yaml
74
name: Deploy with Vault Secrets
75
76
on: [push]
77
78
jobs:
79
deploy:
80
runs-on: ubuntu-latest
81
steps:
82
- uses: actions/checkout@v4
83
84
- name: Import Secrets from Vault
85
uses: hashicorp/vault-action@v2
86
with:
87
url: https://vault.example.com:8200
88
token: ${{ secrets.VAULT_TOKEN }}
89
secrets: |
90
secret/data/database username | DB_USERNAME ;
91
secret/data/database password | DB_PASSWORD ;
92
secret/data/api key | API_KEY
93
94
- name: Use secrets
95
run: |
96
echo "Connecting to database as $DB_USERNAME"
97
# Use $DB_PASSWORD, $API_KEY
98
```
99
100
### GitLab CI with Vault
101
102
```yaml
103
deploy:
104
image: vault:1.17
105
before_script:
106
- export VAULT_ADDR=https://vault.example.com:8200
107
- export VAULT_TOKEN=$VAULT_TOKEN
108
- apk add curl jq
109
script:
110
- |
111
DB_PASSWORD=$(vault kv get -field=password secret/database/config)
112
API_KEY=$(vault kv get -field=key secret/api/credentials)
113
echo "Deploying with secrets..."
114
# Use $DB_PASSWORD, $API_KEY
115
```
116
117
## AWS Secrets Manager
118
119
### Store Secret
120
121
```bash
122
aws secretsmanager create-secret \
123
--name production/database/password \
124
--secret-string "super-secret-password"
125
```
126
127
### Retrieve in GitHub Actions
128
129
```yaml
130
- name: Configure AWS credentials
131
uses: aws-actions/configure-aws-credentials@v4
132
with:
133
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
134
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
135
aws-region: us-west-2
136
137
- name: Get secret from AWS
138
run: |
139
SECRET=$(aws secretsmanager get-secret-value \
140
--secret-id production/database/password \
141
--query SecretString \
142
--output text)
143
echo "::add-mask::$SECRET"
144
echo "DB_PASSWORD=$SECRET" >> $GITHUB_ENV
145
146
- name: Use secret
147
run: |
148
# Use $DB_PASSWORD
149
./deploy.sh
150
```
151
152
### Terraform with AWS Secrets Manager
153
154
```hcl
155
data "aws_secretsmanager_secret_version" "db_password" {
156
secret_id = "production/database/password"
157
}
158
159
resource "aws_db_instance" "main" {
160
allocated_storage = 100
161
engine = "postgres"
162
instance_class = "db.t3.large"
163
username = "admin"
164
password = jsondecode(data.aws_secretsmanager_secret_version.db_password.secret_string)["password"]
165
}
166
```
167
168
## GitHub Secrets
169
170
### Organization/Repository Secrets
171
172
```yaml
173
- name: Use GitHub secret
174
env:
175
API_KEY: ${{ secrets.API_KEY }}
176
DATABASE_URL: ${{ secrets.DATABASE_URL }}
177
run: |
178
# Secrets are injected as env vars — never print them to logs
179
./deploy.sh
180
```
181
182
### Environment Secrets
183
184
```yaml
185
deploy:
186
runs-on: ubuntu-latest
187
environment: production
188
steps:
189
- name: Deploy
190
env:
191
PROD_API_KEY: ${{ secrets.PROD_API_KEY }}
192
run: |
193
# Secret injected as env var — never print to logs
194
./deploy.sh
195
```
196
197
## GitLab CI/CD Variables
198
199
### Project Variables
200
201
```yaml
202
deploy:
203
script:
204
# Fail if a variable is missing, but never print its value
205
- test -n "$API_KEY" || { echo "API_KEY is not set"; exit 1; }
206
- test -n "$DATABASE_URL" || { echo "DATABASE_URL is not set"; exit 1; }
207
```
208
209
### Protected and Masked Variables
210
211
- Protected: Only available in protected branches
212
- Masked: Hidden in job logs
213
- File type: Stored as file
214
215
## Best Practices
216
217
1. **Never commit secrets** to Git
218
2. **Use different secrets** per environment
219
3. **Rotate secrets regularly**
220
4. **Implement least-privilege access**
221
5. **Enable audit logging**
222
6. **Use secret scanning** (GitGuardian, TruffleHog)
223
7. **Mask secrets in logs**
224
8. **Encrypt secrets at rest**
225
9. **Use short-lived tokens** when possible
226
10. **Document secret requirements**
227
228
## Secret Rotation
229
230
### Automated Rotation with AWS
231
232
```python
233
import boto3
234
import json
235
236
def lambda_handler(event, context):
237
client = boto3.client('secretsmanager')
238
239
# Get current secret
240
response = client.get_secret_value(SecretId='my-secret')
241
current_secret = json.loads(response['SecretString'])
242
243
# Generate new password
244
new_password = generate_strong_password()
245
246
# Update database password
247
update_database_password(new_password)
248
249
# Update secret
250
client.put_secret_value(
251
SecretId='my-secret',
252
SecretString=json.dumps({
253
'username': current_secret['username'],
254
'password': new_password
255
})
256
)
257
258
return {'statusCode': 200}
259
```
260
261
### Manual Rotation Process
262
263
1. Generate new secret
264
2. Update secret in secret store
265
3. Update applications to use new secret
266
4. Verify functionality
267
5. Revoke old secret
268
269
## External Secrets Operator
270
271
### Kubernetes Integration
272
273
```yaml
274
apiVersion: external-secrets.io/v1beta1
275
kind: SecretStore
276
metadata:
277
name: vault-backend
278
namespace: production
279
spec:
280
provider:
281
vault:
282
server: "https://vault.example.com:8200"
283
path: "secret"
284
version: "v2"
285
auth:
286
kubernetes:
287
mountPath: "kubernetes"
288
role: "production"
289
290
---
291
apiVersion: external-secrets.io/v1beta1
292
kind: ExternalSecret
293
metadata:
294
name: database-credentials
295
namespace: production
296
spec:
297
refreshInterval: 1h
298
secretStoreRef:
299
name: vault-backend
300
kind: SecretStore
301
target:
302
name: database-credentials
303
creationPolicy: Owner
304
data:
305
- secretKey: username
306
remoteRef:
307
key: database/config
308
property: username
309
- secretKey: password
310
remoteRef:
311
key: database/config
312
property: password
313
```
314
315
## Secret Scanning
316
317
### Pre-commit Hook
318
319
```bash
320
#!/bin/bash
321
# .git/hooks/pre-commit
322
323
# Check for secrets with TruffleHog
324
docker run --rm -v "$(pwd):/repo" \
325
trufflesecurity/trufflehog:3.88 \
326
filesystem --directory=/repo
327
328
if [ $? -ne 0 ]; then
329
echo "❌ Secret detected! Commit blocked."
330
exit 1
331
fi
332
```
333
334
### CI/CD Secret Scanning
335
336
```yaml
337
secret-scan:
338
stage: security
339
image: trufflesecurity/trufflehog:3.88
340
script:
341
- trufflehog filesystem .
342
allow_failure: false
343
```
344
345
346
## Related Skills
347
348
- `github-actions-templates` - For GitHub Actions integration
349
- `gitlab-ci-patterns` - For GitLab CI integration
350
- `deployment-pipeline-design` - For pipeline architecture
351