diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..3f816aa --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +indent_style = tab +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.yml] +indent_style = space +indent_size = 2 \ No newline at end of file diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6313b56 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f06235c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +dist diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..9d7745e --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: + - '14' + - '12' + - '10' diff --git a/license b/license new file mode 100644 index 0000000..978b994 --- /dev/null +++ b/license @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) Sam Verschueren + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/package.json b/package.json new file mode 100644 index 0000000..9486173 --- /dev/null +++ b/package.json @@ -0,0 +1,51 @@ +{ + "name": "got4aws", + "version": "0.0.0", + "description": "Convenience wrapper for Got to interact with AWS v4 signed APIs", + "license": "MIT", + "repository": "SamVerschueren/got4aws", + "author": { + "name": "Sam Verschueren", + "email": "sam.verschueren@gmail.com", + "url": "https://github.com/SamVerschueren" + }, + "main": "dist/index.js", + "engines": { + "node": ">=10" + }, + "scripts": { + "test": "xo", + "compile": "tsc -p ." + }, + "files": [ + "dist" + ], + "keywords": [ + "got-plugin", + "got", + "aws", + "aws4", + "api", + "sigv4", + "request", + "http", + "https", + "get", + "url", + "utility" + ], + "peerDependencies": { + "aws-sdk": "^2.700.0" + }, + "dependencies": { + "aws4": "^1.10.0", + "got": "^11.3.0" + }, + "devDependencies": { + "@sindresorhus/tsconfig": "^0.7.0", + "@types/aws4": "^1.5.1", + "aws-sdk": "^2.700.0", + "typescript": "^3.9.5", + "xo": "^0.32.0" + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..4bc3dc5 --- /dev/null +++ b/readme.md @@ -0,0 +1,125 @@ +# aws4got [![Build Status](https://travis-ci.org/samverschueren/got4aws.svg?branch=master)](https://travis-ci.org/samverschueren/got4aws) + +> Convenience wrapper for [Got](https://github.com/sindresorhus/got) to interact with [AWS v4 signed](https://docs.aws.amazon.com/general/latest/gr/signing_aws_api_requests.html) APIs + +## Install + +``` +$ npm install got4aws +``` + +## Usage + +Instead of: + +```ts +import got from 'got'; +import * as aws4 from 'aws4'; +import * as url from 'url'; + +(async () => { + const {protocol, host, path} = url.parse('https://12abc34.execute-api.us-east-1.amazonaws.com/v0'); + + const request = { + protocol, + host, + path, + responseType: 'json' + }; + + aws4.sign(request); + + const {body} = await got.get(request); + + console.log(body); + //=> {status: 'ok'} +})(); +``` + +You can do: + +```ts +import got4aws from 'got4aws'; + +const got = got4aws(); + +(async () => { + const {body} = await got.get('https://12abc34.execute-api.us-east-1.amazonaws.com/v0', { + responseType: 'json' + }); + + console.log(body); + //=> {status: 'ok'} +})(); +``` + +If you want to load credentials from somewhere else, you can provide extra options to the factory function. + +```ts +import got4aws from 'got4aws'; + +// Load credentials from `~/.aws/credentials` +const got = got4aws({ + providers: new AWS.SharedIniFileCredentials({profile: 'myProfile'}) +}); + +(async () => { + const {body} = await got.get('https://12abc34.execute-api.us-east-1.amazonaws.com/v0', { + responseType: 'json' + }); + + console.log(body); + //=> {status: 'ok'} +})(); +``` + +If you want to invoke an API Gateway endpoint with a custom domain, you will have to set the `service` and `region` as well because they can't be inferred. + +```ts +import got4aws from 'got4aws'; + +const got = got4aws({ + providers: new AWS.SharedIniFileCredentials({profile: 'myProfile'}), + service: 'execute-api', + region: 'eu-west-1' +}); + +(async () => { + const {body} = await got.get('https://api.unicorn.com/v0', { + responseType: 'json' + }); + + console.log(body); + //=> {status: 'ok'} +})(); +``` + + +## API + +### got4aws(options?) + +Returns a [`Got`](https://github.com/sindresorhus/got) instance. + +#### options + +##### providers + +Type: [`Credentials | Credentials[]`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Credentials.html)
+Default: [`EnvironmentCredentials`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EnvironmentCredentials.html) + +One ore more credential providers that are passed through to the [CredentialsProviderChain](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CredentialProviderChain.html). These providers are used to infer the AWS credentials used for signing the requests. + +##### service + +Type: `string`
+Default: *inferred through URL* + +The name of the service to sign the request for. For example, when signing a request for API Gateway with a custom domain, this should be `execute-api`. + +##### region + +Type: `string`
+Default: *inferred through URL* + +The region of the service being invoked. If it could not be inferred through the URL, it will default to `us-east-1`. diff --git a/source/index.ts b/source/index.ts new file mode 100644 index 0000000..49aad25 --- /dev/null +++ b/source/index.ts @@ -0,0 +1,78 @@ +import got from 'got'; +import * as AWS from 'aws-sdk'; +import * as aws4 from 'aws4'; + +export interface GotAWSOptions { + /** + * A provider or a list of providers used to search for AWS credentials. If no providers are provided, + * it will use the [EnvironmentCredentials](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EnvironmentCredentials.html) provider. + * + * See the [CredentialProviderChain](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CredentialProviderChain.html) documentation for more information. + */ + providers?: AWS.Credentials | AWS.Credentials[]; + + /** + * The AWS service the request is being signed for. Will try to be inferred by the URL if not provided. + */ + service?: string; + + /** + * The AWS region the request is executed in. Will try to be inferred by the URL if not provided. + */ + region?: string; +} + +/** + * Create a Got instance which will automatically sign the requests with a [AWS Version 4 signature](https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html). + * + * @param awsOptions - A provider or a list of providers used to search for AWS credentials. + */ +const got4aws = (awsOptions: GotAWSOptions = {}) => { + let credentialProviders: AWS.Credentials[] | undefined; + + if (awsOptions.providers) { + credentialProviders = Array.isArray(awsOptions.providers) ? awsOptions.providers : [awsOptions.providers]; + } + + // Setup the credential provider chain to retrieve the signature credentials + const chain = new AWS.CredentialProviderChain(credentialProviders as any); + + return got.extend({ + hooks: { + beforeRequest: [ + async options => { + if ((options as any).isStream) { + // Don't touch streams + return; + } + + // Make sure the credentials are resolved + const credentials = await chain.resolvePromise(); + + const {url, headers} = options; + + // Map the request to something that is signable by aws4 + const request = { + protocol: url.protocol, + host: url.host, + path: url.pathname, + headers, + body: options.json ? JSON.stringify(options.json) : options.body, + service: awsOptions.service, + region: awsOptions.region + }; + + aws4.sign(request, credentials); + + options.headers = request.headers; + } + ] + } + }); +}; + +export default got4aws; + +// For CommonJS default export support +module.exports = got4aws; +module.exports.default = got4aws; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..4064763 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,15 @@ + +{ + "extends": "@sindresorhus/tsconfig", + "compilerOptions": { + "outDir": "dist", + "target": "es2018", // Node.js 10 + "lib": [ + "es2018", + "es2019.string" + ] + }, + "include": [ + "source" + ] +}