@middy/sts 2.5.2 → 2.5.6

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.d.ts +5 -12
  2. package/index.js +43 -55
  3. package/package.json +4 -4
package/index.d.ts CHANGED
@@ -1,18 +1,11 @@
1
1
  import { STS } from 'aws-sdk'
2
- import { captureAWSClient } from 'aws-xray-sdk'
3
2
  import middy from '@middy/core'
3
+ import { Options as MiddyOptions } from '@middy/util'
4
4
 
5
- interface Options<S = STS> {
6
- AwsClient?: new() => S
7
- awsClientOptions?: Partial<STS.Types.ClientConfiguration>
8
- // awsClientAssumeRole?: string,
9
- awsClientCapture?: typeof captureAWSClient
10
- fetchData?: { [key: string]: string }
11
- disablePrefetch?: boolean
12
- cacheKey?: string
13
- cacheExpiry?: number
14
- // setToEnv?: boolean,
15
- setToContext?: boolean
5
+ interface Options<S = STS>
6
+ extends Pick<MiddyOptions<S, STS.Types.ClientConfiguration>,
7
+ 'AwsClient' | 'awsClientOptions' | 'awsClientCapture' |
8
+ 'fetchData' | 'disablePrefetch' | 'cacheKey' | 'cacheExpiry' | 'setToContext'> {
16
9
  }
17
10
 
18
11
  declare function sts (options?: Options): middy.MiddlewareObj
package/index.js CHANGED
@@ -1,5 +1,3 @@
1
- "use strict";
2
-
3
1
  const {
4
2
  canPrefetch,
5
3
  createPrefetchClient,
@@ -8,89 +6,79 @@ const {
8
6
  processCache,
9
7
  getCache,
10
8
  modifyCache
11
- } = require('@middy/util');
12
-
13
- const STS = require('aws-sdk/clients/sts'); // v2
9
+ } = require('@middy/util')
10
+ const STS = require('aws-sdk/clients/sts') // v2
14
11
  // const { STS } = require('@aws-sdk/client-sts') // v3
15
12
 
16
-
17
13
  const defaults = {
18
14
  AwsClient: STS,
19
15
  awsClientOptions: {},
20
16
  // awsClientAssumeRole: undefined, // Not Applicable, as this is the middleware that defines the roles
21
17
  awsClientCapture: undefined,
22
- fetchData: {},
23
- // { contextKey: {RoleArn, RoleSessionName} }
18
+ fetchData: {}, // { contextKey: {RoleArn, RoleSessionName} }
24
19
  disablePrefetch: false,
25
20
  cacheKey: 'sts',
26
21
  cacheExpiry: -1,
27
22
  // setToEnv: false, // returns object, cannot set to process.env
28
23
  setToContext: false
29
- };
24
+ }
30
25
 
