@x-sls/env 1.0.0 → 1.0.2
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/package.json +2 -6
- package/src/index.js +24 -22
package/package.json
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-sls/env",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"main": "src/index.js",
|
|
5
|
-
"scripts": {
|
|
6
|
-
"test": "jest",
|
|
7
|
-
"lint": "eslint src --ext .js",
|
|
8
|
-
"clean": "rm -rf dist"
|
|
9
|
-
},
|
|
5
|
+
"scripts": {},
|
|
10
6
|
"dependencies": {
|
|
11
7
|
"@aws-sdk/client-ssm": "^3.840.0"
|
|
12
8
|
}
|
package/src/index.js
CHANGED
|
@@ -1,42 +1,44 @@
|
|
|
1
|
-
const { SSMClient, GetParameterCommand } = require(
|
|
2
|
-
const client = new SSMClient();
|
|
1
|
+
const { SSMClient, GetParameterCommand } = require('@aws-sdk/client-ssm')
|
|
3
2
|
|
|
4
|
-
const
|
|
5
|
-
const SSM_SECRET_PREFIX = `ssm:x:`;
|
|
3
|
+
const client = new SSMClient()
|
|
6
4
|
|
|
7
|
-
const
|
|
5
|
+
const SSM_PREFIX = 'ssm:'
|
|
6
|
+
const SSM_SECRET_PREFIX = `ssm:x:`
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
if (!lookup[key]) {
|
|
11
|
-
lookup[key] = await resolveEnv(process.env[key]);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
return lookup[key];
|
|
15
|
-
}
|
|
8
|
+
const lookup = {}
|
|
16
9
|
|
|
17
10
|
async function resolveEnv(val) {
|
|
18
11
|
// If this is not an SSM key, return the value as-is
|
|
19
12
|
if (val.substr(0, SSM_PREFIX.length) !== SSM_PREFIX) {
|
|
20
|
-
return val
|
|
13
|
+
return val
|
|
21
14
|
}
|
|
22
15
|
|
|
23
16
|
// values with `ssm:x:` prefix are SecureString and need to be decrypted
|
|
24
17
|
const WithDecryption =
|
|
25
|
-
val.substr(0, SSM_SECRET_PREFIX.length) === SSM_SECRET_PREFIX
|
|
18
|
+
val.substr(0, SSM_SECRET_PREFIX.length) === SSM_SECRET_PREFIX
|
|
26
19
|
const Name = val.substr(
|
|
27
|
-
WithDecryption ? SSM_SECRET_PREFIX.length : SSM_PREFIX.length
|
|
28
|
-
)
|
|
20
|
+
WithDecryption ? SSM_SECRET_PREFIX.length : SSM_PREFIX.length
|
|
21
|
+
)
|
|
29
22
|
|
|
30
23
|
try {
|
|
31
24
|
const command = new GetParameterCommand({
|
|
32
25
|
Name,
|
|
33
|
-
WithDecryption
|
|
34
|
-
})
|
|
35
|
-
const res = await client.send(command)
|
|
36
|
-
return res?.Parameter?.Value
|
|
26
|
+
WithDecryption
|
|
27
|
+
})
|
|
28
|
+
const res = await client.send(command)
|
|
29
|
+
return res?.Parameter?.Value
|
|
37
30
|
} catch (error) {
|
|
38
|
-
console.error(error)
|
|
31
|
+
console.error(error)
|
|
32
|
+
return null
|
|
39
33
|
}
|
|
40
34
|
}
|
|
41
35
|
|
|
42
|
-
|
|
36
|
+
async function getEnv(key) {
|
|
37
|
+
if (!lookup[key]) {
|
|
38
|
+
lookup[key] = await resolveEnv(process.env[key])
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return lookup[key]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
module.exports = getEnv
|