Airflow - 如何使用REST API的安全授权

Kan*_*ada 6 authentication rest post airflow

介绍:

大家好,我正在尝试使用 Airflow 的 REST API 来通过外部触发器激活 DAG,例如:

POST: http://{{url}}:{{port}}/api/experimental/dags/MY_DAG_ID/dag_runs

headers = {
        'Content-Type': 'application/json',
        'Cache-Control': 'no-cache',
    }
Run Code Online (Sandbox Code Playgroud)

问题:

它工作得很好(答案:状态 200),但我需要一些安全性,因为它不能向公众开放,所以我阅读了 API 身份验证,我可以设置auth_backend airflow.cfg其工作方式与用于 Web 的密码身份验证非常相似界面。


[api]
auth_backend = airflow.contrib.auth.backends.password_auth 
Run Code Online (Sandbox Code Playgroud)

但现在,答案是(401 - 未经授权),我不知道如何配置 REST API 以使用具有此安全性的外部触发器。

  • 是否需要在标题上传递我的用户名密码才能工作?
  • 我怎样才能做到这一点?

  • 假设存在一个用户: admin通行证: admin权限: Admin


链接:

aro*_*man 6

您必须通过带有字符串 user:pass 的 Base 64 编码标头传递授权标头

您可以在此处检查它是如何发生的:https://github.com/apache/airflow/blob/029c84e5527b6db6bdbdbe026f455da325bedef3/airflow/contrib/auth/backends/password_auth.py#L205

    header = request.headers.get("Authorization")
    if header:
        userpass = ''.join(header.split()[1:])
        username, password = base64.b64decode(userpass).decode("utf-8").split(":", 1)
Run Code Online (Sandbox Code Playgroud)

用法示例:

https://github.com/apache/airflow/blob/7cba83333c5227ce37967c65d189a5e994898c68/tests/www/api/experimental/test_password_endpoints.py

        response = c.post(
            url_template.format('example_bash_operator'),
            data=json.dumps(dict(run_id='my_run' + datetime.now().isoformat())),
            content_type="application/json",
            headers={'Authorization': 'Basic aGVsbG86d29ybGQ='}  # hello:world
        )
Run Code Online (Sandbox Code Playgroud)