31
26
  const stsMiddleware = (opts = {}) => {
32
- const options = { ...defaults,
33
- ...opts
34
- };
27
+ const options = { ...defaults, ...opts }
35
28
 
36
29
  const fetch = (request, cachedValues = {}) => {
37
- const values = {};
30
+ const values = {}
38
31
 
39
32
  for (const internalKey of Object.keys(options.fetchData)) {
40
- var _assumeRoleOptions$Ro;
41
-
42
- if (cachedValues[internalKey]) continue;
43
- const assumeRoleOptions = options.fetchData[internalKey]; // Date cannot be used here to assign default session name, possibility of collision when > 1 role defined
44
-
45
- assumeRoleOptions.RoleSessionName = (_assumeRoleOptions$Ro = assumeRoleOptions === null || assumeRoleOptions === void 0 ? void 0 : assumeRoleOptions.RoleSessionName) !== null && _assumeRoleOptions$Ro !== void 0 ? _assumeRoleOptions$Ro : 'middy-sts-session-' + Math.ceil(Math.random() * 99999);
46
- values[internalKey] = client.assumeRole(assumeRoleOptions).promise() // Required for aws-sdk v2
47
- .then(resp => ({
48
- accessKeyId: resp.Credentials.AccessKeyId,
49
- secretAccessKey: resp.Credentials.SecretAccessKey,
50
- sessionToken: resp.Credentials.SessionToken
51
- })).catch(e => {
52
- var _getCache$value, _getCache;
53
-
54
- const value = (_getCache$value = (_getCache = getCache(options.cacheKey)) === null || _getCache === void 0 ? void 0 : _getCache.value) !== null && _getCache$value !== void 0 ? _getCache$value : {};
55
- value[internalKey] = undefined;
56
- modifyCache(options.cacheKey, value);
57
- throw e;
58
- });
33
+ if (cachedValues[internalKey]) continue
34
+ const assumeRoleOptions = options.fetchData[internalKey]
35
+ // Date cannot be used here to assign default session name, possibility of collision when > 1 role defined
36
+ assumeRoleOptions.RoleSessionName =
37
+ assumeRoleOptions?.RoleSessionName ??
38
+ 'middy-sts-session-' + Math.ceil(Math.random() * 99999)
39
+ values[internalKey] = client
40
+ .assumeRole(assumeRoleOptions)
41
+ .promise() // Required for aws-sdk v2
42
+ .then((resp) => ({
43
+ accessKeyId: resp.Credentials.AccessKeyId,
44
+ secretAccessKey: resp.Credentials.SecretAccessKey,
45
+ sessionToken: resp.Credentials.SessionToken
46
+ }))
47
+ .catch((e) => {
48
+ const value = getCache(options.cacheKey)?.value ?? {}
49
+ value[internalKey] = undefined
50
+ modifyCache(options.cacheKey, value)
51
+ throw e
52
+ })
59
53
  }
60
54
 
61
- return values;
62
- };
63
-
64
- let prefetch, client;
55
+ return values
56
+ }
65
57
 
58
+ let prefetch, client
66
59
  if (canPrefetch(options)) {
67
- client = createPrefetchClient(options);
68
- prefetch = processCache(options, fetch);
60
+ client = createPrefetchClient(options)
61
+ prefetch = processCache(options, fetch)
69
62
  }
70
63
 
71
- const stsMiddlewareBefore = async request => {
72
- var _prefetch;
73
-
64
+ const stsMiddlewareBefore = async (request) => {
74
65
  if (!client) {
75
- client = await createClient(options, request);
66
+ client = await createClient(options, request)
76
67
  }
77
68
 
78
- const {
79
- value
80
- } = (_prefetch = prefetch) !== null && _prefetch !== void 0 ? _prefetch : processCache(options, fetch, request);
81
- Object.assign(request.internal, value);
69
+ const { value } = prefetch ?? processCache(options, fetch, request)
70
+
71
+ Object.assign(request.internal, value)
82
72
 
83
73
  if (options.setToContext) {
84
- const data = await getInternal(Object.keys(options.fetchData), request);
85
- if (options.setToContext) Object.assign(request.context, data);
74
+ const data = await getInternal(Object.keys(options.fetchData), request)
75
+ if (options.setToContext) Object.assign(request.context, data)
86
76
  }
87
-
88
- prefetch = null;
89
- };
77
+ prefetch = null
78
+ }
90
79
 
91
80
  return {
92
81
  before: stsMiddlewareBefore
93
- };
94
- };
95
-
96
- module.exports = stsMiddleware;
82
+ }
83
+ }
84
+ module.exports = stsMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/sts",
3
- "version": "2.5.2",
3
+ "version": "2.5.6",
4
4
  "description": "STS (Security Token Service) credentials middleware for the middy framework",
5
5
  "type": "commonjs",
6
6
  "engines": {
@@ -46,12 +46,12 @@
46
46
  },
47
47
  "homepage": "https://github.com/middyjs/middy#readme",
48
48
  "dependencies": {
49
- "@middy/util": "^2.5.2"
49
+ "@middy/util": "^2.5.6"
50
50
  },
51
51
  "devDependencies": {
52
- "@middy/core": "^2.5.2",
52
+ "@middy/core": "^2.5.6",
53
53
  "aws-sdk": "^2.939.0",
54
54
  "aws-xray-sdk": "^3.3.3"
55
55
  },
56
- "gitHead": "a2bb757a7a13638ae64277f8eecfcf11c1af17d4"
56
+ "gitHead": "0c789f55b4adf691f977b0d9904d1a805bb3bb2b"
57
57
  }