@dsmrt/axiom-aws-sdk 0.0.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/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./ssm-parameters";
2
+ export * from "./ssm-parameter-collection";
package/lib/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./ssm-parameters"), exports);
18
+ __exportStar(require("./ssm-parameter-collection"), exports);
@@ -0,0 +1,15 @@
1
+ import { Parameter, SSMClient } from "@aws-sdk/client-ssm";
2
+ export type Params = {
3
+ [key: string]: Parameter;
4
+ };
5
+ export declare class ParameterCollection<TParams extends Params> {
6
+ readonly path: string;
7
+ private readonly client?;
8
+ readonly map: Map<keyof TParams, Parameter>;
9
+ constructor(path: string, client?: SSMClient | undefined);
10
+ hasParam(param: keyof TParams): Promise<boolean>;
11
+ findParam(param: keyof TParams): Promise<Parameter | undefined>;
12
+ getParam(param: keyof TParams): Promise<Parameter>;
13
+ get(): Promise<Map<keyof TParams, Parameter>>;
14
+ private loadParameters;
15
+ }
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ParameterCollection = void 0;
4
+ const ssm_parameters_1 = require("./ssm-parameters");
5
+ class ParameterCollection {
6
+ path;
7
+ client;
8
+ map = new Map();
9
+ constructor(path, client) {
10
+ this.path = path;
11
+ this.client = client;
12
+ }
13
+ async hasParam(param) {
14
+ if (this.map.size < 1) {
15
+ await this.loadParameters();
16
+ }
17
+ return this.map.has(param);
18
+ }
19
+ async findParam(param) {
20
+ if (this.map.size < 1) {
21
+ await this.loadParameters();
22
+ }
23
+ return this.map.get(param);
24
+ }
25
+ async getParam(param) {
26
+ if (!(await this.hasParam(param))) {
27
+ throw new Error(`Parameter '${param.toString()}' does not exist'`);
28
+ }
29
+ return this.map.get(param);
30
+ }
31
+ async get() {
32
+ if (this.map.size < 1) {
33
+ await this.loadParameters();
34
+ }
35
+ return this.map;
36
+ }
37
+ async loadParameters() {
38
+ if (!this.path) {
39
+ throw new Error(`Parameter path is not set or is undefined`);
40
+ }
41
+ const params = await (0, ssm_parameters_1.getParametersByPath)(this.path, undefined, undefined, this.client);
42
+ params.forEach((param) => {
43
+ const fullName = param.Name;
44
+ const name = fullName.replace(`${this.path}/`, "");
45
+ this.map.set(name, param);
46
+ });
47
+ }
48
+ }
49
+ exports.ParameterCollection = ParameterCollection;
@@ -0,0 +1,11 @@
1
+ import { SSMClient, Parameter } from "@aws-sdk/client-ssm";
2
+ export declare const ssmClient: SSMClient;
3
+ export declare const getParametersByPath: (path: string, parameters?: Parameter[], nextToken?: string, client?: SSMClient) => Promise<Parameter[]>;
4
+ export declare const getParameters: (names: string[], client?: SSMClient) => Promise<Parameter[]>;
5
+ /**
6
+ * Extract a single parameter from an array of params (typically from params by path)
7
+ *
8
+ * @param name
9
+ * @param params
10
+ */
11
+ export declare const extractParamValue: (path: string, name: string, params: Parameter[]) => string;
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extractParamValue = exports.getParameters = exports.getParametersByPath = exports.ssmClient = void 0;
4
+ const client_ssm_1 = require("@aws-sdk/client-ssm");
5
+ exports.ssmClient = new client_ssm_1.SSMClient({});
6
+ // to use this, you need to give the lambda function permission to the
7
+ // ** CloudFormation Example **
8
+ // Resources:
9
+ // LambdaPolicies:
10
+ // Type: AWS::IAM::Policy
11
+ // Properties:
12
+ // PolicyName: LambdaRole
13
+ // PolicyDocument:
14
+ // Statement:
15
+ // - Effect: Allow
16
+ // Action:
17
+ // - "ssm:GetParameters"
18
+ // - "ssm:GetParametersByPath"
19
+ // Resource: !Sub 'arn:aws:ssm:*:*:parameter/${PARAMETER_PATH}/*'
20
+ const getParametersByPath = async (path, parameters, nextToken, client) => {
21
+ client ??= exports.ssmClient;
22
+ let returnParams = parameters || [];
23
+ const command = new client_ssm_1.GetParametersByPathCommand({
24
+ Path: path,
25
+ Recursive: true,
26
+ WithDecryption: true,
27
+ NextToken: nextToken,
28
+ });
29
+ const output = await client.send(command);
30
+ // merge parameters
31
+ if (output.Parameters !== undefined) {
32
+ returnParams = [...returnParams, ...output.Parameters];
33
+ }
34
+ if (output.NextToken !== undefined) {
35
+ return (0, exports.getParametersByPath)(path, returnParams, output.NextToken, client);
36
+ }
37
+ // return all
38
+ return returnParams;
39
+ };
40
+ exports.getParametersByPath = getParametersByPath;
41
+ // ** CloudFormation Example **
42
+ // Resources:
43
+ // LambdaPolicies:
44
+ // Type: AWS::IAM::Policy
45
+ // Properties:
46
+ // PolicyName: LambdaRole
47
+ // PolicyDocument:
48
+ // Statement:
49
+ // - Effect: Allow
50
+ // Action:
51
+ // - "ssm:GetParameters"
52
+ // - "ssm:GetParametersByPath"
53
+ // Resource:
54
+ // - ${name1}
55
+ // - ${name2}
56
+ // - ${name3
57
+ const getParameters = async (names, client) => {
58
+ client ??= exports.ssmClient;
59
+ const command = new client_ssm_1.GetParametersCommand({
60
+ Names: names,
61
+ WithDecryption: true,
62
+ });
63
+ const result = await client.send(command);
64
+ if (result.Parameters === undefined) {
65
+ return [];
66
+ }
67
+ return result.Parameters;
68
+ };
69
+ exports.getParameters = getParameters;
70
+ /**
71
+ * Extract a single parameter from an array of params (typically from params by path)
72
+ *
73
+ * @param name
74
+ * @param params
75
+ */
76
+ const extractParamValue = (path, name, params) => {
77
+ const fullName = `${path.replace(/\/$/, "")}/${name}`;
78
+ const item = params.find((item) => item.Name === fullName);
79
+ if (!item || !item.Value) {
80
+ throw new Error(`Parameter '${fullName}' is not set.`);
81
+ }
82
+ return item.Value;
83
+ };
84
+ exports.extractParamValue = extractParamValue;
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@dsmrt/axiom-aws-sdk",
3
+ "version": "0.0.1",
4
+ "description": "AWS sdk library for working with axiom cli",
5
+ "main": "lib/index.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git@github.com:dsmrt/axiom.git",
9
+ "directory": "aws-sdk"
10
+ },
11
+ "publishConfig": {
12
+ "registry": "https://registry.npmjs.org/"
13
+ },
14
+ "scripts": {
15
+ "lint": "eslint ./src/ --ext .ts",
16
+ "lint:fix": "eslint ./src/ --ext .ts --fix",
17
+ "test": "vitest run --coverage -c ./vitest.config.ts",
18
+ "watch": "vitest watch --coverage -c ./vitest.config.ts",
19
+ "build": "tsc -d -p ./tsconfig.json"
20
+ },
21
+ "keywords": [
22
+ "axiom",
23
+ "config",
24
+ "aws"
25
+ ],
26
+ "author": {
27
+ "name": "Damien Smrt",
28
+ "url": "https://dsmrt.com"
29
+ },
30
+ "license": "MIT",
31
+ "devDependencies": {
32
+ "@types/node": "^20.9.0",
33
+ "@typescript-eslint/eslint-plugin": "^6.12.0",
34
+ "@typescript-eslint/parser": "^6.12.0",
35
+ "@vitest/coverage-v8": "^0.34.6",
36
+ "eslint": "^8.54.0",
37
+ "prettier": "^3.1.0",
38
+ "ts-node": "^10.9.1",
39
+ "typescript": "^5.2.2",
40
+ "vitest": "^0.34.6"
41
+ },
42
+ "dependencies": {
43
+ "@aws-sdk/client-ssm": "^3.451.0",
44
+ "@aws-sdk/client-sts": "^3.454.0",
45
+ "@aws-sdk/credential-providers": "^3.454.0",
46
+ "@aws-sdk/types": "^3.451.0"
47
+ }
48
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2022",
4
+ "module": "node16",
5
+ "strict": true,
6
+ "esModuleInterop": true,
7
+ "moduleResolution": "node16",
8
+ "outDir": "lib"
9
+ },
10
+ "include": ["src/index.ts"]
11
+ }