@middy/secrets-manager 3.0.0-alpha.2 → 3.0.0-alpha.3

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 +49 -38
  2. package/package.json +6 -5
package/index.js CHANGED
@@ -1,68 +1,79 @@
1
- import { canPrefetch, createPrefetchClient, createClient, processCache, getCache, modifyCache, jsonSafeParse, getInternal } from '@middy/util';
2
- import SecretsManager from 'aws-sdk/clients/secretsmanager.js';
1
+ import {
2
+ canPrefetch,
3
+ createPrefetchClient,
4
+ createClient,
5
+ getCache,
6
+ getInternal,
7
+ processCache,
8
+ modifyCache,
9
+ jsonSafeParse
10
+ } from '@middy/util'
11
+ import SecretsManager from 'aws-sdk/clients/secretsmanager.js' // v2
12
+ // import { SecretsManager } from '@aws-sdk/client-secrets-manager' // v3
13
+
3
14
  const defaults = {
4
15
  AwsClient: SecretsManager,
5
16
  awsClientOptions: {},
6
17
  awsClientAssumeRole: undefined,
7
18
  awsClientCapture: undefined,
8
- fetchData: {},
19
+ fetchData: {}, // If more than 2, consider writing own using ListSecrets
9
20
  disablePrefetch: false,
10
21
  cacheKey: 'secrets-manager',
11
22
  cacheExpiry: -1,
12
23
  setToContext: false
13
- };
24
+ }
14
25
 
15
26
  const secretsManagerMiddleware = (opts = {}) => {
16
- const options = { ...defaults,
17
- ...opts
18
- };
27
+ const options = { ...defaults, ...opts }
19
28
 
20
29
  const fetch = (request, cachedValues = {}) => {
21
- const values = {};
30
+ const values = {}
22
31
 
32
+ // Multiple secrets can be requested in a single requests,
33
+ // however this is likely uncommon IRL, increases complexity to handle,
34
+ // and will require recursive promise resolution impacting performance.
35
+ // See https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SecretsManager.html#listSecrets-property
23
36
  for (const internalKey of Object.keys(options.fetchData)) {
24
- if (cachedValues[internalKey]) continue;
25
- values[internalKey] = client.getSecretValue({
26
- SecretId: options.fetchData[internalKey]
27
- }).promise().then(resp => jsonSafeParse(resp.SecretString)).catch(e => {
28
- const value = getCache(options.cacheKey).value ?? {};
29
- value[internalKey] = undefined;
30
- modifyCache(options.cacheKey, value);
31
- throw e;
32
- });
37
+ if (cachedValues[internalKey]) continue
38
+ values[internalKey] = client
39
+ .getSecretValue({ SecretId: options.fetchData[internalKey] })
40
+ .promise() // Required for aws-sdk v2
41
+ .then((resp) => jsonSafeParse(resp.SecretString))
42
+ .catch((e) => {
43
+ const value = getCache(options.cacheKey).value ?? {}
44
+ value[internalKey] = undefined
45
+ modifyCache(options.cacheKey, value)
46
+ throw e
47
+ })
33
48
  }
49
+ return values
50
+ }
34
51
 
35
- return values;
36
- };
37
-
38
- let prefetch, client;
39
-
52
+ let prefetch, client
40
53
  if (canPrefetch(options)) {
41
- client = createPrefetchClient(options);
42
- prefetch = processCache(options, fetch);
54
+ client = createPrefetchClient(options)
55
+ prefetch = processCache(options, fetch)
43
56
  }
44
57
 
45
- const secretsManagerMiddlewareBefore = async request => {
58
+ const secretsManagerMiddlewareBefore = async (request) => {
46
59
  if (!client) {
47
- client = await createClient(options, request);
60
+ client = await createClient(options, request)
48
61
  }
49
62
 
50
- const {
51
- value
52
- } = prefetch ?? processCache(options, fetch, request);
53
- Object.assign(request.internal, value);
63
+ const { value } = prefetch ?? processCache(options, fetch, request)
64
+
65
+ Object.assign(request.internal, value)
54
66
 
55
67
  if (options.setToContext) {
56
- const data = await getInternal(Object.keys(options.fetchData), request);
57
- Object.assign(request.context, data);
68
+ const data = await getInternal(Object.keys(options.fetchData), request)
69
+ Object.assign(request.context, data)
58
70
  }
59
71
 
60
- prefetch = null;
61
- };
72
+ prefetch = null
73
+ }
62
74
 
63
75
  return {
64
76
  before: secretsManagerMiddlewareBefore
65
- };
66
- };
67
-
68
- export default secretsManagerMiddleware;
77
+ }
78
+ }
79
+ export default secretsManagerMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/secrets-manager",
3
- "version": "3.0.0-alpha.2",
3
+ "version": "3.0.0-alpha.3",
4
4
  "description": "Secrets Manager middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -18,7 +18,8 @@
18
18
  ],
19
19
  "scripts": {
20
20
  "test": "npm run test:unit",
21
- "test:unit": "ava"
21
+ "test:unit": "ava",
22
+ "test:benchmark": "node __benchmarks__/index.js"
22
23
  },
23
24
  "license": "MIT",
24
25
  "keywords": [
@@ -45,12 +46,12 @@
45
46
  },
46
47
  "homepage": "https://github.com/middyjs/middy#readme",
47
48
  "dependencies": {
48
- "@middy/util": "^3.0.0-alpha.2"
49
+ "@middy/util": "^3.0.0-alpha.3"
49
50
  },
50
51
  "devDependencies": {
51
- "@middy/core": "^3.0.0-alpha.2",
52
+ "@middy/core": "^3.0.0-alpha.3",
52
53
  "aws-sdk": "^2.939.0",
53
54
  "aws-xray-sdk": "^3.3.3"
54
55
  },
55
- "gitHead": "de30419273ecbff08f367f47c7e320ec981cf145"
56
+ "gitHead": "1441158711580313765e6d156046ef0fade0d156"
56
57
  }