@middy/ssm 2.5.5 → 2.5.6
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/index.js +117 -133
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
1
|
const {
|
|
4
2
|
canPrefetch,
|
|
5
3
|
createPrefetchClient,
|
|
@@ -10,185 +8,171 @@ const {
|
|
|
10
8
|
jsonSafeParse,
|
|
11
9
|
getInternal,
|
|
12
10
|
sanitizeKey
|
|
13
|
-
} = require('@middy/util')
|
|
14
|
-
|
|
15
|
-
const SSM = require('aws-sdk/clients/ssm'); // v2
|
|
11
|
+
} = require('@middy/util')
|
|
12
|
+
const SSM = require('aws-sdk/clients/ssm') // v2
|
|
16
13
|
// const { SSM } = require('@aws-sdk/client-ssm') // v3
|
|
17
14
|
|
|
18
|
-
|
|
19
|
-
const awsRequestLimit = 10;
|
|
15
|
+
const awsRequestLimit = 10
|
|
20
16
|
const defaults = {
|
|
21
|
-
AwsClient: SSM,
|
|
22
|
-
// Allow for XRay
|
|
17
|
+
AwsClient: SSM, // Allow for XRay
|
|
23
18
|
awsClientOptions: {},
|
|
24
19
|
awsClientAssumeRole: undefined,
|
|
25
20
|
awsClientCapture: undefined,
|
|
26
|
-
fetchData: {},
|
|
27
|
-
// { contextKey: fetchKey, contextPrefix: fetchPath/ }
|
|
21
|
+
fetchData: {}, // { contextKey: fetchKey, contextPrefix: fetchPath/ }
|
|
28
22
|
disablePrefetch: false,
|
|
29
23
|
cacheKey: 'ssm',
|
|
30
24
|
cacheExpiry: -1,
|
|
31
25
|
setToEnv: false,
|
|
32
26
|
setToContext: false
|
|
33
|
-
}
|
|
27
|
+
}
|
|
34
28
|
|
|
35
29
|
const ssmMiddleware = (opts = {}) => {
|
|
36
|
-
const options = { ...defaults,
|
|
37
|
-
...opts
|
|
38
|
-
};
|
|
30
|
+
const options = { ...defaults, ...opts }
|
|
39
31
|
|
|
40
32
|
const fetch = (request, cachedValues) => {
|
|
41
|
-
return {
|
|
33
|
+
return {
|
|
34
|
+
...fetchSingle(request, cachedValues),
|
|
42
35
|
...fetchByPath(request, cachedValues)
|
|
43
|
-
}
|
|
44
|
-
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
45
38
|
|
|
46
39
|
const fetchSingle = (request, cachedValues = {}) => {
|
|
47
|
-
const values = {}
|
|
48
|
-
let batchReq = null
|
|
49
|
-
let batchInternalKeys = []
|
|
50
|
-
let batchFetchKeys = []
|
|
51
|
-
const internalKeys = Object.keys(options.fetchData);
|
|
52
|
-
const fetchKeys = Object.values(options.fetchData);
|
|
40
|
+
const values = {}
|
|
41
|
+
let batchReq = null
|
|
42
|
+
let batchInternalKeys = []
|
|
43
|
+
let batchFetchKeys = []
|
|
53
44
|
|
|
45
|
+
const internalKeys = Object.keys(options.fetchData)
|
|
46
|
+
const fetchKeys = Object.values(options.fetchData)
|
|
54
47
|
for (const [idx, internalKey] of internalKeys.entries()) {
|
|
55
|
-
if (cachedValues[internalKey]) continue
|
|
56
|
-
const fetchKey = options.fetchData[internalKey]
|
|
57
|
-
if (fetchKey.substr(-1) === '/') continue
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
48
|
+
if (cachedValues[internalKey]) continue
|
|
49
|
+
const fetchKey = options.fetchData[internalKey]
|
|
50
|
+
if (fetchKey.substr(-1) === '/') continue // Skip path passed in
|
|
51
|
+
batchInternalKeys.push(internalKey)
|
|
52
|
+
batchFetchKeys.push(fetchKey)
|
|
53
|
+
// from the first to the batch size skip, unless it's the last entry
|
|
54
|
+
if (
|
|
55
|
+
(!idx || (idx + 1) % awsRequestLimit !== 0) &&
|
|
56
|
+
!(idx + 1 === internalKeys.length)
|
|
57
|
+
) {
|
|
58
|
+
continue
|
|
64
59
|
}
|
|
65
60
|
|
|
66
|
-
batchReq = client
|
|
67
|
-
Names: batchFetchKeys,
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
61
|
+
batchReq = client
|
|
62
|
+
.getParameters({ Names: batchFetchKeys, WithDecryption: true })
|
|
63
|
+
.promise() // Required for aws-sdk v2
|
|
64
|
+
.then((resp) => {
|
|
65
|
+
// Don't sanitize key, mapped to set value in options
|
|
66
|
+
return Object.assign(
|
|
67
|
+
...(resp.InvalidParameters ?? []).map((fetchKey) => {
|
|
68
|
+
return {
|
|
69
|
+
[fetchKey]: new Promise(() => {
|
|
70
|
+
const internalKey = internalKeys[fetchKeys.indexOf(fetchKey)]
|
|
71
|
+
const value = getCache(options.cacheKey)?.value ?? {}
|
|
72
|
+
value[internalKey] = undefined
|
|
73
|
+
modifyCache(options.cacheKey, value)
|
|
74
|
+
throw new Error('ssm.InvalidParameter ' + fetchKey)
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
}),
|
|
78
|
+
...(resp.Parameters ?? []).map((param) => {
|
|
79
|
+
return { [param.Name]: parseValue(param) }
|
|
84
80
|
})
|
|
85
|
-
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const value = (_getCache$value2 = getCache(options.cacheKey).value) !== null && _getCache$value2 !== void 0 ? _getCache$value2 : {};
|
|
95
|
-
value[internalKey] = undefined;
|
|
96
|
-
modifyCache(options.cacheKey, value);
|
|
97
|
-
throw e;
|
|
98
|
-
});
|
|
81
|
+
)
|
|
82
|
+
})
|
|
83
|
+
.catch((e) => {
|
|
84
|
+
const value = getCache(options.cacheKey).value ?? {}
|
|
85
|
+
value[internalKey] = undefined
|
|
86
|
+
modifyCache(options.cacheKey, value)
|
|
87
|
+
throw e
|
|
88
|
+
})
|
|
99
89
|
|
|
100
90
|
for (const internalKey of batchInternalKeys) {
|
|
101
|
-
values[internalKey] = batchReq.then(params => {
|
|
102
|
-
return params[options.fetchData[internalKey]]
|
|
103
|
-
})
|
|
91
|
+
values[internalKey] = batchReq.then((params) => {
|
|
92
|
+
return params[options.fetchData[internalKey]]
|
|
93
|
+
})
|
|
104
94
|
}
|
|
105
95
|
|
|
106
|
-
batchInternalKeys = []
|
|
107
|
-
batchFetchKeys = []
|
|
108
|
-
batchReq = null
|
|
96
|
+
batchInternalKeys = []
|
|
97
|
+
batchFetchKeys = []
|
|
98
|
+
batchReq = null
|
|
109
99
|
}
|
|
110
100
|
|
|
111
|
-
return values
|
|
112
|
-
}
|
|
101
|
+
return values
|
|
102
|
+
}
|
|
113
103
|
|
|
114
104
|
const fetchByPath = (request, cachedValues = {}) => {
|
|
115
|
-
const values = {}
|
|
116
|
-
|
|
105
|
+
const values = {}
|
|
117
106
|
for (const internalKey in options.fetchData) {
|
|
118
|
-
if (cachedValues[internalKey]) continue
|
|
119
|
-
const fetchKey = options.fetchData[internalKey]
|
|
120
|
-
if (fetchKey.substr(-1) !== '/') continue
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
modifyCache(options.cacheKey, value);
|
|
128
|
-
throw e;
|
|
129
|
-
});
|
|
107
|
+
if (cachedValues[internalKey]) continue
|
|
108
|
+
const fetchKey = options.fetchData[internalKey]
|
|
109
|
+
if (fetchKey.substr(-1) !== '/') continue // Skip not path passed in
|
|
110
|
+
values[internalKey] = fetchPath(fetchKey).catch((e) => {
|
|
111
|
+
const value = getCache(options.cacheKey)?.value ?? {}
|
|
112
|
+
value[internalKey] = undefined
|
|
113
|
+
modifyCache(options.cacheKey, value)
|
|
114
|
+
throw e
|
|
115
|
+
})
|
|
130
116
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
};
|
|
117
|
+
return values
|
|
118
|
+
}
|
|
134
119
|
|
|
135
120
|
const fetchPath = (path, nextToken, values = {}) => {
|
|
136
|
-
return client
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
121
|
+
return client
|
|
122
|
+
.getParametersByPath({
|
|
123
|
+
Path: path,
|
|
124
|
+
NextToken: nextToken,
|
|
125
|
+
Recursive: true,
|
|
126
|
+
WithDecryption: true
|
|
127
|
+
})
|
|
128
|
+
.promise() // Required for aws-sdk v2
|
|
129
|
+
.then((resp) => {
|
|
130
|
+
Object.assign(
|
|
131
|
+
values,
|
|
132
|
+
...resp.Parameters.map((param) => {
|
|
133
|
+
return {
|
|
134
|
+
[sanitizeKey(param.Name.replace(path, ''))]: parseValue(param)
|
|
135
|
+
}
|
|
136
|
+
})
|
|
137
|
+
)
|
|
138
|
+
if (resp.NextToken) return fetchPath(path, resp.NextToken, values)
|
|
139
|
+
return values
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const parseValue = (param) => {
|
|
154
144
|
if (param.Type === 'StringList') {
|
|
155
|
-
return param.Value.split(',')
|
|
145
|
+
return param.Value.split(',')
|
|
156
146
|
}
|
|
147
|
+
return jsonSafeParse(param.Value)
|
|
148
|
+
}
|
|
157
149
|
|
|
158
|
-
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
let prefetch, client;
|
|
162
|
-
|
|
150
|
+
let prefetch, client
|
|
163
151
|
if (canPrefetch(options)) {
|
|
164
|
-
client = createPrefetchClient(options)
|
|
165
|
-
prefetch = processCache(options, fetch)
|
|
152
|
+
client = createPrefetchClient(options)
|
|
153
|
+
prefetch = processCache(options, fetch)
|
|
166
154
|
}
|
|
167
155
|
|
|
168
|
-
const ssmMiddlewareBefore = async request => {
|
|
169
|
-
var _prefetch;
|
|
170
|
-
|
|
156
|
+
const ssmMiddlewareBefore = async (request) => {
|
|
171
157
|
if (!client) {
|
|
172
|
-
client = await createClient(options, request)
|
|
158
|
+
client = await createClient(options, request)
|
|
173
159
|
}
|
|
174
160
|
|
|
175
|
-
const {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
Object.assign(request.internal, value);
|
|
161
|
+
const { value } = prefetch ?? processCache(options, fetch, request)
|
|
162
|
+
|
|
163
|
+
Object.assign(request.internal, value)
|
|
179
164
|
|
|
180
165
|
if (options.setToContext || options.setToEnv) {
|
|
181
|
-
const data = await getInternal(Object.keys(options.fetchData), request)
|
|
182
|
-
if (options.setToEnv) Object.assign(process.env, data)
|
|
183
|
-
if (options.setToContext) Object.assign(request.context, data)
|
|
166
|
+
const data = await getInternal(Object.keys(options.fetchData), request)
|
|
167
|
+
if (options.setToEnv) Object.assign(process.env, data)
|
|
168
|
+
if (options.setToContext) Object.assign(request.context, data)
|
|
184
169
|
}
|
|
185
170
|
|
|
186
|
-
prefetch = null
|
|
187
|
-
}
|
|
171
|
+
prefetch = null
|
|
172
|
+
}
|
|
188
173
|
|
|
189
174
|
return {
|
|
190
175
|
before: ssmMiddlewareBefore
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
module.exports = ssmMiddleware;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
module.exports = ssmMiddleware
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/ssm",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.6",
|
|
4
4
|
"description": "SSM (EC2 Systems Manager) parameters middleware for the middy framework",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"engines": {
|
|
@@ -46,12 +46,12 @@
|
|
|
46
46
|
},
|
|
47
47
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@middy/util": "^2.5.
|
|
49
|
+
"@middy/util": "^2.5.6"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@middy/core": "^2.5.
|
|
52
|
+
"@middy/core": "^2.5.6",
|
|
53
53
|
"aws-sdk": "^2.939.0",
|
|
54
54
|
"aws-xray-sdk": "^3.3.3"
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "0c789f55b4adf691f977b0d9904d1a805bb3bb2b"
|
|
57
57
|
}
|