@middy/secrets-manager 2.5.2 → 3.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/LICENSE +1 -1
- package/README.md +1 -4
- package/index.d.ts +2 -13
- package/index.js +9 -36
- package/package.json +8 -7
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017-
|
|
3
|
+
Copyright (c) 2017-2022 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -53,13 +53,10 @@ npm install --save @middy/secrets-manager
|
|
|
53
53
|
- `disablePrefetch` (boolean) (default `false`): On cold start requests will trigger early if they can. Setting `awsClientAssumeRole` disables prefetch.
|
|
54
54
|
- `cacheKey` (string) (default `secrets-manager`): Cache key for the fetched data responses. Must be unique across all middleware.
|
|
55
55
|
- `cacheExpiry` (number) (default `-1`): How long fetch data responses should be cached for. `-1`: cache forever, `0`: never cache, `n`: cache for n ms.
|
|
56
|
-
- `setToEnv` (boolean) (default `false`): Store secrets to `process.env`. **Storing secrets in `process.env` is considered security bad practice**
|
|
57
56
|
- `setToContext` (boolean) (default `false`): Store secrets to `request.context`.
|
|
58
57
|
|
|
59
58
|
NOTES:
|
|
60
59
|
- Lambda is required to have IAM permission for `secretsmanager:GetSecretValue`
|
|
61
|
-
- `setToEnv` and `setToContext` are included for legacy support and should be avoided for performance and security reasons. See main documentation for best practices.
|
|
62
|
-
- `setToEnv` can only assign secrets of type string
|
|
63
60
|
|
|
64
61
|
## Sample usage
|
|
65
62
|
|
|
@@ -99,7 +96,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
|
|
|
99
96
|
|
|
100
97
|
## License
|
|
101
98
|
|
|
102
|
-
Licensed under [MIT License](LICENSE). Copyright (c) 2017-
|
|
99
|
+
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
103
100
|
|
|
104
101
|
<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
|
|
105
102
|
<img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
|
package/index.d.ts
CHANGED
|
@@ -1,19 +1,8 @@
|
|
|
1
1
|
import { SecretsManager } from 'aws-sdk'
|
|
2
|
-
import { captureAWSClient } from 'aws-xray-sdk'
|
|
3
2
|
import middy from '@middy/core'
|
|
3
|
+
import { Options as MiddyOptions } from '@middy/util'
|
|
4
4
|
|
|
5
|
-
interface Options<SM = SecretsManager> {
|
|
6
|
-
AwsClient?: new() => SM
|
|
7
|
-
awsClientOptions?: Partial<SecretsManager.Types.ClientConfiguration>
|
|
8
|
-
awsClientAssumeRole?: string
|
|
9
|
-
awsClientCapture?: typeof captureAWSClient
|
|
10
|
-
fetchData?: { [key: string]: string }
|
|
11
|
-
disablePrefetch?: boolean
|
|
12
|
-
cacheKey?: string
|
|
13
|
-
cacheExpiry?: number
|
|
14
|
-
setToEnv?: boolean
|
|
15
|
-
setToContext?: boolean
|
|
16
|
-
}
|
|
5
|
+
interface Options<SM = SecretsManager> extends MiddyOptions<SM, SecretsManager.Types.ClientConfiguration> {}
|
|
17
6
|
|
|
18
7
|
declare function secretsManager (options?: Options): middy.MiddlewareObj
|
|
19
8
|
|
package/index.js
CHANGED
|
@@ -1,32 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const {
|
|
4
|
-
canPrefetch,
|
|
5
|
-
createPrefetchClient,
|
|
6
|
-
createClient,
|
|
7
|
-
processCache,
|
|
8
|
-
getCache,
|
|
9
|
-
modifyCache,
|
|
10
|
-
jsonSafeParse,
|
|
11
|
-
getInternal
|
|
12
|
-
} = require('@middy/util');
|
|
13
|
-
|
|
14
|
-
const SecretsManager = require('aws-sdk/clients/secretsmanager'); // v2
|
|
15
|
-
// const { SecretsManager } = require('@aws-sdk/client-secrets-manager') // v3
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
import { canPrefetch, createPrefetchClient, createClient, processCache, getCache, modifyCache, jsonSafeParse, getInternal } from '@middy/util';
|
|
2
|
+
import SecretsManager from 'aws-sdk/clients/secretsmanager.js';
|
|
18
3
|
const defaults = {
|
|
19
4
|
AwsClient: SecretsManager,
|
|
20
5
|
awsClientOptions: {},
|
|
21
6
|
awsClientAssumeRole: undefined,
|
|
22
7
|
awsClientCapture: undefined,
|
|
23
8
|
fetchData: {},
|
|
24
|
-
// If more than 2, consider writing own using ListSecrets
|
|
25
9
|
disablePrefetch: false,
|
|
26
10
|
cacheKey: 'secrets-manager',
|
|
27
11
|
cacheExpiry: -1,
|
|
28
|
-
setToEnv: false,
|
|
29
|
-
// can return object when requesting db credentials, cannot set to process.env
|
|
30
12
|
setToContext: false
|
|
31
13
|
};
|
|
32
14
|
|
|
@@ -36,20 +18,14 @@ const secretsManagerMiddleware = (opts = {}) => {
|
|
|
36
18
|
};
|
|
37
19
|
|
|
38
20
|
const fetch = (request, cachedValues = {}) => {
|
|
39
|
-
const values = {};
|
|
40
|
-
// however this is likely uncommon IRL, increases complexity to handle,
|
|
41
|
-
// and will require recursive promise resolution impacting performance.
|
|
42
|
-
// See https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SecretsManager.html#listSecrets-property
|
|
21
|
+
const values = {};
|
|
43
22
|
|
|
44
23
|
for (const internalKey of Object.keys(options.fetchData)) {
|
|
45
24
|
if (cachedValues[internalKey]) continue;
|
|
46
25
|
values[internalKey] = client.getSecretValue({
|
|
47
26
|
SecretId: options.fetchData[internalKey]
|
|
48
|
-
}).promise()
|
|
49
|
-
|
|
50
|
-
var _getCache$value, _getCache;
|
|
51
|
-
|
|
52
|
-
const value = (_getCache$value = (_getCache = getCache(options.cacheKey)) === null || _getCache === void 0 ? void 0 : _getCache.value) !== null && _getCache$value !== void 0 ? _getCache$value : {};
|
|
27
|
+
}).promise().then(resp => jsonSafeParse(resp.SecretString)).catch(e => {
|
|
28
|
+
const value = getCache(options.cacheKey).value ?? {};
|
|
53
29
|
value[internalKey] = undefined;
|
|
54
30
|
modifyCache(options.cacheKey, value);
|
|
55
31
|
throw e;
|
|
@@ -67,21 +43,18 @@ const secretsManagerMiddleware = (opts = {}) => {
|
|
|
67
43
|
}
|
|
68
44
|
|
|
69
45
|
const secretsManagerMiddlewareBefore = async request => {
|
|
70
|
-
var _prefetch;
|
|
71
|
-
|
|
72
46
|
if (!client) {
|
|
73
47
|
client = await createClient(options, request);
|
|
74
48
|
}
|
|
75
49
|
|
|
76
50
|
const {
|
|
77
51
|
value
|
|
78
|
-
} =
|
|
52
|
+
} = prefetch ?? processCache(options, fetch, request);
|
|
79
53
|
Object.assign(request.internal, value);
|
|
80
54
|
|
|
81
|
-
if (options.setToContext
|
|
55
|
+
if (options.setToContext) {
|
|
82
56
|
const data = await getInternal(Object.keys(options.fetchData), request);
|
|
83
|
-
|
|
84
|
-
if (options.setToContext) Object.assign(request.context, data);
|
|
57
|
+
Object.assign(request.context, data);
|
|
85
58
|
}
|
|
86
59
|
|
|
87
60
|
prefetch = null;
|
|
@@ -92,4 +65,4 @@ const secretsManagerMiddleware = (opts = {}) => {
|
|
|
92
65
|
};
|
|
93
66
|
};
|
|
94
67
|
|
|
95
|
-
|
|
68
|
+
export default secretsManagerMiddleware;
|
package/package.json
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/secrets-manager",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-alpha.1",
|
|
4
4
|
"description": "Secrets Manager middleware for the middy framework",
|
|
5
|
-
"type": "
|
|
5
|
+
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=14"
|
|
8
8
|
},
|
|
9
9
|
"engineStrict": true,
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
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": {
|
|
@@ -44,12 +45,12 @@
|
|
|
44
45
|
},
|
|
45
46
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
46
47
|
"dependencies": {
|
|
47
|
-
"@middy/util": "^
|
|
48
|
+
"@middy/util": "^3.0.0-alpha.1"
|
|
48
49
|
},
|
|
49
50
|
"devDependencies": {
|
|
50
|
-
"@middy/core": "^
|
|
51
|
+
"@middy/core": "^3.0.0-alpha.1",
|
|
51
52
|
"aws-sdk": "^2.939.0",
|
|
52
53
|
"aws-xray-sdk": "^3.3.3"
|
|
53
54
|
},
|
|
54
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "a14125c6b2e21b181824f9985a919a47f1e4711f"
|
|
55
56
|
}
|