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