envilder 0.2.2 โ 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.
- package/README.md +58 -9
- package/lib/cli/cliRunner.d.ts +7 -0
- package/lib/cli/cliRunner.d.ts.map +1 -1
- package/lib/cli/cliRunner.js +10 -2
- package/lib/cli/cliRunner.js.map +1 -1
- package/lib/index.d.ts +10 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +36 -6
- package/lib/index.js.map +1 -1
- package/package.json +8 -7
- package/src/cli/cliRunner.ts +10 -2
- package/src/index.ts +36 -5
- package/tests/cli/cliRunner.test.ts +16 -2
- package/tests/index.test.ts +18 -0
package/README.md
CHANGED
|
@@ -1,18 +1,24 @@
|
|
|
1
|
-

|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Envilder is a CLI tool for managing AWS SSM Parameter Store parameters and automatically generating the required
|
|
4
|
+
`.env` file. This tool simplifies project environment variable management, avoiding manual updates and ensuring
|
|
5
|
+
consistency across environments.
|
|
4
6
|
|
|
5
7
|
# โจ Features
|
|
6
8
|
|
|
7
9
|
- ๐ Fetch parameters securely from AWS SSM Parameter Store.
|
|
8
10
|
- โก Automatically generates a `.env` file with specified parameters.
|
|
9
|
-
- ๐ก๏ธ Handles encrypted
|
|
11
|
+
- ๐ก๏ธ Handles encrypted SSM parameters.
|
|
10
12
|
- ๐ชถ Lightweight and simple to use.
|
|
13
|
+
- ๐ Support for multiple AWS profiles.
|
|
11
14
|
|
|
12
15
|
# Prerequisites
|
|
13
|
-
|
|
16
|
+
|
|
17
|
+
Before using `Envilder`, ensure that you have the AWS CLI installed and properly configured on your local
|
|
18
|
+
machine. This configuration is required for `Envilder` to access and manage parameters in AWS SSM.
|
|
14
19
|
|
|
15
20
|
## AWS CLI Installation & Configuration
|
|
21
|
+
|
|
16
22
|
1. Install the AWS CLI by following the instructions [here](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html).
|
|
17
23
|
2. After installation, configure the AWS CLI using the following command:
|
|
18
24
|
|
|
@@ -26,10 +32,13 @@ Before using `Envilder`, ensure that you have the AWS CLI installed and properly
|
|
|
26
32
|
- Default region name (e.g., `us-east-1`)
|
|
27
33
|
- Default output format (e.g., `json`)
|
|
28
34
|
|
|
29
|
-
Make sure that the AWS credentials you're using have the appropriate permissions to access the SSM Parameter
|
|
35
|
+
Make sure that the AWS credentials you're using have the appropriate permissions to access the SSM Parameter
|
|
36
|
+
Store in your AWS account.
|
|
30
37
|
|
|
31
38
|
# Installation
|
|
32
|
-
|
|
39
|
+
|
|
40
|
+
You can install `Envilder` globally using yarn. This will allow you to use the `envilder` command from any
|
|
41
|
+
directory on your system.
|
|
33
42
|
|
|
34
43
|
```bash
|
|
35
44
|
yarn global add envilder
|
|
@@ -50,6 +59,10 @@ Envilder requires two arguments:
|
|
|
50
59
|
- `--map <path>`: Path to a JSON file mapping environment variable names to SSM parameters.
|
|
51
60
|
- `--envfile <path>`: Path where the generated .env file will be saved.
|
|
52
61
|
|
|
62
|
+
Optional arguments:
|
|
63
|
+
|
|
64
|
+
- `--profile <name>`: AWS CLI profile to use for credentials (if not using the default profile).
|
|
65
|
+
|
|
53
66
|
# ๐ง Example
|
|
54
67
|
|
|
55
68
|
1. Create a mapping file `param_map.json`:
|
|
@@ -67,7 +80,43 @@ Envilder requires two arguments:
|
|
|
67
80
|
envilder --map=param_map.json --envfile=.env
|
|
68
81
|
```
|
|
69
82
|
|
|
70
|
-
3.
|
|
83
|
+
3. To use a specific AWS profile:
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
envilder --map=param_map.json --envfile=.env --profile=dev-account
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
4. The `.env` file will be generated in the specified location.
|
|
90
|
+
|
|
91
|
+
## ๐ Working with Multiple AWS Profiles
|
|
92
|
+
|
|
93
|
+
If you work with multiple AWS accounts or environments, you can configure different profiles in your AWS credentials file:
|
|
94
|
+
|
|
95
|
+
1. Edit your AWS credentials file (usually at `~/.aws/credentials` on Linux/Mac or `%USERPROFILE%\.aws\credentials` on Windows):
|
|
96
|
+
|
|
97
|
+
```ini
|
|
98
|
+
[default]
|
|
99
|
+
aws_access_key_id=YOUR_DEFAULT_ACCESS_KEY
|
|
100
|
+
aws_secret_access_key=YOUR_DEFAULT_SECRET_KEY
|
|
101
|
+
|
|
102
|
+
[dev-account]
|
|
103
|
+
aws_access_key_id=YOUR_DEV_ACCESS_KEY
|
|
104
|
+
aws_secret_access_key=YOUR_DEV_SECRET_KEY
|
|
105
|
+
|
|
106
|
+
[prod-account]
|
|
107
|
+
aws_access_key_id=YOUR_PROD_ACCESS_KEY
|
|
108
|
+
aws_secret_access_key=YOUR_PROD_SECRET_KEY
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
2. When running Envilder, specify which profile to use with the `--profile` option:
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
# For development environment
|
|
115
|
+
envilder --map=param_map.json --envfile=.env.development --profile=dev-account
|
|
116
|
+
|
|
117
|
+
# For production environment
|
|
118
|
+
envilder --map=param_map.json --envfile=.env.production --profile=prod-account
|
|
119
|
+
```
|
|
71
120
|
|
|
72
121
|
# ๐ Sample `.env` Output
|
|
73
122
|
|
|
@@ -78,13 +127,13 @@ SECRET_KEY=mockedPassword
|
|
|
78
127
|
|
|
79
128
|
# ๐งช Running Tests
|
|
80
129
|
|
|
81
|
-
To run the tests with coverage:
|
|
130
|
+
To run the tests with coverage:
|
|
82
131
|
|
|
83
132
|
```bash
|
|
84
133
|
yarn test
|
|
85
134
|
```
|
|
86
135
|
|
|
87
|
-
Here you can see the current coverage report: https://macalbert.github.io/envilder
|
|
136
|
+
Here you can see the current coverage report: <https://macalbert.github.io/envilder/>
|
|
88
137
|
|
|
89
138
|
# ๐ License
|
|
90
139
|
|
package/lib/cli/cliRunner.d.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Parses CLI arguments and runs the environment file generator.
|
|
4
|
+
*
|
|
5
|
+
* Expects `--map` and `--envfile` options to be provided, with an optional `--profile` for AWS CLI profile selection. Invokes the main process to generate a `.env` file from AWS SSM parameters based on the provided mapping.
|
|
6
|
+
*
|
|
7
|
+
* @throws {Error} If either `--map` or `--envfile` arguments are missing.
|
|
8
|
+
*/
|
|
2
9
|
export declare function cliRunner(): Promise<void>;
|
|
3
10
|
//# sourceMappingURL=cliRunner.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cliRunner.d.ts","sourceRoot":"","sources":["../../src/cli/cliRunner.ts"],"names":[],"mappings":";AAKA,wBAAsB,SAAS,
|
|
1
|
+
{"version":3,"file":"cliRunner.d.ts","sourceRoot":"","sources":["../../src/cli/cliRunner.ts"],"names":[],"mappings":";AAKA;;;;;;GAMG;AACH,wBAAsB,SAAS,kBAmB9B"}
|
package/lib/cli/cliRunner.js
CHANGED
|
@@ -10,6 +10,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
import { Command } from 'commander';
|
|
12
12
|
import { run } from '../index.js';
|
|
13
|
+
/**
|
|
14
|
+
* Parses CLI arguments and runs the environment file generator.
|
|
15
|
+
*
|
|
16
|
+
* Expects `--map` and `--envfile` options to be provided, with an optional `--profile` for AWS CLI profile selection. Invokes the main process to generate a `.env` file from AWS SSM parameters based on the provided mapping.
|
|
17
|
+
*
|
|
18
|
+
* @throws {Error} If either `--map` or `--envfile` arguments are missing.
|
|
19
|
+
*/
|
|
13
20
|
export function cliRunner() {
|
|
14
21
|
return __awaiter(this, void 0, void 0, function* () {
|
|
15
22
|
const program = new Command();
|
|
@@ -18,13 +25,14 @@ export function cliRunner() {
|
|
|
18
25
|
.description('A CLI tool to generate .env files from AWS SSM parameters')
|
|
19
26
|
.version('0.1.0')
|
|
20
27
|
.requiredOption('--map <path>', 'Path to the JSON file with environment variable mapping')
|
|
21
|
-
.requiredOption('--envfile <path>', 'Path to the .env file to be generated')
|
|
28
|
+
.requiredOption('--envfile <path>', 'Path to the .env file to be generated')
|
|
29
|
+
.option('--profile <name>', 'AWS CLI profile to use');
|
|
22
30
|
yield program.parseAsync(process.argv);
|
|
23
31
|
const options = program.opts();
|
|
24
32
|
if (!options.map || !options.envfile) {
|
|
25
33
|
throw new Error('Missing required arguments: --map and --envfile');
|
|
26
34
|
}
|
|
27
|
-
yield run(options.map, options.envfile);
|
|
35
|
+
yield run(options.map, options.envfile, options.profile);
|
|
28
36
|
});
|
|
29
37
|
}
|
|
30
38
|
cliRunner().catch((error) => {
|
package/lib/cli/cliRunner.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cliRunner.js","sourceRoot":"","sources":["../../src/cli/cliRunner.ts"],"names":[],"mappings":";;;;;;;;;;AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAElC,MAAM,UAAgB,SAAS;;QAC7B,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE,CAAC;QAE9B,OAAO;aACJ,IAAI,CAAC,UAAU,CAAC;aAChB,WAAW,CAAC,2DAA2D,CAAC;aACxE,OAAO,CAAC,OAAO,CAAC;aAChB,cAAc,CAAC,cAAc,EAAE,yDAAyD,CAAC;aACzF,cAAc,CAAC,kBAAkB,EAAE,uCAAuC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"cliRunner.js","sourceRoot":"","sources":["../../src/cli/cliRunner.ts"],"names":[],"mappings":";;;;;;;;;;AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAElC;;;;;;GAMG;AACH,MAAM,UAAgB,SAAS;;QAC7B,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE,CAAC;QAE9B,OAAO;aACJ,IAAI,CAAC,UAAU,CAAC;aAChB,WAAW,CAAC,2DAA2D,CAAC;aACxE,OAAO,CAAC,OAAO,CAAC;aAChB,cAAc,CAAC,cAAc,EAAE,yDAAyD,CAAC;aACzF,cAAc,CAAC,kBAAkB,EAAE,uCAAuC,CAAC;aAC3E,MAAM,CAAC,kBAAkB,EAAE,wBAAwB,CAAC,CAAC;QAExD,MAAM,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACvC,MAAM,OAAO,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;QAE/B,IAAI,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;YACrC,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;QACrE,CAAC;QAED,MAAM,GAAG,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IAC3D,CAAC;CAAA;AAED,SAAS,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;IAC1B,OAAO,CAAC,KAAK,CAAC,2DAA2D,CAAC,CAAC;AAC7E,CAAC,CAAC,CAAC"}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,2 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Orchestrates the process of fetching environment variable values from AWS SSM Parameter Store and writing them to a local environment file.
|
|
3
|
+
*
|
|
4
|
+
* Loads a parameter mapping from a JSON file, retrieves existing environment variables, fetches updated values from SSM (optionally using a specified AWS profile), merges them, and writes the result to the specified environment file.
|
|
5
|
+
*
|
|
6
|
+
* @param mapPath - Path to the JSON file mapping environment variable names to SSM parameter names.
|
|
7
|
+
* @param envFilePath - Path to the local environment file to read and update.
|
|
8
|
+
* @param profile - Optional AWS profile name to use for credentials.
|
|
9
|
+
*/
|
|
10
|
+
export declare function run(mapPath: string, envFilePath: string, profile?: string): Promise<void>;
|
|
2
11
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA;;;;;;;;GAQG;AACH,wBAAsB,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,iBAY/E"}
|
package/lib/index.js
CHANGED
|
@@ -9,13 +9,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import * as fs from 'node:fs';
|
|
11
11
|
import { GetParameterCommand, SSM } from '@aws-sdk/client-ssm';
|
|
12
|
+
import { fromIni } from '@aws-sdk/credential-providers';
|
|
12
13
|
import * as dotenv from 'dotenv';
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Orchestrates the process of fetching environment variable values from AWS SSM Parameter Store and writing them to a local environment file.
|
|
16
|
+
*
|
|
17
|
+
* Loads a parameter mapping from a JSON file, retrieves existing environment variables, fetches updated values from SSM (optionally using a specified AWS profile), merges them, and writes the result to the specified environment file.
|
|
18
|
+
*
|
|
19
|
+
* @param mapPath - Path to the JSON file mapping environment variable names to SSM parameter names.
|
|
20
|
+
* @param envFilePath - Path to the local environment file to read and update.
|
|
21
|
+
* @param profile - Optional AWS profile name to use for credentials.
|
|
22
|
+
*/
|
|
23
|
+
export function run(mapPath, envFilePath, profile) {
|
|
15
24
|
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
const defaultAwsConfig = {};
|
|
26
|
+
const ssmClientConfig = profile ? { credentials: fromIni({ profile }) } : defaultAwsConfig;
|
|
27
|
+
const ssm = new SSM(ssmClientConfig);
|
|
16
28
|
const paramMap = loadParamMap(mapPath);
|
|
17
29
|
const existingEnvVariables = loadExistingEnvVariables(envFilePath);
|
|
18
|
-
const updatedEnvVariables = yield fetchAndUpdateEnvVariables(paramMap, existingEnvVariables);
|
|
30
|
+
const updatedEnvVariables = yield fetchAndUpdateEnvVariables(paramMap, existingEnvVariables, ssm);
|
|
19
31
|
writeEnvFile(envFilePath, updatedEnvVariables);
|
|
20
32
|
console.log(`Environment File generated at '${envFilePath}'`);
|
|
21
33
|
});
|
|
@@ -39,12 +51,24 @@ function loadExistingEnvVariables(envFilePath) {
|
|
|
39
51
|
Object.assign(envVariables, parsedEnv);
|
|
40
52
|
return envVariables;
|
|
41
53
|
}
|
|
42
|
-
|
|
54
|
+
/**
|
|
55
|
+
* Fetches parameter values from AWS SSM for each environment variable in the map and updates the existing environment variables record.
|
|
56
|
+
*
|
|
57
|
+
* For each mapping, retrieves the corresponding SSM parameter value and updates the environment variable if found. Logs masked values and warnings for missing parameters. Throws an error if any parameters fail to fetch.
|
|
58
|
+
*
|
|
59
|
+
* @param paramMap - Mapping of environment variable names to SSM parameter names.
|
|
60
|
+
* @param existingEnvVariables - Current environment variables to be updated.
|
|
61
|
+
* @param ssm - AWS SSM client instance used for fetching parameters.
|
|
62
|
+
* @returns The updated environment variables record.
|
|
63
|
+
*
|
|
64
|
+
* @throws {Error} If any SSM parameters cannot be fetched.
|
|
65
|
+
*/
|
|
66
|
+
function fetchAndUpdateEnvVariables(paramMap, existingEnvVariables, ssm) {
|
|
43
67
|
return __awaiter(this, void 0, void 0, function* () {
|
|
44
68
|
const errors = [];
|
|
45
69
|
for (const [envVar, ssmName] of Object.entries(paramMap)) {
|
|
46
70
|
try {
|
|
47
|
-
const value = yield fetchSSMParameter(ssmName);
|
|
71
|
+
const value = yield fetchSSMParameter(ssmName, ssm);
|
|
48
72
|
if (value) {
|
|
49
73
|
existingEnvVariables[envVar] = value;
|
|
50
74
|
console.log(`${envVar}=${value.length > 3 ? '*'.repeat(value.length - 3) + value.slice(-3) : '*'.repeat(value.length)}`);
|
|
@@ -64,7 +88,13 @@ function fetchAndUpdateEnvVariables(paramMap, existingEnvVariables) {
|
|
|
64
88
|
return existingEnvVariables;
|
|
65
89
|
});
|
|
66
90
|
}
|
|
67
|
-
|
|
91
|
+
/**
|
|
92
|
+
* Retrieves the value of a parameter from AWS SSM Parameter Store with decryption enabled.
|
|
93
|
+
*
|
|
94
|
+
* @param ssmName - The name of the SSM parameter to retrieve.
|
|
95
|
+
* @returns The decrypted parameter value if found, or undefined if the parameter does not exist.
|
|
96
|
+
*/
|
|
97
|
+
function fetchSSMParameter(ssmName, ssm) {
|
|
68
98
|
return __awaiter(this, void 0, void 0, function* () {
|
|
69
99
|
const command = new GetParameterCommand({
|
|
70
100
|
Name: ssmName,
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAE,mBAAmB,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAEjC,MAAM,GAAG,GAAG,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAE,mBAAmB,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EAAE,OAAO,EAAE,MAAM,+BAA+B,CAAC;AACxD,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAEjC;;;;;;;;GAQG;AACH,MAAM,UAAgB,GAAG,CAAC,OAAe,EAAE,WAAmB,EAAE,OAAgB;;QAC9E,MAAM,gBAAgB,GAAG,EAAE,CAAC;QAC5B,MAAM,eAAe,GAAG,OAAO,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,OAAO,CAAC,EAAE,OAAO,EAAE,CAAC,EA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AO,GAAG,IAAI,mBAAmB,CAAC;YACtC,IAAI,EAAE,OAAO;YACb,cAAc,EAAE,IAAI;SACrB,CAAC,CAAC;QAEH,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC9C,OAAO,SAAS,aAAT,SAAS,uBAAT,SAAS,CAAE,KAAK,CAAC;IAC1B,CAAC;CAAA;AAED,SAAS,YAAY,CAAC,WAAmB,EAAE,YAAoC;IAC7E,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC;SAC5C,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;QACpB,MAAM,YAAY,GAAG,KAAK,CAAC,OAAO,CAAC,eAAe,EAAE,KAAK,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QAChF,OAAO,GAAG,GAAG,IAAI,YAAY,EAAE,CAAC;IAClC,CAAC,CAAC;SACD,IAAI,CAAC,IAAI,CAAC,CAAC;IAEd,EAAE,CAAC,aAAa,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC;AAC5C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "envilder",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A CLI tool to generate .env files from AWS SSM parameters",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -38,21 +38,22 @@
|
|
|
38
38
|
"type": "module",
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@aws-sdk/client-ssm": "^3.654.0",
|
|
41
|
-
"@
|
|
42
|
-
"@secretlint/
|
|
41
|
+
"@aws-sdk/credential-providers": "^3.806.0",
|
|
42
|
+
"@secretlint/core": "^9.2.1",
|
|
43
|
+
"@secretlint/secretlint-rule-preset-recommend": "^9.0.0",
|
|
43
44
|
"@types/node": "^22.5.5",
|
|
44
|
-
"commander": "^
|
|
45
|
+
"commander": "^13.1.0",
|
|
45
46
|
"dotenv": "^16.4.5",
|
|
46
47
|
"picocolors": "^1.1.0"
|
|
47
48
|
},
|
|
48
49
|
"devDependencies": {
|
|
49
50
|
"@biomejs/biome": "^1.9.1",
|
|
50
|
-
"@vitest/coverage-v8": "^
|
|
51
|
+
"@vitest/coverage-v8": "^3.1.1",
|
|
51
52
|
"rimraf": "^6.0.1",
|
|
52
|
-
"secretlint": "^
|
|
53
|
+
"secretlint": "^9.0.0",
|
|
53
54
|
"ts-node": "^10.9.2",
|
|
54
55
|
"typescript": "^5.6.2",
|
|
55
|
-
"vitest": "^
|
|
56
|
+
"vitest": "^3.1.1"
|
|
56
57
|
},
|
|
57
58
|
"resolutions": {
|
|
58
59
|
"string-width": "4.2.3",
|
package/src/cli/cliRunner.ts
CHANGED
|
@@ -3,6 +3,13 @@
|
|
|
3
3
|
import { Command } from 'commander';
|
|
4
4
|
import { run } from '../index.js';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Parses CLI arguments and runs the environment file generator.
|
|
8
|
+
*
|
|
9
|
+
* Expects `--map` and `--envfile` options to be provided, with an optional `--profile` for AWS CLI profile selection. Invokes the main process to generate a `.env` file from AWS SSM parameters based on the provided mapping.
|
|
10
|
+
*
|
|
11
|
+
* @throws {Error} If either `--map` or `--envfile` arguments are missing.
|
|
12
|
+
*/
|
|
6
13
|
export async function cliRunner() {
|
|
7
14
|
const program = new Command();
|
|
8
15
|
|
|
@@ -11,7 +18,8 @@ export async function cliRunner() {
|
|
|
11
18
|
.description('A CLI tool to generate .env files from AWS SSM parameters')
|
|
12
19
|
.version('0.1.0')
|
|
13
20
|
.requiredOption('--map <path>', 'Path to the JSON file with environment variable mapping')
|
|
14
|
-
.requiredOption('--envfile <path>', 'Path to the .env file to be generated')
|
|
21
|
+
.requiredOption('--envfile <path>', 'Path to the .env file to be generated')
|
|
22
|
+
.option('--profile <name>', 'AWS CLI profile to use');
|
|
15
23
|
|
|
16
24
|
await program.parseAsync(process.argv);
|
|
17
25
|
const options = program.opts();
|
|
@@ -20,7 +28,7 @@ export async function cliRunner() {
|
|
|
20
28
|
throw new Error('Missing required arguments: --map and --envfile');
|
|
21
29
|
}
|
|
22
30
|
|
|
23
|
-
await run(options.map, options.envfile);
|
|
31
|
+
await run(options.map, options.envfile, options.profile);
|
|
24
32
|
}
|
|
25
33
|
|
|
26
34
|
cliRunner().catch((error) => {
|
package/src/index.ts
CHANGED
|
@@ -1,14 +1,26 @@
|
|
|
1
1
|
import * as fs from 'node:fs';
|
|
2
2
|
import { GetParameterCommand, SSM } from '@aws-sdk/client-ssm';
|
|
3
|
+
import { fromIni } from '@aws-sdk/credential-providers';
|
|
3
4
|
import * as dotenv from 'dotenv';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Orchestrates the process of fetching environment variable values from AWS SSM Parameter Store and writing them to a local environment file.
|
|
8
|
+
*
|
|
9
|
+
* Loads a parameter mapping from a JSON file, retrieves existing environment variables, fetches updated values from SSM (optionally using a specified AWS profile), merges them, and writes the result to the specified environment file.
|
|
10
|
+
*
|
|
11
|
+
* @param mapPath - Path to the JSON file mapping environment variable names to SSM parameter names.
|
|
12
|
+
* @param envFilePath - Path to the local environment file to read and update.
|
|
13
|
+
* @param profile - Optional AWS profile name to use for credentials.
|
|
14
|
+
*/
|
|
15
|
+
export async function run(mapPath: string, envFilePath: string, profile?: string) {
|
|
16
|
+
const defaultAwsConfig = {};
|
|
17
|
+
const ssmClientConfig = profile ? { credentials: fromIni({ profile }) } : defaultAwsConfig;
|
|
18
|
+
const ssm = new SSM(ssmClientConfig);
|
|
6
19
|
|
|
7
|
-
export async function run(mapPath: string, envFilePath: string) {
|
|
8
20
|
const paramMap = loadParamMap(mapPath);
|
|
9
21
|
const existingEnvVariables = loadExistingEnvVariables(envFilePath);
|
|
10
22
|
|
|
11
|
-
const updatedEnvVariables = await fetchAndUpdateEnvVariables(paramMap, existingEnvVariables);
|
|
23
|
+
const updatedEnvVariables = await fetchAndUpdateEnvVariables(paramMap, existingEnvVariables, ssm);
|
|
12
24
|
|
|
13
25
|
writeEnvFile(envFilePath, updatedEnvVariables);
|
|
14
26
|
console.log(`Environment File generated at '${envFilePath}'`);
|
|
@@ -36,15 +48,28 @@ function loadExistingEnvVariables(envFilePath: string): Record<string, string> {
|
|
|
36
48
|
return envVariables;
|
|
37
49
|
}
|
|
38
50
|
|
|
51
|
+
/**
|
|
52
|
+
* Fetches parameter values from AWS SSM for each environment variable in the map and updates the existing environment variables record.
|
|
53
|
+
*
|
|
54
|
+
* For each mapping, retrieves the corresponding SSM parameter value and updates the environment variable if found. Logs masked values and warnings for missing parameters. Throws an error if any parameters fail to fetch.
|
|
55
|
+
*
|
|
56
|
+
* @param paramMap - Mapping of environment variable names to SSM parameter names.
|
|
57
|
+
* @param existingEnvVariables - Current environment variables to be updated.
|
|
58
|
+
* @param ssm - AWS SSM client instance used for fetching parameters.
|
|
59
|
+
* @returns The updated environment variables record.
|
|
60
|
+
*
|
|
61
|
+
* @throws {Error} If any SSM parameters cannot be fetched.
|
|
62
|
+
*/
|
|
39
63
|
async function fetchAndUpdateEnvVariables(
|
|
40
64
|
paramMap: Record<string, string>,
|
|
41
65
|
existingEnvVariables: Record<string, string>,
|
|
66
|
+
ssm: SSM,
|
|
42
67
|
): Promise<Record<string, string>> {
|
|
43
68
|
const errors: string[] = [];
|
|
44
69
|
|
|
45
70
|
for (const [envVar, ssmName] of Object.entries(paramMap)) {
|
|
46
71
|
try {
|
|
47
|
-
const value = await fetchSSMParameter(ssmName);
|
|
72
|
+
const value = await fetchSSMParameter(ssmName, ssm);
|
|
48
73
|
if (value) {
|
|
49
74
|
existingEnvVariables[envVar] = value;
|
|
50
75
|
console.log(
|
|
@@ -66,7 +91,13 @@ async function fetchAndUpdateEnvVariables(
|
|
|
66
91
|
return existingEnvVariables;
|
|
67
92
|
}
|
|
68
93
|
|
|
69
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Retrieves the value of a parameter from AWS SSM Parameter Store with decryption enabled.
|
|
96
|
+
*
|
|
97
|
+
* @param ssmName - The name of the SSM parameter to retrieve.
|
|
98
|
+
* @returns The decrypted parameter value if found, or undefined if the parameter does not exist.
|
|
99
|
+
*/
|
|
100
|
+
async function fetchSSMParameter(ssmName: string, ssm: SSM): Promise<string | undefined> {
|
|
70
101
|
const command = new GetParameterCommand({
|
|
71
102
|
Name: ssmName,
|
|
72
103
|
WithDecryption: true,
|
|
@@ -12,9 +12,9 @@ describe('cliRunner', () => {
|
|
|
12
12
|
beforeEach(() => {
|
|
13
13
|
process.argv = [...originalArgv.slice(0, 2)];
|
|
14
14
|
});
|
|
15
|
-
|
|
16
15
|
afterEach(() => {
|
|
17
16
|
vi.clearAllMocks();
|
|
17
|
+
process.argv = originalArgv;
|
|
18
18
|
});
|
|
19
19
|
|
|
20
20
|
it('Should_CallRunWithCorrectArguments_When_ValidArgumentsAreProvided', async () => {
|
|
@@ -27,7 +27,7 @@ describe('cliRunner', () => {
|
|
|
27
27
|
await cliRunner();
|
|
28
28
|
|
|
29
29
|
// Assert
|
|
30
|
-
expect(run).toHaveBeenCalledWith(mockMapPath, mockEnvFilePath);
|
|
30
|
+
expect(run).toHaveBeenCalledWith(mockMapPath, mockEnvFilePath, undefined);
|
|
31
31
|
});
|
|
32
32
|
|
|
33
33
|
it('Should_ThrowError_When_RequiredArgumentsAreMissing', async () => {
|
|
@@ -42,4 +42,18 @@ describe('cliRunner', () => {
|
|
|
42
42
|
// Assert
|
|
43
43
|
await expect(action).rejects.toThrow('process.exit called');
|
|
44
44
|
});
|
|
45
|
+
|
|
46
|
+
it('Should_CallRunWithCorrectArgumentsIncludingProfile_When_ProfileIsProvided', async () => {
|
|
47
|
+
// Arrange
|
|
48
|
+
const mockMapPath = 'path/to/mockMap.json';
|
|
49
|
+
const mockEnvFilePath = 'path/to/.env';
|
|
50
|
+
const mockProfile = 'test-profile';
|
|
51
|
+
process.argv.push('--map', mockMapPath, '--envfile', mockEnvFilePath, '--profile', mockProfile);
|
|
52
|
+
|
|
53
|
+
// Act
|
|
54
|
+
await cliRunner();
|
|
55
|
+
|
|
56
|
+
// Assert
|
|
57
|
+
expect(run).toHaveBeenCalledWith(mockMapPath, mockEnvFilePath, mockProfile);
|
|
58
|
+
});
|
|
45
59
|
});
|
package/tests/index.test.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as fs from 'node:fs';
|
|
2
|
+
import { SSM } from '@aws-sdk/client-ssm';
|
|
2
3
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
3
4
|
import { run } from '../src/index';
|
|
4
5
|
|
|
@@ -140,4 +141,21 @@ describe('Envilder CLI', () => {
|
|
|
140
141
|
fs.unlinkSync(mockEnvFilePath);
|
|
141
142
|
fs.unlinkSync(mockMapPath);
|
|
142
143
|
});
|
|
144
|
+
|
|
145
|
+
it('Should_ConfigureSSMClientWithProfile_When_ProfileIsProvided', async () => {
|
|
146
|
+
// Arrange
|
|
147
|
+
const mockMapPath = './tests/param_map.json';
|
|
148
|
+
const mockEnvFilePath = './tests/.env.test';
|
|
149
|
+
const mockProfile = 'test-profile';
|
|
150
|
+
const paramMapContent = {
|
|
151
|
+
NEXT_PUBLIC_CREDENTIAL_EMAIL: '/path/to/ssm/email',
|
|
152
|
+
};
|
|
153
|
+
fs.writeFileSync(mockMapPath, JSON.stringify(paramMapContent));
|
|
154
|
+
|
|
155
|
+
// Act
|
|
156
|
+
await run(mockMapPath, mockEnvFilePath, mockProfile);
|
|
157
|
+
|
|
158
|
+
// Assert
|
|
159
|
+
expect(vi.mocked(SSM).mock.calls[0][0]).toEqual(expect.objectContaining({ credentials: expect.anything() }));
|
|
160
|
+
});
|
|
143
161
|
});
|