@middy/sts 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 +52 -42
  2. package/package.json +6 -5
package/index.js CHANGED
@@ -1,71 +1,81 @@
1
- import { canPrefetch, createPrefetchClient, createClient, getInternal, processCache, getCache, modifyCache } from '@middy/util';
2
- import STS from 'aws-sdk/clients/sts.js';
1
+ import {
2
+ canPrefetch,
3
+ createPrefetchClient,
4
+ createClient,
5
+ getCache,
6
+ getInternal,
7
+ processCache,
8
+ modifyCache
9
+ } from '@middy/util'
10
+ import STS from 'aws-sdk/clients/sts.js' // v2
11
+ // import { STS } from '@aws-sdk/client-sts' // v3
12
+
3
13
  const defaults = {
4
14
  AwsClient: STS,
5
15
  awsClientOptions: {},
16
+ // awsClientAssumeRole: undefined, // Not Applicable, as this is the middleware that defines the roles
6
17
  awsClientCapture: undefined,
7
- fetchData: {},
18
+ fetchData: {}, // { contextKey: {RoleArn, RoleSessionName} }
8
19
  disablePrefetch: false,
9
20
  cacheKey: 'sts',
10
21
  cacheExpiry: -1,
11
22
  setToContext: false
12
- };
23
+ }
13
24
 
14
25
  const stsMiddleware = (opts = {}) => {
15
- const options = { ...defaults,
16
- ...opts
17
- };
26
+ const options = { ...defaults, ...opts }
18
27
 
19
28
  const fetch = (request, cachedValues = {}) => {
20
- const values = {};
29
+ const values = {}
21
30
 
22
31
  for (const internalKey of Object.keys(options.fetchData)) {
23
- if (cachedValues[internalKey]) continue;
24
- const assumeRoleOptions = options.fetchData[internalKey];
25
- assumeRoleOptions.RoleSessionName ?? (assumeRoleOptions.RoleSessionName = 'middy-sts-session-' + Math.ceil(Math.random() * 99999));
26
- values[internalKey] = client.assumeRole(assumeRoleOptions).promise().then(resp => ({
27
- accessKeyId: resp.Credentials.AccessKeyId,
28
- secretAccessKey: resp.Credentials.SecretAccessKey,
29
- sessionToken: resp.Credentials.SessionToken
30
- })).catch(e => {
31
- const value = getCache(options.cacheKey).value ?? {};
32
- value[internalKey] = undefined;
33
- modifyCache(options.cacheKey, value);
34
- throw e;
35
- });
32
+ if (cachedValues[internalKey]) continue
33
+ const assumeRoleOptions = options.fetchData[internalKey]
34
+ // Date cannot be used here to assign default session name, possibility of collision when > 1 role defined
35
+ assumeRoleOptions.RoleSessionName ??= 'middy-sts-session-' + Math.ceil(Math.random() * 99999)
36
+ values[internalKey] = client
37
+ .assumeRole(assumeRoleOptions)
38
+ .promise() // Required for aws-sdk v2
39
+ .then((resp) => ({
40
+ accessKeyId: resp.Credentials.AccessKeyId,
41
+ secretAccessKey: resp.Credentials.SecretAccessKey,
42
+ sessionToken: resp.Credentials.SessionToken
43
+ }))
44
+ .catch((e) => {
45
+ const value = getCache(options.cacheKey).value ?? {}
46
+ value[internalKey] = undefined
47
+ modifyCache(options.cacheKey, value)
48
+ throw e
49
+ })
36
50
  }
37
51
 
38
- return values;
39
- };
40
-
41
- let prefetch, client;
52
+ return values
53
+ }
42
54
 
55
+ let prefetch, client
43
56
  if (canPrefetch(options)) {
44
- client = createPrefetchClient(options);
45
- prefetch = processCache(options, fetch);
57
+ client = createPrefetchClient(options)
58
+ prefetch = processCache(options, fetch)
46
59
  }
47
60
 
48
- const stsMiddlewareBefore = async request => {
61
+ const stsMiddlewareBefore = async (request) => {
49
62
  if (!client) {
50
- client = await createClient(options, request);
63
+ client = await createClient(options, request)
51
64
  }
52
65
 
53
- const {
54
- value
55
- } = prefetch ?? processCache(options, fetch, request);
56
- Object.assign(request.internal, value);
66
+ const { value } = prefetch ?? processCache(options, fetch, request)
67
+
68
+ Object.assign(request.internal, value)
57
69
 
58
70
  if (options.setToContext) {
59
- const data = await getInternal(Object.keys(options.fetchData), request);
60
- if (options.setToContext) Object.assign(request.context, data);
71
+ const data = await getInternal(Object.keys(options.fetchData), request)
72
+ if (options.setToContext) Object.assign(request.context, data)
61
73
  }
62
-
63
- prefetch = null;
64
- };
74
+ prefetch = null
75
+ }
65
76
 
66
77
  return {
67
78
  before: stsMiddlewareBefore
68
- };
69
- };
70
-
71
- export default stsMiddleware;
79
+ }
80
+ }
81
+ export default stsMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/sts",
3
- "version": "3.0.0-alpha.2",
3
+ "version": "3.0.0-alpha.3",
4
4
  "description": "STS (Security Token Service) credentials 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": [
@@ -47,12 +48,12 @@
47
48
  },
48
49
  "homepage": "https://github.com/middyjs/middy#readme",
49
50
  "dependencies": {
50
- "@middy/util": "^3.0.0-alpha.2"
51
+ "@middy/util": "^3.0.0-alpha.3"
51
52
  },
52
53
  "devDependencies": {
53
- "@middy/core": "^3.0.0-alpha.2",
54
+ "@middy/core": "^3.0.0-alpha.3",
54
55
  "aws-sdk": "^2.939.0",
55
56
  "aws-xray-sdk": "^3.3.3"
56
57
  },
57
- "gitHead": "de30419273ecbff08f367f47c7e320ec981cf145"
58
+ "gitHead": "1441158711580313765e6d156046ef0fade0d156"
58
59
  }