airflow通过restapi触发的网络大坑的解决办法

本文讲述了作者在使用Airflow通过RESTAPI触发任务时遇到401错误,分析了该错误与身份验证设置的关系,并提供了从无验证到启用基本身份验证的解决措施,包括配置`auth_backends`和相关设置。
摘要由CSDN通过智能技术生成

本帖记录airflow通过restapi触发任务遇到的报错,以及对应解决方法。

问题

        先前我测试是利用request模块向webserver发送的post请求(如果环境可以使用postman等测试工具的话会更方便些),方法如下:

url="http://10.*.*.*:8080/api/v1/dags/{}/dagRuns".format(dag_id)
res = request.post(url,data=json.dumps(data_info),auth=(user,password),headers=headers)

        通过百度网上遇到的情况以及相应的解决方法很少,存在遇到403错误的同学。但我这里是报错401,401的报错目前遇到的情况主要是和权限验证有关

解决措施

官方说明:

API 身份验证

API 的身份验证与 Web 身份验证分开处理。默认值为 检查用户会话:

[api]
auth_backends = airflow.api.auth.backend.session

在1.10.11版更改: 在 Airflow <1.10.11 中,默认设置是允许所有未经身份验证的 API 请求,但 如果 Web 服务器可公开访问,则会带来安全风险。在 2.3.0 版更改: 在 Airflow <2.3.0 中,auth_backend的设置只允许设置1个值。在 2.3.0 中,auth_backend被更改为支持多个值的配置。如果要检查当前设置了哪些身份验证后端,可以使用以下示例中的命令。

$ airflow config get-value api auth_backends
airflow.api.auth.backend.basic_auth
禁用身份验证

如果只是为了API测试,不需要身份验证或者没有啥网络风险的情况下 (或者如果你已经在Airflow前面设计过了身份验证层),可以在airflow.cfg内设置以下内容:

[api]
auth_backends = airflow.api.auth.backend.default

  注意:用户只能禁用用于测试的API 身份验证,无法禁用稳定 REST API 的身份验证。

Kerberos 身份验证

API 当前支持 Kerberos 身份验证。要启用 Kerberos 身份验证,请在配置中设置以下内容:

[api]
auth_backends = airflow.api.auth.backend.kerberos_auth

[kerberos]
keytab = <KEYTAB>

Kerberos 服务配置为 airflow/fully.qualified.domainname@REALM。注意要确保使用 kerberos 完整用户名/领域来命名用户名,例如为user_name@KERBEROS-REALM。

基本身份验证

基本用户名密码身份验证API 支持基本用户名密码身份验证。这适用于通过 LDAP 登录或在Airflow 元数据数据库中使用密码。若要启用基本身份验证,请在配置中设置以下内容:

[api]
auth_backends = airflow.api.auth.backend.basic_auth

用户名和密码需要进行 base64 编码,并通过 HTTP 标头发送,格式如下:Authorization

Authorization: Basic Base64(username:password)

下面是可用于验证设置的示例 curl 命令:

ENDPOINT_URL="http://localhost:8080/"
curl -X GET  \
    --user "username:password" \
    "${ENDPOINT_URL}/api/v1/pools"

请注意,即使Airflow 网络服务器可能正在使用另一个身份验证方法,但你仍然可以启用此设置以允许通过用户名进行 API 访问密码凭据。在此设置下,只有通过LDAP或通过命令airflow users create创建的用户才能通过 API 身份验证。

具体方法:

        这里我们设置为“基本身份验证”的认证方式,利用创建的用户信息进行API的访问。编辑airflow.cfg配置文件,更新选项auth_backend如下(相应的官方文档的参考说明可参见API — Airflow Documentation (apache.org)):

[api]
# Enables the deprecated experimental API. Please note that these APIs do not have access control.
# The authenticated user has full access.
#
# .. warning::
#
#   This `Experimental REST API <https://airflow.readthedocs.io/en/latest/rest-api-ref.html>`__ is
#   deprecated since version 2.0. Please consider using
#   `the Stable REST API <https://airflow.readthedocs.io/en/latest/stable-rest-api-ref.html>`__.
#   For more information on migration, see
#   `RELEASE_NOTES.rst <https://github.com/apache/airflow/blob/main/RELEASE_NOTES.rst>`_
enable_experimental_api = False

# Comma separated list of auth backends to authenticate users of the API. See
# https://airflow.apache.org/docs/apache-airflow/stable/security/api.html for possible values.
# ("airflow.api.auth.backend.default" allows all requests for historic reasons)
# auth_backends = airflow.api.auth.backend.session
auth_backend = airflow.api.auth.backend.basic_auth

# Used to set the maximum page limit for API requests
maximum_page_limit = 100

# Used to set the default page limit when limit is zero. A default limit
# of 100 is set on OpenApi spec. However, this particular default limit
# only work when limit is set equal to zero(0) from API requests.
# If no limit is supplied, the OpenApi spec default is used.
fallback_page_limit = 100

# The intended audience for JWT token credentials used for authorization. This value must match on the client and server sides. If empty, audience will not be tested.
# Example: google_oauth2_audience = project-id-random-value.apps.googleusercontent.com
google_oauth2_audience =

# Path to Google Cloud Service Account key file (JSON). If omitted, authorization based on
# `the Application Default Credentials
# <https://cloud.google.com/docs/authentication/production#finding_credentials_automatically>`__ will
# be used.
# Example: google_key_path = /files/service-account-json
google_key_path =

# Used in response to a preflight request to indicate which HTTP
# headers can be used when making the actual request. This header is
# the server side response to the browser's
# Access-Control-Request-Headers header.
access_control_allow_headers =

# Specifies the method or methods allowed when accessing the resource.
access_control_allow_methods =

# Indicates whether the response can be shared with requesting code from the given origins.
# Separate URLs with space.
access_control_allow_origins =

        更新完成后重启airflow webserver的相应服务即可。

参考文献

API — Airflow Documentation (apache.org)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Felier.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值