@middy/secrets-manager 5.1.0 → 5.2.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 +110 -67
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,72 +1,115 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
canPrefetch,
|
|
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
|
+
|
|
3
17
|
const defaults = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
18
|
+
AwsClient: SecretsManagerClient,
|
|
19
|
+
awsClientOptions: {},
|
|
20
|
+
awsClientAssumeRole: undefined,
|
|
21
|
+
awsClientCapture: undefined,
|
|
22
|
+
fetchData: {},
|
|
23
|
+
fetchRotationDate: false, // true: apply to all or {key: true} for individual
|
|
24
|
+
disablePrefetch: false,
|
|
25
|
+
cacheKey: 'secrets-manager',
|
|
26
|
+
cacheKeyExpiry: {},
|
|
27
|
+
cacheExpiry: -1, // ignored when fetchRotationRules is true/object
|
|
28
|
+
setToContext: false
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const secretsManagerMiddleware = (opts = {}) => {
|
|
32
|
+
const options = { ...defaults, ...opts }
|
|
33
|
+
|
|
34
|
+
const fetch = (request, cachedValues = {}) => {
|
|
35
|
+
const values = {}
|
|
36
|
+
|
|
37
|
+
for (const internalKey of Object.keys(options.fetchData)) {
|
|
38
|
+
if (cachedValues[internalKey]) continue
|
|
39
|
+
|
|
40
|
+
values[internalKey] = Promise.resolve()
|
|
41
|
+
.then(() => {
|
|
42
|
+
if (
|
|
43
|
+
options.fetchRotationDate === true ||
|
|
44
|
+
options.fetchRotationDate?.[internalKey]
|
|
45
|
+
) {
|
|
46
|
+
return client
|
|
47
|
+
.send(
|
|
48
|
+
new DescribeSecretCommand({
|
|
49
|
+
SecretId: options.fetchData[internalKey]
|
|
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
|
+
)
|
|
36
63
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
.then(() =>
|
|
68
|
+
client.send(
|
|
69
|
+
new GetSecretValueCommand({
|
|
70
|
+
SecretId: options.fetchData[internalKey]
|
|
71
|
+
})
|
|
72
|
+
)
|
|
73
|
+
)
|
|
74
|
+
.then((resp) => jsonSafeParse(resp.SecretString))
|
|
75
|
+
.catch((e) => {
|
|
76
|
+
const value = getCache(options.cacheKey).value ?? {}
|
|
77
|
+
value[internalKey] = undefined
|
|
78
|
+
modifyCache(options.cacheKey, value)
|
|
79
|
+
throw e
|
|
80
|
+
})
|
|
52
81
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const { value } = processCache(options, fetch, request)
|
|
97
|
+
|
|
98
|
+
Object.assign(request.internal, value)
|
|
99
|
+
|
|
100
|
+
if (options.setToContext) {
|
|
101
|
+
const data = await getInternal(Object.keys(options.fetchData), request)
|
|
102
|
+
Object.assign(request.context, data)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
before: secretsManagerMiddlewareBefore
|
|
108
|
+
}
|
|
71
109
|
}
|
|
110
|
+
export default secretsManagerMiddleware
|
|
72
111
|
|
|
112
|
+
// used for TS type inference (see index.d.ts)
|
|
113
|
+
export function secret (name) {
|
|
114
|
+
return name
|
|
115
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/secrets-manager",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.2.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.
|
|
61
|
+
"@middy/util": "5.2.0"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
64
|
"@aws-sdk/client-secrets-manager": "^3.0.0",
|
|
65
|
-
"@middy/core": "5.
|
|
65
|
+
"@middy/core": "5.2.0",
|
|
66
66
|
"@types/aws-lambda": "^8.10.101",
|
|
67
67
|
"aws-xray-sdk": "^3.3.3"
|
|
68
68
|
},
|
|
69
|
-
"gitHead": "
|
|
69
|
+
"gitHead": "2d9096a49cd8fb62359517be96d6c93609df41f0"
|
|
70
70
|
}
|