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,399 @@
1
+ ---
2
+ title: Advanced Usage
3
+ ---
4
+
5
+ # Advanced Usage
6
+
7
+ This guide covers advanced usage patterns and techniques for `env-secrets`.
8
+
9
+ ## Environment Variable Patterns
10
+
11
+ ### Nested JSON Handling
12
+
13
+ `env-secrets` flattens nested JSON objects into environment variables:
14
+
15
+ ```bash
16
+ # Secret with nested structure
17
+ aws secretsmanager create-secret \
18
+ --name my/nested/config \
19
+ --secret-string '{
20
+ "database": {
21
+ "host": "localhost",
22
+ "port": 5432,
23
+ "name": "myapp"
24
+ },
25
+ "redis": {
26
+ "host": "redis.example.com",
27
+ "port": 6379
28
+ }
29
+ }'
30
+
31
+ # Results in these environment variables:
32
+ # DATABASE_HOST=localhost
33
+ # DATABASE_PORT=5432
34
+ # DATABASE_NAME=myapp
35
+ # REDIS_HOST=redis.example.com
36
+ # REDIS_PORT=6379
37
+ ```
38
+
39
+ ### Array Handling
40
+
41
+ Arrays are converted to comma-separated values:
42
+
43
+ ```bash
44
+ # Secret with arrays
45
+ aws secretsmanager create-secret \
46
+ --name my/array/config \
47
+ --secret-string '{
48
+ "allowed_ips": ["192.168.1.1", "10.0.0.1"],
49
+ "domains": ["example.com", "api.example.com"]
50
+ }'
51
+
52
+ # Results in:
53
+ # ALLOWED_IPS=192.168.1.1,10.0.0.1
54
+ # DOMAINS=example.com,api.example.com
55
+ ```
56
+
57
+ ### Special Characters
58
+
59
+ Environment variable names are converted to uppercase and special characters are replaced:
60
+
61
+ ```bash
62
+ # Secret with special characters
63
+ aws secretsmanager create-secret \
64
+ --name my/special/config \
65
+ --secret-string '{
66
+ "api-key": "abc123",
67
+ "db_url": "postgres://user:pass@localhost:5432/db",
68
+ "oauth2.client_id": "client123"
69
+ }'
70
+
71
+ # Results in:
72
+ # API_KEY=abc123
73
+ # DB_URL=postgres://user:pass@localhost:5432/db
74
+ # OAUTH2_CLIENT_ID=client123
75
+ ```
76
+
77
+ ## Scripting and Automation
78
+
79
+ ### Shell Scripts
80
+
81
+ Use `env-secrets` in shell scripts:
82
+
83
+ ```bash
84
+ #!/bin/bash
85
+ # deploy.sh
86
+
87
+ # Load secrets and run deployment
88
+ env-secrets aws -s production/deploy -r us-east-1 -- bash -c '
89
+ echo "Deploying with secrets..."
90
+ echo "Database: $DATABASE_URL"
91
+ echo "API Key: $API_KEY"
92
+
93
+ # Run deployment commands
94
+ docker build -t myapp .
95
+ docker run -e DATABASE_URL -e API_KEY myapp
96
+ '
97
+ ```
98
+
99
+ ### Makefile Integration
100
+
101
+ ```makefile
102
+ # Makefile
103
+ .PHONY: dev prod test
104
+
105
+ dev:
106
+ env-secrets aws -s dev/config -r us-east-1 -- npm run dev
107
+
108
+ prod:
109
+ env-secrets aws -s prod/config -r us-east-1 -- npm start
110
+
111
+ test:
112
+ env-secrets aws -s test/config -r us-east-1 -- npm test
113
+ ```
114
+
115
+ ### CI/CD Pipelines
116
+
117
+ #### GitHub Actions
118
+
119
+ ```yaml
120
+ # .github/workflows/deploy.yml
121
+ name: Deploy
122
+ on:
123
+ push:
124
+ branches: [main]
125
+
126
+ jobs:
127
+ deploy:
128
+ runs-on: ubuntu-latest
129
+ steps:
130
+ - uses: actions/checkout@v3
131
+
132
+ - name: Setup Node.js
133
+ uses: actions/setup-node@v3
134
+ with:
135
+ node-version: '18'
136
+
137
+ - name: Configure AWS credentials
138
+ uses: aws-actions/configure-aws-credentials@v2
139
+ with:
140
+ aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
141
+ aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
142
+ aws-region: us-east-1
143
+
144
+ - name: Install env-secrets
145
+ run: npm install -g env-secrets
146
+
147
+ - name: Deploy with secrets
148
+ run: |
149
+ env-secrets aws -s production/app -r us-east-1 -- npm run deploy
150
+ ```
151
+
152
+ #### GitLab CI
153
+
154
+ ```yaml
155
+ # .gitlab-ci.yml
156
+ deploy:
157
+ stage: deploy
158
+ image: node:18
159
+ before_script:
160
+ - npm install -g env-secrets
161
+ script:
162
+ - env-secrets aws -s production/app -r us-east-1 -- npm run deploy
163
+ environment:
164
+ name: production
165
+ ```
166
+
167
+ ## Docker Integration
168
+
169
+ ### Docker Run
170
+
171
+ ```bash
172
+ # Run container with secrets
173
+ env-secrets aws -s docker/app -r us-east-1 -- docker run \
174
+ -e DATABASE_URL \
175
+ -e API_KEY \
176
+ -e REDIS_URL \
177
+ myapp:latest
178
+ ```
179
+
180
+ ### Docker Compose
181
+
182
+ ```yaml
183
+ # docker-compose.yml
184
+ version: '3.8'
185
+ services:
186
+ app:
187
+ build: .
188
+ environment:
189
+ - DATABASE_URL
190
+ - API_KEY
191
+ - REDIS_URL
192
+ command: npm start
193
+ ```
194
+
195
+ ```bash
196
+ # Run with secrets
197
+ env-secrets aws -s docker/app -r us-east-1 -- docker-compose up
198
+ ```
199
+
200
+ ### Multi-Stage Dockerfile
201
+
202
+ ```dockerfile
203
+ # Dockerfile
204
+ FROM node:18-alpine AS builder
205
+ WORKDIR /app
206
+ COPY package*.json ./
207
+ RUN npm ci --only=production
208
+
209
+ FROM node:18-alpine
210
+ WORKDIR /app
211
+ COPY --from=builder /app/node_modules ./node_modules
212
+ COPY . .
213
+
214
+ # Use env-secrets in entrypoint
215
+ ENTRYPOINT ["env-secrets", "aws", "-s", "docker/app", "-r", "us-east-1", "--"]
216
+ CMD ["node", "app.js"]
217
+ ```
218
+
219
+ ## Kubernetes Integration
220
+
221
+ ### ConfigMap Alternative
222
+
223
+ Instead of ConfigMaps, use `env-secrets` in your deployment:
224
+
225
+ ```yaml
226
+ # deployment.yaml
227
+ apiVersion: apps/v1
228
+ kind: Deployment
229
+ metadata:
230
+ name: myapp
231
+ spec:
232
+ template:
233
+ spec:
234
+ containers:
235
+ - name: app
236
+ image: myapp:latest
237
+ command: ['env-secrets']
238
+ args:
239
+ ['aws', '-s', 'k8s/app', '-r', 'us-east-1', '--', 'node', 'app.js']
240
+ env:
241
+ - name: AWS_REGION
242
+ value: 'us-east-1'
243
+ ```
244
+
245
+ ### Init Container Pattern
246
+
247
+ ```yaml
248
+ # deployment-with-init.yaml
249
+ apiVersion: apps/v1
250
+ kind: Deployment
251
+ metadata:
252
+ name: myapp
253
+ spec:
254
+ template:
255
+ spec:
256
+ initContainers:
257
+ - name: load-secrets
258
+ image: node:18-alpine
259
+ command: ['env-secrets']
260
+ args:
261
+ [
262
+ 'aws',
263
+ '-s',
264
+ 'k8s/app',
265
+ '-r',
266
+ 'us-east-1',
267
+ '--',
268
+ 'sh',
269
+ '-c',
270
+ "env > /shared/env && echo 'Secrets loaded'"
271
+ ]
272
+ volumeMounts:
273
+ - name: shared-env
274
+ mountPath: /shared
275
+ containers:
276
+ - name: app
277
+ image: myapp:latest
278
+ command: ['sh']
279
+ args: ['-c', 'source /shared/env && node app.js']
280
+ volumeMounts:
281
+ - name: shared-env
282
+ mountPath: /shared
283
+ volumes:
284
+ - name: shared-env
285
+ emptyDir: {}
286
+ ```
287
+
288
+ ## Environment-Specific Secrets
289
+
290
+ ### Secret Naming Convention
291
+
292
+ Use consistent naming for different environments:
293
+
294
+ ```bash
295
+ # Development
296
+ env-secrets aws -s dev/myapp -r us-east-1 -- npm run dev
297
+
298
+ # Staging
299
+ env-secrets aws -s staging/myapp -r us-east-1 -- npm run dev
300
+
301
+ # Production
302
+ env-secrets aws -s prod/myapp -r us-east-1 -- npm start
303
+ ```
304
+
305
+ ### Environment Detection
306
+
307
+ ```bash
308
+ #!/bin/bash
309
+ # run.sh
310
+
311
+ ENV=${NODE_ENV:-development}
312
+ SECRET_NAME="${ENV}/myapp"
313
+
314
+ echo "Running in $ENV environment with secret: $SECRET_NAME"
315
+
316
+ env-secrets aws -s "$SECRET_NAME" -r us-east-1 -- node app.js
317
+ ```
318
+
319
+ ## Monitoring and Logging
320
+
321
+ ### Health Checks
322
+
323
+ ```bash
324
+ # Check if secrets are accessible
325
+ env-secrets aws -s health/check -r us-east-1 -- echo "Secrets loaded successfully"
326
+ ```
327
+
328
+ ### Secret Validation
329
+
330
+ ```bash
331
+ #!/bin/bash
332
+ # validate-secrets.sh
333
+
334
+ SECRET_NAME="production/app"
335
+ REGION="us-east-1"
336
+
337
+ # Validate required environment variables
338
+ env-secrets aws -s "$SECRET_NAME" -r "$REGION" -- bash -c '
339
+ required_vars=("DATABASE_URL" "API_KEY" "REDIS_URL")
340
+
341
+ for var in "${required_vars[@]}"; do
342
+ if [ -z "${!var}" ]; then
343
+ echo "ERROR: Required environment variable $var is not set"
344
+ exit 1
345
+ fi
346
+ echo "✓ $var is set"
347
+ done
348
+
349
+ echo "All required secrets are available"
350
+ '
351
+ ```
352
+
353
+ ### Debug Logging
354
+
355
+ ```bash
356
+ # Enable detailed logging for troubleshooting
357
+ DEBUG=env-secrets,env-secrets:secretsmanager \
358
+ env-secrets aws -s myapp -r us-east-1 -- node app.js
359
+ ```
360
+
361
+ ## Performance Optimization
362
+
363
+ ### Secret Caching
364
+
365
+ While `env-secrets` doesn't cache secrets, you can optimize by:
366
+
367
+ 1. **Using IAM roles** instead of access keys
368
+ 2. **Keeping secrets small** and focused
369
+ 3. **Using VPC endpoints** for AWS Secrets Manager
370
+ 4. **Running in the same region** as your secrets
371
+
372
+ ### Batch Operations
373
+
374
+ For multiple applications, consider using a single secret with all configurations:
375
+
376
+ ```bash
377
+ # Single secret for all app configs
378
+ aws secretsmanager create-secret \
379
+ --name production/all-configs \
380
+ --secret-string '{
381
+ "app1": {
382
+ "database_url": "postgres://...",
383
+ "api_key": "key1"
384
+ },
385
+ "app2": {
386
+ "database_url": "postgres://...",
387
+ "api_key": "key2"
388
+ }
389
+ }'
390
+
391
+ # Use with environment variable filtering
392
+ env-secrets aws -s production/all-configs -r us-east-1 -- bash -c '
393
+ # Filter for app1 variables
394
+ export APP1_DATABASE_URL=$APP1_DATABASE_URL
395
+ export APP1_API_KEY=$APP1_API_KEY
396
+
397
+ node app1.js
398
+ '
399
+ ```