@middy/sts 2.5.6 → 2.5.7
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 +55 -43
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
1
3
|
const {
|
|
2
4
|
canPrefetch,
|
|
3
5
|
createPrefetchClient,
|
|
@@ -6,79 +8,89 @@ const {
|
|
|
6
8
|
processCache,
|
|
7
9
|
getCache,
|
|
8
10
|
modifyCache
|
|
9
|
-
} = require('@middy/util')
|
|
10
|
-
|
|
11
|
+
} = require('@middy/util');
|
|
12
|
+
|
|
13
|
+
const STS = require('aws-sdk/clients/sts'); // v2
|
|
11
14
|
// const { STS } = require('@aws-sdk/client-sts') // v3
|
|
12
15
|
|
|
16
|
+
|
|
13
17
|
const defaults = {
|
|
14
18
|
AwsClient: STS,
|
|
15
19
|
awsClientOptions: {},
|
|
16
20
|
// awsClientAssumeRole: undefined, // Not Applicable, as this is the middleware that defines the roles
|
|
17
21
|
awsClientCapture: undefined,
|
|
18
|
-
fetchData: {},
|
|
22
|
+
fetchData: {},
|
|
23
|
+
// { contextKey: {RoleArn, RoleSessionName} }
|
|
19
24
|
disablePrefetch: false,
|
|
20
25
|
cacheKey: 'sts',
|
|
21
26
|
cacheExpiry: -1,
|
|
22
27
|
// setToEnv: false, // returns object, cannot set to process.env
|
|
23
28
|
setToContext: false
|
|
24
|
-
}
|
|
29
|
+
};
|
|
25
30
|
|
|
26
31
|
const stsMiddleware = (opts = {}) => {
|
|
27
|
-
const options = { ...defaults,
|
|
32
|
+
const options = { ...defaults,
|
|
33
|
+
...opts
|
|
34
|
+
};
|
|
28
35
|
|
|
29
36
|
const fetch = (request, cachedValues = {}) => {
|
|
30
|
-
const values = {}
|
|
37
|
+
const values = {};
|
|
31
38
|
|
|
32
39
|
for (const internalKey of Object.keys(options.fetchData)) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
assumeRoleOptions.
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
values[internalKey] = client
|
|
40
|
-
|
|
41
|
-
.
|
|
42
|
-
.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
})
|
|
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
|
+
});
|
|
53
59
|
}
|
|
54
60
|
|
|
55
|
-
return values
|
|
56
|
-
}
|
|
61
|
+
return values;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
let prefetch, client;
|
|
57
65
|
|
|
58
|
-
let prefetch, client
|
|
59
66
|
if (canPrefetch(options)) {
|
|
60
|
-
client = createPrefetchClient(options)
|
|
61
|
-
prefetch = processCache(options, fetch)
|
|
67
|
+
client = createPrefetchClient(options);
|
|
68
|
+
prefetch = processCache(options, fetch);
|
|
62
69
|
}
|
|
63
70
|
|
|
64
|
-
const stsMiddlewareBefore = async
|
|
71
|
+
const stsMiddlewareBefore = async request => {
|
|
72
|
+
var _prefetch;
|
|
73
|
+
|
|
65
74
|
if (!client) {
|
|
66
|
-
client = await createClient(options, request)
|
|
75
|
+
client = await createClient(options, request);
|
|
67
76
|
}
|
|
68
77
|
|
|
69
|
-
const {
|
|
70
|
-
|
|
71
|
-
|
|
78
|
+
const {
|
|
79
|
+
value
|
|
80
|
+
} = (_prefetch = prefetch) !== null && _prefetch !== void 0 ? _prefetch : processCache(options, fetch, request);
|
|
81
|
+
Object.assign(request.internal, value);
|
|
72
82
|
|
|
73
83
|
if (options.setToContext) {
|
|
74
|
-
const data = await getInternal(Object.keys(options.fetchData), request)
|
|
75
|
-
if (options.setToContext) Object.assign(request.context, data)
|
|
84
|
+
const data = await getInternal(Object.keys(options.fetchData), request);
|
|
85
|
+
if (options.setToContext) Object.assign(request.context, data);
|
|
76
86
|
}
|
|
77
|
-
|
|
78
|
-
|
|
87
|
+
|
|
88
|
+
prefetch = null;
|
|
89
|
+
};
|
|
79
90
|
|
|
80
91
|
return {
|
|
81
92
|
before: stsMiddlewareBefore
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
module.exports = stsMiddleware;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/sts",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.7",
|
|
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.
|
|
49
|
+
"@middy/util": "^2.5.7"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@middy/core": "^2.5.
|
|
52
|
+
"@middy/core": "^2.5.7",
|
|
53
53
|
"aws-sdk": "^2.939.0",
|
|
54
54
|
"aws-xray-sdk": "^3.3.3"
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "3983c4b138e1a4d7fcb3ed805d3b8832fff06fc1"
|
|
57
57
|
}
|