Coder Social home page Coder Social logo

awslabs / aws-sdk-v3-js-proxy Goto Github PK

View Code? Open in Web Editor NEW
17.0 6.0 3.0 1.57 MB

A wrapper for adding proxy configurations to AWS SDK v3 clients.

License: Apache License 2.0

Shell 1.55% TypeScript 98.45%
aws-sdk-v3 aws-sdk proxy http-proxy https-proxy aws-proxy hacktoberfest

aws-sdk-v3-js-proxy's Introduction

AWS SDK v3 Proxy

npm package Build Status Downloads Issues Code Coverage Commitizen Friendly Semantic Release

A wrapper for adding proxy support to AWS SDK v3 clients

This wrapper adds a proxy configuration to AWS SDK clients by checking environment variables and attaching the necessary request handler. By default, an error will be thrown if no proxy is found in process.env, but also has options to not throw which can be useful when developing other node utilities using this library.

Note: http_proxy and HTTP_PROXY take precedence over https_proxy and HTTPS_PROXY. If you would like to change this behavior it can be specified in the options.

Install

npm install aws-sdk-v3-proxy

Usage

HTTP Proxy

// process.env.HTTP_PROXY = 'http://127.0.0.1'
import { S3Client } from '@aws-sdk/client-s3';
import { addProxyToClient } from 'aws-sdk-v3-proxy';

const client = addProxyToClient(new S3Client({}));
// `client` now has HTTP proxy config at 'http://127.0.0.1'

or

import { S3Client } from '@aws-sdk/client-s3';
import { addProxyToClient } from 'aws-sdk-v3-proxy';

const client = addProxyToClient(new S3Client({}), {
  httpProxy: 'http://127.0.0.1',
});
// `client` now has HTTP proxy config at 'http://127.0.0.1'

HTTPS Proxy

// process.env.HTTPS_PROXY = 'https://127.0.0.1'
import { S3Client } from '@aws-sdk/client-s3';
import { addProxyToClient } from 'aws-sdk-v3-proxy';

const client = addProxyToClient(new S3Client({}));
// `client` now has HTTPS proxy config at 'https://127.0.0.1'

or

import { S3Client } from '@aws-sdk/client-s3';
import { addProxyToClient } from 'aws-sdk-v3-proxy';

const client = addProxyToClient(new S3Client({}), {
  httpsProxy: 'https://127.0.0.1',
});
// `client` now has HTTPS proxy config at 'https://127.0.0.1'

No Proxy with exception disabled

// process.env.HTTPS_PROXY = undefined
// process.env.HTTP_PROXY = undefined
import { S3Client } from '@aws-sdk/client-s3';
import { addProxyToClient } from 'aws-sdk-v3-proxy';

const client = addProxyToClient(new S3Client({}), { throwOnNoProxy: false });
// `client` has no proxy assigned and no error thrown

Proxy with certs in header

// process.env.HTTPS_PROXY = 'https://127.0.0.1'
import { readFileSync } from 'fs';
import { S3Client } from '@aws-sdk/client-s3';
import { addProxyToClient } from 'aws-sdk-v3-proxy';

const client = addProxyToClient(new S3Client({}), {
  agentOptions: {
    ca: [readFileSync('custom-proxy-cert.pem').toString()],
  },
});

// `client` now has HTTPS proxy config at 'https://127.0.0.1' with ca `custom-proxy-cert.pem`

API

addProxyToClient(client, options?)

client

Type: Object

Any client from @aws-sdk.

options

Type: object

throwOnNoProxy

Type: boolean Default: true

Throw an error if no proxy is found in the environment.

httpsOnly

Type: boolean Default: false

Can be specified in cases where you have both http_proxy and https_proxy set, and would like to override the default behavior of the http_proxy taking precedence.

debug

Type: boolean Default: false

Toggles additional logging for debugging.

httpProxy

Type: string

The URL for the HTTP proxy server. If not specified, the value of process.env.http_proxy or process.env.HTTP_RPOXY will be used.

httpsProxy

The URL for the HTTPS proxy server. If not specified, the value of process.env.https_proxy or process.env.HTTPS_RPOXY will be used.

agentOptions

Type: HttpsProxyAgentOptions

Used to pass specific options to the proxy agent.

Security

See CONTRIBUTING for more information.

License

This project is licensed under the Apache-2.0 License.

aws-sdk-v3-js-proxy's People

Contributors

dependabot[bot] avatar fossamagna avatar ryansonshine avatar smguggen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

aws-sdk-v3-js-proxy's Issues

Either httpAgent or httpsAgent are set, but not both

Current Behavior

According to the code, either httpAgent or httpsAgent are set, but never both.
However, aws-sdk v3 documentation clearly states:
"httpAgent is not the same as httpsAgent, and since most calls from the client will be to https, both should be set."
https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/node-configuring-proxies.html

Expected Behavior

If both http and https are provided, both should be passed on to the client.

Steps to Reproduce the Problem

Try use this package with AmazonConnect, which requires both.

Environment

  • Version: 0.0.0-development
  • Platform: Linux
  • Node.js Version: 18

Client network socket disconnected before secure TLS connection was established

Current Behavior

Setting https proxy to https://172.20.7.148:9090
recoverIdToken error: TimeoutError: Client network socket disconnected before secure TLS connection was established

Works without "addProxyToClient".

Expected Behavior

Traffic is proxied through Proxyman.

Steps to Reproduce the Problem

import { addProxyToClient } from 'aws-sdk-v3-proxy';

...

const cognitoIdpCient: CognitoIdentityProviderClient = addProxyToClient(
  new CognitoIdentityProviderClient({
    region: REGION,
  }),
  {
    debug: true,
  }
);

