@middy/secrets-manager 4.6.5 → 5.0.0-alpha.1
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.d.ts +31 -10
- package/index.js +111 -65
- package/package.json +5 -11
- package/index.cjs +0 -79
package/index.d.ts
CHANGED
|
@@ -3,20 +3,41 @@ import { Options as MiddyOptions } from '@middy/util'
|
|
|
3
3
|
import { Context as LambdaContext } from 'aws-lambda'
|
|
4
4
|
import { SecretsManagerClient, SecretsManagerClientConfig } from '@aws-sdk/client-secrets-manager'
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
export type SecretType<T> = string & { __returnType?: T }
|
|
7
|
+
export declare function secret<T> (path: string): SecretType<T>
|
|
8
|
+
|
|
9
|
+
interface SecretsManagerOptions<AwsSecretsManagerClient = SecretsManagerClient>
|
|
10
|
+
extends Omit<MiddyOptions<
|
|
8
11
|
AwsSecretsManagerClient,
|
|
9
12
|
SecretsManagerClientConfig
|
|
10
|
-
> {
|
|
11
|
-
|
|
12
|
-
export type Context<TOptions extends Options | undefined> = TOptions extends {
|
|
13
|
-
setToContext: true
|
|
13
|
+
>, 'fetchData'> {
|
|
14
|
+
fetchData?: { [key: string]: string | SecretType<unknown> }
|
|
14
15
|
}
|
|
15
|
-
? LambdaContext & Record<keyof TOptions['fetchData'], any>
|
|
16
|
-
: LambdaContext
|
|
17
16
|
|
|
18
|
-
|
|
17
|
+
export type Context<TOptions extends SecretsManagerOptions | undefined> =
|
|
18
|
+
TOptions extends { setToContext: true }
|
|
19
|
+
? TOptions extends { fetchData: infer TFetchData }
|
|
20
|
+
? LambdaContext & {
|
|
21
|
+
[Key in keyof TFetchData]: TFetchData[Key] extends SecretType<infer T>
|
|
22
|
+
? T
|
|
23
|
+
: unknown
|
|
24
|
+
}
|
|
25
|
+
: never
|
|
26
|
+
: LambdaContext
|
|
27
|
+
|
|
28
|
+
export type Internal<TOptions extends SecretsManagerOptions | undefined> =
|
|
29
|
+
TOptions extends SecretsManagerOptions
|
|
30
|
+
? TOptions extends { fetchData: infer TFetchData }
|
|
31
|
+
? {
|
|
32
|
+
[Key in keyof TFetchData]: TFetchData[Key] extends SecretType<infer T>
|
|
33
|
+
? T
|
|
34
|
+
: unknown
|
|
35
|
+
}
|
|
36
|
+
: {}
|
|
37
|
+
: {}
|
|
38
|
+
|
|
39
|
+
declare function secretsManager<TOptions extends SecretsManagerOptions | undefined> (
|
|
19
40
|
options?: TOptions
|
|
20
|
-
): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>>
|
|
41
|
+
): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>, Internal<TOptions>>
|
|
21
42
|
|
|
22
43
|
export default secretsManager
|
package/index.js
CHANGED
|
@@ -1,69 +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
|
-
|
|
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
|
+
}
|
|
109
|
+
}
|
|
110
|
+
export default secretsManagerMiddleware
|
|
69
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,33 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/secrets-manager",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0-alpha.1",
|
|
4
4
|
"description": "Secrets Manager middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=18"
|
|
8
8
|
},
|
|
9
9
|
"engineStrict": true,
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"main": "./index.cjs",
|
|
14
13
|
"module": "./index.js",
|
|
15
14
|
"exports": {
|
|
16
15
|
".": {
|
|
17
16
|
"import": {
|
|
18
17
|
"types": "./index.d.ts",
|
|
19
18
|
"default": "./index.js"
|
|
20
|
-
},
|
|
21
|
-
"require": {
|
|
22
|
-
"types": "./index.d.ts",
|
|
23
|
-
"default": "./index.cjs"
|
|
24
19
|
}
|
|
25
20
|
}
|
|
26
21
|
},
|
|
27
22
|
"types": "index.d.ts",
|
|
28
23
|
"files": [
|
|
29
24
|
"index.js",
|
|
30
|
-
"index.cjs",
|
|
31
25
|
"index.d.ts"
|
|
32
26
|
],
|
|
33
27
|
"scripts": {
|
|
@@ -64,13 +58,13 @@
|
|
|
64
58
|
"url": "https://github.com/sponsors/willfarrell"
|
|
65
59
|
},
|
|
66
60
|
"dependencies": {
|
|
67
|
-
"@middy/util": "
|
|
61
|
+
"@middy/util": "5.0.0-alpha.1"
|
|
68
62
|
},
|
|
69
63
|
"devDependencies": {
|
|
70
64
|
"@aws-sdk/client-secrets-manager": "^3.0.0",
|
|
71
|
-
"@middy/core": "
|
|
65
|
+
"@middy/core": "5.0.0-alpha.1",
|
|
72
66
|
"@types/aws-lambda": "^8.10.101",
|
|
73
67
|
"aws-xray-sdk": "^3.3.3"
|
|
74
68
|
},
|
|
75
|
-
"gitHead": "
|
|
69
|
+
"gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
|
|
76
70
|
}
|
package/index.cjs
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(module, "exports", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return _default;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _util = require("@middy/util");
|
|
12
|
-
const _clientsecretsmanager = require("@aws-sdk/client-secrets-manager");
|
|
13
|
-
const defaults = {
|
|
14
|
-
AwsClient: _clientsecretsmanager.SecretsManagerClient,
|
|
15
|
-
awsClientOptions: {},
|
|
16
|
-
awsClientAssumeRole: undefined,
|
|
17
|
-
awsClientCapture: undefined,
|
|
18
|
-
fetchData: {},
|
|
19
|
-
fetchRotationDate: false,
|
|
20
|
-
disablePrefetch: false,
|
|
21
|
-
cacheKey: 'secrets-manager',
|
|
22
|
-
cacheKeyExpiry: {},
|
|
23
|
-
cacheExpiry: -1,
|
|
24
|
-
setToContext: false
|
|
25
|
-
};
|
|
26
|
-
const secretsManagerMiddleware = (opts = {})=>{
|
|
27
|
-
const options = {
|
|
28
|
-
...defaults,
|
|
29
|
-
...opts
|
|
30
|
-
};
|
|
31
|
-
const fetch = (request, cachedValues = {})=>{
|
|
32
|
-
const values = {};
|
|
33
|
-
for (const internalKey of Object.keys(options.fetchData)){
|
|
34
|
-
if (cachedValues[internalKey]) continue;
|
|
35
|
-
values[internalKey] = Promise.resolve().then(()=>{
|
|
36
|
-
if (options.fetchRotationDate === true || options.fetchRotationDate?.[internalKey]) {
|
|
37
|
-
return client.send(new _clientsecretsmanager.DescribeSecretCommand({
|
|
38
|
-
SecretId: options.fetchData[internalKey]
|
|
39
|
-
})).then((resp)=>{
|
|
40
|
-
if (options.cacheExpiry < 0) {
|
|
41
|
-
options.cacheKeyExpiry[internalKey] = resp.NextRotationDate * 1000;
|
|
42
|
-
} else {
|
|
43
|
-
options.cacheKeyExpiry[internalKey] = Math.min(Math.max(resp.LastRotationDate, resp.LastChangedDate) * 1000 + options.cacheExpiry, resp.NextRotationDate * 1000);
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
}).then(()=>client.send(new _clientsecretsmanager.GetSecretValueCommand({
|
|
48
|
-
SecretId: options.fetchData[internalKey]
|
|
49
|
-
}))).then((resp)=>(0, _util.jsonSafeParse)(resp.SecretString)).catch((e)=>{
|
|
50
|
-
const value = (0, _util.getCache)(options.cacheKey).value ?? {};
|
|
51
|
-
value[internalKey] = undefined;
|
|
52
|
-
(0, _util.modifyCache)(options.cacheKey, value);
|
|
53
|
-
throw e;
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
return values;
|
|
57
|
-
};
|
|
58
|
-
let client;
|
|
59
|
-
if ((0, _util.canPrefetch)(options)) {
|
|
60
|
-
client = (0, _util.createPrefetchClient)(options);
|
|
61
|
-
(0, _util.processCache)(options, fetch);
|
|
62
|
-
}
|
|
63
|
-
const secretsManagerMiddlewareBefore = async (request)=>{
|
|
64
|
-
if (!client) {
|
|
65
|
-
client = await (0, _util.createClient)(options, request);
|
|
66
|
-
}
|
|
67
|
-
const { value } = (0, _util.processCache)(options, fetch, request);
|
|
68
|
-
Object.assign(request.internal, value);
|
|
69
|
-
if (options.setToContext) {
|
|
70
|
-
const data = await (0, _util.getInternal)(Object.keys(options.fetchData), request);
|
|
71
|
-
Object.assign(request.context, data);
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
return {
|
|
75
|
-
before: secretsManagerMiddlewareBefore
|
|
76
|
-
};
|
|
77
|
-
};
|
|
78
|
-
const _default = secretsManagerMiddleware;
|
|
79
|
-
|