@middy/secrets-manager 3.0.0-alpha.3 → 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 +38 -49
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,79 +1,68 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
createPrefetchClient,
|
|
4
|
-
createClient,
|
|
5
|
-
getCache,
|
|
6
|
-
getInternal,
|
|
7
|
-
processCache,
|
|
8
|
-
modifyCache,
|
|
9
|
-
jsonSafeParse
|
|
10
|
-
} from '@middy/util'
|
|
11
|
-
import SecretsManager from 'aws-sdk/clients/secretsmanager.js' // v2
|
|
12
|
-
// import { SecretsManager } from '@aws-sdk/client-secrets-manager' // v3
|
|
13
|
-
|
|
1
|
+
import { canPrefetch, createPrefetchClient, createClient, getCache, getInternal, processCache, modifyCache, jsonSafeParse } from '@middy/util';
|
|
2
|
+
import SecretsManager from 'aws-sdk/clients/secretsmanager.js';
|
|
14
3
|
const defaults = {
|
|
15
4
|
AwsClient: SecretsManager,
|
|
16
5
|
awsClientOptions: {},
|
|
17
6
|
awsClientAssumeRole: undefined,
|
|
18
7
|
awsClientCapture: undefined,
|
|
19
|
-
fetchData: {},
|
|
8
|
+
fetchData: {},
|
|
20
9
|
disablePrefetch: false,
|
|
21
10
|
cacheKey: 'secrets-manager',
|
|
22
11
|
cacheExpiry: -1,
|
|
23
12
|
setToContext: false
|
|
24
|
-
}
|
|
13
|
+
};
|
|
25
14
|
|
|
26
15
|
const secretsManagerMiddleware = (opts = {}) => {
|
|
27
|
-
const options = { ...defaults,
|
|
16
|
+
const options = { ...defaults,
|
|
17
|
+
...opts
|
|
18
|
+
};
|
|
28
19
|
|
|
29
20
|
const fetch = (request, cachedValues = {}) => {
|
|
30
|
-
const values = {}
|
|
21
|
+
const values = {};
|
|
31
22
|
|
|
32
|
-
// Multiple secrets can be requested in a single requests,
|
|
33
|
-
// however this is likely uncommon IRL, increases complexity to handle,
|
|
34
|
-
// and will require recursive promise resolution impacting performance.
|
|
35
|
-
// See https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SecretsManager.html#listSecrets-property
|
|
36
23
|
for (const internalKey of Object.keys(options.fetchData)) {
|
|
37
|
-
if (cachedValues[internalKey]) continue
|
|
38
|
-
values[internalKey] = client
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
throw e
|
|
47
|
-
})
|
|
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
|
+
});
|
|
48
33
|
}
|
|
49
|
-
return values
|
|
50
|
-
}
|
|
51
34
|
|
|
52
|
-
|
|
35
|
+
return values;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
let prefetch, client;
|
|
39
|
+
|
|
53
40
|
if (canPrefetch(options)) {
|
|
54
|
-
client = createPrefetchClient(options)
|
|
55
|
-
prefetch = processCache(options, fetch)
|
|
41
|
+
client = createPrefetchClient(options);
|
|
42
|
+
prefetch = processCache(options, fetch);
|
|
56
43
|
}
|
|
57
44
|
|
|
58
|
-
const secretsManagerMiddlewareBefore = async
|
|
45
|
+
const secretsManagerMiddlewareBefore = async request => {
|
|
59
46
|
if (!client) {
|
|
60
|
-
client = await createClient(options, request)
|
|
47
|
+
client = await createClient(options, request);
|
|
61
48
|
}
|
|
62
49
|
|
|
63
|
-
const {
|
|
64
|
-
|
|
65
|
-
|
|
50
|
+
const {
|
|
51
|
+
value
|
|
52
|
+
} = prefetch ?? processCache(options, fetch, request);
|
|
53
|
+
Object.assign(request.internal, value);
|
|
66
54
|
|
|
67
55
|
if (options.setToContext) {
|
|
68
|
-
const data = await getInternal(Object.keys(options.fetchData), request)
|
|
69
|
-
Object.assign(request.context, data)
|
|
56
|
+
const data = await getInternal(Object.keys(options.fetchData), request);
|
|
57
|
+
Object.assign(request.context, data);
|
|
70
58
|
}
|
|
71
59
|
|
|
72
|
-
prefetch = null
|
|
73
|
-
}
|
|
60
|
+
prefetch = null;
|
|
61
|
+
};
|
|
74
62
|
|
|
75
63
|
return {
|
|
76
64
|
before: secretsManagerMiddlewareBefore
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
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": {
|
|
@@ -46,12 +46,12 @@
|
|
|
46
46
|
},
|
|
47
47
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@middy/util": "^3.0.0-alpha.
|
|
49
|
+
"@middy/util": "^3.0.0-alpha.4"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@middy/core": "^3.0.0-alpha.
|
|
52
|
+
"@middy/core": "^3.0.0-alpha.4",
|
|
53
53
|
"aws-sdk": "^2.939.0",
|
|
54
54
|
"aws-xray-sdk": "^3.3.3"
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "d4bea7f4e21f6a9bbb1f6f6908361169598b9e53"
|
|
57
57
|
}
|