...

const forgotPasswordCommand: ForgotPasswordCommand = new ForgotPasswordCommand({
      ClientId: CLIENT_ID,
      Username: PHONE,
});
forgotPasswordResponse = await cognitoIdpCient.send(forgotPasswordCommand);

Environment

  • Version: 2.0.14
  • Platform: Mac/Linux
  • Node.js Version: v18.16.0

The automated release is failing 🚨

🚨 The automated release from the main branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the main branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here are some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


No npm token specified.

An npm token must be created and set in the NPM_TOKEN environment variable on your CI environment.

Please make sure to create an npm token and to set it in the NPM_TOKEN environment variable on your CI environment. The token must allow to publish to the registry https://registry.npmjs.org/.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

It doesn't work

Current Behavior

Bellow is a simple proxy server that receives requests from AWS SDK V3 Clients and an example using aws-sdk-v3-proxy configured to work with the proxy server.

// example.ts

import { S3Client, ListBucketsCommand } from "@aws-sdk/client-s3";
import { addProxyToClient } from 'aws-sdk-v3-proxy';

console.log(`HTTP_PROXY: ${process.env.HTTP_PROXY}`);
console.log(`HTTPS_PROXY: ${process.env.HTTPS_PROXY}`);

const s3 = addProxyToClient(new S3Client({
  region: "us-east-2",
}));

s3.middlewareStack.add(
  (next, context) => async (args: any) => {
    args.request.headers["x-my-account-id"] =
      "9b8c209d-73c9-4d97-9923-e256de630f39";
    console.log(args);
    const result = await next(args);
    // result.response contains data returned from next middleware.
    return result;
  },
  {
    step: "build",
  }
);

(async function main() {
  try {
    const response = await s3.send(new ListBucketsCommand());
    console.log(response);
  } catch (err) {
    console.log(err);
  }
})();

//proxy.ts

import express, { Request, Response } from 'express';

const app = express();
const port = 3000;

app.use(express.json());

app.all('*', async (req: Request, res: Response) => {
    try {
        console.log(req);
        const accountId = req.headers["x-my-account-id"];
        console.log(accountId);
        res.status(200).json({ message: "works" });
    } catch (error) {
        console.error("unknown error");
        res.status(500).json({ error: "unknown error" });
    }
});

app.listen(port, () => {
    console.log(`Proxy server listening at http://localhost:${port}`);
});

Expected Behavior

  • proxy server should receive requests

Steps to Reproduce the Problem

  1. start the proxy at http://localhost:3000
  2. export HTTP_PROXY=http://localhost:3000
  3. export HTTPS_PROXY=http://localhost:3000
  4. run the example code
  5. verify that you get this error message.
image
  1. stop the proxy
  2. export HTTPS_PROXY=https://localhost:300
  3. start the proxy at http://localhost:3000
  4. verify you ge the following error message
image

I believe that the sdk client is trying to send the request through the proxy but the proxy isn't accepting and there is no clue why! I also tried with fastify and had no lucky. I need help

Environment

  • Version: "aws-sdk-v3-proxy": "^2.1.4",
{
  "name": "aws-sdk-proxy-example",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "ts-node index.ts",
    "proxy": "ts-node proxy.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@aws-crypto/sha256-browser": "^5.2.0",
    "@aws-sdk/client-s3": "^3.600.0",
    "@aws-sdk/client-sts": "^3.600.0",
    "@aws-sdk/middleware-stack": "^3.374.0",
    "@aws-sdk/node-http-handler": "^3.374.0",
    "@aws-sdk/protocol-http": "^3.374.0",
    "@aws-sdk/signature-v4": "^3.374.0",
    "aws-sdk-v3-proxy": "^2.1.4",
    "axios": "^1.7.2",
    "express": "^4.19.2",
    "hpagent": "^1.2.0",
    "https-proxy-agent": "^7.0.4",
    "proxy-agent": "^6.4.0"
  },
  "devDependencies": {
    "@types/express": "^4.17.21",
    "@types/node": "^20.14.6",
    "nodemon": "^3.1.3",
    "ts-node": "^10.9.2",
    "typescript": "^5.4.5"
  }
}
  • Platform: mac
  • Node.js Version: 18.19.1

Deprecated dependencies warnings (move to Smithy)

Not a bug neither a feature request.

Current Behavior

$ npm install aws-sdk-v3-proxy

npm WARN deprecated @aws-sdk/[email protected]: This package has moved to @smithy/node-http-handler
npm WARN deprecated @aws-sdk/[email protected]: This package has moved to @smithy/protocol-http

Expected Behavior

No warnings about deprecated packages.

Steps to Reproduce the Problem

  1. npm install aws-sdk-v3-proxy
  2. Observe the warnings.

Environment

  • Version: 2.1.2
  • Platform: Linux
  • Node.js Version: v20.12.2

add `httpProxy` and `httpsProxy` in the options of the addProxyToClient function

Feature Request

Is your feature request related to a problem? Please describe.

I would like to use aws-sdk-v3-js-proxy in my VSCode extension implementation. I would like to use the HTTP Proxy set in VSCode's Configuration, not an environment variable. And I would like to be able to specify it as httpProxy, httpsProxy in the options of the addProxyToClient function.

Describe the solution you'd like

import { S3Client } from '@aws-sdk/client-s3';
import { addProxyToClient } from 'aws-sdk-v3-proxy';

const client = addProxyToClient(new S3Client({}), { 
  httpProxy: "http://127.0.0.1",
  httpsProxy: "https://127.0.0.1"
});

If these options are not specified, the values are still obtained from environment variables.

Describe alternatives you've considered

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don't know how to start. I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.