Airflow user creation

风格不统一 提交于 2019-12-03 08:06:47

As of Airflow 1.10, there is an airflow create_user CLI: https://airflow.apache.org/cli.html#create_user.

It supports roles and passwords:

airflow create_user [-h] [-r ROLE] [-u USERNAME] [-e EMAIL] [-f FIRSTNAME]
                    [-l LASTNAME] [-p PASSWORD] [--use_random_password]

The user models in Airflow are currently simplistic and (as of at least 1.9.0) there is no way via the UI to set passwords.

The approach I use is the following python script:

#!/usr/bin/env python

import argparse
import getpass
import sys


def create_user(opts):
    from airflow.contrib.auth.backends.password_auth import PasswordUser
    from airflow import models, settings

    u = PasswordUser(models.User())
    u.username = opts['username']
    u.email = opts['email']
    u.password = opts['password']

    s = settings.Session()
    s.add(u)
    s.commit()
    s.close()

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('email')
    parser.add_argument('username', nargs='?', help="Defaults to local part of email")
    args = parser.parse_args()

    if not args.username:
        # Default username is the local part of the email address
        args.username = args.email[:args.email.index('@')]

    args.password = getpass.getpass(prompt="Enter new user password: ")
    confirm = getpass.getpass(prompt="Confirm:  ")

    if args.password != confirm:
        sys.stderr.write("Passwords don't match\n")
        sys.exit(1)
    create_user(vars(args))

This versoin doesn't support changing passwords as we haven't needed it yet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!