@middy/service-discovery 2.5.5

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-2021 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # Middy sts middleware
2
+
3
+ <div align="center">
4
+ <img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.png"/>
5
+ </div>
6
+
7
+ <div align="center">
8
+ <p><strong>STS middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
9
+ </div>
10
+
11
+ <div align="center">
12
+ <p>
13
+ <a href="http://badge.fury.io/js/%40middy%2Fsts">
14
+ <img src="https://badge.fury.io/js/%40middy%2Fsts.svg" alt="npm version" style="max-width:100%;">
15
+ </a>
16
+ <a href="https://snyk.io/test/github/middyjs/middy">
17
+ <img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
18
+ </a>
19
+ <a href="https://standardjs.com/">
20
+ <img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
21
+ </a>
22
+ <a href="https://gitter.im/middyjs/Lobby">
23
+ <img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
24
+ </a>
25
+ </p>
26
+ </div>
27
+
28
+ Fetches STS credentials to be used when connecting to other AWS services.
29
+
30
+ ## Install
31
+
32
+ To install this middleware you can use NPM:
33
+
34
+ ```bash
35
+ npm install --save @middy/sts
36
+ ```
37
+
38
+
39
+ ## Options
40
+
41
+ - `AwsClient` (object) (default `AWS.STS`): AWS.STS class constructor (e.g. that has been instrumented with AWS XRay). Must be from `aws-sdk` v2.
42
+ - `awsClientOptions` (object) (optional): Options to pass to AWS.STS class constructor.
43
+ - `awsClientCapture` (function) (optional): Enable XRay by passing `captureAWSClient` from `aws-xray-sdk` in.
44
+ - `fetchData` (object) (required): Mapping of internal key name to API request parameters.
45
+ - `disablePrefetch` (boolean) (default `false`): On cold start requests will trigger early if they can. Setting `awsClientAssumeRole` disables prefetch.
46
+ - `cacheKey` (string) (default `sts`): Cache key for the fetched data responses. Must be unique across all middleware.
47
+ - `cacheExpiry` (number) (default `-1`): How long fetch data responses should be cached for. `-1`: cache forever, `0`: never cache, `n`: cache for n ms.
48
+ - `setToContext` (boolean) (default `false`): Store credentials to `request.context`.
49
+
50
+ NOTES:
51
+ - Lambda is required to have IAM permission for `sts:AssumeRole`
52
+ - `setToContext` are included for legacy support and should be avoided for performance and security reasons. See main documentation for best practices.
53
+
54
+ ## Sample usage
55
+
56
+ ```javascript
57
+ import middy from '@middy/core'
58
+ import sts from '@middy/sts'
59
+
60
+ const handler = middy((event, context) => {
61
+ const response = {
62
+ statusCode: 200,
63
+ headers: {},
64
+ body: JSON.stringify({ message: 'hello world' })
65
+ };
66
+
67
+ return response
68
+ })
69
+
70
+ handler
71
+ .use(sts({
72
+ fetchData: {
73
+ assumeRole: {
74
+ RoleArn: '...',
75
+ RoleSessionName:'' // optional
76
+ }
77
+ }
78
+ }))
79
+ ```
80
+
81
+
82
+ ## Middy documentation and examples
83
+
84
+ For more documentation and examples, refers to the main [Middy monorepo on GitHub](https://github.com/middyjs/middy) or [Middy official website](https://middy.js.org).
85
+
86
+
87
+ ## Contributing
88
+
89
+ Everyone is very welcome to contribute to this repository. Feel free to [raise issues](https://github.com/middyjs/middy/issues) or to [submit Pull Requests](https://github.com/middyjs/middy/pulls).
90
+
91
+
92
+ ## License
93
+
94
+ Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
95
+
96
+ <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
97
+ <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
98
+ </a>
package/index.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import { STS } from 'aws-sdk'
2
+ import middy from '@middy/core'
3
+ import { Options as MiddyOptions } from '@middy/util'
4
+
5
+ interface Options<S = STS>
6
+ extends Pick<MiddyOptions<S, STS.Types.ClientConfiguration>,
7
+ 'AwsClient' | 'awsClientOptions' | 'awsClientCapture' |
8
+ 'fetchData' | 'disablePrefetch' | 'cacheKey' | 'cacheExpiry' | 'setToContext'> {
9
+ }
10
+
11
+ declare function sts (options?: Options): middy.MiddlewareObj
12
+
13
+ export default sts
package/index.js ADDED
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _util = require("@middy/util");
9
+
10
+ var _sts = _interopRequireDefault(require("aws-sdk/clients/sts.js"));
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ // v2
15
+ // import { STS } from '@aws-sdk/client-sts' // v3
16
+ const defaults = {
17
+ AwsClient: _sts.default,
18
+ awsClientOptions: {},
19
+ // awsClientAssumeRole: undefined, // Not Applicable, as this is the middleware that defines the roles
20
+ awsClientCapture: undefined,
21
+ fetchData: {},
22
+ // { contextKey: {RoleArn, RoleSessionName} }
23
+ disablePrefetch: false,
24
+ cacheKey: 'sts',
25
+ cacheExpiry: -1,
26
+ setToContext: false
27
+ };
28
+
29
+ const stsMiddleware = (opts = {}) => {
30
+ const options = { ...defaults,
31
+ ...opts
32
+ };
33
+
34
+ const fetch = (request, cachedValues = {}) => {
35
+ const values = {};
36
+
37
+ for (const internalKey of Object.keys(options.fetchData)) {
38
+ var _assumeRoleOptions$Ro;
39
+
40
+ if (cachedValues[internalKey]) continue;
41
+ const assumeRoleOptions = options.fetchData[internalKey]; // Date cannot be used here to assign default session name, possibility of collision when > 1 role defined
42
+
43
+ (_assumeRoleOptions$Ro = assumeRoleOptions.RoleSessionName) !== null && _assumeRoleOptions$Ro !== void 0 ? _assumeRoleOptions$Ro : assumeRoleOptions.RoleSessionName = 'middy-sts-session-' + Math.ceil(Math.random() * 99999);
44
+ values[internalKey] = client.assumeRole(assumeRoleOptions).promise() // Required for aws-sdk v2
45
+ .then(resp => ({
46
+ accessKeyId: resp.Credentials.AccessKeyId,
47
+ secretAccessKey: resp.Credentials.SecretAccessKey,
48
+ sessionToken: resp.Credentials.SessionToken
49
+ })).catch(e => {
50
+ var _getCache$value;
51
+
52
+ const value = (_getCache$value = (0, _util.getCache)(options.cacheKey).value) !== null && _getCache$value !== void 0 ? _getCache$value : {};
53
+ value[internalKey] = undefined;
54
+ (0, _util.modifyCache)(options.cacheKey, value);
55
+ throw e;
56
+ });
57
+ }
58
+
59
+ return values;
60
+ };
61
+
62
+ let prefetch, client;
63
+
64
+ if ((0, _util.canPrefetch)(options)) {
65
+ client = (0, _util.createPrefetchClient)(options);
66
+ prefetch = (0, _util.processCache)(options, fetch);
67
+ }
68
+
69
+ const stsMiddlewareBefore = async request => {
70
+ var _prefetch;
71
+
72
+ if (!client) {
73
+ client = await (0, _util.createClient)(options, request);
74
+ }
75
+
76
+ const {
77
+ value
78
+ } = (_prefetch = prefetch) !== null && _prefetch !== void 0 ? _prefetch : (0, _util.processCache)(options, fetch, request);
79
+ Object.assign(request.internal, value);
80
+
81
+ if (options.setToContext) {
82
+ const data = await (0, _util.getInternal)(Object.keys(options.fetchData), request);
83
+ if (options.setToContext) Object.assign(request.context, data);
84
+ }
85
+
86
+ prefetch = null;
87
+ };
88
+
89
+ return {
90
+ before: stsMiddlewareBefore
91
+ };
92
+ };
93
+
94
+ var _default = stsMiddleware;
95
+ exports.default = _default;
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@middy/service-discovery",
3
+ "version": "2.5.5",
4
+ "description": "STS (Security Token Service) credentials middleware for the middy framework",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=14"
8
+ },
9
+ "engineStrict": true,
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "exports": "./index.js",
14
+ "types": "index.d.ts",
15
+ "files": [
16
+ "index.js",
17
+ "index.d.ts"
18
+ ],
19
+ "scripts": {
20
+ "test": "npm run test:unit",
21
+ "test:unit": "ava"
22
+ },
23
+ "license": "MIT",
24
+ "keywords": [
25
+ "Lambda",
26
+ "Middleware",
27
+ "Serverless",
28
+ "Framework",
29
+ "AWS",
30
+ "AWS Lambda",
31
+ "Middy",
32
+ "STS",
33
+ "Security Token Service",
34
+ "Credentials"
35
+ ],
36
+ "author": {
37
+ "name": "Middy contributors",
38
+ "url": "https://github.com/middyjs/middy/graphs/contributors"
39
+ },
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "github:middyjs/middy",
43
+ "directory": "packages/sts"
44
+ },
45
+ "bugs": {
46
+ "url": "https://github.com/middyjs/middy/issues"
47
+ },
48
+ "homepage": "https://github.com/middyjs/middy#readme",
49
+ "dependencies": {
50
+ "@middy/util": "^3.0.0-alpha.2"
51
+ },
52
+ "devDependencies": {
53
+ "@middy/core": "^3.0.0-alpha.2",
54
+ "aws-sdk": "^2.939.0",
55
+ "aws-xray-sdk": "^3.3.3"
56
+ },
57
+ "gitHead": "b84840ec8afd289f6decfd0d645be4899051792d"
58
+ }