env-secrets 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/.devcontainer/devcontainer.json +10 -6
  2. package/.dockerignore +9 -0
  3. package/.eslintignore +4 -2
  4. package/.github/dependabot.yml +4 -0
  5. package/.github/workflows/build-main.yml +6 -2
  6. package/.github/workflows/deploy-docs.yml +50 -0
  7. package/.github/workflows/e2e-tests.yaml +54 -0
  8. package/.github/workflows/lint.yaml +6 -2
  9. package/.github/workflows/release.yml +2 -2
  10. package/.github/workflows/snyk.yaml +5 -1
  11. package/.github/workflows/unittests.yaml +9 -66
  12. package/.lintstagedrc +2 -7
  13. package/.prettierignore +6 -0
  14. package/AGENTS.md +149 -0
  15. package/Dockerfile +14 -0
  16. package/README.md +331 -13
  17. package/__e2e__/README.md +160 -0
  18. package/__e2e__/index.test.ts +334 -32
  19. package/__e2e__/setup.ts +58 -0
  20. package/__e2e__/utils/debug-logger.ts +45 -0
  21. package/__e2e__/utils/test-utils.ts +645 -0
  22. package/__tests__/index.test.ts +266 -9
  23. package/__tests__/vaults/secretsmanager.test.ts +460 -0
  24. package/__tests__/vaults/utils.test.ts +9 -9
  25. package/dist/index.js +36 -10
  26. package/dist/vaults/secretsmanager.js +17 -5
  27. package/dist/vaults/utils.js +2 -2
  28. package/docker-compose.yaml +29 -0
  29. package/docs/AWS.md +257 -0
  30. package/jest.config.js +3 -1
  31. package/jest.e2e.config.js +8 -0
  32. package/package.json +10 -7
  33. package/src/index.ts +44 -10
  34. package/src/vaults/secretsmanager.ts +16 -5
  35. package/src/vaults/utils.ts +6 -4
  36. package/website/docs/advanced-usage.mdx +399 -0
  37. package/website/docs/best-practices.mdx +416 -0
  38. package/website/docs/cli-reference.mdx +204 -0
  39. package/website/docs/examples.mdx +960 -0
  40. package/website/docs/faq.mdx +302 -0
  41. package/website/docs/index.mdx +56 -0
  42. package/website/docs/installation.mdx +30 -0
  43. package/website/docs/overview.mdx +17 -0
  44. package/website/docs/production-deployment.mdx +622 -0
  45. package/website/docs/providers/aws-secrets-manager.mdx +28 -0
  46. package/website/docs/security.mdx +122 -0
  47. package/website/docs/troubleshooting.mdx +236 -0
  48. package/website/docs/tutorials/local-dev/devcontainer-localstack.mdx +31 -0
  49. package/website/docs/tutorials/local-dev/docker-compose.mdx +22 -0
  50. package/website/docs/tutorials/local-dev/nextjs.mdx +18 -0
  51. package/website/docs/tutorials/local-dev/node-python-go.mdx +39 -0
  52. package/website/docs/tutorials/local-dev/quickstart.mdx +23 -0
  53. package/website/docusaurus.config.ts +89 -0
  54. package/website/package.json +21 -0
  55. package/website/sidebars.ts +33 -0
  56. package/website/src/css/custom.css +1 -0
  57. package/website/static/img/env-secrets.png +0 -0
  58. package/website/static/img/favicon.ico +0 -0
  59. package/website/static/img/logo.svg +4 -0
  60. package/website/yarn.lock +8764 -0
