envilder 0.2.3 β 0.3.1
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/.gitattributes +66 -66
- package/.gitconfig +2 -2
- package/.github/dependabot.yml +39 -39
- package/.github/pull_request_template.md +20 -20
- package/.github/workflows/codeql-analysis.yml +49 -49
- package/.github/workflows/coverage-report.yml +73 -73
- package/.github/workflows/publish.yml +78 -0
- package/.github/workflows/unit-tests.yml +51 -48
- package/LICENSE +21 -21
- package/README.md +144 -95
- package/biome.json +2 -11
- package/package.json +19 -20
- package/src/cli/cliRunner.ts +10 -3
- package/src/index.ts +36 -5
- package/tests/cli/cliRunner.test.ts +16 -2
- package/tests/index.test.ts +27 -20
- package/tests/sample/autogenerated.env +1 -0
- package/vitest.config.js +12 -12
- package/lib/cli/cliRunner.d.ts +0 -3
- package/lib/cli/cliRunner.d.ts.map +0 -1
- package/lib/cli/cliRunner.js +0 -33
- package/lib/cli/cliRunner.js.map +0 -1
- package/lib/index.d.ts +0 -2
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -86
- package/lib/index.js.map +0 -1
- /package/tests/sample/{param_map.json β param-map.json} +0 -0
|
@@ -1,48 +1,51 @@
|
|
|
1
|
-
name: π± unit-tests
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
workflow_dispatch: {}
|
|
5
|
-
|
|
6
|
-
pull_request:
|
|
7
|
-
branches:
|
|
8
|
-
- "*"
|
|
9
|
-
types:
|
|
10
|
-
- opened
|
|
11
|
-
- reopened
|
|
12
|
-
- synchronize
|
|
13
|
-
- ready_for_review
|
|
14
|
-
paths:
|
|
15
|
-
- ".github/workflows/unit-tests.yml"
|
|
16
|
-
- "src/**"
|
|
17
|
-
|
|
18
|
-
concurrency:
|
|
19
|
-
group: ${{ github.workflow }}-${{ github.head_ref || github.sha }}
|
|
20
|
-
cancel-in-progress: true
|
|
21
|
-
|
|
22
|
-
jobs:
|
|
23
|
-
envilder-test:
|
|
24
|
-
runs-on: ubuntu-24.04
|
|
25
|
-
if: ${{ !github.event.pull_request.draft }}
|
|
26
|
-
timeout-minutes: 30
|
|
27
|
-
|
|
28
|
-
steps:
|
|
29
|
-
- name: π βοΈ Checkout
|
|
30
|
-
uses: actions/checkout@v4
|
|
31
|
-
|
|
32
|
-
- name: π οΈ Setup Node.js with Cache
|
|
33
|
-
uses: actions/setup-node@v4
|
|
34
|
-
with:
|
|
35
|
-
node-version: '20.x'
|
|
36
|
-
cache: 'yarn'
|
|
37
|
-
|
|
38
|
-
- name: π¦ Install packages
|
|
39
|
-
run: yarn install
|
|
40
|
-
|
|
41
|
-
- name: π Run
|
|
42
|
-
run: yarn
|
|
43
|
-
|
|
44
|
-
- name:
|
|
45
|
-
run: yarn
|
|
46
|
-
|
|
47
|
-
- name:
|
|
48
|
-
run: yarn
|
|
1
|
+
name: π± unit-tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
workflow_dispatch: {}
|
|
5
|
+
|
|
6
|
+
pull_request:
|
|
7
|
+
branches:
|
|
8
|
+
- "*"
|
|
9
|
+
types:
|
|
10
|
+
- opened
|
|
11
|
+
- reopened
|
|
12
|
+
- synchronize
|
|
13
|
+
- ready_for_review
|
|
14
|
+
paths:
|
|
15
|
+
- ".github/workflows/unit-tests.yml"
|
|
16
|
+
- "src/**"
|
|
17
|
+
|
|
18
|
+
concurrency:
|
|
19
|
+
group: ${{ github.workflow }}-${{ github.head_ref || github.sha }}
|
|
20
|
+
cancel-in-progress: true
|
|
21
|
+
|
|
22
|
+
jobs:
|
|
23
|
+
envilder-test:
|
|
24
|
+
runs-on: ubuntu-24.04
|
|
25
|
+
if: ${{ !github.event.pull_request.draft }}
|
|
26
|
+
timeout-minutes: 30
|
|
27
|
+
|
|
28
|
+
steps:
|
|
29
|
+
- name: π βοΈ Checkout
|
|
30
|
+
uses: actions/checkout@v4
|
|
31
|
+
|
|
32
|
+
- name: π οΈ Setup Node.js with Cache
|
|
33
|
+
uses: actions/setup-node@v4
|
|
34
|
+
with:
|
|
35
|
+
node-version: '20.x'
|
|
36
|
+
cache: 'yarn'
|
|
37
|
+
|
|
38
|
+
- name: π¦ Install packages
|
|
39
|
+
run: yarn install --frozen-lockfile
|
|
40
|
+
|
|
41
|
+
- name: π Run formatting checker
|
|
42
|
+
run: yarn format
|
|
43
|
+
|
|
44
|
+
- name: π Run code quality checker
|
|
45
|
+
run: yarn lint
|
|
46
|
+
|
|
47
|
+
- name: π§ Build
|
|
48
|
+
run: yarn build
|
|
49
|
+
|
|
50
|
+
- name: π΄ββοΈ Run unit tests
|
|
51
|
+
run: yarn test
|
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 MarΓ§al Albert
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 MarΓ§al Albert
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,95 +1,144 @@
|
|
|
1
|
-

