@middy/rds-signer 2.5.2

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,101 @@
1
+ # Middy rds-signer 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>RDS Signer 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%2Frds-signer">
14
+ <img src="https://badge.fury.io/js/%40middy%2Frds-signer.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 RDS credentials to be used when connecting to RDS with IAM roles.
29
+
30
+ ## Install
31
+
32
+ To install this middleware you can use NPM:
33
+
34
+ ```bash
35
+ npm install --save @middy/rds-signer
36
+ ```
37
+
38
+
39
+ ## Options
40
+
41
+ - `AwsClient` (object) (default `AWS.RDS.Signer`): AWS.RDS.Signer 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.RDS.Signer class constructor.
43
+ - `fetchData` (object) (required): Mapping of internal key name to API request parameters.
44
+ - `disablePrefetch` (boolean) (default `false`): On cold start requests will trigger early if they can. Setting `awsClientAssumeRole` disables prefetch.
45
+ - `cacheKey` (string) (default `rds-signer`): Cache key for the fetched data responses. Must be unique across all middleware.
46
+ - `cacheExpiry` (number) (default `-1`): How long fetch data responses should be cached for. `-1`: cache forever, `0`: never cache, `n`: cache for n ms.
47
+ - `setToEnv` (boolean) (default `false`): Store role tokens to `process.env`. **Storing secrets in `process.env` is considered security bad practice**
48
+ - `setToContext` (boolean) (default `false`): Store role tokens to `request.context`.
49
+
50
+ NOTES:
51
+ - Lambda is required to have IAM permission for `rds-db:connect` with a resource like `arn:aws:rds-db:#{AWS::Region}:#{AWS::AccountId}:dbuser:${database_resource}/${iam_role}`
52
+ - `setToEnv` and `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 rdsSigner from '@middy/rds-signer'
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(rdsSigner({
72
+ fetchData: {
73
+ rdsToken: {
74
+ region: 'ca-central-1',
75
+ hostname: '***.rds.amazonaws.com',
76
+ username: 'iam_role',
77
+ database: 'postgres',
78
+ port: 5432
79
+ }
80
+ }
81
+ }))
82
+ ```
83
+
84
+
85
+ ## Middy documentation and examples
86
+
87
+ 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).
88
+
89
+
90
+ ## Contributing
91
+
92
+ 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).
93
+
94
+
95
+ ## License
96
+
97
+ Licensed under [MIT License](LICENSE). Copyright (c) 2017-2021 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
98
+
99
+ <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
100
+ <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
101
+ </a>
package/index.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { RDS } from 'aws-sdk'
2
+ import { captureAWSClient } from 'aws-xray-sdk'
3
+ import middy from '@middy/core'
4
+
5
+ interface Options<Signer = RDS.Signer> {
6
+ AwsClient?: new() => Signer
7
+ awsClientOptions?: Partial<RDS.Types.ClientConfiguration>
8
+ awsClientAssumeRole?: string
9
+ awsClientCapture?: typeof captureAWSClient
10
+ fetchData?: { [key: string]: string }
11
+ disablePrefetch?: boolean
12
+ cacheKey?: string
13
+ cacheExpiry?: number
14
+ setToEnv?: boolean
15
+ setToContext?: boolean
16
+ }
17
+
18
+ declare function rdsSigner (options?: Options): middy.MiddlewareObj
19
+
20
+ export default rdsSigner
package/index.js ADDED
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+
3
+ const {
4
+ canPrefetch,
5
+ getInternal,
6
+ processCache,
7
+ getCache,
8
+ modifyCache
9
+ } = require('@middy/util');
10
+
11
+ const {
12
+ Signer
13
+ } = require('aws-sdk/clients/rds.js'); // v2
14
+ // const { RDS:{Signer} } = require('@aws-sdk/client-rds') // v3
15
+
16
+
17
+ const defaults = {
18
+ AwsClient: Signer,
19
+ awsClientOptions: {},
20
+ fetchData: {},
21
+ // { contextKey: {region, hostname, username, database, port} }
22
+ disablePrefetch: false,
23
+ cacheKey: 'rds-signer',
24
+ cacheExpiry: -1,
25
+ setToEnv: false,
26
+ setToContext: false
27
+ };
28
+
29
+ const rdsSignerMiddleware = (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
+ if (cachedValues[internalKey]) continue;
39
+ const client = new options.AwsClient({ ...options.awsClientOptions,
40
+ ...options.fetchData[internalKey]
41
+ }); // AWS doesn't support getAuthToken.promise() in aws-sdk v2 :( See https://github.com/aws/aws-sdk-js/issues/3595
42
+
43
+ values[internalKey] = new Promise((resolve, reject) => {
44
+ client.getAuthToken({}, (err, token) => {
45
+ if (err) {
46
+ reject(err);
47
+ } // Catch Missing token, this usually means their is something wrong with the credentials
48
+
49
+
50
+ if (!token.includes('X-Amz-Security-Token=')) {
51
+ reject(new Error('X-Amz-Security-Token Missing'));
52
+ }
53
+
54
+ resolve(token);
55
+ });
56
+ }).catch(e => {
57
+ var _getCache$value, _getCache;
58
+
59
+ const value = (_getCache$value = (_getCache = getCache(options.cacheKey)) === null || _getCache === void 0 ? void 0 : _getCache.value) !== null && _getCache$value !== void 0 ? _getCache$value : {};
60
+ value[internalKey] = undefined;
61
+ modifyCache(options.cacheKey, value);
62
+ throw e;
63
+ }); // aws-sdk v3
64
+ // values[internalKey] = createClient(awsClientOptions, request).then(client => client.getAuthToken())
65
+ }
66
+
67
+ return values;
68
+ };
69
+
70
+ let prefetch;
71
+
72
+ if (canPrefetch(options)) {
73
+ prefetch = processCache(options, fetch);
74
+ }
75
+
76
+ const rdsSignerMiddlewareBefore = async request => {
77
+ var _prefetch;
78
+
79
+ const {
80
+ value
81
+ } = (_prefetch = prefetch) !== null && _prefetch !== void 0 ? _prefetch : processCache(options, fetch, request);
82
+ Object.assign(request.internal, value);
83
+
84
+ if (options.setToContext || options.setToEnv) {
85
+ const data = await getInternal(Object.keys(options.fetchData), request);
86
+ if (options.setToEnv) Object.assign(process.env, data);
87
+ if (options.setToContext) Object.assign(request.context, data);
88
+ }
89
+
90
+ prefetch = null;
91
+ };
92
+
93
+ return {
94
+ before: rdsSignerMiddlewareBefore
95
+ };
96
+ };
97
+
98
+ module.exports = rdsSignerMiddleware;
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@middy/rds-signer",
3
+ "version": "2.5.2",
4
+ "description": "RDS (Relational Database Service) credentials middleware for the middy framework",
5
+ "type": "commonjs",
6
+ "engines": {
7
+ "node": ">=12"
8
+ },
9
+ "engineStrict": true,
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "main": "index.js",
14
+ "types": "index.d.ts",
15
+ "files": [
16
+ "index.d.ts"
17
+ ],
18
+ "scripts": {
19
+ "test": "npm run test:unit",
20
+ "test:unit": "ava"
21
+ },
22
+ "license": "MIT",
23
+ "keywords": [
24
+ "Lambda",
25
+ "Middleware",
26
+ "Serverless",
27
+ "Framework",
28
+ "AWS",
29
+ "AWS Lambda",
30
+ "Middy",
31
+ "RDS",
32
+ "Relational Database Service",
33
+ "Credentials"
34
+ ],
35
+ "author": {
36
+ "name": "Middy contributors",
37
+ "url": "https://github.com/middyjs/middy/graphs/contributors"
38
+ },
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "github:middyjs/middy",
42
+ "directory": "packages/rds-signer"
43
+ },
44
+ "bugs": {
45
+ "url": "https://github.com/middyjs/middy/issues"
46
+ },
47
+ "homepage": "https://github.com/middyjs/middy#readme",
48
+ "dependencies": {
49
+ "@middy/util": "^2.5.2"
50
+ },
51
+ "devDependencies": {
52
+ "@middy/core": "^2.5.2",
53
+ "@types/node": "^16.0.0",
54
+ "aws-sdk": "^2.939.0",
55
+ "aws-xray-sdk": "^3.3.3"
56
+ },
57
+ "gitHead": "a2bb757a7a13638ae64277f8eecfcf11c1af17d4"
58
+ }