使用 Airflow API 进行 JWT 身份验证 [英] JWT Authentication with Airflow API
本文介绍了使用 Airflow API 进行 JWT 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试实施基于令牌的身份验证,作为触发气流 dag 的一部分.有没有办法添加 JWT 身份验证来生成 access_token 来触发 API?非常感谢任何帮助!
I am trying to implement a token based authentication as part of triggering an airflow dag. Is there way we can add JWT authentication to generate an access_token to trigger the API? Any help is much appreciated!
推荐答案
我们的身份验证服务返回一个 JSON
响应,如下所示:
Our authentication service returns a JSON
response like this :
{
"clientToken": "322e8df6-0597-479e-984d-db6d8705ee66"
}
这是我在气流 2.1 中使用 SimpleHttpOperator
和 XCOM
变量传递机制来克服这个问题的示例代码:
Here is my sample code in airflow 2.1 using SimpleHttpOperator
and XCOM
variable passing mechanism to overcome this problem :
get_token = SimpleHttpOperator(
task_id='get_token',
method='POST',
http_conn_id='http_service',
data=json.dumps( {
"username": "user_name",
"password": "n46r4A83"
}),
endpoint='/authenticate',
dag=dag,
headers = {
'Content-Type': 'application/json',
'Cache-Control': 'no-cache',
},
response_filter=lambda response: response.json()['clientToken'],
)
get_cities = SimpleHttpOperator(
task_id='get_cities',
method='GET',
http_conn_id='http_service',
endpoint='/cities?dsCode=120',
dag=dag,
headers = {
'X-CLIENT-TOKEN':'{{ ti.xcom_pull(task_ids="get_token") }}'
}
# response_filter=lambda response: response.json()['clientToken'],
)
get_token >> get_cities
这篇关于使用 Airflow API 进行 JWT 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文