Google Cloud Run Authentication Service-to-Service

问题描述 投票:0回答:2

我在GCP Cloud Run上部署了两个服务(API)。分别命名为service-one.myDomain.comservice-two.myDomain.com。我希望在调用服务2时对服务一进行身份验证,而与任何用户正在执行的操作无关。

我已经阅读并实现了GCP Cloud Run文档中有关认证服务到服务(https://cloud.google.com/run/docs/authenticating/service-to-service)的说明,但service-one.myDomain.com未能成功调用service-two.myDomain.com接收到401:未经授权的响应。

关于如何获取service-one以成功调用service-two的任何想法?

这是我的设置:

IAM和服务帐户:

在Google IAM上,我创建了两个服务帐户,并授予了他们两个“ Cloud Run Invoker”(roles/run.invoker)角色:[email protected][email protected]

在Cloud Run内部,我将服务帐户从“默认计算服务帐户”更改为我创建的服务帐户。我为[email protected]分配了service-one.myDomain.com,为[email protected]分配了service-two.myDomain.com

OAuth令牌:

service-one.myDomain.com中,我调用元数据服务器以从以下URL获取OAuth令牌(jwt):http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=https://service-two.myDomain.com,请求标头设置为{'Metadata-Flavor': 'Google'},请求成功,我收到的令牌被解码为具有以下有效负载:

{
  "alg": "RS256",
  "kid": "9cef5340642b157fa8a4f0d874fe7543872d82db",
  "typ": "JWT"
}

{
  "aud": "https://service-two.mydomain.com",
  "azp": "100959068407876085761",
  "email": "[email protected]",
  "email_verified": true,
  "exp": 1572806540,
  "iat": 1572802940,
  "iss": "https://accounts.google.com",
  "sub": "100953168404568085761"
}

Http请求:

使用令牌,我从service-one.myDomain.comservice-two.myDomain.com上的http端点发出请求。我用{'Authorization': 'Bearer {token}'}设置了请求标头({token}是令牌的值)。

Http响应:

响应是未经授权的401,我的日志显示响应头包括:

{'WWW-Authenticate': 'Bearer error="invalid_token" error_description="The access token could not be verified"'}

内容为:

"
<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>401 Unauthorized</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Unauthorized</h1>
<h2>Your client does not have permission to the requested URL <code>/health</code>.</h2>
<h2></h2>
</body></html>
" 

我很困惑。...关于我缺少使service-one认证为service-two的任何想法?

google-cloud-platform google-oauth google-authentication google-cloud-run google-iam
2个回答
0
投票

您在Google Cloud中的错误(HTTP 401)通常表示您的令牌格式无效/损坏/格式错误。

错误消息显示“无法验证访问令牌”,这确认了这一假设。此消息表示令牌签名无效。

您显示的令牌似乎有效。 kid用于当前的Google公共签名证书,您可以在https://www.googleapis.com/oauth2/v1/certs

令牌的主体也是正确的,具有正确的听众,等等。>>

[这留下了一个问题,在接收令牌和发送令牌之间,您可能破坏了它或不发送接收到的令牌。


0
投票

答案是使用gcp云运行生成的网址作为OAuth令牌请求中的受众。并与jwt中的“ aud”字段相关。

© www.soinside.com 2019 - 2024. All rights reserved.