@middy/sts 5.0.0-alpha.1 → 5.0.0-alpha.2

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