在 GCP Cloud composer 上获取 IAM 代理的 client_id

Get the client_id of the IAM proxy on GCP Cloud composer

我正在尝试在具有云功能的作曲家环境中触发 Airflow DAG。为此,我需要按照 here 所述获取客户端 ID。我试过使用 curl 命令,但它没有 return 任何值。使用 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 吗?

发布此 Community Wiki 是为了更好 visibility

正如 @LEC 在评论部分中提到的,此配置与 Cloud Composer V1 兼容,可在 GCP 文档 Triggering DAGs with Cloud Functions.

中找到

目前可以找到两个选项卡 Cloud Composer 1 GuidesCloud Composer 2 GuidesCloud Composer 1 下是 OP 使用的代码,但如果您在 Manage DAGs > Triggering DAGs with Cloud Functions 下检查 Cloud Composer 2,您将得到信息,表明还没有适当的文档。

This documentation page for Cloud Composer 2 is not yet available. Please use the page for Cloud Composer 1.

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

这里是描述如何在 Composer 2/Airflow 中触发 DAG 的指南 - https://cloud.google.com/composer/docs/composer-2/access-airflow-api