@middy/secrets-manager 5.0.0-alpha.1 → 5.0.0-rc.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/index.js +68 -110
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,115 +1,73 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
createPrefetchClient,
|
|
4
|
-
createClient,
|
|
5
|
-
getCache,
|
|
6
|
-
getInternal,
|
|
7
|
-
processCache,
|
|
8
|
-
modifyCache,
|
|
9
|
-
jsonSafeParse
|
|
10
|
-
} from '@middy/util'
|
|
11
|
-
import {
|
|
12
|
-
SecretsManagerClient,
|
|
13
|
-
DescribeSecretCommand,
|
|
14
|
-
GetSecretValueCommand
|
|
15
|
-
} from '@aws-sdk/client-secrets-manager'
|
|
16
|
-
|
|
1
|
+
import { canPrefetch, createPrefetchClient, createClient, getCache, getInternal, processCache, modifyCache, jsonSafeParse } from '@middy/util';
|
|
2
|
+
import { SecretsManagerClient, DescribeSecretCommand, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';
|
|
17
3
|
const defaults = {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
})
|
|
51
|
-
)
|
|
52
|
-
.then((resp) => {
|
|
53
|
-
if (options.cacheExpiry < 0) {
|
|
54
|
-
options.cacheKeyExpiry[internalKey] =
|
|
55
|
-
resp.NextRotationDate * 1000
|
|
56
|
-
} else {
|
|
57
|
-
options.cacheKeyExpiry[internalKey] = Math.min(
|
|
58
|
-
Math.max(resp.LastRotationDate, resp.LastChangedDate) *
|
|
59
|
-
1000 +
|
|
60
|
-
options.cacheExpiry,
|
|
61
|
-
resp.NextRotationDate * 1000
|
|
62
|
-
)
|
|
4
|
+
AwsClient: SecretsManagerClient,
|
|
5
|
+
awsClientOptions: {},
|
|
6
|
+
awsClientAssumeRole: undefined,
|
|
7
|
+
awsClientCapture: undefined,
|
|
8
|
+
fetchData: {},
|
|
9
|
+
fetchRotationDate: false,
|
|
10
|
+
disablePrefetch: false,
|
|
11
|
+
cacheKey: 'secrets-manager',
|
|
12
|
+
cacheKeyExpiry: {},
|
|
13
|
+
cacheExpiry: -1,
|
|
14
|
+
setToContext: false
|
|
15
|
+
};
|
|
16
|
+
const secretsManagerMiddleware = (opts = {})=>{
|
|
17
|
+
const options = {
|
|
18
|
+
...defaults,
|
|
19
|
+
...opts
|
|
20
|
+
};
|
|
21
|
+
const fetch = (request, cachedValues = {})=>{
|
|
22
|
+
const values = {};
|
|
23
|
+
for (const internalKey of Object.keys(options.fetchData)){
|
|
24
|
+
if (cachedValues[internalKey]) continue;
|
|
25
|
+
values[internalKey] = Promise.resolve().then(()=>{
|
|
26
|
+
if (options.fetchRotationDate === true || options.fetchRotationDate?.[internalKey]) {
|
|
27
|
+
return client.send(new DescribeSecretCommand({
|
|
28
|
+
SecretId: options.fetchData[internalKey]
|
|
29
|
+
})).then((resp)=>{
|
|
30
|
+
if (options.cacheExpiry < 0) {
|
|
31
|
+
options.cacheKeyExpiry[internalKey] = resp.NextRotationDate * 1000;
|
|
32
|
+
} else {
|
|
33
|
+
options.cacheKeyExpiry[internalKey] = Math.min(Math.max(resp.LastRotationDate, resp.LastChangedDate) * 1000 + options.cacheExpiry, resp.NextRotationDate * 1000);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
63
36
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
throw e
|
|
80
|
-
})
|
|
81
|
-
}
|
|
82
|
-
return values
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
let client
|
|
86
|
-
if (canPrefetch(options)) {
|
|
87
|
-
client = createPrefetchClient(options)
|
|
88
|
-
processCache(options, fetch)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const secretsManagerMiddlewareBefore = async (request) => {
|
|
92
|
-
if (!client) {
|
|
93
|
-
client = await createClient(options, request)
|
|
37
|
+
}).then(()=>client.send(new GetSecretValueCommand({
|
|
38
|
+
SecretId: options.fetchData[internalKey]
|
|
39
|
+
}))).then((resp)=>jsonSafeParse(resp.SecretString)).catch((e)=>{
|
|
40
|
+
const value = getCache(options.cacheKey).value ?? {};
|
|
41
|
+
value[internalKey] = undefined;
|
|
42
|
+
modifyCache(options.cacheKey, value);
|
|
43
|
+
throw e;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return values;
|
|
47
|
+
};
|
|
48
|
+
let client;
|
|
49
|
+
if (canPrefetch(options)) {
|
|
50
|
+
client = createPrefetchClient(options);
|
|
51
|
+
processCache(options, fetch);
|
|
94
52
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
export default secretsManagerMiddleware
|
|
111
|
-
|
|
53
|
+
const secretsManagerMiddlewareBefore = async (request)=>{
|
|
54
|
+
if (!client) {
|
|
55
|
+
client = await createClient(options, request);
|
|
56
|
+
}
|
|
57
|
+
const { value } = processCache(options, fetch, request);
|
|
58
|
+
Object.assign(request.internal, value);
|
|
59
|
+
if (options.setToContext) {
|
|
60
|
+
const data = await getInternal(Object.keys(options.fetchData), request);
|
|
61
|
+
Object.assign(request.context, data);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
return {
|
|
65
|
+
before: secretsManagerMiddlewareBefore
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
export default secretsManagerMiddleware;
|
|
112
69
|
// used for TS type inference (see index.d.ts)
|
|
113
|
-
export function secret
|
|
114
|
-
|
|
70
|
+
export function secret(name) {
|
|
71
|
+
return name;
|
|
115
72
|
}
|
|
73
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/secrets-manager",
|
|
3
|
-
"version": "5.0.0-
|
|
3
|
+
"version": "5.0.0-rc.0",
|
|
4
4
|
"description": "Secrets Manager middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -58,13 +58,13 @@
|
|
|
58
58
|
"url": "https://github.com/sponsors/willfarrell"
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@middy/util": "5.0.0-
|
|
61
|
+
"@middy/util": "5.0.0-rc.0"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
64
|
"@aws-sdk/client-secrets-manager": "^3.0.0",
|
|
65
|
-
"@middy/core": "5.0.0-
|
|
65
|
+
"@middy/core": "5.0.0-rc.0",
|
|
66
66
|
"@types/aws-lambda": "^8.10.101",
|
|
67
67
|
"aws-xray-sdk": "^3.3.3"
|
|
68
68
|
},
|
|
69
|
-
"gitHead": "
|
|
69
|
+
"gitHead": "403c54ba9f05e038d1ee7541f9e4a7a6d46e9916"
|
|
70
70
|
}
|