获取 GCP Cloud Composer 上 IAM 代理的客户端 ID

3

我正在尝试使用云函数在composer环境中触发Airflow DAG。为了做到这一点,我需要按照此处所述获取客户端ID。我尝试使用curl命令,但它没有返回任何值。通过Python脚本,我不断收到以下错误:

Traceback (most recent call last):
  File "get_client_id.py", line 55, in <module>
    get_client_id(
  File "get_client_id.py", line 40, in get_client_id
    print(query_string['client_id'][0])
KeyError: 'client_id'

Python代码:

import google.auth
import google.auth.transport.requests
import requests
import six.moves.urllib.parse

# Authenticate with Google Cloud.
# See: https://cloud.google.com/docs/authentication/getting-started
credentials, _ = google.auth.default(
    scopes=['https://www.googleapis.com/auth/cloud-platform'])
authed_session = google.auth.transport.requests.AuthorizedSession(
    credentials)

project_id = 'my-project'
location = 'my-region'
composer_environment = 'my-env'

environment_url = (
    'https://composer.googleapis.com/v1beta1/projects/{}/locations/{}'
    '/environments/{}').format(project_id, location, composer_environment)
composer_response = authed_session.request('GET', environment_url)
environment_data = composer_response.json()
airflow_uri = environment_data['config']['airflowUri']



# The Composer environment response does not include the IAP client ID.
# Make a second, unauthenticated HTTP request to the web server to get the
# redirect URI.
redirect_response = requests.get(airflow_uri, allow_redirects=False)
redirect_location = redirect_response.headers['location']

# Extract the client_id query parameter from the redirect.
parsed = six.moves.urllib.parse.urlparse(redirect_location)
query_string = six.moves.urllib.parse.parse_qs(parsed.query)
print(query_string['client_id'][0])

cURL命令:

curl -v my_airflow_url 2>&1 >/dev/null | grep -o "client_id\=[A-Za-z0-9-]*\.apps\.googleusercontent\.com"

有没有人知道如何获取IAM代理的client_id


1
您能明确一下您是要求 IAM 还是 IAP 吗?您的标题指向 IAM,但在代码中却有 ...IAP 客户端 ID。如果您要求的是 IAP,您是否看到了这个帖子 如何使用用户默认凭据以编程方式对 Cloud Identity-Aware Proxy(Cloud IAP)安全资源进行身份验证??此外,GCP 中还有一些文档 在此处。这是否是您正在寻找的内容? - PjoterS
@PjoterS 我在问IAM。代码可以在Google文档中找到:https://cloud.google.com/composer/docs/how-to/using/triggering-with-gcf#python - Dudes
1
嗨@伙计们,你们在使用Composer v2吗?这段代码是为Composer v1编写的,我认为只需要更新一下就可以在Composer v2上运行了,而Composer v2仍处于预览阶段。我负责这些教程,会提出一个bug来解决这个问题。 - LEC
确实是Composer v2。 - Dudes
@伙计们,你们尝试过将这个重写为V2版本吗?或者你们使用Composer V1来运行这段代码了吗? - PjoterS
我已经尝试过使用V1,它可以正常工作@PjoterS。 - Dudes
2个回答

1
发布此 Community Wiki 以获得更好的 可见性
如评论区所述,此配置与 Cloud Composer V1 兼容,可在 GCP 文档 使用 Cloud Functions 触发 DAG 中找到。
目前可以找到两个选项卡 Cloud Composer 1 GuidesCloud Composer 2 Guides。 在 Cloud Composer 1 下是 OP 使用的代码,但如果您检查 Cloud Composer 2Manage DAGs > 使用 Cloud Functions 触发 DAG 下,您将获得没有适当文档的信息。

Cloud Composer 2 的此文档页面尚不可用,请使用 Cloud Composer 1 页面

作为解决方案,请使用 Cloud Composer V1

最近发布了一个关于如何使用Composer 2中的稳定API触发DAG的指南:https://cloud.google.com/composer/docs/composer-2/access-airflow-api - rafalbiegacz

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接