@middy/sts 5.0.3 → 5.2.0
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.
- package/index.js +77 -56
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,59 +1,80 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
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
|
+
|
|
3
12
|
const defaults = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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)
|
|
42
73
|
}
|
|
43
|
-
|
|
44
|
-
if (!client) {
|
|
45
|
-
client = await createClient(options, request);
|
|
46
|
-
}
|
|
47
|
-
const { value } = processCache(options, fetch, request);
|
|
48
|
-
Object.assign(request.internal, value);
|
|
49
|
-
if (options.setToContext) {
|
|
50
|
-
const data = await getInternal(Object.keys(options.fetchData), request);
|
|
51
|
-
if (options.setToContext) Object.assign(request.context, data);
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
return {
|
|
55
|
-
before: stsMiddlewareBefore
|
|
56
|
-
};
|
|
57
|
-
};
|
|
58
|
-
export default stsMiddleware;
|
|
74
|
+
}
|
|
59
75
|
|
|
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
|
|
3
|
+
"version": "5.2.0",
|
|
4
4
|
"description": "STS (Security Token Service) credentials middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -60,13 +60,13 @@
|
|
|
60
60
|
"url": "https://github.com/sponsors/willfarrell"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@middy/util": "5.0
|
|
63
|
+
"@middy/util": "5.2.0"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
66
|
"@aws-sdk/client-sts": "^3.0.0",
|
|
67
|
-
"@middy/core": "5.0
|
|
67
|
+
"@middy/core": "5.2.0",
|
|
68
68
|
"@types/aws-lambda": "^8.10.101",
|
|
69
69
|
"aws-xray-sdk": "^3.3.3"
|
|
70
70
|
},
|
|
71
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "2d9096a49cd8fb62359517be96d6c93609df41f0"
|
|
72
72
|
}
|