@gearbox-protocol/cli-utils 5.34.0 → 5.35.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/dist/SSMManager.d.ts +5 -0
- package/dist/SSMManager.js +32 -0
- package/dist/SecretsManager.d.ts +6 -0
- package/dist/SecretsManager.js +54 -0
- package/dist/getPromisePath.d.ts +7 -0
- package/dist/getPromisePath.js +17 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/resolveYamlFiles.d.ts +3 -2
- package/dist/resolveYamlFiles.js +23 -5
- package/package.json +3 -1
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { GetParameterCommand, SSMClient } from "@aws-sdk/client-ssm";
|
|
2
|
+
export class SSMManager {
|
|
3
|
+
#ssmClient;
|
|
4
|
+
#parameters = new Map();
|
|
5
|
+
async parameter(name) {
|
|
6
|
+
if (this.#parameters.has(name)) {
|
|
7
|
+
return this.#parameters.get(name);
|
|
8
|
+
}
|
|
9
|
+
if (!this.#ssmClient) {
|
|
10
|
+
this.#ssmClient = new SSMClient({});
|
|
11
|
+
}
|
|
12
|
+
const response = await this.#ssmClient.send(new GetParameterCommand({
|
|
13
|
+
Name: name,
|
|
14
|
+
}));
|
|
15
|
+
const value = response.Parameter?.Value;
|
|
16
|
+
if (!value) {
|
|
17
|
+
throw new Error(`parameter ${name} value not found`);
|
|
18
|
+
}
|
|
19
|
+
this.#parameters.set(name, value);
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export const ssmManagerProxy = (mgr) => new Proxy(mgr ?? new SSMManager(), {
|
|
24
|
+
get(target, prop) {
|
|
25
|
+
if (prop === "parameter") {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
if (typeof prop === "string") {
|
|
29
|
+
return target.parameter(prop);
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { GetSecretValueCommand, SecretsManagerClient, } from "@aws-sdk/client-secrets-manager";
|
|
2
|
+
export class SecretsManager {
|
|
3
|
+
#smClient;
|
|
4
|
+
#secrets = new Map();
|
|
5
|
+
async secret(key) {
|
|
6
|
+
const [SecretId, ...jsonPath] = key.split(".");
|
|
7
|
+
const secretStr = await this.#getSecret(SecretId);
|
|
8
|
+
if (!jsonPath.length) {
|
|
9
|
+
return secretStr;
|
|
10
|
+
}
|
|
11
|
+
let v = JSON.parse(secretStr);
|
|
12
|
+
for (const p of jsonPath) {
|
|
13
|
+
v = v[p];
|
|
14
|
+
}
|
|
15
|
+
if (!v || typeof v !== "string") {
|
|
16
|
+
throw new Error(`cannot find secret '${key}'`);
|
|
17
|
+
}
|
|
18
|
+
return v;
|
|
19
|
+
}
|
|
20
|
+
async secretRaw(key) {
|
|
21
|
+
const val = await this.#getSecret(key);
|
|
22
|
+
try {
|
|
23
|
+
return JSON.parse(val);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return val;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async #getSecret(SecretId) {
|
|
30
|
+
if (this.#secrets.has(SecretId)) {
|
|
31
|
+
return this.#secrets.get(SecretId);
|
|
32
|
+
}
|
|
33
|
+
if (!this.#smClient) {
|
|
34
|
+
this.#smClient = new SecretsManagerClient({});
|
|
35
|
+
}
|
|
36
|
+
// https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/ssm/command/GetParameterCommand/
|
|
37
|
+
const { SecretString } = await this.#smClient.send(new GetSecretValueCommand({ SecretId }));
|
|
38
|
+
if (!SecretString) {
|
|
39
|
+
throw new Error(`secret '${SecretId}' not found`);
|
|
40
|
+
}
|
|
41
|
+
this.#secrets.set(SecretId, SecretString);
|
|
42
|
+
return SecretString;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export const secretsManagerProxy = (mgr) => new Proxy(mgr ?? new SecretsManager(), {
|
|
46
|
+
get(target, prop) {
|
|
47
|
+
if (prop === "secret" || prop === "secretRaw") {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
if (typeof prop === "string") {
|
|
51
|
+
return target.secretRaw(prop);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Similar to lodash.get, but supports promises
|
|
3
|
+
* @param object
|
|
4
|
+
* @param keys
|
|
5
|
+
* @returns
|
|
6
|
+
*/
|
|
7
|
+
export default async function getPromisePath(object, path) {
|
|
8
|
+
return getPromised(object, path.split("."));
|
|
9
|
+
}
|
|
10
|
+
function getPromised(object, keys) {
|
|
11
|
+
if (keys.length === 0) {
|
|
12
|
+
return Promise.resolve(object);
|
|
13
|
+
}
|
|
14
|
+
const [key, ...rest] = keys;
|
|
15
|
+
const value = object[key];
|
|
16
|
+
return Promise.resolve(value).then(v => getPromised(v, rest));
|
|
17
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import YAML from "yaml";
|
|
2
|
+
export type TemplateData = Record<string, any>;
|
|
2
3
|
/**
|
|
3
4
|
* Merges yaml files, respecting .extends references.
|
|
4
5
|
* Performs env variable substitution on the config.
|
|
@@ -10,7 +11,7 @@ import YAML from "yaml";
|
|
|
10
11
|
* @param templateData
|
|
11
12
|
* @returns
|
|
12
13
|
*/
|
|
13
|
-
export declare function resolveYamlFiles(file: string, templateData?:
|
|
14
|
+
export declare function resolveYamlFiles(file: string, templateData?: TemplateData): Promise<object>;
|
|
14
15
|
/**
|
|
15
16
|
* Merges yaml documents, respecting .extends references.
|
|
16
17
|
* Optionally, performs env-style variable substitution on the config.
|
|
@@ -20,4 +21,4 @@ export declare function resolveYamlFiles(file: string, templateData?: Record<str
|
|
|
20
21
|
* @param templateData
|
|
21
22
|
* @returns
|
|
22
23
|
*/
|
|
23
|
-
export declare function resolveYamlDoc(file: string, templateData?:
|
|
24
|
+
export declare function resolveYamlDoc(file: string, templateData?: TemplateData): Promise<YAML.Document>;
|
package/dist/resolveYamlFiles.js
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import { readFile } from "node:fs/promises";
|
|
2
2
|
import { dirname, resolve as pathResolve } from "node:path";
|
|
3
|
-
import { template } from "lodash-es";
|
|
4
3
|
import YAML, { isPair, isScalar, isSeq } from "yaml";
|
|
4
|
+
import getPromisePath from "./getPromisePath.js";
|
|
5
|
+
/**
|
|
6
|
+
* Template key regexp
|
|
7
|
+
* Consists of alphanumeric+underscore parts separated by dots, all wrappped in env-style brackets
|
|
8
|
+
* e.g. ${KEY} or ${KEY.SUBKEY}
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
const templateKeyRegex = /^\$\{(?:\w+(?:\.\w+)*)\}$/;
|
|
5
12
|
/**
|
|
6
13
|
* Merges yaml files, respecting .extends references.
|
|
7
14
|
* Performs env variable substitution on the config.
|
|
@@ -29,10 +36,10 @@ export async function resolveYamlFiles(file, templateData) {
|
|
|
29
36
|
*/
|
|
30
37
|
export async function resolveYamlDoc(file, templateData) {
|
|
31
38
|
let content = await readFile(file, "utf-8");
|
|
32
|
-
if (templateData) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
39
|
+
// if (templateData) {
|
|
40
|
+
// const compiled = template(content);
|
|
41
|
+
// content = compiled(templateData);
|
|
42
|
+
// }
|
|
36
43
|
let cfgRaw = YAML.parseDocument(content, { merge: true });
|
|
37
44
|
// let cfgRaw = YAML.parse(content, { merge: true });
|
|
38
45
|
if (cfgRaw.has("extends")) {
|
|
@@ -53,6 +60,17 @@ export async function resolveYamlDoc(file, templateData) {
|
|
|
53
60
|
},
|
|
54
61
|
});
|
|
55
62
|
}
|
|
63
|
+
await YAML.visitAsync(cfgRaw, {
|
|
64
|
+
Scalar: async (_, node) => {
|
|
65
|
+
if (typeof node.value === "string" && templateKeyRegex.test(node.value)) {
|
|
66
|
+
const templateKey = node.value.slice(2, -1);
|
|
67
|
+
const templateValue = getPromisePath(templateData, templateKey);
|
|
68
|
+
if (templateValue) {
|
|
69
|
+
node.value = await Promise.resolve(templateValue);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
});
|
|
56
74
|
return cfgRaw;
|
|
57
75
|
}
|
|
58
76
|
function getNodesPath(nodes) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gearbox-protocol/cli-utils",
|
|
3
3
|
"description": "Utils for creating cli apps",
|
|
4
|
-
"version": "5.
|
|
4
|
+
"version": "5.35.0",
|
|
5
5
|
"homepage": "https://gearbox.fi",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"gearbox"
|
|
@@ -33,6 +33,8 @@
|
|
|
33
33
|
"package:version": "yarn version"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
+
"@aws-sdk/client-secrets-manager": "^3.823.0",
|
|
37
|
+
"@aws-sdk/client-ssm": "^3.823.0",
|
|
36
38
|
"abitype": "^1.0.8",
|
|
37
39
|
"commander": "^14.0.0",
|
|
38
40
|
"lodash-es": "^4.17.21",
|