|
|
2
|
-
|
|
3
|
-
Envilder is a CLI tool for managing AWS SSM Parameter Store parameters and automatically generating the required
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
#
|
|
39
|
-
|
|
40
|
-
You can install
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
1
|
+

|
|
2
|
+
|
|
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.
|
|
6
|
+
|
|
7
|
+
# β¨ Features
|
|
8
|
+
|
|
9
|
+
- π Fetch parameters securely from AWS SSM Parameter Store.
|
|
10
|
+
- β‘ Automatically generates a `.env` file with specified parameters.
|
|
11
|
+
- π‘οΈ Handles encrypted SSM parameters.
|
|
12
|
+
- πͺΆ Lightweight and simple to use.
|
|
13
|
+
- π Support for multiple AWS profiles.
|
|
14
|
+
|
|
15
|
+
# Prerequisites
|
|
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.
|
|
19
|
+
|
|
20
|
+
## AWS CLI Installation & Configuration
|
|
21
|
+
|
|
22
|
+
1. Install the AWS CLI by following the instructions [here](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html).
|
|
23
|
+
2. After installation, configure the AWS CLI using the following command:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
aws configure
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
You'll be prompted to provide:
|
|
30
|
+
- AWS Access Key ID
|
|
31
|
+
- AWS Secret Access Key
|
|
32
|
+
- Default region name (e.g., `us-east-1`)
|
|
33
|
+
- Default output format (e.g., `json`)
|
|
34
|
+
|
|
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.
|
|
37
|
+
|
|
38
|
+
# Installation
|
|
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.
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
yarn global add envilder
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
# π¦ Installation
|
|
48
|
+
|
|
49
|
+
You can install **envilder** globally or locally using npm:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
npm install -g envilder
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
# π Usage
|
|
56
|
+
|
|
57
|
+
Envilder requires two arguments:
|
|
58
|
+
|
|
59
|
+
- `--map <path>`: Path to a JSON file mapping environment variable names to SSM parameters.
|
|
60
|
+
- `--envfile <path>`: Path where the generated .env file will be saved.
|
|
61
|
+
|
|
62
|
+
Optional arguments:
|
|
63
|
+
|
|
64
|
+
- `--profile <name>`: AWS CLI profile to use for credentials (if not using the default profile).
|
|
65
|
+
|
|
66
|
+
# π§ Example
|
|
67
|
+
|
|
68
|
+
1. Create a mapping file `param-map.json`:
|
|
69
|
+
|
|
70
|
+
```json
|
|
71
|
+
{
|
|
72
|
+
"SECRET_TOKEN": "/path/to/ssm/token",
|
|
73
|
+
"SECRET_KEY": "/path/to/ssm/password"
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
2. Run envilder to generate your `.env` file:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
envilder --map=param-map.json --envfile=.env
|
|
81
|
+
```
|
|
82
|
+
|
|
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
|
+
```
|
|
120
|
+
|
|
121
|
+
# π Sample `.env` Output
|
|
122
|
+
|
|
123
|
+
```makefile
|
|
124
|
+
SECRET_TOKEN=mockedEmail@example.com
|
|
125
|
+
SECRET_KEY=mockedPassword
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
# π§ͺ Running Tests
|
|
129
|
+
|
|
130
|
+
To run the tests with coverage:
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
yarn test
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Here you can see the current coverage report: <https://macalbert.github.io/envilder/>
|
|
137
|
+
|
|
138
|
+
# π License
|
|
139
|
+
|
|
140
|
+
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.
|
|
141
|
+
|
|
142
|
+
# π Contributing
|
|
143
|
+
|
|
144
|
+
Contributions are welcome! Feel free to submit issues and pull requests.
|
package/biome.json
CHANGED
|
@@ -1,17 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
|
|
3
3
|
"files": {
|
|
4
|
-
"include": [
|
|
5
|
-
|
|
6
|
-
"./tests/**",
|
|
7
|
-
"package.json",
|
|
8
|
-
"biome.json",
|
|
9
|
-
".secretlintrc.json",
|
|
10
|
-
"tsconfig.json",
|
|
11
|
-
"tsconfig.build.json",
|
|
12
|
-
"vite.config.ts",
|
|
13
|
-
"repopack.config.json"
|
|
14
|
-
]
|
|
4
|
+
"include": ["./src/**", "./tests/**"],
|
|
5
|
+
"ignore": ["**/node_modules/**", "**/lib/**", "**/dist/**", "**/coverage/**", "**/.lock", "**/.md"]
|
|
15
6
|
},
|
|
16
7
|
"organizeImports": {
|
|
17
8
|
"enabled": true
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "envilder",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "A CLI tool to generate .env files from AWS SSM parameters",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -11,16 +11,19 @@
|
|
|
11
11
|
"envilder": "lib/cli/cliRunner.js"
|
|
12
12
|
},
|
|
13
13
|
"scripts": {
|
|
14
|
-
"clean": "rimraf lib",
|
|
15
|
-
"test-run": "
|
|
16
|
-
"build": "
|
|
17
|
-
"
|
|
14
|
+
"clean": "npx jest --clearCache && yarn cache clean --force && npx rimraf lib && npx rimraf node_modules && npx rimraf coverage && npx rimraf yarn.lock",
|
|
15
|
+
"test-run": "yarn build && node lib/cli/cliRunner.js --map=tests/sample/param-map.json --envfile=tests/sample/autogenerated.env",
|
|
16
|
+
"build": "tsc -p tsconfig.build.json --sourceMap --declaration",
|
|
17
|
+
"format": "npx biome format",
|
|
18
|
+
"format:write": "npx biome format --write",
|
|
19
|
+
"lint": "npx secretlint \"**/*\" && biome lint --write && biome format --write && biome check --write && tsc --noEmit",
|
|
20
|
+
"lint:fix": "npx biome lint --fix",
|
|
18
21
|
"test": "vitest run --reporter verbose --coverage",
|
|
19
|
-
"cli-run": "
|
|
20
|
-
"npm-publish": "
|
|
21
|
-
"npm-release-patch": "
|
|
22
|
-
"npm-release-minor": "
|
|
23
|
-
"npm-release-prerelease": "
|
|
22
|
+
"cli-run": "yarn build && node --trace-warnings lib",
|
|
23
|
+
"npm-publish": "yarn lint && yarn build && yarn publish",
|
|
24
|
+
"npm-release-patch": "yarn version --new-version patch",
|
|
25
|
+
"npm-release-minor": "yarn version --new-version minor",
|
|
26
|
+
"npm-release-prerelease": "yarn version --new-version prerelease"
|
|
24
27
|
},
|
|
25
28
|
"keywords": [],
|
|
26
29
|
"repository": {
|
|
@@ -37,29 +40,25 @@
|
|
|
37
40
|
},
|
|
38
41
|
"type": "module",
|
|
39
42
|
"dependencies": {
|
|
40
|
-
"@aws-sdk/client-ssm": "^3.
|
|
41
|
-
"@
|
|
42
|
-
"@secretlint/secretlint-rule-preset-recommend": "^9.0.0",
|
|
43
|
+
"@aws-sdk/client-ssm": "^3.806.0",
|
|
44
|
+
"@aws-sdk/credential-providers": "^3.806.0",
|
|
43
45
|
"@types/node": "^22.5.5",
|
|
44
|
-
"commander": "^
|
|
46
|
+
"commander": "^13.1.0",
|
|
45
47
|
"dotenv": "^16.4.5",
|
|
46
48
|
"picocolors": "^1.1.0"
|
|
47
49
|
},
|
|
48
50
|
"devDependencies": {
|
|
49
51
|
"@biomejs/biome": "^1.9.1",
|
|
52
|
+
"@secretlint/secretlint-rule-preset-recommend": "^9.3.2",
|
|
50
53
|
"@vitest/coverage-v8": "^3.1.1",
|
|
51
54
|
"rimraf": "^6.0.1",
|
|
52
|
-
"secretlint": "^9.
|
|
55
|
+
"secretlint": "^9.3.2",
|
|
53
56
|
"ts-node": "^10.9.2",
|
|
54
57
|
"typescript": "^5.6.2",
|
|
55
58
|
"vitest": "^3.1.1"
|
|
56
59
|
},
|
|
57
|
-
"resolutions": {
|
|
58
|
-
"string-width": "4.2.3",
|
|
59
|
-
"strip-ansi": "6.0.1"
|
|
60
|
-
},
|
|
61
60
|
"engines": {
|
|
62
61
|
"node": ">=20.0.0",
|
|
63
62
|
"yarn": ">=1.22"
|
|
64
63
|
}
|
|
65
|
-
}
|
|
64
|
+
}
|
package/src/cli/cliRunner.ts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
2
|
import { Command } from 'commander';
|
|
4
3
|
import { run } from '../index.js';
|
|
5
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Parses CLI arguments and runs the environment file generator.
|
|
7
|
+
*
|
|
8
|
+
* 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.
|
|
9
|
+
*
|
|
10
|
+
* @throws {Error} If either `--map` or `--envfile` arguments are missing.
|
|
11
|
+
*/
|
|
6
12
|
export async function cliRunner() {
|
|
7
13
|
const program = new Command();
|
|
8
14
|
|
|
@@ -11,7 +17,8 @@ export async function cliRunner() {
|
|
|
11
17
|
.description('A CLI tool to generate .env files from AWS SSM parameters')
|
|
12
18
|
.version('0.1.0')
|
|
13
19
|
.requiredOption('--map <path>', 'Path to the JSON file with environment variable mapping')
|
|
14
|
-
.requiredOption('--envfile <path>', 'Path to the .env file to be generated')
|
|
20
|
+
.requiredOption('--envfile <path>', 'Path to the .env file to be generated')
|
|
21
|
+
.option('--profile <name>', 'AWS CLI profile to use');
|
|
15
22
|
|
|
16
23
|
await program.parseAsync(process.argv);
|
|
17
24
|
const options = program.opts();
|
|
@@ -20,7 +27,7 @@ export async function cliRunner() {
|
|
|
20
27
|
throw new Error('Missing required arguments: --map and --envfile');
|
|
21
28
|
}
|
|
22
29
|
|
|
23
|
-
await run(options.map, options.envfile);
|
|
30
|
+
await run(options.map, options.envfile, options.profile);
|
|
24
31
|
}
|
|
25
32
|
|
|
26
33
|
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
|
});
|