@middy/secrets-manager 3.0.0-alpha.0 → 3.0.0-alpha.4
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 +68 -0
- package/package.json +7 -5
package/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { canPrefetch, createPrefetchClient, createClient, getCache, getInternal, processCache, modifyCache, jsonSafeParse } from '@middy/util';
|
|
2
|
+
import SecretsManager from 'aws-sdk/clients/secretsmanager.js';
|
|
3
|
+
const defaults = {
|
|
4
|
+
AwsClient: SecretsManager,
|
|
5
|
+
awsClientOptions: {},
|
|
6
|
+
awsClientAssumeRole: undefined,
|
|
7
|
+
awsClientCapture: undefined,
|
|
8
|
+
fetchData: {},
|
|
9
|
+
disablePrefetch: false,
|
|
10
|
+
cacheKey: 'secrets-manager',
|
|
11
|
+
cacheExpiry: -1,
|
|
12
|
+
setToContext: false
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const secretsManagerMiddleware = (opts = {}) => {
|
|
16
|
+
const options = { ...defaults,
|
|
17
|
+
...opts
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const fetch = (request, cachedValues = {}) => {
|
|
21
|
+
const values = {};
|
|
22
|
+
|
|
23
|
+
for (const internalKey of Object.keys(options.fetchData)) {
|
|
24
|
+
if (cachedValues[internalKey]) continue;
|
|
25
|
+
values[internalKey] = client.getSecretValue({
|
|
26
|
+
SecretId: options.fetchData[internalKey]
|
|
27
|
+
}).promise().then(resp => jsonSafeParse(resp.SecretString)).catch(e => {
|
|
28
|
+
const value = getCache(options.cacheKey).value ?? {};
|
|
29
|
+
value[internalKey] = undefined;
|
|
30
|
+
modifyCache(options.cacheKey, value);
|
|
31
|
+
throw e;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return values;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
let prefetch, client;
|
|
39
|
+
|
|
40
|
+
if (canPrefetch(options)) {
|
|
41
|
+
client = createPrefetchClient(options);
|
|
42
|
+
prefetch = processCache(options, fetch);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const secretsManagerMiddlewareBefore = async request => {
|
|
46
|
+
if (!client) {
|
|
47
|
+
client = await createClient(options, request);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const {
|
|
51
|
+
value
|
|
52
|
+
} = prefetch ?? processCache(options, fetch, request);
|
|
53
|
+
Object.assign(request.internal, value);
|
|
54
|
+
|
|
55
|
+
if (options.setToContext) {
|
|
56
|
+
const data = await getInternal(Object.keys(options.fetchData), request);
|
|
57
|
+
Object.assign(request.context, data);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
prefetch = null;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
before: secretsManagerMiddlewareBefore
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export default secretsManagerMiddleware;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/secrets-manager",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.4",
|
|
4
4
|
"description": "Secrets Manager middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -13,11 +13,13 @@
|
|
|
13
13
|
"exports": "./index.js",
|
|
14
14
|
"types": "index.d.ts",
|
|
15
15
|
"files": [
|
|
16
|
+
"index.js",
|
|
16
17
|
"index.d.ts"
|
|
17
18
|
],
|
|
18
19
|
"scripts": {
|
|
19
20
|
"test": "npm run test:unit",
|
|
20
|
-
"test:unit": "ava"
|
|
21
|
+
"test:unit": "ava",
|
|
22
|
+
"test:benchmark": "node __benchmarks__/index.js"
|
|
21
23
|
},
|
|
22
24
|
"license": "MIT",
|
|
23
25
|
"keywords": [
|
|
@@ -44,12 +46,12 @@
|
|
|
44
46
|
},
|
|
45
47
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
46
48
|
"dependencies": {
|
|
47
|
-
"@middy/util": "^3.0.0-alpha.
|
|
49
|
+
"@middy/util": "^3.0.0-alpha.4"
|
|
48
50
|
},
|
|
49
51
|
"devDependencies": {
|
|
50
|
-
"@middy/core": "^3.0.0-alpha.
|
|
52
|
+
"@middy/core": "^3.0.0-alpha.4",
|
|
51
53
|
"aws-sdk": "^2.939.0",
|
|
52
54
|
"aws-xray-sdk": "^3.3.3"
|
|
53
55
|
},
|
|
54
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "d4bea7f4e21f6a9bbb1f6f6908361169598b9e53"
|
|
55
57
|
}
|