2019-05-08 14:58:41 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import redis
|
|
|
|
import configparser
|
|
|
|
|
|
|
|
import bcrypt
|
|
|
|
import secrets
|
|
|
|
|
|
|
|
# Import config
|
|
|
|
sys.path.append('./modules/')
|
|
|
|
|
2019-06-06 21:27:13 +02:00
|
|
|
def get_all_role():
|
|
|
|
return r_serv_db.zrange('ail:all_role', 0 , -1)
|
|
|
|
|
2019-05-08 14:58:41 +02:00
|
|
|
def hashing_password(bytes_password):
|
|
|
|
hashed = bcrypt.hashpw(bytes_password, bcrypt.gensalt())
|
|
|
|
return hashed
|
|
|
|
|
2019-06-06 21:27:13 +02:00
|
|
|
def create_user_db(username_id , password, default=False, role=None, update=False):
|
2019-05-08 14:58:41 +02:00
|
|
|
password = password.encode()
|
|
|
|
password_hash = hashing_password(password)
|
|
|
|
r_serv_db.hset('user:all', username_id, password_hash)
|
2019-06-06 21:27:13 +02:00
|
|
|
if update:
|
|
|
|
r_serv_db.hdel('user_metadata:{}'.format(username_id), 'change_passwd')
|
|
|
|
else:
|
|
|
|
if default:
|
|
|
|
r_serv_db.hset('user_metadata:{}'.format(username_id), 'change_passwd', True)
|
|
|
|
if role:
|
|
|
|
if role in get_all_role():
|
|
|
|
r_serv_db.sadd('user_role:{}'.format(role), username_id)
|
2019-05-08 14:58:41 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
configfile = os.path.join(os.environ['AIL_BIN'], 'packages/config.cfg')
|
|
|
|
if not os.path.exists(configfile):
|
|
|
|
raise Exception('Unable to find the configuration file. \
|
|
|
|
Did you set environment variables? \
|
|
|
|
Or activate the virtualenv.')
|
|
|
|
|
|
|
|
cfg = configparser.ConfigParser()
|
|
|
|
cfg.read(configfile)
|
|
|
|
|
|
|
|
r_serv_db = redis.StrictRedis(
|
|
|
|
host=cfg.get("ARDB_DB", "host"),
|
|
|
|
port=cfg.getint("ARDB_DB", "port"),
|
|
|
|
db=cfg.getint("ARDB_DB", "db"),
|
|
|
|
decode_responses=True)
|
|
|
|
|
|
|
|
username = 'admin@admin.test'
|
2019-06-06 21:27:13 +02:00
|
|
|
password = secrets.token_urlsafe()
|
|
|
|
create_user_db(username, password, role='admin', default=True)
|
2019-05-08 14:58:41 +02:00
|
|
|
|
|
|
|
# create user token
|
|
|
|
token = secrets.token_urlsafe(41)
|
|
|
|
r_serv_db.hset('user:tokens', token, username)
|
|
|
|
|
2019-06-06 21:27:13 +02:00
|
|
|
default_passwd_file = os.path.join(os.environ['AIL_HOME'], 'DEFAULT_PASSWORD')
|
|
|
|
to_write_str = '# Password Generated by default\n# This file is deleted after the first login\n#\nemail=admin@admin.test\npassword='
|
|
|
|
to_write_str = to_write_str + password + '\nAPI_Key=' + token
|
|
|
|
with open(default_passwd_file, 'w') as f:
|
|
|
|
f.write(to_write_str)
|
|
|
|
|
2019-05-08 14:58:41 +02:00
|
|
|
print('new user created: {}'.format(username))
|
|
|
|
print('password: {}'.format(password))
|