@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.
Files changed (2) hide show
  1. package/index.js +68 -110
  2. package/package.json +4 -4
package/index.js CHANGED
@@ -1,115 +1,73 @@
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
-
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
- 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
- )
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
- .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
- })
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
- 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
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 (name) {
114
- return name
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-alpha.1",
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-alpha.1"
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-alpha.1",
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": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
69
+ "gitHead": "403c54ba9f05e038d1ee7541f9e4a7a6d46e9916"
70
70
  }