无法用服务帐户身份验证我的Google云运行服务
我在需要身份验证的云运行中部署了一项服务:
gcloud run部署my-service -project my-project-image eu.gcr.io/my-project/my-project/rest-ofst/rest-of-path-platform-管理 - - 欧洲区 - 欧洲 - 不允许未经申请的
这似乎很好。但是,当我尝试从另一个服务访问我的服务时(就我的情况而言)时,它会给我一个响应[403]
,这意味着它拒绝授权它。据我所知,我的服务帐户确实具有正确的角色:云运行调用器,服务帐户令牌创建者,服务控制器。即使我要添加所有者角色,它也无法正常工作。
这是我访问服务的代码:
API_URL="https://my-url.run.app/"
def create_signed_jwt(credentials_json, run_service_url):
iat = time.time()
exp = iat + 3600
payload = {
'iss': credentials_json['client_email'],
'sub': credentials_json['client_email'],
'target_audience': run_service_url,
'aud': 'https://www.googleapis.com/oauth2/v4/token',
'iat': iat,
'exp': exp
}
additional_headers = {
'kid': credentials_json['private_key_id']
}
signed_jwt = jwt.encode(
payload,
credentials_json['private_key'],
headers=additional_headers,
algorithm='RS256'
)
return signed_jwt
def exchange_jwt_for_token(signed_jwt):
body = {
'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion': signed_jwt
}
token_request = requests.post(
url='https://www.googleapis.com/oauth2/v4/token',
headers={
'Content-Type': 'application/x-www-form-urlencoded'
},
data=urllib.parse.urlencode(body)
)
return token_request.json()['id_token']
def get_headers():
"""
Creates the headers for each request to the API on google cloud run
"""
credentials = {
"type": "service_account",
"project_id": "my-project-id",
"private_key_id": my-key-id,
"private_key": "-----BEGIN PRIVATE KEY----- very long token-----END PRIVATE KEY-----\n",
"client_email": "[email protected]",
"client_id": my-client-id,
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": some-standard-url,
"client_x509_cert_url": some-standard-url
}
token = exchange_jwt_for_token(create_signed_jwt(credentials, API_URL))
return {
"Authorization": f"Bearer {token}"
}
def test_request_function():
""" request example url"""
response = requests.get(f'{API_URL}/health', get_headers())
print(test_request_function())
为什么不可能授权?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我鼓励您考虑使用Google的 auth library (for python)或任何其他值得信赖的验证库生成JWT。
当您经历的是,制作JWT的工作很粗糙,即使您使其正常工作,您也可以支持支持代码,这可能会更好地留给他人。
请参阅:
产生一个身份令牌并希望(
200
)。然后,您可以将身份令牌插入Eg https://jwt.io 进行检查。
I encourage you to consider using Google's auth library (for Python) or any other reputable auth library to generate the JWT.
As you're experiencing, crafting JWT's is gnarly and, even when you get it working, you're on the hook for supporting code that would probably be better left to others.
See: Authenticating Service-to-Service
Yields an identity token and hopefully (
200
).You can then plug the identity token into e.g. https://jwt.io to inspect it.