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