@@ -0,0 +1,416 @@
1
+ ---
2
+ title: Best Practices
3
+ ---
4
+
5
+ # Best Practices
6
+
7
+ This guide outlines best practices for using `env-secrets` effectively and securely.
8
+
9
+ ## Secret Management
10
+
11
+ ### Secret Structure
12
+
13
+ **✅ Good: Flat, focused secrets**
14
+
15
+ ```json
16
+ {
17
+ "DATABASE_URL": "postgres://user:pass@localhost:5432/db",
18
+ "API_KEY": "abc123",
19
+ "REDIS_URL": "redis://localhost:6379"
20
+ }
21
+ ```
22
+
23
+ **❌ Avoid: Overly nested structures**
24
+
25
+ ```json
26
+ {
27
+ "app": {
28
+ "database": {
29
+ "connection": {
30
+ "url": "postgres://user:pass@localhost:5432/db"
31
+ }
32
+ }
33
+ }
34
+ }
35
+ ```
36
+
37
+ ### Secret Naming
38
+
39
+ **✅ Good: Environment-specific naming**
40
+
41
+ ```bash
42
+ # Development
43
+ dev/myapp/database
44
+ dev/myapp/api-keys
45
+
46
+ # Staging
47
+ staging/myapp/database
48
+ staging/myapp/api-keys
49
+
50
+ # Production
51
+ prod/myapp/database
52
+ prod/myapp/api-keys
53
+ ```
54
+
55
+ **❌ Avoid: Generic names**
56
+
57
+ ```bash
58
+ # Too generic
59
+ database
60
+ api-keys
61
+ secrets
62
+ ```
63
+
64
+ ### Secret Size
65
+
66
+ **✅ Good: Keep secrets small and focused**
67
+
68
+ ```bash
69
+ # Separate secrets by concern
70
+ aws secretsmanager create-secret \
71
+ --name prod/myapp/database \
72
+ --secret-string '{"DATABASE_URL":"postgres://..."}'
73
+
74
+ aws secretsmanager create-secret \
75
+ --name prod/myapp/api \
76
+ --secret-string '{"API_KEY":"abc123","API_SECRET":"xyz789"}'
77
+ ```
78
+
79
+ **❌ Avoid: Large, monolithic secrets**
80
+
81
+ ```bash
82
+ # Too large and mixed concerns
83
+ aws secretsmanager create-secret \
84
+ --name prod/myapp/all-config \
85
+ --secret-string '{"database":"...","api":"...","redis":"...","email":"...","logging":"..."}'
86
+ ```
87
+
88
+ ## Security Practices
89
+
90
+ ### IAM Policies
91
+
92
+ **✅ Good: Least privilege access**
93
+
94
+ ```json
95
+ {
96
+ "Version": "2012-10-17",
97
+ "Statement": [
98
+ {
99
+ "Effect": "Allow",
100
+ "Action": "secretsmanager:GetSecretValue",
101
+ "Resource": [
102
+ "arn:aws:secretsmanager:us-east-1:123456789012:secret:prod/myapp/*",
103
+ "arn:aws:secretsmanager:us-east-1:123456789012:secret:staging/myapp/*"
104
+ ]
105
+ }
106
+ ]
107
+ }
108
+ ```
109
+
110
+ > **Note:** Replace `123456789012` with your actual AWS account ID in the resource ARNs above.
111
+
112
+ **❌ Avoid: Overly permissive policies**
113
+
114
+ ```json
115
+ {
116
+ "Version": "2012-10-17",
117
+ "Statement": [
118
+ {
119
+ "Effect": "Allow",
120
+ "Action": "secretsmanager:*",
121
+ "Resource": "*"
122
+ }
123
+ ]
124
+ }
125
+ ```
126
+
127
+ ### Credential Management
128
+
129
+ **✅ Good: Use IAM roles**
130
+
131
+ ```bash
132
+ # On EC2/ECS/Lambda
133
+ env-secrets aws -s prod/myapp -r us-east-1 -- node app.js
134
+ ```
135
+
136
+ **✅ Good: Use environment variables for CI/CD**
137
+
138
+ ```bash
139
+ # In CI/CD pipeline
140
+ export AWS_ACCESS_KEY_ID=$CI_AWS_ACCESS_KEY_ID
141
+ export AWS_SECRET_ACCESS_KEY=$CI_AWS_SECRET_ACCESS_KEY
142
+ export AWS_DEFAULT_REGION=us-east-1
143
+
144
+ env-secrets aws -s prod/myapp -r us-east-1 -- npm run deploy
145
+ ```
146
+
147
+ **❌ Avoid: Hardcoded credentials**
148
+
149
+ ```bash
150
+ # Don't do this
151
+ env-secrets aws -s prod/myapp -r us-east-1 -p my-profile -- node app.js
152
+ # Where my-profile contains hardcoded credentials
153
+ ```
154
+
155
+ ## Application Integration
156
+
157
+ ### Environment Variable Validation
158
+
159
+ **✅ Good: Validate required variables**
160
+
161
+ ```javascript
162
+ // app.js
163
+ const requiredEnvVars = ['DATABASE_URL', 'API_KEY', 'REDIS_URL'];
164
+
165
+ for (const envVar of requiredEnvVars) {
166
+ if (!process.env[envVar]) {
167
+ console.error(`Missing required environment variable: ${envVar}`);
168
+ process.exit(1);
169
+ }
170
+ }
171
+
172
+ console.log('All required environment variables are present');
173
+ ```
174
+
175
+ ### Graceful Degradation
176
+
177
+ **✅ Good: Handle missing secrets gracefully**
178
+
179
+ ```bash
180
+ #!/bin/bash
181
+ # start.sh
182
+
183
+ # Try to load secrets, but don't fail if they're not available
184
+ if env-secrets aws -s dev/myapp -r us-east-1 -- echo "Secrets loaded" 2>/dev/null; then
185
+ echo "Running with secrets from AWS"
186
+ env-secrets aws -s dev/myapp -r us-east-1 -- node app.js
187
+ else
188
+ echo "Running with local environment variables"
189
+ node app.js
190
+ fi
191
+ ```
192
+
193
+ ## Development Workflow
194
+
195
+ ### Local Development
196
+
197
+ **✅ Good: Use separate development secrets**
198
+
199
+ ```bash
200
+ # Create development secrets
201
+ aws secretsmanager create-secret \
202
+ --name dev/myapp \
203
+ --secret-string '{
204
+ "DATABASE_URL": "postgres://dev:dev@localhost:5432/dev",
205
+ "API_KEY": "dev-key-123"
206
+ }'
207
+
208
+ # Use in development
209
+ env-secrets aws -s dev/myapp -r us-east-1 -- npm run dev
210
+ ```
211
+
212
+ ### Testing
213
+
214
+ **✅ Good: Use test-specific secrets**
215
+
216
+ ```bash
217
+ # Create test secrets
218
+ aws secretsmanager create-secret \
219
+ --name test/myapp \
220
+ --secret-string '{
221
+ "DATABASE_URL": "postgres://test:test@localhost:5432/test",
222
+ "API_KEY": "test-key-456"
223
+ }'
224
+
225
+ # Run tests with test secrets
226
+ env-secrets aws -s test/myapp -r us-east-1 -- npm test
227
+ ```
228
+
229
+ ## Deployment Patterns
230
+
231
+ ### Blue-Green Deployment
232
+
233
+ **✅ Good: Use environment-specific secrets**
234
+
235
+ ```bash
236
+ # Blue environment
237
+ env-secrets aws -s prod-blue/myapp -r us-east-1 -- node app.js
238
+
239
+ # Green environment
240
+ env-secrets aws -s prod-green/myapp -r us-east-1 -- node app.js
241
+ ```
242
+
243
+ ### Canary Deployment
244
+
245
+ **✅ Good: Gradual secret rollout**
246
+
247
+ ```bash
248
+ # Deploy to 10% of traffic with new secrets
249
+ env-secrets aws -s prod-canary/myapp -r us-east-1 -- node app.js
250
+
251
+ # Monitor and gradually increase
252
+ env-secrets aws -s prod-stable/myapp -r us-east-1 -- node app.js
253
+ ```
254
+
255
+ ## Monitoring and Observability
256
+
257
+ ### Health Checks
258
+
259
+ **✅ Good: Include secret validation in health checks**
260
+
261
+ ```bash
262
+ #!/bin/bash
263
+ # health-check.sh
264
+
265
+ # Check if secrets are accessible
266
+ if env-secrets aws -s health/check -r us-east-1 -- echo "OK" 2>/dev/null; then
267
+ echo "Secrets accessible"
268
+ exit 0
269
+ else
270
+ echo "Secrets not accessible"
271
+ exit 1
272
+ fi
273
+ ```
274
+
275
+ ### Logging
276
+
277
+ **✅ Good: Log secret access (not values)**
278
+
279
+ ```bash
280
+ # Log secret access for audit purposes
281
+ env-secrets aws -s prod/myapp -r us-east-1 -- bash -c '
282
+ echo "Loading secrets from prod/myapp"
283
+ node app.js
284
+ '
285
+ ```
286
+
287
+ **❌ Avoid: Logging secret values**
288
+
289
+ ```bash
290
+ # Never do this
291
+ env-secrets aws -s prod/myapp -r us-east-1 -- bash -c '
292
+ echo "Database URL: $DATABASE_URL" # DON'T DO THIS
293
+ node app.js
294
+ '
295
+ ```
296
+
297
+ ## Performance Optimization
298
+
299
+ ### Region Selection
300
+
301
+ **✅ Good: Use secrets in the same region as your application**
302
+
303
+ ```bash
304
+ # If your app runs in us-west-2, store secrets there too
305
+ env-secrets aws -s prod/myapp -r us-west-2 -- node app.js
306
+ ```
307
+
308
+ ### Secret Organization
309
+
310
+ **✅ Good: Organize secrets by application and environment**
311
+
312
+ ```bash
313
+ # Clear hierarchy
314
+ prod/frontend/api-keys
315
+ prod/frontend/database
316
+ prod/backend/api-keys
317
+ prod/backend/database
318
+ staging/frontend/api-keys
319
+ staging/frontend/database
320
+ ```
321
+
322
+ ## Error Handling
323
+
324
+ ### Robust Scripts
325
+
326
+ **✅ Good: Handle errors gracefully**
327
+
328
+ ```bash
329
+ #!/bin/bash
330
+ # deploy.sh
331
+
332
+ set -e # Exit on any error
333
+
334
+ echo "Starting deployment..."
335
+
336
+ # Load secrets and deploy
337
+ if env-secrets aws -s prod/myapp -r us-east-1 -- npm run deploy; then
338
+ echo "Deployment successful"
339
+ else
340
+ echo "Deployment failed"
341
+ exit 1
342
+ fi
343
+ ```
344
+
345
+ ### Fallback Strategies
346
+
347
+ **✅ Good: Provide fallback options**
348
+
349
+ ```bash
350
+ #!/bin/bash
351
+ # start.sh
352
+
353
+ # Try primary secrets first
354
+ if env-secrets aws -s prod/myapp -r us-east-1 -- node app.js; then
355
+ echo "Started with primary secrets"
356
+ else
357
+ echo "Primary secrets failed, trying backup..."
358
+ # Try backup secrets
359
+ env-secrets aws -s prod/myapp-backup -r us-east-1 -- node app.js
360
+ fi
361
+ ```
362
+
363
+ ## Compliance and Audit
364
+
365
+ ### Access Logging
366
+
367
+ **✅ Good: Enable CloudTrail logging**
368
+
369
+ ```bash
370
+ # CloudTrail automatically logs all Secrets Manager access
371
+ # No additional configuration needed with env-secrets
372
+ ```
373
+
374
+ ### Secret Rotation
375
+
376
+ **✅ Good: Implement secret rotation**
377
+
378
+ ```bash
379
+ # Rotate secrets regularly
380
+ aws secretsmanager rotate-secret --secret-id prod/myapp/api-keys
381
+
382
+ # Update applications to use new secret versions
383
+ env-secrets aws -s prod/myapp/api-keys -r us-east-1 -- node app.js
384
+ ```
385
+
386
+ ## Common Anti-Patterns
387
+
388
+ ### ❌ Don't: Store secrets in version control
389
+
390
+ ```bash
391
+ # Never commit secrets to git
392
+ echo "DATABASE_URL=postgres://..." >> .env # DON'T DO THIS
393
+ ```
394
+
395
+ ### ❌ Don't: Use the same secrets across environments
396
+
397
+ ```bash
398
+ # Don't use production secrets in development
399
+ env-secrets aws -s prod/myapp -r us-east-1 -- npm run dev # DON'T DO THIS
400
+ ```
401
+
402
+ ### ❌ Don't: Share secrets between applications
403
+
404
+ ```bash
405
+ # Don't use the same secret for multiple apps
406
+ env-secrets aws -s shared/secrets -r us-east-1 -- node app1.js # DON'T DO THIS
407
+ env-secrets aws -s shared/secrets -r us-east-1 -- node app2.js # DON'T DO THIS
408
+ ```
409
+
410
+ ### ❌ Don't: Use long-lived access keys
411
+
412
+ ```bash
413
+ # Prefer IAM roles over long-lived access keys
414
+ export AWS_ACCESS_KEY_ID=AKIA... # DON'T DO THIS
415
+ export AWS_SECRET_ACCESS_KEY=... # DON'T DO THIS
416
+ ```
@@ -0,0 +1,204 @@
1
+ ---
2
+ title: CLI Reference
3
+ ---
4
+
5
+ # CLI Reference
6
+
7
+ The `env-secrets` command-line interface provides a simple way to inject secrets as environment variables into your applications.
8
+
9
+ ## Basic Syntax
10
+
11
+ ```bash
12
+ env-secrets <provider> [options] -- <program-to-run>
13
+ ```
14
+
15
+ ## Providers
16
+
17
+ Currently supported provider: `aws`
18
+
19
+ ## AWS Secrets Manager Options
20
+
21
+ ### Required Parameters
22
+
23
+ - `-s, --secret <secret-name>` - The name or ARN of the secret in AWS Secrets Manager
24
+
25
+ ### Optional Parameters
26
+
27
+ - `-r, --region <region>` - AWS region where the secret is stored (defaults to `AWS_DEFAULT_REGION`)
28
+ - `-p, --profile <profile>` - AWS profile to use (defaults to environment variables or IAM role)
29
+ - `-o, --output <file>` - Output secrets to a file instead of injecting into environment variables. File will be created with 0400 permissions and will not overwrite existing files
30
+
31
+ ### Global Options
32
+
33
+ - `--help` - Show help information
34
+ - `--version` - Show version information
35
+
36
+ ## Examples
37
+
38
+ ### Basic Usage
39
+
40
+ ```bash
41
+ # Run a Node.js application with secrets
42
+ env-secrets aws -s my-app-secrets -r us-east-1 -- node app.js
43
+
44
+ # Run a Python application
45
+ env-secrets aws -s my-app-secrets -r us-east-1 -- python app.py
46
+
47
+ # Run a shell command
48
+ env-secrets aws -s my-app-secrets -r us-east-1 -- echo "Hello, $USER_NAME!"
49
+
50
+ # Output secrets to a file
51
+ env-secrets aws -s my-app-secrets -r us-east-1 -o secrets.env
52
+ ```
53
+
54
+ ### Environment Variable Inspection
55
+
56
+ ```bash
57
+ # Check what environment variables are injected
58
+ env-secrets aws -s my-app-secrets -r us-east-1 -- env | grep -E "(DATABASE|API|SECRET)"
59
+
60
+ # List all environment variables
61
+ env-secrets aws -s my-app-secrets -r us-east-1 -- env
62
+
63
+ # Check specific variables
64
+ env-secrets aws -s my-app-secrets -r us-east-1 -- bash -c 'echo "DB: $DATABASE_URL, API: $API_KEY"'
65
+ ```
66
+
67
+ ### Docker Integration
68
+
69
+ ```bash
70
+ # Run Docker container with secrets
71
+ env-secrets aws -s docker-secrets -r us-east-1 -- docker run \
72
+ -e DATABASE_URL \
73
+ -e API_KEY \
74
+ -e REDIS_URL \
75
+ my-app:latest
76
+
77
+ # Use with docker-compose
78
+ env-secrets aws -s docker-secrets -r us-east-1 -- docker-compose up
79
+ ```
80
+
81
+ ### Kubernetes Integration
82
+
83
+ ```bash
84
+ # Run in Kubernetes pod
85
+ env-secrets aws -s k8s-secrets -r us-east-1 -- node app.js
86
+
87
+ # Use with kubectl exec
88
+ kubectl exec -it my-pod -- env-secrets aws -s k8s-secrets -r us-east-1 -- node app.js
89
+ ```
90
+
91
+ ### CI/CD Pipelines
92
+
93
+ ```bash
94
+ # GitHub Actions
95
+ env-secrets aws -s prod/app -r us-east-1 -- npm run deploy
96
+
97
+ # GitLab CI
98
+ env-secrets aws -s prod/app -r us-east-1 -- npm run deploy
99
+ ```
100
+
101
+ ### Debug Mode
102
+
103
+ ```bash
104
+ # Enable debug logging
105
+ DEBUG=env-secrets env-secrets aws -s my-secret -r us-east-1 -- node app.js
106
+
107
+ # Detailed debug logging
108
+ DEBUG=env-secrets,env-secrets:secretsmanager env-secrets aws -s my-secret -r us-east-1 -- node app.js
109
+ ```
110
+
111
+ ### File Output Mode
112
+
113
+ ```bash
114
+ # Output secrets to a file with secure permissions
115
+ env-secrets aws -s my-app-secrets -r us-east-1 -o secrets.env
116
+
117
+ # Output with specific profile
118
+ env-secrets aws -s my-secret -r us-east-1 -p my-profile -o /tmp/secrets.env
119
+
120
+ # File content example (secrets.env):
121
+ # export DATABASE_URL=postgres://user:pass@localhost:5432/db
122
+ # export API_KEY=abc123
123
+ # export REDIS_URL=redis://localhost:6379
124
+
125
+ # Source the file in your application
126
+ source secrets.env
127
+ node app.js
128
+
129
+ # Or use with Docker
130
+ env-secrets aws -s docker-secrets -r us-east-1 -o .env
131
+ docker run --env-file .env my-app:latest
132
+ ```
133
+
134
+ ## Environment Variable Behavior
135
+
136
+ ### JSON Secret Parsing
137
+
138
+ Secrets stored as JSON are automatically parsed and converted to environment variables:
139
+
140
+ ```bash
141
+ # Secret content: {"DATABASE_URL":"postgres://...","API_KEY":"abc123"}
142
+ # Results in: DATABASE_URL=postgres://..., API_KEY=abc123
143
+ ```
144
+
145
+ ### Nested Object Handling
146
+
147
+ Nested JSON objects are flattened:
148
+
149
+ ```bash
150
+ # Secret: {"database":{"url":"postgres://...","port":5432}}
151
+ # Results in: DATABASE_URL=postgres://..., DATABASE_PORT=5432
152
+ ```
153
+
154
+ ### Array Handling
155
+
156
+ Arrays are converted to comma-separated values:
157
+
158
+ ```bash
159
+ # Secret: {"allowed_ips":["192.168.1.1","10.0.0.1"]}
160
+ # Results in: ALLOWED_IPS=192.168.1.1,10.0.0.1
161
+ ```
162
+
163
+ ### Special Character Handling
164
+
165
+ Special characters in keys are converted to underscores:
166
+
167
+ ```bash
168
+ # Secret: {"api-key":"abc123","db_url":"postgres://..."}
169
+ # Results in: API_KEY=abc123, DB_URL=postgres://...
170
+ ```
171
+
172
+ ## Error Handling
173
+
174
+ ### Common Error Messages
175
+
176
+ | Error | Description | Solution |
177
+ | --------------------------- | ------------------------------ | --------------------------------- |
178
+ | `ConfigError` | AWS credentials not configured | Set up AWS credentials or profile |
179
+ | `ResourceNotFoundException` | Secret doesn't exist | Verify secret name and region |
180
+ | `AccessDeniedException` | Insufficient permissions | Check IAM policies |
181
+ | `ValidationException` | Invalid secret name | Use valid secret name format |
182
+
183
+ ### Exit Codes
184
+
185
+ - `0` - Success
186
+ - `1` - General error
187
+ - `2` - Configuration error
188
+ - `3` - Secret not found
189
+ - `4` - Permission denied
190
+
191
+ ## Security Notes
192
+
193
+ - **No Local Storage**: Secrets are never stored locally (unless using `-o` flag)
194
+ - **Process Isolation**: Secrets are only injected into the child process
195
+ - **No Logging**: Secret values are never logged
196
+ - **Clean Exit**: Environment variables are cleaned up when the process exits
197
+ - **File Security**: When using `-o` flag, files are created with 0400 permissions (read-only for owner) and existing files are never overwritten
198
+
199
+ ## Performance Considerations
200
+
201
+ - **Network Latency**: First secret retrieval may take longer due to AWS API calls
202
+ - **Region Proximity**: Use secrets in the same region as your application
203
+ - **Secret Size**: Keep secrets small for better performance
204
+ - **Connection Reuse**: AWS SDK connections are reused automatically