Airflow添加用户登录

Posted braveym

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Airflow添加用户登录相关的知识,希望对你有一定的参考价值。

 

 

 

 

通常我们部署airflow调度系统的时候,默认是直接以admin用户登录进来的,而且不需要输入账号密码

如果业务要求必须通过不同的用户登录进来,可以采用以下的方法给airflow添加用户

 

在 airflow.cfg 文件中 [webserver] 下添加如下配置

[webserver]
authenticate = True
auth_backend = airflow.contrib.auth.backends.password_auth

 

在这里提醒一下,authenticate这个配置项在配置文件里面很多地方都有,非常容易配错了,如果配置错了,很容易掉坑里

个人建议先在配置文件找到[webserver],然后直接添加内容,然后保存退出

 

接下来通过命令行添加用户,我这里的airflow是部署在容器里面的,先进入airflow所在的容器,如果部署在服务器上就直接在服务器里执行

以下是添加用户的命令

 

python

import airflow
from airflow import models, settings from airflow.contrib.auth.backends.password_auth import PasswordUser user = PasswordUser(models.User()) user.username = usertest1 user.email = usertest1@163.com user.password = usertest1 user.superuser = 1 //赋予管理员权限,如果是普通用户就不需要这个 session = settings.Session() session.add(user) session.commit() session.close() exit()

 

 

考虑到现在新的airflow版本用的是python3,在执行语句

from airflow.contrib.auth.backends.password_auth import PasswordUser

的时候会报错,大概也是是没有对应的包,可以通过    pip install  包名    来下载对应的依赖包。

 

 

 

下面是具体例子

root@airflowdataxv2-copy-dfd9d84f4-9hr9b:/usr/local/airflow# python
Python 3.7.5 (default, Nov 15 2019, 02:58:08) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import airflow
/usr/local/lib/python3.7/site-packages/airflow/configuration.py:226: FutureWarning: The task_runner setting in [core] has the old default value of BashTaskRunner. This value has been changed to StandardTaskRunner in the running config, but please update your config before Apache Airflow 2.0.
  FutureWarning
/usr/local/lib/python3.7/site-packages/airflow/configuration.py:606: DeprecationWarning: Specifying both AIRFLOW_HOME environment variable and airflow_home in the config file is deprecated. Please use only the AIRFLOW_HOME environment variable and remove the config file entry.
  warnings.warn(msg, category=DeprecationWarning)
/usr/local/lib/python3.7/site-packages/airflow/utils/sqlalchemy.py:40: DeprecationWarning: get: Accessing configuration method get directly from the configuration module is deprecated. Please access the configuration from the configuration.conf object via conf.get
  tz = conf.get("core", "default_timezone")
/usr/local/lib/python3.7/site-packages/airflow/utils/timezone.py:30: DeprecationWarning: get: Accessing configuration method get directly from the configuration module is deprecated. Please access the configuration from the configuration.conf object via conf.get
  tz = conf.get("core", "default_timezone")
/usr/local/lib/python3.7/site-packages/airflow/config_templates/airflow_local_settings.py:65: DeprecationWarning: The elasticsearch_host option in [elasticsearch] has been renamed to host - the old setting has been used, but please update your config.
  ELASTICSEARCH_HOST = conf.get(elasticsearch, HOST)
/usr/local/lib/python3.7/site-packages/airflow/config_templates/airflow_local_settings.py:67: DeprecationWarning: The elasticsearch_log_id_template option in [elasticsearch] has been renamed to log_id_template - the old setting has been used, but please update your config.
  ELASTICSEARCH_LOG_ID_TEMPLATE = conf.get(elasticsearch, LOG_ID_TEMPLATE)
/usr/local/lib/python3.7/site-packages/airflow/config_templates/airflow_local_settings.py:69: DeprecationWarning: The elasticsearch_end_of_log_mark option in [elasticsearch] has been renamed to end_of_log_mark - the old setting has been used, but please update your config.
  ELASTICSEARCH_END_OF_LOG_MARK = conf.get(elasticsearch, END_OF_LOG_MARK)
[2020-04-08 17:41:17,838] {settings.py:252} INFO - settings.configure_orm(): Using pool settings. pool_size=5, max_overflow=10, pool_recycle=1800, pid=21338
>>> from airflow import models, settings
>>> from airflow.contrib.auth.backends.password_auth import PasswordUser
>>> user = PasswordUser(models.User())
>>> user.username = usertest
>>> user.email = usertest@163.com
>>> user.password = usertest
>>> session = settings.Session()
>>> session.add(user)
>>> session.commit()
>>> user = PasswordUser(models.User())
>>> user.username = usertest1
>>> user.email = usertest1@163.com
>>> user.password = usertest1
>>> session = settings.Session()
>>> session.add(user)
>>> session.commit()
>>> session.close()
>>> exit()

 

 

然后重启一下web,进入浏览器

技术图片

 

 

 

 

点击右上角的退出

技术图片

 

 

 

这个时候就可以通过用户名密码登录了!!

 

以上是关于Airflow添加用户登录的主要内容,如果未能解决你的问题,请参考以下文章

将日志添加到 Airflow 日志

如何识别哪个活动调用片段

Airflow 中文文档:保护连接

airflow使用mysql数据库,LocalExecutor并发调度

教程4 - 验证和权限

Apache Airflow 错误的会话验证漏洞 (CVE-2020-17526)利用