无法设置到私有 AWS API 网关 API 的 SSH 隧道
Failing to set up SSH tunnel to private AWS API gateway API
我有一个私有 AWS API 网关 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>
这个命令 returns 很好,但是当我尝试执行 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,否则我会更喜欢这种类型的东西。
API 网关需要主机 header 匹配 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 代理并通过堡垒代理您的请求。
中一shellsession启动代理
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>
本来可以对所选答案发表评论,但我没有足够的声誉,所以这对我有用。
通过跳转服务器转发端口:
ssh -o ExitOnForwardFailure=yes -f -N \
-L 127.0.0.1:<PORT>:<api-id>.execute-api.<region>.amazonaws.com:443 -p <PORT> <user>@<ip_address>
使用 -k 将主机添加到 curl
curl -H "Host: <api-id>.execute-api.<region>.amazonaws.com" https://127.0.0.1:<PORT>/<my_endpoint> -k
我有一个私有 AWS API 网关 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>
这个命令 returns 很好,但是当我尝试执行 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,否则我会更喜欢这种类型的东西。
API 网关需要主机 header 匹配 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 代理并通过堡垒代理您的请求。
中一shellsession启动代理
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>
本来可以对所选答案发表评论,但我没有足够的声誉,所以这对我有用。
通过跳转服务器转发端口:
ssh -o ExitOnForwardFailure=yes -f -N \
-L 127.0.0.1:<PORT>:<api-id>.execute-api.<region>.amazonaws.com:443 -p <PORT> <user>@<ip_address>
使用 -k 将主机添加到 curl
curl -H "Host: <api-id>.execute-api.<region>.amazonaws.com" https://127.0.0.1:<PORT>/<my_endpoint> -k