Anoop R
Anoop R

Reputation: 555

Airflow user creation

I am using Airflow version 1.8.2 and set up couple of Dags.Everything running as expected .I have admin user created for airflow web server access.But for other teams to monitor their job we can't provide this admin user So I tried to create a different user from the UI '/admin/user/'. But only the following fields are available .No options to provide roles or password etc. enter image description here

Does anyone faced the same issue or I am doing some wrong thing .How to create role based users So that I can tag some specific dags to those teams

Thanks

Upvotes: 19

Views: 86221

Answers (3)

officialrahulmandal
officialrahulmandal

Reputation: 3078

If anyone is looking a way that is easy to understand then here's the thing.

Activate you airflow python environment and start python shell. Then with the help of these commands you can easily set a new user in airflow.

>>> import airflow
>>> from airflow import models, settings
>>> from airflow.contrib.auth.backends.password_auth import PasswordUser
>>> user = PasswordUser(models.User())
>>> user.username = 'new_user_name'
>>> user.email = '[email protected]'
>>> user.password = 'set_the_password'
>>> session = settings.Session()
>>> session.add(user)
>>> session.commit()
>>> session.close()
>>> exit()

If you want to create a admin user you can simply do that by adding user.superuser = True It'll do the job.

Upvotes: 9

Micah Smith
Micah Smith

Reputation: 4453

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]

Update: As of Airflow 2, this has been rolled into airflow users create: https://airflow.apache.org/docs/apache-airflow/stable/cli-and-env-variables-ref.html#create_repeat1

airflow users create [-h] -e EMAIL -f FIRSTNAME -l LASTNAME [-p PASSWORD] -r
                     ROLE [--use-random-password] -u USERNAME

Upvotes: 38

Ash Berlin-Taylor
Ash Berlin-Taylor

Reputation: 4048

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

Upvotes: 11

Related Questions