@middy/ssm 3.0.0-alpha.6 → 3.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/README.md +26 -16
- package/index.cjs +3 -0
- package/index.js +2 -158
- package/package.json +13 -6
package/README.md
CHANGED
|
@@ -1,26 +1,36 @@
|
|
|
1
|
-
# Middy ssm middleware
|
|
2
|
-
|
|
3
|
-
<div align="center">
|
|
4
|
-
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.png"/>
|
|
5
|
-
</div>
|
|
6
|
-
|
|
7
1
|
<div align="center">
|
|
2
|
+
<h1>Middy ssm middleware</h1>
|
|
3
|
+
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
|
|
8
4
|
<p><strong>SSM (AWS System Manager Parameter) middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
|
|
9
|
-
</div>
|
|
10
|
-
|
|
11
|
-
<div align="center">
|
|
12
5
|
<p>
|
|
13
|
-
<a href="
|
|
6
|
+
<a href="https://www.npmjs.com/package/@middy/ssm?activeTab=versions">
|
|
14
7
|
<img src="https://badge.fury.io/js/%40middy%2Fssm.svg" alt="npm version" style="max-width:100%;">
|
|
15
8
|
</a>
|
|
9
|
+
<a href="https://packagephobia.com/result?p=@middy/ssm">
|
|
10
|
+
<img src="https://packagephobia.com/badge?p=@middy/ssm" alt="npm install size" style="max-width:100%;">
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://github.com/middyjs/middy/actions">
|
|
13
|
+
<img src="https://github.com/middyjs/middy/workflows/Tests/badge.svg" alt="GitHub Actions test status badge" style="max-width:100%;">
|
|
14
|
+
</a>
|
|
15
|
+
<br/>
|
|
16
|
+
<a href="https://standardjs.com/">
|
|
17
|
+
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
|
|
18
|
+
</a>
|
|
16
19
|
<a href="https://snyk.io/test/github/middyjs/middy">
|
|
17
20
|
<img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
|
|
18
21
|
</a>
|
|
19
|
-
<a href="https://
|
|
20
|
-
<img src="https://img.shields.io/
|
|
22
|
+
<a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
|
|
23
|
+
<img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
|
|
24
|
+
</a>
|
|
25
|
+
<a href="https://bestpractices.coreinfrastructure.org/projects/5280">
|
|
26
|
+
<img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
|
|
21
27
|
</a>
|
|
28
|
+
<br/>
|
|
22
29
|
<a href="https://gitter.im/middyjs/Lobby">
|
|
23
|
-
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter"
|
|
30
|
+
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
|
|
31
|
+
</a>
|
|
32
|
+
<a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
|
|
33
|
+
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
|
|
24
34
|
</a>
|
|
25
35
|
</p>
|
|
26
36
|
</div>
|
|
@@ -48,9 +58,9 @@ npm install --save @middy/ssm
|
|
|
48
58
|
## Options
|
|
49
59
|
|
|
50
60
|
- `AwsClient` (object) (default `AWS.SSM`): AWS.SSM class constructor (e.g. that has been instrumented with AWS X-Ray). Must be from `aws-sdk` v2.
|
|
51
|
-
- `awsClientOptions` (object) (
|
|
52
|
-
- `awsClientAssumeRole` (string) (
|
|
53
|
-
- `awsClientCapture` (function) (
|
|
61
|
+
- `awsClientOptions` (object) (default `undefined`): Options to pass to AWS.SSM class constructor.
|
|
62
|
+
- `awsClientAssumeRole` (string) (default `undefined`): Internal key where role tokens are stored. See [@middy/sts](/packages/sts/README.md) on to set this.
|
|
63
|
+
- `awsClientCapture` (function) (default `undefined`): Enable AWS X-Ray by passing `captureAWSClient` from `aws-xray-sdk` in.
|
|
54
64
|
- `fetchData` (object) (required): Mapping of internal key name to API request parameter `Names`/`Path`. `SecureString` are automatically decrypted.
|
|
55
65
|
- `disablePrefetch` (boolean) (default `false`): On cold start requests will trigger early if they can. Setting `awsClientAssumeRole` disables prefetch.
|
|
56
66
|
- `cacheKey` (string) (default `ssm`): Cache key for the fetched data responses. Must be unique across all middleware.
|
package/index.cjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _util=require("@middy/util");var _ssmJs=_interopRequireDefault(require("aws-sdk/clients/ssm.js"));function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}const awsRequestLimit=10;const defaults={AwsClient:_ssmJs.default,awsClientOptions:{},awsClientAssumeRole:undefined,awsClientCapture:undefined,fetchData:{},disablePrefetch:false,cacheKey:"ssm",cacheExpiry:-1,setToContext:false};const ssmMiddleware=(opts={})=>{const options={...defaults,...opts};const fetch=(request,cachedValues)=>{return{...fetchSingle(request,cachedValues),...fetchByPath(request,cachedValues)}};const fetchSingle=(request,cachedValues={})=>{const values={};let batchReq=null;let batchInternalKeys=[];let batchFetchKeys=[];const internalKeys=Object.keys(options.fetchData);const fetchKeys=Object.values(options.fetchData);for(const[idx,internalKey2]of internalKeys.entries()){if(cachedValues[internalKey2])continue;const fetchKey1=options.fetchData[internalKey2];if(fetchKey1.substr(-1)==="/")continue;batchInternalKeys.push(internalKey2);batchFetchKeys.push(fetchKey1);if((!idx||(idx+1)%awsRequestLimit!==0)&&!(idx+1===internalKeys.length)){continue}batchReq=client.getParameters({Names:batchFetchKeys,WithDecryption:true}).promise().then(resp=>{return Object.assign(...(resp.InvalidParameters??[]).map(fetchKey=>{return{[fetchKey]:new Promise(()=>{const internalKey=internalKeys[fetchKeys.indexOf(fetchKey)];const value=(0,_util).getCache(options.cacheKey).value??{};value[internalKey]=undefined;(0,_util).modifyCache(options.cacheKey,value);throw new Error("[ssm] InvalidParameter "+fetchKey)})}}),...(resp.Parameters??[]).map(param=>{return{[param.Name]:parseValue(param)}}))}).catch(e=>{const value=(0,_util).getCache(options.cacheKey).value??{};value[internalKey2]=undefined;(0,_util).modifyCache(options.cacheKey,value);throw e});for(const internalKey1 of batchInternalKeys){values[internalKey1]=batchReq.then(params=>{return params[options.fetchData[internalKey1]]})}batchInternalKeys=[];batchFetchKeys=[];batchReq=null}return values};const fetchByPath=(request,cachedValues={})=>{const values={};for(const internalKey in options.fetchData){if(cachedValues[internalKey])continue;const fetchKey=options.fetchData[internalKey];if(fetchKey.substr(-1)!=="/")continue;values[internalKey]=fetchPath(fetchKey).catch(e=>{const value=(0,_util).getCache(options.cacheKey).value??{};value[internalKey]=undefined;(0,_util).modifyCache(options.cacheKey,value);throw e})}return values};const fetchPath=(path,nextToken,values={})=>{return client.getParametersByPath({Path:path,NextToken:nextToken,Recursive:true,WithDecryption:true}).promise().then(resp=>{Object.assign(values,...resp.Parameters.map(param=>{return{[(0,_util).sanitizeKey(param.Name.replace(path,""))]:parseValue(param)}}));if(resp.NextToken)return fetchPath(path,resp.NextToken,values);return values})};const parseValue=param=>{if(param.Type==="StringList"){return param.Value.split(",")}return(0,_util).jsonSafeParse(param.Value)};let prefetch,client;if((0,_util).canPrefetch(options)){client=(0,_util).createPrefetchClient(options);prefetch=(0,_util).processCache(options,fetch)}const ssmMiddlewareBefore=async request=>{if(!client){client=await (0,_util).createClient(options,request)}const{value}=prefetch??(0,_util).processCache(options,fetch,request);Object.assign(request.internal,value);if(options.setToContext){const data=await (0,_util).getInternal(Object.keys(options.fetchData),request);Object.assign(request.context,data)}prefetch=null};return{before:ssmMiddlewareBefore}};var _default=ssmMiddleware;exports.default=_default
|
|
2
|
+
|
|
3
|
+
//# sourceMappingURL=index.cjs.map
|
package/index.js
CHANGED
|
@@ -1,159 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import SSM from 'aws-sdk/clients/ssm.js';
|
|
3
|
-
const awsRequestLimit = 10;
|
|
4
|
-
const defaults = {
|
|
5
|
-
AwsClient: SSM,
|
|
6
|
-
awsClientOptions: {},
|
|
7
|
-
awsClientAssumeRole: undefined,
|
|
8
|
-
awsClientCapture: undefined,
|
|
9
|
-
fetchData: {},
|
|
10
|
-
disablePrefetch: false,
|
|
11
|
-
cacheKey: 'ssm',
|
|
12
|
-
cacheExpiry: -1,
|
|
13
|
-
setToContext: false
|
|
14
|
-
};
|
|
1
|
+
import{canPrefetch,createPrefetchClient,createClient,processCache,getCache,modifyCache,jsonSafeParse,getInternal,sanitizeKey}from"@middy/util";import SSM from"aws-sdk/clients/ssm.js";const awsRequestLimit=10;const defaults={AwsClient:SSM,awsClientOptions:{},awsClientAssumeRole:undefined,awsClientCapture:undefined,fetchData:{},disablePrefetch:false,cacheKey:"ssm",cacheExpiry:-1,setToContext:false};const ssmMiddleware=(opts={})=>{const options={...defaults,...opts};const fetch=(request,cachedValues)=>{return{...fetchSingle(request,cachedValues),...fetchByPath(request,cachedValues)}};const fetchSingle=(request,cachedValues={})=>{const values={};let batchReq=null;let batchInternalKeys=[];let batchFetchKeys=[];const internalKeys=Object.keys(options.fetchData);const fetchKeys=Object.values(options.fetchData);for(const[idx,internalKey2]of internalKeys.entries()){if(cachedValues[internalKey2])continue;const fetchKey1=options.fetchData[internalKey2];if(fetchKey1.substr(-1)==="/")continue;batchInternalKeys.push(internalKey2);batchFetchKeys.push(fetchKey1);if((!idx||(idx+1)%awsRequestLimit!==0)&&!(idx+1===internalKeys.length)){continue}batchReq=client.getParameters({Names:batchFetchKeys,WithDecryption:true}).promise().then(resp=>{return Object.assign(...(resp.InvalidParameters??[]).map(fetchKey=>{return{[fetchKey]:new Promise(()=>{const internalKey=internalKeys[fetchKeys.indexOf(fetchKey)];const value=getCache(options.cacheKey).value??{};value[internalKey]=undefined;modifyCache(options.cacheKey,value);throw new Error("[ssm] InvalidParameter "+fetchKey)})}}),...(resp.Parameters??[]).map(param=>{return{[param.Name]:parseValue(param)}}))}).catch(e=>{const value=getCache(options.cacheKey).value??{};value[internalKey2]=undefined;modifyCache(options.cacheKey,value);throw e});for(const internalKey1 of batchInternalKeys){values[internalKey1]=batchReq.then(params=>{return params[options.fetchData[internalKey1]]})}batchInternalKeys=[];batchFetchKeys=[];batchReq=null}return values};const fetchByPath=(request,cachedValues={})=>{const values={};for(const internalKey in options.fetchData){if(cachedValues[internalKey])continue;const fetchKey=options.fetchData[internalKey];if(fetchKey.substr(-1)!=="/")continue;values[internalKey]=fetchPath(fetchKey).catch(e=>{const value=getCache(options.cacheKey).value??{};value[internalKey]=undefined;modifyCache(options.cacheKey,value);throw e})}return values};const fetchPath=(path,nextToken,values={})=>{return client.getParametersByPath({Path:path,NextToken:nextToken,Recursive:true,WithDecryption:true}).promise().then(resp=>{Object.assign(values,...resp.Parameters.map(param=>{return{[sanitizeKey(param.Name.replace(path,""))]:parseValue(param)}}));if(resp.NextToken)return fetchPath(path,resp.NextToken,values);return values})};const parseValue=param=>{if(param.Type==="StringList"){return param.Value.split(",")}return jsonSafeParse(param.Value)};let prefetch,client;if(canPrefetch(options)){client=createPrefetchClient(options);prefetch=processCache(options,fetch)}const ssmMiddlewareBefore=async request=>{if(!client){client=await createClient(options,request)}const{value}=prefetch??processCache(options,fetch,request);Object.assign(request.internal,value);if(options.setToContext){const data=await getInternal(Object.keys(options.fetchData),request);Object.assign(request.context,data)}prefetch=null};return{before:ssmMiddlewareBefore}};export default ssmMiddleware
|
|
15
2
|
|
|
16
|
-
|
|
17
|
-
const options = { ...defaults,
|
|
18
|
-
...opts
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const fetch = (request, cachedValues) => {
|
|
22
|
-
return { ...fetchSingle(request, cachedValues),
|
|
23
|
-
...fetchByPath(request, cachedValues)
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const fetchSingle = (request, cachedValues = {}) => {
|
|
28
|
-
const values = {};
|
|
29
|
-
let batchReq = null;
|
|
30
|
-
let batchInternalKeys = [];
|
|
31
|
-
let batchFetchKeys = [];
|
|
32
|
-
const internalKeys = Object.keys(options.fetchData);
|
|
33
|
-
const fetchKeys = Object.values(options.fetchData);
|
|
34
|
-
|
|
35
|
-
for (const [idx, internalKey] of internalKeys.entries()) {
|
|
36
|
-
if (cachedValues[internalKey]) continue;
|
|
37
|
-
const fetchKey = options.fetchData[internalKey];
|
|
38
|
-
if (fetchKey.substr(-1) === '/') continue;
|
|
39
|
-
batchInternalKeys.push(internalKey);
|
|
40
|
-
batchFetchKeys.push(fetchKey);
|
|
41
|
-
|
|
42
|
-
if ((!idx || (idx + 1) % awsRequestLimit !== 0) && !(idx + 1 === internalKeys.length)) {
|
|
43
|
-
continue;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
batchReq = client.getParameters({
|
|
47
|
-
Names: batchFetchKeys,
|
|
48
|
-
WithDecryption: true
|
|
49
|
-
}).promise().then(resp => {
|
|
50
|
-
return Object.assign(...(resp.InvalidParameters ?? []).map(fetchKey => {
|
|
51
|
-
return {
|
|
52
|
-
[fetchKey]: new Promise(() => {
|
|
53
|
-
const internalKey = internalKeys[fetchKeys.indexOf(fetchKey)];
|
|
54
|
-
const value = getCache(options.cacheKey).value ?? {};
|
|
55
|
-
value[internalKey] = undefined;
|
|
56
|
-
modifyCache(options.cacheKey, value);
|
|
57
|
-
throw new Error('[ssm] InvalidParameter ' + fetchKey);
|
|
58
|
-
})
|
|
59
|
-
};
|
|
60
|
-
}), ...(resp.Parameters ?? []).map(param => {
|
|
61
|
-
return {
|
|
62
|
-
[param.Name]: parseValue(param)
|
|
63
|
-
};
|
|
64
|
-
}));
|
|
65
|
-
}).catch(e => {
|
|
66
|
-
const value = getCache(options.cacheKey).value ?? {};
|
|
67
|
-
value[internalKey] = undefined;
|
|
68
|
-
modifyCache(options.cacheKey, value);
|
|
69
|
-
throw e;
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
for (const internalKey of batchInternalKeys) {
|
|
73
|
-
values[internalKey] = batchReq.then(params => {
|
|
74
|
-
return params[options.fetchData[internalKey]];
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
batchInternalKeys = [];
|
|
79
|
-
batchFetchKeys = [];
|
|
80
|
-
batchReq = null;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return values;
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
const fetchByPath = (request, cachedValues = {}) => {
|
|
87
|
-
const values = {};
|
|
88
|
-
|
|
89
|
-
for (const internalKey in options.fetchData) {
|
|
90
|
-
if (cachedValues[internalKey]) continue;
|
|
91
|
-
const fetchKey = options.fetchData[internalKey];
|
|
92
|
-
if (fetchKey.substr(-1) !== '/') continue;
|
|
93
|
-
values[internalKey] = fetchPath(fetchKey).catch(e => {
|
|
94
|
-
const value = getCache(options.cacheKey).value ?? {};
|
|
95
|
-
value[internalKey] = undefined;
|
|
96
|
-
modifyCache(options.cacheKey, value);
|
|
97
|
-
throw e;
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return values;
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
const fetchPath = (path, nextToken, values = {}) => {
|
|
105
|
-
return client.getParametersByPath({
|
|
106
|
-
Path: path,
|
|
107
|
-
NextToken: nextToken,
|
|
108
|
-
Recursive: true,
|
|
109
|
-
WithDecryption: true
|
|
110
|
-
}).promise().then(resp => {
|
|
111
|
-
Object.assign(values, ...resp.Parameters.map(param => {
|
|
112
|
-
return {
|
|
113
|
-
[sanitizeKey(param.Name.replace(path, ''))]: parseValue(param)
|
|
114
|
-
};
|
|
115
|
-
}));
|
|
116
|
-
if (resp.NextToken) return fetchPath(path, resp.NextToken, values);
|
|
117
|
-
return values;
|
|
118
|
-
});
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
const parseValue = param => {
|
|
122
|
-
if (param.Type === 'StringList') {
|
|
123
|
-
return param.Value.split(',');
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
return jsonSafeParse(param.Value);
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
let prefetch, client;
|
|
130
|
-
|
|
131
|
-
if (canPrefetch(options)) {
|
|
132
|
-
client = createPrefetchClient(options);
|
|
133
|
-
prefetch = processCache(options, fetch);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const ssmMiddlewareBefore = async request => {
|
|
137
|
-
if (!client) {
|
|
138
|
-
client = await createClient(options, request);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const {
|
|
142
|
-
value
|
|
143
|
-
} = prefetch ?? processCache(options, fetch, request);
|
|
144
|
-
Object.assign(request.internal, value);
|
|
145
|
-
|
|
146
|
-
if (options.setToContext) {
|
|
147
|
-
const data = await getInternal(Object.keys(options.fetchData), request);
|
|
148
|
-
Object.assign(request.context, data);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
prefetch = null;
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
return {
|
|
155
|
-
before: ssmMiddlewareBefore
|
|
156
|
-
};
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
export default ssmMiddleware;
|
|
3
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/ssm",
|
|
3
|
-
"version": "3.0.0
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "SSM (EC2 Systems Manager) parameters middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -10,10 +10,17 @@
|
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"exports":
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./index.js",
|
|
16
|
+
"require": "./index.cjs",
|
|
17
|
+
"types": "./index.d.ts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
14
20
|
"types": "index.d.ts",
|
|
15
21
|
"files": [
|
|
16
22
|
"index.js",
|
|
23
|
+
"index.cjs",
|
|
17
24
|
"index.d.ts"
|
|
18
25
|
],
|
|
19
26
|
"scripts": {
|
|
@@ -46,14 +53,14 @@
|
|
|
46
53
|
"bugs": {
|
|
47
54
|
"url": "https://github.com/middyjs/middy/issues"
|
|
48
55
|
},
|
|
49
|
-
"homepage": "https://
|
|
56
|
+
"homepage": "https://middy.js.org",
|
|
50
57
|
"dependencies": {
|
|
51
|
-
"@middy/util": "^3.0.0
|
|
58
|
+
"@middy/util": "^3.0.0"
|
|
52
59
|
},
|
|
53
60
|
"devDependencies": {
|
|
54
|
-
"@middy/core": "^3.0.0
|
|
61
|
+
"@middy/core": "^3.0.0",
|
|
55
62
|
"aws-sdk": "^2.939.0",
|
|
56
63
|
"aws-xray-sdk": "^3.3.3"
|
|
57
64
|
},
|
|
58
|
-
"gitHead": "
|
|
65
|
+
"gitHead": "01520fa8628a36c2f89e126cad656a16547ea0d6"
|
|
59
66
|
}
|