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