-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_authentication_setup.py
45 lines (34 loc) · 1.33 KB
/
web_authentication_setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
This file should be run before starting the webserver to set up user-password authentication
"When password auth is enabled, an initial user credential will need to be created before anyone can login.
Creating a new user has to be done via a Python REPL on the same machine Airflow is installed."
More details at https://airflow.apache.org/security.html#web-authentication
"""
import os
from configparser import ConfigParser
from airflow import models, settings
from airflow.contrib.auth.backends.password_auth import PasswordUser
def create_superuser():
"""
Creates a superuser (admin level) for accessing the web UI for Airflow
:return: None
"""
# get path to config file relative to this file
current_dir = os.path.dirname(__file__)
filepath = os.path.join(current_dir, 'config.ini')
# read config.ini file
config = ConfigParser()
config.read(filepath)
# create User object
user = PasswordUser(models.User())
user.username = config['web_authentication']['username']
user.email = config['web_authentication']['email']
user.password = config['web_authentication']['password']
user.superuser = True
# Add user to Airflow session
session = settings.Session()
session.add(user)
session.commit()
session.close()
if __name__ == '__main__':
create_superuser()