(admin_password_list)
Setup given list of users as admins.
Setup given list of users as admins.
Setup given list of users as admins .
def ensure_admins(admin_password_list):
    """
    Setup given list of users as admins.
    """
    os.makedirs(STATE_DIR, mode=0o700, exist_ok=True)

    if not admin_password_list:
        return
    logger.info("Setting up admin users")
    config_path = CONFIG_FILE
    if os.path.exists(config_path):
        with open(config_path) as f:
            config = yaml.load(f)
    else:
        config = {}

    config["users"] = config.get("users", {})

    db_passw = os.path.join(STATE_DIR, "passwords.dbm")

    admins = []
    for admin_password_entry in admin_password_list:
        for admin_password_pair in admin_password_entry:
            if ":" in admin_password_pair:
                admin, password = admin_password_pair.split(":")
                admins.append(admin)
                # Add admin:password to the db
                password = bcrypt.hashpw(password.encode(), bcrypt.gensalt())
                with dbm.open(db_passw, "c", 0o600) as db:
                    db[admin] = password
            else:
                admins.append(admin_password_pair)
    config["users"]["admin"] = admins

    with open(config_path, "w+") as f:
        yaml.dump(config, f)
def ensure_admins ( admin_password_list ) : os . makedirs ( STATE_DIR , mode = 0o700 , exist_ok = True ) if not admin_password_list : return logger . info ( "Setting up admin users" ) config_path = CONFIG_FILE if os . path . exists ( config_path ) : with open ( config_path ) as f : config = yaml . load ( f ) else : config = { } config [ "users" ] = config . get ( "users" , { } ) db_passw = os . path . join ( STATE_DIR , "passwords.dbm" ) admins = [ ] for admin_password_entry in admin_password_list : for admin_password_pair in admin_password_entry : if ":" in admin_password_pair : admin , password = admin_password_pair . split ( ":" ) admins . append ( admin ) # Add admin:password to the db password = bcrypt . hashpw ( password . encode ( ) , bcrypt . gensalt ( ) ) with dbm . open ( db_passw , "c" , 0o600 ) as db : db [ admin ] = password else : admins . append ( admin_password_pair ) config [ "users" ] [ "admin" ] = admins with open ( config_path , "w+" ) as f : yaml . dump ( config , f )