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