env-secrets 0.1.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/.eslintignore +2 -0
- package/.eslintrc +15 -0
- package/.nvmrc +1 -0
- package/.prettierrc +6 -0
- package/LICENSE +21 -0
- package/README.md +15 -0
- package/package.json +40 -0
- package/src/index.ts +42 -0
- package/src/vaults/secretsmanager.ts +101 -0
- package/src/vaults/utils.ts +29 -0
- package/src/version.ts +1 -0
- package/tsconfig.json +16 -0
package/.eslintignore
ADDED
package/.eslintrc
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"root": true,
|
|
3
|
+
"parser": "@typescript-eslint/parser",
|
|
4
|
+
"plugins": ["@typescript-eslint", "prettier"],
|
|
5
|
+
"extends": [
|
|
6
|
+
"eslint:recommended",
|
|
7
|
+
"plugin:@typescript-eslint/eslint-recommended",
|
|
8
|
+
"plugin:@typescript-eslint/recommended",
|
|
9
|
+
"prettier"
|
|
10
|
+
],
|
|
11
|
+
"rules": {
|
|
12
|
+
"no-console": 1,
|
|
13
|
+
"prettier/prettier": 2
|
|
14
|
+
}
|
|
15
|
+
}
|
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
v16.17.0
|
package/.prettierrc
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Mark C Allen (@markcallen)
|
|
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
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# env-secrets
|
|
2
|
+
|
|
3
|
+
Get secrets from a vault and inject them as environment variables
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
Install node
|
|
8
|
+
|
|
9
|
+
## Debug
|
|
10
|
+
|
|
11
|
+
Use debug-js pass in env-secrets for the main application and env-secrets:<vault> for vault specific debugging
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
DEBUG=env-secrets,env-secrets:secretsmanager npx ts-node src/index.ts aws -s local/sample -r ca-central-1 -p marka -- env
|
|
15
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "env-secrets",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "get secrets from a secrets vault and inject them into the running environment",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"author": "Mark C Allen (@markcallen)",
|
|
7
|
+
"repository": "markcallen/env-secrets",
|
|
8
|
+
"homepage": "https://github.com/markcallen/env-secrets",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"private": false,
|
|
11
|
+
"scripts": {
|
|
12
|
+
"prebuild": "node -p \"'export const LIB_VERSION = ' + JSON.stringify(require('./package.json').version) + ';'\" > src/version.ts",
|
|
13
|
+
"build": "rimraf ./dist && tsc -b",
|
|
14
|
+
"postbuild": "chmod 755 ./dist/index.js",
|
|
15
|
+
"lint": "eslint . --ext .ts",
|
|
16
|
+
"prettier:fix": "npx prettier --write .",
|
|
17
|
+
"prettier:check": "npx prettier --check ."
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/debug": "^4.1.7",
|
|
21
|
+
"@types/node": "^18.11.9",
|
|
22
|
+
"@typescript-eslint/eslint-plugin": "^5.43.0",
|
|
23
|
+
"@typescript-eslint/parser": "^5.43.0",
|
|
24
|
+
"eslint": "^8.27.0",
|
|
25
|
+
"eslint-config-prettier": "^8.5.0",
|
|
26
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
27
|
+
"prettier": "^2.7.1",
|
|
28
|
+
"rimraf": "^3.0.2",
|
|
29
|
+
"ts-node": "^10.9.1",
|
|
30
|
+
"typescript": "^4.8.4"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"aws-sdk": "^2.1257.0",
|
|
34
|
+
"commander": "^9.4.1",
|
|
35
|
+
"debug": "^4.3.4"
|
|
36
|
+
},
|
|
37
|
+
"bin": {
|
|
38
|
+
"env-secrets": "./dist/index.js"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { Command, Argument } from 'commander';
|
|
4
|
+
import { spawn } from 'node:child_process';
|
|
5
|
+
import Debug from 'debug';
|
|
6
|
+
|
|
7
|
+
import { LIB_VERSION } from './version';
|
|
8
|
+
import { secretsmanager } from './vaults/secretsmanager';
|
|
9
|
+
|
|
10
|
+
const debug = Debug('env-secrets');
|
|
11
|
+
|
|
12
|
+
const program = new Command();
|
|
13
|
+
|
|
14
|
+
program
|
|
15
|
+
.name('env-secrets')
|
|
16
|
+
.description(
|
|
17
|
+
'pull secrets from vaults and inject them into the running environment'
|
|
18
|
+
)
|
|
19
|
+
.version(LIB_VERSION);
|
|
20
|
+
|
|
21
|
+
program
|
|
22
|
+
.command('aws')
|
|
23
|
+
.description('get secrets from AWS secrets manager')
|
|
24
|
+
.addArgument(new Argument('[program...]', 'program to run'))
|
|
25
|
+
.requiredOption('-s, --secret <secret>', 'secret to get')
|
|
26
|
+
.option('-p, --profile <profile>', 'profile to use')
|
|
27
|
+
.option('-r, --region <region>', 'region to use')
|
|
28
|
+
.action(async (program, options) => {
|
|
29
|
+
let env = await secretsmanager(options);
|
|
30
|
+
env = Object.assign({}, process.env, env);
|
|
31
|
+
debug(env);
|
|
32
|
+
if (program) {
|
|
33
|
+
debug(`${program[0]} ${program.slice(1)}`);
|
|
34
|
+
spawn(program[0], program.slice(1), {
|
|
35
|
+
stdio: 'inherit',
|
|
36
|
+
shell: true,
|
|
37
|
+
env
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
program.parse();
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import AWS from 'aws-sdk';
|
|
2
|
+
import Debug from 'debug';
|
|
3
|
+
|
|
4
|
+
const debug = Debug('env-secrets:secretsmanager');
|
|
5
|
+
|
|
6
|
+
interface secretsmanagerType {
|
|
7
|
+
secret: string;
|
|
8
|
+
profile?: string;
|
|
9
|
+
region?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const checkConnection = async () => {
|
|
13
|
+
const sts = new AWS.STS();
|
|
14
|
+
|
|
15
|
+
const myPromise = new Promise((resolve, reject) => {
|
|
16
|
+
sts.getCallerIdentity({}, (err, data) => {
|
|
17
|
+
if (err) reject(err);
|
|
18
|
+
else {
|
|
19
|
+
resolve(data);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
let value;
|
|
25
|
+
let err;
|
|
26
|
+
|
|
27
|
+
await myPromise
|
|
28
|
+
.then((v) => {
|
|
29
|
+
value = v;
|
|
30
|
+
})
|
|
31
|
+
.catch((e) => {
|
|
32
|
+
err = e;
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
if (err) {
|
|
36
|
+
console.error(err);
|
|
37
|
+
}
|
|
38
|
+
debug(value);
|
|
39
|
+
|
|
40
|
+
return !!value;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const secretsmanager = async (options: secretsmanagerType) => {
|
|
44
|
+
const { secret, profile, region } = options;
|
|
45
|
+
const {
|
|
46
|
+
AWS_ACCESS_KEY_ID: awsAccessKeyId,
|
|
47
|
+
AWS_SECRET_ACCESS_KEY: awsSecretAccessKey
|
|
48
|
+
} = process.env;
|
|
49
|
+
if (profile) {
|
|
50
|
+
console.log(`Using profile: ${profile}`);
|
|
51
|
+
const credentials = new AWS.SharedIniFileCredentials({
|
|
52
|
+
profile
|
|
53
|
+
});
|
|
54
|
+
AWS.config.credentials = credentials;
|
|
55
|
+
} else if (awsAccessKeyId && awsSecretAccessKey) {
|
|
56
|
+
console.log('Using environment variables');
|
|
57
|
+
} else {
|
|
58
|
+
console.log('Using profile: default');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (region) {
|
|
62
|
+
AWS.config.update({ region });
|
|
63
|
+
}
|
|
64
|
+
if (!AWS.config.region) {
|
|
65
|
+
console.log('no region set');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const connected = await checkConnection();
|
|
69
|
+
|
|
70
|
+
if (connected) {
|
|
71
|
+
const sm = new AWS.SecretsManager();
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
const response = await sm
|
|
75
|
+
.getSecretValue({
|
|
76
|
+
SecretId: secret
|
|
77
|
+
})
|
|
78
|
+
.promise();
|
|
79
|
+
|
|
80
|
+
const secretvalue = response.SecretString;
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
if (secretvalue) {
|
|
84
|
+
return JSON.parse(secretvalue);
|
|
85
|
+
}
|
|
86
|
+
} catch (err) {
|
|
87
|
+
console.error(err);
|
|
88
|
+
}
|
|
89
|
+
} catch (err: any) {
|
|
90
|
+
if (err && err.code === 'ResourceNotFoundException') {
|
|
91
|
+
console.error(`${secret} not found`);
|
|
92
|
+
} else if (err && err.code === 'ConfigError') {
|
|
93
|
+
console.error(err.message);
|
|
94
|
+
} else {
|
|
95
|
+
console.error(err);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return {};
|
|
100
|
+
}
|
|
101
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as os from 'os';
|
|
2
|
+
|
|
3
|
+
export const replaceWithAstrisk = (str: string | undefined) => {
|
|
4
|
+
if (str) {
|
|
5
|
+
return [...str]
|
|
6
|
+
.map((e, i) => {
|
|
7
|
+
if (i > 0 && i < str.length - 4) {
|
|
8
|
+
return '*';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return e;
|
|
12
|
+
})
|
|
13
|
+
.join('');
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const objectToExport = (obj: Record<string, any>) => {
|
|
18
|
+
return Object.entries(obj).reduce(
|
|
19
|
+
(env, [OutputKey, OutputValue]) =>
|
|
20
|
+
`${env}export ${OutputKey}=${OutputValue}${os.EOL}`,
|
|
21
|
+
''
|
|
22
|
+
);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const objectToEnv = (obj: Record<string, any>) => {
|
|
26
|
+
return Object.entries(obj).map(
|
|
27
|
+
([OutputKey, OutputValue]) => (process.env[OutputKey] = OutputValue)
|
|
28
|
+
);
|
|
29
|
+
};
|
package/src/version.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const LIB_VERSION = "0.1.0";
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2016",
|
|
4
|
+
"lib": ["es6"],
|
|
5
|
+
"module": "commonjs",
|
|
6
|
+
"rootDir": "src",
|
|
7
|
+
"resolveJsonModule": true,
|
|
8
|
+
"allowJs": true,
|
|
9
|
+
"outDir": "dist",
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
|
12
|
+
"strict": true,
|
|
13
|
+
"noImplicitAny": true,
|
|
14
|
+
"skipLibCheck": true
|
|
15
|
+
}
|
|
16
|
+
}
|