Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It doesn't work #32

Open
AllanOricil opened this issue Jun 20, 2024 · 1 comment
Open

It doesn't work #32

AllanOricil opened this issue Jun 20, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@AllanOricil
Copy link

AllanOricil commented Jun 20, 2024

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
@AllanOricil AllanOricil added the bug Something isn't working label Jun 20, 2024
@AllanOricil
Copy link
Author

I also console.log req.originalUrl and req.haders and discovered they were changed. Why? I just want to forward the request. My plan is to add some credentials to it before forwarding the request to AWS.

image

//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('Original URL:', req.originalUrl);
        console.log('Headers:', req.headers);
        res.status(200).json({});
    } 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}`);
});
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant