@bifravst/aws-ssm-settings-helpers 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/LICENSE +29 -0
- package/README.md +15 -0
- package/dist/isNotEmpty.d.ts +2 -0
- package/dist/isNotEmpty.js +2 -0
- package/dist/isNullOrUndefined.d.ts +1 -0
- package/dist/isNullOrUndefined.js +1 -0
- package/dist/paginate.d.ts +8 -0
- package/dist/paginate.js +17 -0
- package/dist/settings.d.ts +35 -0
- package/dist/settings.js +86 -0
- package/dist/settings.spec.d.ts +1 -0
- package/dist/settings.spec.js +66 -0
- package/package.json +91 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024, Nordic Semiconductor ASA | nordicsemi.no
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# AWS SSM Settings helpers [](https://www.npmjs.com/package/@bifravst/aws-ssm-settings-helpers)
|
|
2
|
+
|
|
3
|
+
[](https://github.com/bifravst/aws-ssm-settings-helpers/actions)
|
|
4
|
+
[](https://github.com/semantic-release/semantic-release)
|
|
5
|
+
[](https://renovatebot.com)
|
|
6
|
+
[](https://github.com/conventional-changelog/commitlint/tree/master/@commitlint/config-conventional)
|
|
7
|
+
[](https://github.com/prettier/prettier/)
|
|
8
|
+
[](https://github.com/typescript-eslint/typescript-eslint)
|
|
9
|
+
|
|
10
|
+
Helper functions written in TypeScript for storing and retrieving application
|
|
11
|
+
settings in AWS SSM Parameter Store.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
npm i --save --save-exact @bifravst/aws-ssm-settings-helpers
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const isNullOrUndefined: (arg?: unknown) => arg is null | undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const isNullOrUndefined = (arg) => arg === undefined || arg === null;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Iteratively follows paginated results.
|
|
3
|
+
* NOTE: This method has no upper runtime limit and may time out.
|
|
4
|
+
*/
|
|
5
|
+
export declare const paginate: ({ paginator, startKey, }: {
|
|
6
|
+
paginator: (startKey?: any) => Promise<unknown>;
|
|
7
|
+
startKey?: any;
|
|
8
|
+
}) => Promise<void>;
|
package/dist/paginate.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { isEmpty } from './isNotEmpty.js';
|
|
2
|
+
import { isNullOrUndefined } from './isNullOrUndefined.js';
|
|
3
|
+
/**
|
|
4
|
+
* Iteratively follows paginated results.
|
|
5
|
+
* NOTE: This method has no upper runtime limit and may time out.
|
|
6
|
+
*/
|
|
7
|
+
export const paginate = async ({ paginator, startKey, }) => {
|
|
8
|
+
const nextStartKey = await paginator(startKey);
|
|
9
|
+
if (isNullOrUndefined(nextStartKey))
|
|
10
|
+
return;
|
|
11
|
+
if (typeof nextStartKey === 'string' && isEmpty(nextStartKey))
|
|
12
|
+
return;
|
|
13
|
+
await paginate({
|
|
14
|
+
paginator,
|
|
15
|
+
startKey: nextStartKey,
|
|
16
|
+
});
|
|
17
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { SSMClient } from '@aws-sdk/client-ssm';
|
|
2
|
+
export declare const settingsPath: ({ stackName, scope, property, }: {
|
|
3
|
+
stackName: string;
|
|
4
|
+
scope: string;
|
|
5
|
+
property?: string | undefined;
|
|
6
|
+
}) => string;
|
|
7
|
+
export declare const getSettings: <Settings extends Record<string, string>>({ ssm, stackName, scope, }: {
|
|
8
|
+
ssm: SSMClient;
|
|
9
|
+
stackName: string;
|
|
10
|
+
scope: string;
|
|
11
|
+
}) => () => Promise<Settings>;
|
|
12
|
+
export declare const putSettings: ({ ssm, stackName, scope, }: {
|
|
13
|
+
ssm: SSMClient;
|
|
14
|
+
stackName: string;
|
|
15
|
+
scope: string;
|
|
16
|
+
}) => ({ property, value, deleteBeforeUpdate, }: {
|
|
17
|
+
property: string;
|
|
18
|
+
value: string;
|
|
19
|
+
/**
|
|
20
|
+
* Useful when depending on the parameter having version 1, e.g. for use in CloudFormation
|
|
21
|
+
*/
|
|
22
|
+
deleteBeforeUpdate?: boolean | undefined;
|
|
23
|
+
}) => Promise<{
|
|
24
|
+
name: string;
|
|
25
|
+
}>;
|
|
26
|
+
export declare const deleteSettings: ({ ssm, stackName, scope, }: {
|
|
27
|
+
ssm: SSMClient;
|
|
28
|
+
stackName: string;
|
|
29
|
+
scope: string;
|
|
30
|
+
}) => ({ property }: {
|
|
31
|
+
property: string;
|
|
32
|
+
}) => Promise<{
|
|
33
|
+
name: string;
|
|
34
|
+
}>;
|
|
35
|
+
export declare const getSettingsOptional: <Settings extends Record<string, string>, Default>(args: Parameters<typeof getSettings>[0]) => (defaultValue: Default) => Promise<Settings | Default>;
|
package/dist/settings.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { DeleteParameterCommand, GetParametersByPathCommand, PutParameterCommand, SSMClient, } from '@aws-sdk/client-ssm';
|
|
2
|
+
import { paginate } from './paginate.js';
|
|
3
|
+
const scopeRx = new RegExp(`^[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+$`);
|
|
4
|
+
const validScope = (scope) => scopeRx.test(scope);
|
|
5
|
+
export const settingsPath = ({ stackName, scope, property, }) => {
|
|
6
|
+
if (!validScope(scope))
|
|
7
|
+
throw new Error(`Invalid scope name`);
|
|
8
|
+
const base = `/${stackName}/${scope}`;
|
|
9
|
+
return property === undefined ? base : `${base}/${property}`;
|
|
10
|
+
};
|
|
11
|
+
const settingsName = ({ stackName, scope, property, }) => settingsPath({ stackName, scope, property });
|
|
12
|
+
export const getSettings = ({ ssm, stackName, scope, }) => async () => {
|
|
13
|
+
const Path = settingsPath({ stackName, scope });
|
|
14
|
+
const Parameters = [];
|
|
15
|
+
await paginate({
|
|
16
|
+
paginator: async (NextToken) => ssm
|
|
17
|
+
.send(new GetParametersByPathCommand({
|
|
18
|
+
Path,
|
|
19
|
+
Recursive: true,
|
|
20
|
+
NextToken,
|
|
21
|
+
}))
|
|
22
|
+
.then(({ Parameters: p, NextToken }) => {
|
|
23
|
+
if (p !== undefined)
|
|
24
|
+
Parameters.push(...p);
|
|
25
|
+
return NextToken;
|
|
26
|
+
}),
|
|
27
|
+
});
|
|
28
|
+
if (Parameters.length === 0)
|
|
29
|
+
throw new Error(`System not configured: ${Path}!`);
|
|
30
|
+
return Parameters.map(({ Name, ...rest }) => ({
|
|
31
|
+
...rest,
|
|
32
|
+
Name: Name?.replace(`${Path}/`, ''),
|
|
33
|
+
})).reduce((settings, { Name, Value }) => ({
|
|
34
|
+
...settings,
|
|
35
|
+
[Name ?? '']: Value ?? '',
|
|
36
|
+
}), {});
|
|
37
|
+
};
|
|
38
|
+
export const putSettings = ({ ssm, stackName, scope, }) => async ({ property, value, deleteBeforeUpdate, }) => {
|
|
39
|
+
const Name = settingsName({ stackName, scope, property });
|
|
40
|
+
if (deleteBeforeUpdate ?? false) {
|
|
41
|
+
try {
|
|
42
|
+
await ssm.send(new DeleteParameterCommand({
|
|
43
|
+
Name,
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// pass
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
await ssm.send(new PutParameterCommand({
|
|
51
|
+
Name,
|
|
52
|
+
Value: value,
|
|
53
|
+
Type: 'String',
|
|
54
|
+
Overwrite: !(deleteBeforeUpdate ?? false),
|
|
55
|
+
}));
|
|
56
|
+
return { name: Name };
|
|
57
|
+
};
|
|
58
|
+
export const deleteSettings = ({ ssm, stackName, scope, }) => async ({ property }) => {
|
|
59
|
+
const Name = settingsName({ stackName, scope, property });
|
|
60
|
+
try {
|
|
61
|
+
await ssm.send(new DeleteParameterCommand({
|
|
62
|
+
Name,
|
|
63
|
+
}));
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
if (error.name === 'ParameterNotFound') {
|
|
67
|
+
// pass
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return { name: Name };
|
|
74
|
+
};
|
|
75
|
+
export const getSettingsOptional = (args) =>
|
|
76
|
+
/**
|
|
77
|
+
* In case of an unconfigured stack, returns default values
|
|
78
|
+
*/
|
|
79
|
+
async (defaultValue) => {
|
|
80
|
+
try {
|
|
81
|
+
return await getSettings(args)();
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return defaultValue;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { getSettingsOptional, settingsPath, getSettings } from './settings.js';
|
|
4
|
+
void describe('getSettingsOptional()', () => {
|
|
5
|
+
void it('should return the given default value if parameter does not exist', async () => {
|
|
6
|
+
const stackConfig = getSettingsOptional({
|
|
7
|
+
ssm: {
|
|
8
|
+
send: async () => Promise.resolve({ Parameters: undefined }),
|
|
9
|
+
},
|
|
10
|
+
stackName: 'STACK_NAME',
|
|
11
|
+
scope: 'stack/context',
|
|
12
|
+
});
|
|
13
|
+
const result = await stackConfig({});
|
|
14
|
+
assert.deepEqual(result, {});
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
void describe('settingsPath()', () => {
|
|
18
|
+
void it('should produce a fully qualified parameter name', () => assert.equal(settingsPath({
|
|
19
|
+
scope: 'stack/context',
|
|
20
|
+
stackName: 'hello-nrfcloud',
|
|
21
|
+
property: 'someProperty',
|
|
22
|
+
}), '/hello-nrfcloud/stack/context/someProperty'));
|
|
23
|
+
void it('should produce a fully qualified parameter name for valid string scope', () => assert.equal(settingsPath({
|
|
24
|
+
scope: 'thirdParty/elite',
|
|
25
|
+
stackName: 'hello-nrfcloud',
|
|
26
|
+
property: 'someProperty',
|
|
27
|
+
}), '/hello-nrfcloud/thirdParty/elite/someProperty'));
|
|
28
|
+
void it('should error for invalid string scope', () => {
|
|
29
|
+
assert.throws(() => settingsPath({
|
|
30
|
+
scope: 'invalidScope',
|
|
31
|
+
stackName: 'hello-nrfcloud',
|
|
32
|
+
property: 'someProperty',
|
|
33
|
+
}));
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
void describe('getSettings()', () => {
|
|
37
|
+
void it('should return the object with same scope', async () => {
|
|
38
|
+
const returnedValues = [
|
|
39
|
+
{
|
|
40
|
+
Name: `/hello-nrfcloud/stack/context/key1`,
|
|
41
|
+
Value: 'value1',
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
Name: `/hello-nrfcloud/stack/context/key2`,
|
|
45
|
+
Value: 'value2',
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
Name: `/hello-nrfcloud/stack/context/key3`,
|
|
49
|
+
Value: 'value3',
|
|
50
|
+
},
|
|
51
|
+
];
|
|
52
|
+
const stackConfig = getSettings({
|
|
53
|
+
ssm: {
|
|
54
|
+
send: async () => Promise.resolve({ Parameters: returnedValues }),
|
|
55
|
+
},
|
|
56
|
+
stackName: 'hello-nrfcloud',
|
|
57
|
+
scope: 'stack/context',
|
|
58
|
+
});
|
|
59
|
+
const result = await stackConfig();
|
|
60
|
+
assert.deepEqual(result, {
|
|
61
|
+
key1: 'value1',
|
|
62
|
+
key2: 'value2',
|
|
63
|
+
key3: 'value3',
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bifravst/aws-ssm-settings-helpers",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Helper functions written in TypeScript for storing and retrieving application settings in AWS SSM Parameter Store.",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"import": {
|
|
8
|
+
"types": "./dist/settings.d.ts",
|
|
9
|
+
"default": "./dist/settings.js"
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"scripts": {
|
|
15
|
+
"test": "tsx --no-warnings --test ./src/*.spec.ts",
|
|
16
|
+
"prepare": "husky",
|
|
17
|
+
"prepublishOnly": "npx tsc --noEmit false --outDir ./dist -d"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/bifravst/aws-ssm-settings-helpers.git"
|
|
22
|
+
},
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/bifravst/aws-ssm-settings-helpers/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/bifravst/aws-ssm-settings-helpers",
|
|
27
|
+
"keywords": [
|
|
28
|
+
"aws",
|
|
29
|
+
"ssm",
|
|
30
|
+
"typescript"
|
|
31
|
+
],
|
|
32
|
+
"author": "Nordic Semiconductor ASA | nordicsemi.no",
|
|
33
|
+
"license": "BSD-3-Clause",
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@bifravst/eslint-config-typescript": "6.0.17",
|
|
36
|
+
"@bifravst/prettier-config": "1.0.0",
|
|
37
|
+
"@commitlint/config-conventional": "19.1.0",
|
|
38
|
+
"@types/aws-lambda": "8.10.137",
|
|
39
|
+
"@types/node": "20.12.3",
|
|
40
|
+
"husky": "9.0.11",
|
|
41
|
+
"tsx": "4.7.2"
|
|
42
|
+
},
|
|
43
|
+
"lint-staged": {
|
|
44
|
+
"*.ts": [
|
|
45
|
+
"prettier --write",
|
|
46
|
+
"eslint"
|
|
47
|
+
],
|
|
48
|
+
"*.{md,json,yaml,yml}": [
|
|
49
|
+
"prettier --write"
|
|
50
|
+
]
|
|
51
|
+
},
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=20",
|
|
54
|
+
"npm": ">=9"
|
|
55
|
+
},
|
|
56
|
+
"release": {
|
|
57
|
+
"branches": [
|
|
58
|
+
"saga",
|
|
59
|
+
{
|
|
60
|
+
"name": "!(saga)",
|
|
61
|
+
"prerelease": true
|
|
62
|
+
}
|
|
63
|
+
],
|
|
64
|
+
"remoteTags": true,
|
|
65
|
+
"plugins": [
|
|
66
|
+
"@semantic-release/commit-analyzer",
|
|
67
|
+
"@semantic-release/release-notes-generator",
|
|
68
|
+
"@semantic-release/npm",
|
|
69
|
+
[
|
|
70
|
+
"@semantic-release/github",
|
|
71
|
+
{
|
|
72
|
+
"successComment": false,
|
|
73
|
+
"failTitle": false
|
|
74
|
+
}
|
|
75
|
+
]
|
|
76
|
+
]
|
|
77
|
+
},
|
|
78
|
+
"publishConfig": {
|
|
79
|
+
"access": "public"
|
|
80
|
+
},
|
|
81
|
+
"files": [
|
|
82
|
+
"package-lock.json",
|
|
83
|
+
"dist",
|
|
84
|
+
"LICENSE",
|
|
85
|
+
"README.md"
|
|
86
|
+
],
|
|
87
|
+
"prettier": "@bifravst/prettier-config",
|
|
88
|
+
"peerDependencies": {
|
|
89
|
+
"@aws-sdk/client-ssm": "^3.549.0"
|
|
90
|
+
}
|
|
91
|
+
}
|