@middy/sts 4.6.5 → 5.0.0-alpha.1
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.d.ts +34 -17
- package/index.js +77 -58
- package/package.json +5 -11
- package/index.cjs +0 -71
package/index.d.ts
CHANGED
|
@@ -1,35 +1,52 @@
|
|
|
1
1
|
import middy from '@middy/core'
|
|
2
2
|
import { Options as MiddyOptions } from '@middy/util'
|
|
3
3
|
import { Context as LambdaContext } from 'aws-lambda'
|
|
4
|
-
import { STSClient, STSClientConfig } from '@aws-sdk/client-sts'
|
|
4
|
+
import { AssumeRoleCommandInput, STSClient, STSClientConfig } from '@aws-sdk/client-sts'
|
|
5
5
|
|
|
6
|
-
interface
|
|
6
|
+
export interface AssumedRoleCredentials {
|
|
7
|
+
accessKeyId: string
|
|
8
|
+
secretAccessKey: string
|
|
9
|
+
sessionToken: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type AssumeRoleCommandInputWithOptionalRoleSessionName = Omit<
|
|
13
|
+
AssumeRoleCommandInput, 'RoleSessionName'
|
|
14
|
+
> & { RoleSessionName?: string | undefined }
|
|
15
|
+
|
|
16
|
+
interface STSOptions<AwsSTSClient = STSClient>
|
|
7
17
|
extends Pick<
|
|
8
18
|
MiddyOptions<AwsSTSClient, STSClientConfig>,
|
|
9
19
|
| 'AwsClient'
|
|
10
20
|
| 'awsClientOptions'
|
|
11
21
|
| 'awsClientCapture'
|
|
12
|
-
| 'fetchData'
|
|
13
22
|
| 'disablePrefetch'
|
|
14
23
|
| 'cacheKey'
|
|
15
24
|
| 'cacheExpiry'
|
|
16
25
|
| 'setToContext'
|
|
17
|
-
> {
|
|
18
|
-
|
|
19
|
-
export type Context<TOptions extends Options | undefined> = TOptions extends {
|
|
20
|
-
setToContext: true
|
|
26
|
+
> {
|
|
27
|
+
fetchData?: { [key: string]: AssumeRoleCommandInputWithOptionalRoleSessionName }
|
|
21
28
|
}
|
|
22
|
-
? LambdaContext &
|
|
23
|
-
Record<
|
|
24
|
-
keyof TOptions['fetchData'],
|
|
25
|
-
{
|
|
26
|
-
credentials: STSClientConfig['credentials']
|
|
27
|
-
}
|
|
28
|
-
>
|
|
29
|
-
: LambdaContext
|
|
30
29
|
|
|
31
|
-
|
|
30
|
+
export type Context<TOptions extends STSOptions | undefined> =
|
|
31
|
+
TOptions extends { setToContext: true }
|
|
32
|
+
? TOptions extends { fetchData: infer TFetchData }
|
|
33
|
+
? LambdaContext & {
|
|
34
|
+
[Key in keyof TFetchData]: AssumedRoleCredentials
|
|
35
|
+
}
|
|
36
|
+
: never
|
|
37
|
+
: LambdaContext
|
|
38
|
+
|
|
39
|
+
export type Internal<TOptions extends STSOptions | undefined> =
|
|
40
|
+
TOptions extends STSOptions
|
|
41
|
+
? TOptions extends { fetchData: infer TFetchData }
|
|
42
|
+
? {
|
|
43
|
+
[Key in keyof TFetchData]: AssumedRoleCredentials
|
|
44
|
+
}
|
|
45
|
+
: {}
|
|
46
|
+
: {}
|
|
47
|
+
|
|
48
|
+
declare function sts<TOptions extends STSOptions | undefined> (
|
|
32
49
|
options?: TOptions
|
|
33
|
-
): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>>
|
|
50
|
+
): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>, Internal<TOptions>>
|
|
34
51
|
|
|
35
52
|
export default sts
|
package/index.js
CHANGED
|
@@ -1,61 +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
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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)
|
|
44
73
|
}
|
|
45
|
-
|
|
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;
|
|
74
|
+
}
|
|
61
75
|
|
|
76
|
+
return {
|
|
77
|
+
before: stsMiddlewareBefore
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
export default stsMiddleware
|
package/package.json
CHANGED
|
@@ -1,33 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/sts",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0-alpha.1",
|
|
4
4
|
"description": "STS (Security Token Service) credentials middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=18"
|
|
8
8
|
},
|
|
9
9
|
"engineStrict": true,
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"main": "./index.cjs",
|
|
14
13
|
"module": "./index.js",
|
|
15
14
|
"exports": {
|
|
16
15
|
".": {
|
|
17
16
|
"import": {
|
|
18
17
|
"types": "./index.d.ts",
|
|
19
18
|
"default": "./index.js"
|
|
20
|
-
},
|
|
21
|
-
"require": {
|
|
22
|
-
"types": "./index.d.ts",
|
|
23
|
-
"default": "./index.cjs"
|
|
24
19
|
}
|
|
25
20
|
}
|
|
26
21
|
},
|
|
27
22
|
"types": "index.d.ts",
|
|
28
23
|
"files": [
|
|
29
24
|
"index.js",
|
|
30
|
-
"index.cjs",
|
|
31
25
|
"index.d.ts"
|
|
32
26
|
],
|
|
33
27
|
"scripts": {
|
|
@@ -66,13 +60,13 @@
|
|
|
66
60
|
"url": "https://github.com/sponsors/willfarrell"
|
|
67
61
|
},
|
|
68
62
|
"dependencies": {
|
|
69
|
-
"@middy/util": "
|
|
63
|
+
"@middy/util": "5.0.0-alpha.1"
|
|
70
64
|
},
|
|
71
65
|
"devDependencies": {
|
|
72
66
|
"@aws-sdk/client-sts": "^3.0.0",
|
|
73
|
-
"@middy/core": "
|
|
67
|
+
"@middy/core": "5.0.0-alpha.1",
|
|
74
68
|
"@types/aws-lambda": "^8.10.101",
|
|
75
69
|
"aws-xray-sdk": "^3.3.3"
|
|
76
70
|
},
|
|
77
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
|
|
78
72
|
}
|
package/index.cjs
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(module, "exports", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return _default;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _util = require("@middy/util");
|
|
12
|
-
const _clientsts = require("@aws-sdk/client-sts");
|
|
13
|
-
const defaults = {
|
|
14
|
-
AwsClient: _clientsts.STSClient,
|
|
15
|
-
awsClientOptions: {},
|
|
16
|
-
// awsClientAssumeRole: undefined, // Not Applicable, as this is the middleware that defines the roles
|
|
17
|
-
awsClientCapture: undefined,
|
|
18
|
-
fetchData: {},
|
|
19
|
-
disablePrefetch: false,
|
|
20
|
-
cacheKey: 'sts',
|
|
21
|
-
cacheKeyExpiry: {},
|
|
22
|
-
cacheExpiry: -1,
|
|
23
|
-
setToContext: false
|
|
24
|
-
};
|
|
25
|
-
const stsMiddleware = (opts = {})=>{
|
|
26
|
-
const options = {
|
|
27
|
-
...defaults,
|
|
28
|
-
...opts
|
|
29
|
-
};
|
|
30
|
-
const fetch = (request, cachedValues = {})=>{
|
|
31
|
-
const values = {};
|
|
32
|
-
for (const internalKey of Object.keys(options.fetchData)){
|
|
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 ??= 'middy-sts-session-' + Math.ceil(Math.random() * 99999);
|
|
37
|
-
values[internalKey] = client.send(new _clientsts.AssumeRoleCommand(assumeRoleOptions)).then((resp)=>({
|
|
38
|
-
accessKeyId: resp.Credentials.AccessKeyId,
|
|
39
|
-
secretAccessKey: resp.Credentials.SecretAccessKey,
|
|
40
|
-
sessionToken: resp.Credentials.SessionToken
|
|
41
|
-
})).catch((e)=>{
|
|
42
|
-
const value = (0, _util.getCache)(options.cacheKey).value ?? {};
|
|
43
|
-
value[internalKey] = undefined;
|
|
44
|
-
(0, _util.modifyCache)(options.cacheKey, value);
|
|
45
|
-
throw e;
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
return values;
|
|
49
|
-
};
|
|
50
|
-
let client;
|
|
51
|
-
if ((0, _util.canPrefetch)(options)) {
|
|
52
|
-
client = (0, _util.createPrefetchClient)(options);
|
|
53
|
-
(0, _util.processCache)(options, fetch);
|
|
54
|
-
}
|
|
55
|
-
const stsMiddlewareBefore = async (request)=>{
|
|
56
|
-
if (!client) {
|
|
57
|
-
client = await (0, _util.createClient)(options, request);
|
|
58
|
-
}
|
|
59
|
-
const { value } = (0, _util.processCache)(options, fetch, request);
|
|
60
|
-
Object.assign(request.internal, value);
|
|
61
|
-
if (options.setToContext) {
|
|
62
|
-
const data = await (0, _util.getInternal)(Object.keys(options.fetchData), request);
|
|
63
|
-
if (options.setToContext) Object.assign(request.context, data);
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
return {
|
|
67
|
-
before: stsMiddlewareBefore
|
|
68
|
-
};
|
|
69
|
-
};
|
|
70
|
-
const _default = stsMiddleware;
|
|
71
|
-
|