Beispiel #1
0
async def update_global(request: Request, updated_global: GlobalVariable, global_var: UUID,
                        global_col: AsyncIOMotorCollection = Depends(get_mongo_c)):
    """
    Updates a specific Global Variable (fetched by id) and returns it.
    """
    walkoff_db = get_mongo_d(request)
    curr_user_id = await get_jwt_identity(request)

    old_global = await mongo_helpers.get_item(global_col, GlobalVariable, global_var)
    if not old_global:
        raise DoesNotExistException("update", "Global Variable", global_var)
    global_id = old_global.id_

    new_permissions = updated_global.permissions
    access_level = new_permissions.access_level

    to_update = await auth_check(old_global, curr_user_id, "update", walkoff_db)
    if to_update:
        if access_level == AccessLevel.CREATOR_ONLY:
            updated_global.permissions = await creator_only_permissions(curr_user_id)
        elif access_level == AccessLevel.EVERYONE:
            updated_global.permissions = await default_permissions(curr_user_id, walkoff_db, "global_variables")
        elif access_level == AccessLevel.ROLE_BASED:
            await append_super_and_internal(updated_global.permissions)
            updated_global.permissions.creator = curr_user_id

        # try:
        key = config.get_from_file(config.ENCRYPTION_KEY_PATH, mode='rb')
        updated_global.value = fernet_encrypt(key, updated_global.value)
        return await mongo_helpers.update_item(global_col, GlobalVariable, global_id, updated_global)
        # except Exception as e:
        #     logger.info(e)
        #     raise UniquenessException("global_variable", "update", updated_global.name)
    else:
        raise UnauthorizedException("update data for", "Global Variable", old_global.name)
Beispiel #2
0
async def create_global(
    request: Request,
    new_global: GlobalVariable,
    global_col: AsyncIOMotorCollection = Depends(get_mongo_c)):
    """
    Creates a new Global Variable in WALKOFF and returns it.
    """
    walkoff_db = get_mongo_d(request)
    curr_user_id = await get_jwt_identity(request)

    permissions = new_global.permissions
    access_level = permissions.access_level
    if access_level == AccessLevel.CREATOR_ONLY:
        new_global.permissions = await creator_only_permissions(curr_user_id)
    elif access_level == AccessLevel.EVERYONE:
        new_global.permissions = await default_permissions(
            curr_user_id, walkoff_db, "global_variables")
    elif access_level == AccessLevel.ROLE_BASED:
        await append_super_and_internal(new_global.permissions)
        new_global.permissions.creator = curr_user_id
    try:
        key = config.get_from_file(config.ENCRYPTION_KEY_PATH)  #, 'rb')
        # for testing
        try:
            key = key.encode('utf-8')
            key = base64.b64encode(key)
        except:
            key = key
        new_global.value = fernet_encrypt(key, new_global.value)
        return await mongo_helpers.create_item(global_col, GlobalVariable,
                                               new_global)
    except Exception as e:
        logger.info(e)
        raise UniquenessException("global_variable", "create", new_global.name)
Beispiel #3
0
def update_global(global_var):
    data = request.get_json()
    global_id = data["id_"]

    new_permissions = data['permissions']

    to_update = auth_check(global_id, "update", "global_variables")
    if to_update:
        if new_permissions:
            auth_check(global_id,
                       "update",
                       "global_variables",
                       updated_roles=new_permissions)
        else:
            default_permissions("global_variables", global_id)

        try:
            with open(config.ENCRYPTION_KEY_PATH, 'rb') as f:
                data['value'] = fernet_encrypt(f.read(), data['value'])

            global_variable_schema.load(data, instance=global_var)
            current_app.running_context.execution_db.session.commit()
            return global_variable_schema.dump(global_var), HTTPStatus.OK
        except (IntegrityError, StatementError):
            current_app.running_context.execution_db.session.rollback()
            return unique_constraint_problem("global_variable", "update",
                                             data["name"])
    else:
        return None, HTTPStatus.FORBIDDEN
Beispiel #4
0
def update_global(global_var):
    username = get_jwt_claims().get('username', None)
    curr_user_id = (db.session.query(User).filter(User.username == username).first()).id

    data = request.get_json()
    global_id = data["id_"]

    new_permissions = data['permissions']
    access_level = data['access_level']

    to_update = auth_check(global_id, "update", "global_variables")
    if (global_var.creator == curr_user_id) or to_update:
        if access_level == 0:
            auth_check(global_id, "update", "global_variables",
                       updated_roles=[{"role": 1, "permissions": ["delete", "execute", "read", "update"]}])
        if access_level == 1:
            default_permissions("global_variables", global_id, data=data)
        elif access_level == 2:
            auth_check(global_id, "update", "global_variables", updated_roles=new_permissions)
        # if new_permissions:
        #     auth_check(global_id, "update", "global_variables", updated_roles=new_permissions)
        # else:
        #     default_permissions("global_variables", global_id, data=data)
        try:
            key = config.get_from_file(config.ENCRYPTION_KEY_PATH)#, 'rb')
            data['value'] = fernet_encrypt(key, data['value'])
            global_variable_schema.load(data, instance=global_var)
            current_app.running_context.execution_db.session.commit()
            return global_variable_schema.dump(global_var), HTTPStatus.OK
        except (IntegrityError, StatementError):
            current_app.running_context.execution_db.session.rollback()
            return unique_constraint_problem("global_variable", "update", data["name"])
    else:
        return None, HTTPStatus.FORBIDDEN
Beispiel #5
0
def create_global():
    data = request.get_json()
    global_id = data['id_']

    username = get_jwt_claims().get('username', None)
    curr_user = db.session.query(User).filter(
        User.username == username).first()
    data.update({'creator': curr_user.id})

    new_permissions = data['permissions']
    access_level = data['access_level']

    # creator only
    if access_level == 0:
        update_permissions("global_variables",
                           global_id,
                           new_permissions=[{
                               "role":
                               1,
                               "permissions":
                               ["delete", "execute", "read", "update"]
                           }],
                           creator=curr_user.id)
    # default permissions
    elif access_level == 1:
        default_permissions("global_variables",
                            global_id,
                            data=data,
                            creator=curr_user.id)
    # user-specified permissions
    elif access_level == 2:
        update_permissions("global_variables",
                           global_id,
                           new_permissions=new_permissions,
                           creator=curr_user.id)

    # if new_permissions:
    #     update_permissions("global_variables", global_id, new_permissions=new_permissions, creator=curr_user.id)
    # else:
    #     default_permissions("global_variables", global_id, data=data, creator=curr_user.id)

    try:
        key = config.get_from_file(config.ENCRYPTION_KEY_PATH, 'rb')
        data['value'] = fernet_encrypt(key, data['value'])
        global_variable = global_variable_schema.load(data)
        current_app.running_context.execution_db.session.add(global_variable)
        current_app.running_context.execution_db.session.commit()
        return global_variable_schema.dump(global_variable), HTTPStatus.CREATED
    except IntegrityError:
        current_app.running_context.execution_db.session.rollback()
        return unique_constraint_problem("global_variable", "create",
                                         data["name"])
Beispiel #6
0
def create_global():
    data = request.get_json()
    global_id = data['id_']

    new_permissions = data['permissions']
    if new_permissions:
        update_permissions("global_variables",
                           global_id,
                           new_permissions=new_permissions)
    else:
        default_permissions("global_variables", global_id)

    try:
        with open(config.ENCRYPTION_KEY_PATH, 'rb') as f:
            data['value'] = fernet_encrypt(f.read(), data['value'])

        global_variable = global_variable_schema.load(data)
        current_app.running_context.execution_db.session.add(global_variable)
        current_app.running_context.execution_db.session.commit()
        return global_variable_schema.dump(global_variable), HTTPStatus.CREATED
    except IntegrityError:
        current_app.running_context.execution_db.session.rollback()
        return unique_constraint_problem("global_variable", "create",
                                         data["name"])