@middy/secrets-manager 4.5.5 → 4.6.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 (3) hide show
  1. package/index.cjs +17 -3
  2. package/index.js +18 -4
  3. package/package.json +4 -4
package/index.cjs CHANGED
@@ -16,8 +16,10 @@ const defaults = {
16
16
  awsClientAssumeRole: undefined,
17
17
  awsClientCapture: undefined,
18
18
  fetchData: {},
19
+ fetchRotationDate: false,
19
20
  disablePrefetch: false,
20
21
  cacheKey: 'secrets-manager',
22
+ cacheKeyExpiry: {},
21
23
  cacheExpiry: -1,
22
24
  setToContext: false
23
25
  };
@@ -30,9 +32,21 @@ const secretsManagerMiddleware = (opts = {})=>{
30
32
  const values = {};
31
33
  for (const internalKey of Object.keys(options.fetchData)){
32
34
  if (cachedValues[internalKey]) continue;
33
- values[internalKey] = client.send(new _clientsecretsmanager.GetSecretValueCommand({
34
- SecretId: options.fetchData[internalKey]
35
- })).then((resp)=>(0, _util.jsonSafeParse)(resp.SecretString)).catch((e)=>{
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)=>{
36
50
  const value = (0, _util.getCache)(options.cacheKey).value ?? {};
37
51
  value[internalKey] = undefined;
38
52
  (0, _util.modifyCache)(options.cacheKey, value);
package/index.js CHANGED
@@ -1,13 +1,15 @@
1
1
  import { canPrefetch, createPrefetchClient, createClient, getCache, getInternal, processCache, modifyCache, jsonSafeParse } from '@middy/util';
2
- import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';
2
+ import { SecretsManagerClient, DescribeSecretCommand, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';
3
3
  const defaults = {
4
4
  AwsClient: SecretsManagerClient,
5
5
  awsClientOptions: {},
6
6
  awsClientAssumeRole: undefined,
7
7
  awsClientCapture: undefined,
8
8
  fetchData: {},
9
+ fetchRotationDate: false,
9
10
  disablePrefetch: false,
10
11
  cacheKey: 'secrets-manager',
12
+ cacheKeyExpiry: {},
11
13
  cacheExpiry: -1,
12
14
  setToContext: false
13
15
  };
@@ -20,9 +22,21 @@ const secretsManagerMiddleware = (opts = {})=>{
20
22
  const values = {};
21
23
  for (const internalKey of Object.keys(options.fetchData)){
22
24
  if (cachedValues[internalKey]) continue;
23
- values[internalKey] = client.send(new GetSecretValueCommand({
24
- SecretId: options.fetchData[internalKey]
25
- })).then((resp)=>jsonSafeParse(resp.SecretString)).catch((e)=>{
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
+ });
36
+ }
37
+ }).then(()=>client.send(new GetSecretValueCommand({
38
+ SecretId: options.fetchData[internalKey]
39
+ }))).then((resp)=>jsonSafeParse(resp.SecretString)).catch((e)=>{
26
40
  const value = getCache(options.cacheKey).value ?? {};
27
41
  value[internalKey] = undefined;
28
42
  modifyCache(options.cacheKey, value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/secrets-manager",
3
- "version": "4.5.5",
3
+ "version": "4.6.1",
4
4
  "description": "Secrets Manager middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -64,13 +64,13 @@
64
64
  "url": "https://github.com/sponsors/willfarrell"
65
65
  },
66
66
  "dependencies": {
67
- "@middy/util": "4.5.5"
67
+ "@middy/util": "4.6.1"
68
68
  },
69
69
  "devDependencies": {
70
70
  "@aws-sdk/client-secrets-manager": "^3.0.0",
71
- "@middy/core": "4.5.5",
71
+ "@middy/core": "4.6.1",
72
72
  "@types/aws-lambda": "^8.10.101",
73
73
  "aws-xray-sdk": "^3.3.3"
74
74
  },
75
- "gitHead": "e8458a63a1b10932c30556cd8e2bdc4775858d0b"
75
+ "gitHead": "253ed0e4ca95623decbade03938a07d837a1eba2"
76
76
  }