首页 > 解决方案 > 无法设置到私有 AWS API 网关 API 的 SSH 隧道

问题描述

我有一个私有 AWS API Gateway REST API,这意味着它只能在我的 VPC 中访问。我有一个在 VPC 中运行的堡垒 SSH 实例,这意味着我可以执行以下操作:

ssh -J ec2-user@<bastion> ec2-user@<ip of EC2 instance within my VPC>

从那个实例我可以然后curl我的 API 使用https://<api-id>.execute-api.eu-west-1.amazonaws.com/dev/<my endpoint>URL。

现在,对于本地测试,我想将此实例隧道传输到本地端口,所以我尝试

ssh -J ec2-user@<bastion> -L 8888:<api-id>.execute-api.eu-west-1.amazonaws.com:443 ec2-user@<ip of EC2 instance within my VPC>

此命令返回正常,但是当我尝试执行时curl localhost:8888/dev/<my endpoint>,我首先收到证书错误,这是很自然的,但是当我尝试使用curl -k localhost:8888/dev/<my endpoint>忽略证书时,我只是从 AWS 收到 403 Forbidden 响应。对于这些请求,我的 REST API 访问日志中没有任何内容。

403 是否与我忽略 TLS 证书或其他原因有关?有可能建立这样的隧道吗?不幸的是,似乎不可能对 API 网关 REST API:s 使用纯 HTTP,否则我会更喜欢这种类型的东西。

标签: amazon-web-servicesaws-api-gatewayamazon-vpcssh-tunnel

解决方案


API Gateway 要求主机标头与 API 端点 URL 匹配。将主机名添加到您的/etc/hosts

<api-id>.execute-api.eu-west-1.amazonaws.com 127.0.0.1

并正常调用它curl https://<api-id>.execute-api.eu-west-1.amazonaws.com:8888/dev/<my endpoint>

或使用 curl 的--resolve标志

curl \
  --resolve <api-id>.execute-api.eu-west-1.amazonaws.com:443:localhost:8888 \
  localhost:8888/dev/<my endpoint>

或者,如果您的堡垒配置为允许,您可以将ssh其用作 SOCKS5 代理并通过堡垒代理您的请求。

在一个 shell 会话中启动代理

ssh -D 8888 ec2-user@<bastion>

然后在另一个shell中,使用它

export HTTPS_PROXY=socks5://localhost:8888
curl https://<api-id>.execute-api.eu-west-1.amazonaws.com/dev/<my endpoint>

推荐阅读