简体   繁体   English

使用 GCP Compute Engine 作为 Authenticated Cloud Run Service 的代理

[英]Use GCP Compute Engine as proxy for Authenticated Cloud Run Service

I currently have a dockerized web application hosted on a Google Cloud Compute Instance, which is only accessible from our companies private network.我目前有一个托管在 Google Cloud Compute Instance 上的 dockerized Web 应用程序,该应用程序只能从我们公司的专用网络访问。 This set up has worked well over the past year, but with additional development requirements and increased usage, I find myself constantly modifying the instances size, and having to restart the server with new updates.这种设置在过去一年中运行良好,但随着额外的开发要求和使用量的增加,我发现自己不断修改实例大小,并且不得不用新的更新重新启动服务器。 Also, other developers on the team have less experience with deploying this code which makes this app my responsibility.此外,团队中的其他开发人员在部署此代码方面的经验较少,这使我对这个应用程序负责。

I'd like to move this application to Cloud Run for scalability, ease of maintenance and deployments but still have it accessible only on our companies network.我想将此应用程序移至 Cloud Run 以实现可扩展性、易于维护和部署,但仍然只能在我们公司的网络上访问它。 My idea was to move the application to an authenticated cloud run service and use the original server as an nginx proxy that would add an authentication header and forward the request to the cloud run service.我的想法是将应用程序移动到经过身份验证的云运行服务,并将原始服务器用作 nginx 代理,该代理将添加身份验证标头并将请求转发到云运行服务。

My question would be how would I use nginx to get the token (the server will have the necessary permissions), and add it to the request before passing it to the app.我的问题是我将如何使用 nginx 获取令牌(服务器将具有必要的权限),并在将其传递给应用程序之前将其添加到请求中。 This is my current idea, but not sure where to go from here.这是我目前的想法,但不知道从哪里开始。

location / {
    proxy_set_header Authentication "Bearer $ID_TOKEN";
    proxy_pass https://the-library-clwysxi3sq-ue.a.run.app;
}

You're on the right track.你在正确的轨道上。

At this point, I recommend you to consider using Envoy Proxy instead of NGINX.此时,我建议您考虑使用 Envoy Proxy 而不是 NGINX。 Envoy has a well-documented protocol to fetch dynamic data such as $ID_TOKEN from an external source. Envoy 有一个有据可查的协议来从外部来源获取动态数据,例如 $ID_TOKEN。

Whatever solution you choose, make sure you actually end up rewriting the "Host" header to your [...].run.app hostname, because if you preserve the hostname as is ( somedomain.com ), Cloud Run's load balancer won't know which app to route it.无论您选择哪种解决方案,请确保您最终将“主机”标头重写为[...].run.app主机名,因为如果您按原样保留主机名 ( somedomain.com ),Cloud Run 的负载均衡器将不会不知道要路由哪个应用程序。

The remaining task is to figure out how to get the $ID_TOKEN dynamically.剩下的任务是弄清楚如何动态获取 $ID_TOKEN。

Google Compute VM instance needs to retrieve an identity token (JWT) by querying the instance metadata service: Google Compute VM 实例需要通过查询实例元数据服务来检索身份令牌 (JWT):

curl -H "Metadata-Flavor: Google" \
  http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=https://hello-2wvlk7vg3a-uc.a.run.app

Make sure to replace the value of ?audience= with the targeted service's URL.确保将?audience=的值替换为目标服务的 URL。

The response body of this call returns a JWT token that expires within an hour.此调用的响应正文返回一个在一小时内过期的 JWT 令牌。 You should cache this response (based on audience, and TTL<60 mins), or simply get a new one every time.您应该缓存此响应(基于受众,并且 TTL <60 分钟),或者每次都获得一个新响应。

Note that on Cloud Run, you can only generate 50 identity tokens per second currently.请注意,在 Cloud Run 上,您目前每秒只能生成50 个身份令牌 However you are running on GCE (and I'm repeating myself here ) I don't think there's a documented rate limit for metadata service on GCE.但是,您在 GCE 上运行( 我在这里重复一遍)我认为 GCE 上的元数据服务没有记录的速率限制。 It's likely higher .可能会更高

Then, you need to add it to the outgoing request (to Cloud Run) as an HTTP header:然后,您需要将其作为 HTTP 标头添加到传出请求(到 Cloud Run)中:

Authorization: Bearer <TOKEN>

This procedure is explained at Service-to-service authentication documentation.此过程在服务到服务身份验证文档中进行了说明。

You can search Stack Overflow or Google on how to execute a Lua or Bash script in NGINX for every request.您可以在 Stack Overflow 或 Google 上搜索如何在 NGINX 中为每个请求执行 Lua 或 Bash 脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法在GCP Compute Engine上访问我的Docker容器 - I can't access my Docker container on GCP Compute Engine 谷歌云计算引擎 http 连接超时 - Google Cloud Compute Engine http Connection Timeout 使用Google Compute Engine作为Google App Engine网络应用的代理 - Using Google Compute Engine as a proxy for a Google App Engine web app GCP Cloud Run 返回“忠实您的,nginx” - GCP Cloud Run returns "Faithfully yours, nginx" Cloud Run 中的 API 和 VM 中的 Nginx 反向代理 - APIs in Cloud Run and Nginx reverse proxy in VM Google Cloud Compute Engine上的Flask + NGINX + UWSGI提供了502错误的网关 - Flask+NGINX+UWSGI on Google Cloud Compute Engine gives 502 bad gateway 如何使用 Google Cloud Compute Engine 为 Node.JS 应用程序配置端口转发 - How to configure Port Forwarding with Google Cloud Compute Engine for a Node.JS application 错误:无法以非 root 用户身份放弃特权:容器在 Google Cloud Compute Engine 上不断重启 - Error: Can't drop privilege as nonroot user: container keeps restarting on Google Cloud Compute Engine Google Cloud Endpoint可扩展服务代理无法启动 - Google Cloud Endpoint Extensible Service Proxy does not start 如何在 Google Cloud Run 中配置 nginx 反向代理以指向不同的 Google Cloud Run 应用 - How to configure nginx reverse proxy in Google Cloud Run to point to a different Google Cloud Run app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM