Example #1
0
async def read_global(
    request: Request,
    global_var: UUID,
    to_decrypt: str = "false",
    global_col: AsyncIOMotorCollection = Depends(get_mongo_c)):
    """
    Returns the Global Variable for the specified id.
    """
    walkoff_db = get_mongo_d(request)
    curr_user_id = await get_jwt_identity(request)

    global_variable = await mongo_helpers.get_item(global_col, GlobalVariable,
                                                   global_var)

    to_read = await auth_check(global_variable, curr_user_id, "read",
                               walkoff_db)
    if to_read:
        if to_decrypt == "false":
            return global_variable.value
        else:
            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
            return fernet_decrypt(key, global_variable.value)
    else:
        raise UnauthorizedException("read data for", "Global Variable",
                                    global_variable.name)
Example #2
0
async def read_all_globals(request: Request, to_decrypt: str = False,
                           global_col: AsyncIOMotorCollection = Depends(get_mongo_c),
                           page: int = 1):
    """
    Returns a list of all Global Variables currently loaded in WALKOFF.
    Pagination is currently not supported.
    """
    walkoff_db = get_mongo_d(request)
    curr_user_id = await get_jwt_identity(request)

    # Pagination is currently not supported.
    if page > 1:
        return []

    key = config.get_from_file(config.ENCRYPTION_KEY_PATH, mode='rb')
    query = await mongo_helpers.get_all_items(global_col, GlobalVariable)

    ret = []
    if to_decrypt == "false":
        return query
    else:
        for global_var in query:
            to_read = await auth_check(global_var, curr_user_id, "read", walkoff_db)
            if to_read:
                temp_var = deepcopy(global_var)
                temp_var.value = fernet_decrypt(key, global_var.value)
                ret.append(temp_var)

        return ret
Example #3
0
async def read_all_globals(
    request: Request,
    to_decrypt: str = False,
    global_col: AsyncIOMotorCollection = Depends(get_mongo_c)):
    """
    Returns a list of all Global Variables currently loaded in WALKOFF.
    """
    walkoff_db = get_mongo_d(request)
    curr_user_id = await get_jwt_identity(request)

    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
    query = await mongo_helpers.get_all_items(global_col, GlobalVariable)

    ret = []
    if to_decrypt == "false":
        return query
    else:
        for global_var in query:
            to_read = await auth_check(global_var, curr_user_id, "read",
                                       walkoff_db)
            if to_read:
                temp_var = deepcopy(global_var)
                temp_var.value = fernet_decrypt(key, global_var.value)
                ret.append(temp_var)

        return ret
Example #4
0
    def validate_global(self, data, **kwargs):
        try:
            if "schema" in data:
                with open(config.ENCRYPTION_KEY_PATH, 'rb') as f:
                    temp = fernet_decrypt(f.read(), data['value'])
                Draft4Validator(data['schema']['schema']).validate(temp)

        except (SchemaError, JSONSchemaValidationError):
            raise MarshmallowValidationError(
                f"Global variable did not validate with provided schema: "
                f"{data['schema']['schema']}")
Example #5
0
def read_global(global_var):
    global_id = str(global_var.id_)
    to_read = auth_check(global_id, "read", "global_variables")

    if to_read:
        global_json = global_variable_schema.dump(global_var)

        if request.args.get('to_decrypt') == "false":
            return jsonify(global_json), HTTPStatus.OK
        else:
            with open(config.ENCRYPTION_KEY_PATH, 'rb') as f:
                return jsonify(fernet_decrypt(
                    f.read(), global_json['value'])), HTTPStatus.OK
    else:
        return None, HTTPStatus.FORBIDDEN
Example #6
0
def read_global(global_var):
    username = get_jwt_claims().get('username', None)
    curr_user_id = (db.session.query(User).filter(User.username == username).first()).id

    global_id = str(global_var.id_)
    to_read = auth_check(global_id, "read", "global_variables")

    if (global_var.creator == curr_user_id) or to_read:
        global_json = global_variable_schema.dump(global_var)

        if request.args.get('to_decrypt') == "false":
            return jsonify(global_json), HTTPStatus.OK
        else:
            key = config.get_from_file(config.ENCRYPTION_KEY_PATH)#, 'rb')
            return jsonify(fernet_decrypt(key, global_json['value'])), HTTPStatus.OK
    else:
        return None, HTTPStatus.FORBIDDEN
Example #7
0
def read_all_globals():
    with open(config.ENCRYPTION_KEY_PATH, 'rb') as f:
        key = f.read()
        ret = []
        query = current_app.running_context.execution_db.session.query(
            GlobalVariable).order_by(GlobalVariable.name).all()

        if request.args.get('to_decrypt') == "false":
            return query, HTTPStatus.OK
        else:
            for global_var in query:
                to_read = auth_check(str(global_var.id_), "read",
                                     "global_variables")
                if to_read:
                    temp_var = deepcopy(global_var)
                    temp_var.value = fernet_decrypt(key, global_var.value)
                    ret.append(temp_var)

            return ret, HTTPStatus.OK
Example #8
0
def read_all_globals():
    username = get_jwt_claims().get('username', None)
    curr_user_id = (db.session.query(User).filter(User.username == username).first()).id

    key = config.get_from_file(config.ENCRYPTION_KEY_PATH) #, 'rb')
    ret = []
    query = current_app.running_context.execution_db.session.query(GlobalVariable).order_by(GlobalVariable.name).all()

    if request.args.get('to_decrypt') == "false":
        return query, HTTPStatus.OK
    else:
        for global_var in query:
            to_read = auth_check(str(global_var.id_), "read", "global_variables")
            if (global_var.creator == curr_user_id) or to_read:
                temp_var = deepcopy(global_var)
                temp_var.value = fernet_decrypt(key, global_var.value)
                ret.append(temp_var)

        return ret, HTTPStatus.OK
Example #9
0
    async def execute_action(self, action: Action):
        """ Execute an action, and push its result to Redis. """
        # TODO: Is there a better way to do this?
        self.logger.handlers[0].stream.execution_id = action.execution_id
        self.logger.handlers[0].stream.workflow_id = action.workflow_id

        self.logger.debug(
            f"Attempting execution of: {action.label}-{action.execution_id}")
        self.current_execution_id = action.execution_id
        self.current_workflow_id = action.workflow_id

        results_stream = f"{action.execution_id}:results"

        if hasattr(self, action.name):
            # Tell everyone we started execution
            action.started_at = datetime.datetime.now()
            start_action_msg = NodeStatusMessage.executing_from_node(
                action, action.execution_id, started_at=action.started_at)
            await self.redis.xadd(
                results_stream,
                {action.execution_id: message_dumps(start_action_msg)})

            try:
                func = getattr(self, action.name, None)
                if callable(func):
                    if len(action.parameters) < 1:
                        result = await func()
                    else:
                        params = {}
                        for p in action.parameters:
                            if p.variant == ParameterVariant.GLOBAL:
                                key = config.get_from_file(
                                    config.ENCRYPTION_KEY_PATH, 'rb')
                                params[p.name] = fernet_decrypt(key, p.value)
                            else:
                                params[p.name] = p.value
                        result = await func(**params)

                    action_result = NodeStatusMessage.success_from_node(
                        action,
                        action.execution_id,
                        result=result,
                        started_at=action.started_at)
                    self.logger.debug(
                        f"Executed {action.label}-{action.execution_id} "
                        f"with result: {result}")

                else:
                    self.logger.error(
                        f"App {self.__class__.__name__}.{action.name} is not callable"
                    )
                    action_result = NodeStatusMessage.failure_from_node(
                        action,
                        action.execution_id,
                        result="Action not callable",
                        started_at=action.started_at)

            except Exception as e:
                self.logger.exception(
                    f"Failed to execute {action.label}-{action.execution_id}")
                action_result = NodeStatusMessage.failure_from_node(
                    action,
                    action.execution_id,
                    result=repr(e),
                    started_at=action.started_at)

        else:
            self.logger.error(
                f"App {self.__class__.__name__} has no method {action.name}")
            action_result = NodeStatusMessage.failure_from_node(
                action,
                action.execution_id,
                result="Action does not exist",
                started_at=action.started_at)

        await self.redis.xadd(
            results_stream,
            {action.execution_id: message_dumps(action_result)})