コード例 #1
0
    def post(self):
        args = request.get_json()
        db_session = DatabaseManager().get_session()

        redirect_rule_instance = add_redirect_rule(db_session, **args)

        if isinstance(redirect_rule_instance, int):
            DatabaseManager().return_session(db_session)
            return api_error(
                message="Unable to add redirect rule",
                errors="A rule like this already exists" if redirect_rule_instance == 2 else
                       "Invalid rule with rewrite! Check your path and destination fields.",
                status_code=400
            )
        if isinstance(redirect_rule_instance, RedirectRule):
            redirect_rule_data = db_encode_model(redirect_rule_instance, expand=True)
            DatabaseManager().return_session(db_session)

            # Metrics
            metric_update_rules_total()

            return make_response(jsonify({
                "new_rule": redirect_rule_data,
                "status": "done"
            }), 200)
コード例 #2
0
    def test_delete_ambiguous_request(self, database_ambiguous):
        """
        Starts with an populated database with 5 ambiguous request entries
        Test the delete_ambiguous_request() function.
        Expected behaviour:
            1. Deletes an existing entry amd returns True
            2. Tries to delete a non existent entry and returns False
        """
        # Get session
        from redirectory.libs_int.database import DatabaseManager
        db_session = DatabaseManager().get_session()

        # Import needed functions and classes
        from redirectory.libs_int.database import delete_ambiguous_request, db_get_table_row_count
        from redirectory.models import AmbiguousRequest

        # Check
        result = delete_ambiguous_request(db_session, 1)
        count = db_get_table_row_count(db_session, AmbiguousRequest)
        assert result
        assert count == 4

        result = delete_ambiguous_request(db_session, 1001)
        assert result is False

        result = delete_ambiguous_request(db_session, 2)
        count = db_get_table_row_count(db_session, AmbiguousRequest)
        assert result
        assert count == 3

        # Return session
        DatabaseManager().return_session(db_session)
コード例 #3
0
    def test_list_ambiguous_requests(self, database_ambiguous):
        """
        Starts with an populated database with 5 ambiguous request entries
        Test the list_ambiguous_requests() function.
        Expected behaviour:
            1. Returns a list of all the entries in the Ambiguous request table
        """
        # Get session
        from redirectory.libs_int.database import DatabaseManager
        db_session = DatabaseManager().get_session()

        # Import needed functions and classes
        from redirectory.libs_int.database import list_ambiguous_requests, db_get_table_row_count
        from redirectory.models import AmbiguousRequest

        # Base for comparison
        original_count = db_get_table_row_count(db_session, AmbiguousRequest)

        # Check
        result = list_ambiguous_requests(db_session)
        assert isinstance(result, list)
        assert len(result) == original_count

        # Return session
        DatabaseManager().return_session(db_session)
コード例 #4
0
    def test_get_table_row_count(self, database_populated):
        """
        Starts with an populated database with 5 redirect rules.
        Test the get_table_row_count() function.
        Expected behaviour:
            1. Counts the entries in a table in the DB correctly
            2. When one is deleted the count should decrease
        """
        # Get session
        from redirectory.libs_int.database import DatabaseManager
        db_session = DatabaseManager().get_session()

        # Import needed functions and classes
        from redirectory.libs_int.database import db_get_table_row_count
        from redirectory.models import RedirectRule, PathRule

        # Check
        count = db_get_table_row_count(db_session, RedirectRule)
        assert count == 5

        instance: RedirectRule = db_session.query(RedirectRule).get(1)
        instance.delete(db_session)
        count = db_get_table_row_count(db_session, RedirectRule)
        assert count == 4

        count = db_get_table_row_count(db_session, PathRule)
        assert count == 4

        # Return session
        DatabaseManager().return_session(db_session)
コード例 #5
0
    def get(self):
        db_session = DatabaseManager().get_session()
        sync = Synchronizer()

        # Check if sqlite DB is empty.
        redirect_rule_table_row_count = db_get_table_row_count(
            db_session, RedirectRule)
        DatabaseManager().return_session(db_session)

        if redirect_rule_table_row_count == 0:
            return api_error(
                message="Unable to compile new Hyperscan database",
                errors=
                f"Can't compile new Hyperscan database from no Redirect rules stored in the SQL database",
                status_code=400)

        compiler = CompilerJob(sync.util_new_hs_db_version_callback_test)

        compile_thread = Thread(target=compiler.run)
        compile_thread.start()

        # Metrics
        metric_update_rules_total()

        return make_response(
            jsonify({
                "message": "New hyperscan DB are compiled for testing",
                "status": "done"
            }), 200)
コード例 #6
0
    def test_get_or_create(self, database_empty):
        """
        Starts with an empty database.
        Test the get_or_create() function.
        Expected behaviour:
            1. When not existent it should create it and say it is new
            2. If already existent it should return the instance and say it is old
        """
        # Get session
        from redirectory.libs_int.database import DatabaseManager
        db_session = DatabaseManager().get_session()

        # Import needed functions and classes
        from redirectory.libs_int.database import db_get_or_create
        from redirectory.models import DomainRule

        # Check with brand new
        domain = 'test.com'
        domain_is_regex = False
        domain_instance, is_new_domain = db_get_or_create(
            db_session, DomainRule, rule=domain, is_regex=domain_is_regex)
        assert is_new_domain is True
        assert domain_instance.rule == domain
        assert domain_instance.is_regex == domain_is_regex

        # Check existing
        domain_instance, is_new_domain = db_get_or_create(
            db_session, DomainRule, rule=domain, is_regex=domain_is_regex)
        assert is_new_domain is False
        assert domain_instance.rule == domain
        assert domain_instance.is_regex == domain_is_regex

        # Return session
        DatabaseManager().return_session(db_session)
コード例 #7
0
    def test_delete_redirect_rule(self, database_populated):
        """
        Starts with a populated database with 5 Redirect Rule entries
        Test the delete_redirect_rule() function.
        Expected behaviour:
            1. Deletes a rule correctly
            2. If a rule does not exist it should return False
        """
        # Get session
        from redirectory.libs_int.database import DatabaseManager
        db_session = DatabaseManager().get_session()

        # Import needed functions and classes
        from redirectory.libs_int.database import delete_redirect_rule, db_get_table_row_count
        from redirectory.models import RedirectRule

        # Check deletion
        result = delete_redirect_rule(db_session, 1)
        assert result
        count = db_get_table_row_count(db_session, RedirectRule)
        assert count == 4

        # Check deletion of non existent
        result = delete_redirect_rule(db_session, 1001)
        assert result is False

        # Return session
        DatabaseManager().return_session(db_session)
コード例 #8
0
    def post(self):
        args = request.get_json()
        db_session = DatabaseManager().get_session()

        updated_redirect_rule = update_redirect_rule(db_session, **args)
        if isinstance(updated_redirect_rule, int):
            DatabaseManager().return_session(db_session)
            metric_update_rules_total()  # Metrics
            return api_error(
                message="Unable to update redirect rule",
                errors=
                f"Redirect rule with id: {args['redirect_rule_id']} does not exist"
                if updated_redirect_rule == 2 else
                "The update rules fails the validation for rewrite! Check your path and destination fields.",
                status_code=400)
        if updated_redirect_rule:
            serialized_updated_redirect_rule = db_encode_model(
                updated_redirect_rule, expand=True)

            DatabaseManager().return_session(db_session)
            metric_update_rules_total()  # Metrics

            return make_response(
                jsonify({
                    "updated_rule": serialized_updated_redirect_rule,
                    "status": "done"
                }), 200)
コード例 #9
0
    def test_get_model_by_id(self, database_populated):
        """
        Starts with a populated database with 5 Redirect Rule entries
        Test the get_model_by_id() function.
        Expected behaviour:
            1. Returns the correct model instance
            2. Returns the correct instance of that model with the given id
            3. Returns None when an instance of that model with id does not exist
        """
        # Get session
        from redirectory.libs_int.database import DatabaseManager
        db_session = DatabaseManager().get_session()

        # Import needed functions and classes
        from redirectory.libs_int.database import get_model_by_id
        from redirectory.models import RedirectRule, PathRule

        # Check
        result = get_model_by_id(db_session, RedirectRule, 1)
        assert isinstance(result, RedirectRule)

        result = get_model_by_id(db_session, PathRule, 1)
        assert isinstance(result, PathRule)

        result = get_model_by_id(db_session, PathRule, 100)
        assert result is None

        # Return session
        DatabaseManager().return_session(db_session)
コード例 #10
0
    def test_pick_result(self, hyperscan):
        """
        Starts with a populated hyperscan database with 5 rules.
        Test the pick_result() function.
        Expected behaviour:
            1. Picks the correct one with the highest value
        """
        # Get session
        from redirectory.libs_int.database import DatabaseManager
        db_session = DatabaseManager().get_session()

        # Import needed functions and classes
        from redirectory.libs_int.hyperscan import HsManager

        # Check
        matched_ids = HsManager().search(domain='asd.test.kumina.nl',
                                         path='/test/path')
        assert matched_ids == [1, 5]
        result, is_ambiguous = HsManager.pick_result(db_session, matched_ids)
        assert is_ambiguous
        assert result.id == 1

        matched_ids = HsManager().search(domain='asd.test.kumina.nl',
                                         path='/test/path/aa')
        assert matched_ids == [3, 5]
        result, is_ambiguous = HsManager.pick_result(db_session, matched_ids)
        assert is_ambiguous
        assert result.id == 3

        matched_ids = HsManager().search(domain='123.test.kumina.nl',
                                         path='/test/path')
        assert matched_ids == [4, 5]
        result, is_ambiguous = HsManager.pick_result(db_session, matched_ids)
        assert is_ambiguous
        assert result.id == 4

        matched_ids = HsManager().search(domain='123!.test.kumina.nl',
                                         path='/test/path')
        assert matched_ids == [5]
        result, is_ambiguous = HsManager.pick_result(db_session, matched_ids)
        assert not is_ambiguous
        assert result.id == 5

        matched_ids = HsManager().search(domain='123a.test.kumina.nl',
                                         path='/test/path/a')
        assert matched_ids == [3, 5]
        result, is_ambiguous = HsManager.pick_result(db_session, matched_ids)
        assert is_ambiguous
        assert result.id == 3

        matched_ids = HsManager().search(domain='ggg.test.kumina.nl',
                                         path='/test/path/a')
        assert matched_ids == [2, 3, 5]
        result, is_ambiguous = HsManager.pick_result(db_session, matched_ids)
        assert is_ambiguous
        assert result.id == 2

        # Return session
        DatabaseManager().return_session(db_session)
コード例 #11
0
    def post(self):
        url = request.get_json()["request_url"]
        host, path = self.parse_url(url)

        # Init managers and sessions
        hs_manager = HsManager()
        db_session = DatabaseManager().get_session()

        # Search
        try:
            start_search_time = time()
            search_data = hs_manager.search(domain=host,
                                            path=path,
                                            is_test=True)
            end_search_time = time()
        except AssertionError as e:
            Logger() \
                .event(category="hyperscan", action="hyperscan search") \
                .error(message=str(e)) \
                .out(severity=Severity.ERROR)
            DatabaseManager().return_session(db_session)
            return api_error(
                message="Something went wrong during Hyperscan search!",
                errors=str(e),
                status_code=400)

        # Convert ids into models
        domain_rules_data = {}
        redirect_rules_data = {}
        d_r_map = search_data["domain_rule_map"]

        for domain_id in d_r_map.keys():
            domain_rule = get_model_by_id(db_session, DomainRule, domain_id)
            domain_rules_data[domain_id] = db_encode_model(domain_rule)

            for rule_id in d_r_map[domain_id]:
                redirect_rule = get_model_by_id(db_session, RedirectRule,
                                                rule_id)
                redirect_rules_data[rule_id] = db_encode_model(redirect_rule,
                                                               expand=True)

        # Get final result
        final_redirect_rule, is_ambiguous = HsManager.pick_result(
            db_session, list(redirect_rules_data.keys()))

        # Fill in test data
        search_data[
            "final_result_id"] = final_redirect_rule.id if final_redirect_rule is not None else None
        search_data["is_ambiguous"] = is_ambiguous
        search_data["time"] = str(end_search_time - start_search_time)

        DatabaseManager().return_session(db_session)
        return make_response(
            jsonify({
                "domain_rules": domain_rules_data,
                "redirect_rules": redirect_rules_data,
                "search_data": search_data
            }), 200)
コード例 #12
0
ファイル: hs_actions.py プロジェクト: kumina/k8s-redirectory
def get_expressions_ids_flags(
        db_model: DeclarativeMeta,
        expression_path: str,
        expression_regex_path: str,
        id_path: str,
        combine_expr_with: str = None
) -> Tuple[List[bytes], List[int], List[int]]:
    """
    Gets the expression in the correct format from the database.
    Depending on the arguments the expression can be combined with another piece
    of data. The expression will also be regex escaped if it is a literal.
    If the expression is a regex then a second check will be conducted which checks if the expression
    matches an empty string. If so a different flag than the default is applied.

    Args:
        db_model: The model/table of the current database
        expression_path: The attribute where the expression can be found in the model
        expression_regex_path: The attribute holding the value if an expression is regex or not
        id_path: The attribute where the id can be found
        combine_expr_with: The attribute of extra piece of data that can be appended before the expression

    Returns:
        a tuple containing the expressions, the ids and the flags. tuple(expressions, ids, flags)
    """
    expressions = []
    ids = []
    flags = []

    db_session = DatabaseManager().get_session()

    for instance in db_session.query(db_model):
        expression: str = multi_getattr(instance, expression_path)
        expression_regex: bool = multi_getattr(instance, expression_regex_path)
        expression_id: int = multi_getattr(instance, id_path)

        if combine_expr_with:
            combine_with = multi_getattr(instance, combine_expr_with)
            expression = str(combine_with) + expression

        if expression_regex:
            compiled_ex = re.compile(expression)
            if compiled_ex.search(""):
                flags.append(hs.HS_FLAG_ALLOWEMPTY)
            else:
                flags.append(hs.HS_FLAG_SOM_LEFTMOST)
            del compiled_ex
        else:
            expression = re.escape(expression)
            flags.append(hs.HS_FLAG_SOM_LEFTMOST)

        ids.append(expression_id)
        expressions.append(expression.encode("UTF-8"))

    # Release db session
    DatabaseManager().return_session(db_session)

    return expressions, ids, flags
コード例 #13
0
def database_empty(configuration):
    # Import DB Manager first before the models
    from redirectory.libs_int.database import DatabaseManager

    # Import the models now so that the DB Manager know about them
    import redirectory.models

    # Delete any previous creations of the Database Manager and tables
    DatabaseManager().reload()
    DatabaseManager().delete_db_tables()

    # Create all tables based on the just imported modules
    DatabaseManager().create_db_tables()
コード例 #14
0
ファイル: get_page.py プロジェクト: kumina/k8s-redirectory
    def post(self):
        # Gather arguments
        args = request.get_json()
        page_number = args["page_number"]
        page_size = args["page_size"]

        # Get DB session
        db_session = DatabaseManager().get_session()

        start_process_time = time()
        # Generate query for pages
        query = db_session.query(RedirectRule)
        # Apply filters to query if needed
        if "filter" in args:
            query = self.apply_filters(query, args["filter"])

        # Get the page
        page: Page = paginate(query, page_number, page_size)

        # Check if page exists
        if not page.items:
            # Release the DB session and return an api error
            DatabaseManager().return_session(db_session)
            return api_error(
                message="Unable to get specified page",
                errors=f"Page with number: {page_number} does not exist",
                status_code=404)

        # Get the data in json
        data = []
        for redirect_rule in page.items:
            data.append(db_encode_model(redirect_rule, expand=True))
        end_process_time = time()

        # Return DB session and return response
        DatabaseManager().return_session(db_session)

        # Metrics
        metric_update_rules_total()

        return make_response(
            jsonify({
                "data": data,
                "page": {
                    "total_items": page.total,
                    "total_pages": page.pages,
                    "has_previous": page.has_previous,
                    "has_next": page.has_next
                },
                "time": str(end_process_time - start_process_time)
            }), 200)
コード例 #15
0
ファイル: hs_actions.py プロジェクト: kumina/k8s-redirectory
def update_hs_db_version(new_db_version: str = None) -> str:
    """
    Updates the SQLite3 database about the new version of Hyperscan database.

    Returns:
        the new version of the hyperscan database
    """
    # Get new db version if not given
    if new_db_version is None:
        new_db_version: str = get_timestamp()

    from redirectory.models import HsDbVersion
    db_session = DatabaseManager().get_session()

    try:
        db_version: HsDbVersion = db_session.query(HsDbVersion).one()
        db_version.old_version = db_version.current_version
        db_version.current_version = new_db_version
    except NoResultFound:
        new_version = HsDbVersion()
        new_version.current_version = new_db_version
        db_session.add(new_version)

    db_session.commit()
    DatabaseManager().return_session(db_session)
    return new_db_version
コード例 #16
0
def update_rules_total():
    """
    This function updates the RULES_TOTAL metric every time it is called with a count from the DB
    """
    from redirectory.libs_int.database import db_get_table_row_count, DatabaseManager
    from redirectory.models import RedirectRule

    # Get count from DB
    db_session = DatabaseManager().get_session()
    new_count = db_get_table_row_count(db_session, RedirectRule)
    DatabaseManager().return_session(db_session)

    # Set count to metric
    RULES_TOTAL.set(new_count)
コード例 #17
0
def database_ambiguous(database_empty):
    from redirectory.libs_int.database import DatabaseManager, add_ambiguous_request

    # Get the database session
    db_session = DatabaseManager().get_session()

    # Add all the rules
    add_ambiguous_request(db_session, 'https://www.google.com')
    add_ambiguous_request(db_session, 'https://www.kumina.nl')
    add_ambiguous_request(db_session, 'https://www.example.com')
    add_ambiguous_request(db_session, 'https://www.test.com')
    add_ambiguous_request(db_session, 'https://www.youtube.com')

    # Return session
    DatabaseManager().return_session(db_session)
コード例 #18
0
ファイル: list.py プロジェクト: kumina/k8s-redirectory
    def get(self):
        db_session = DatabaseManager().get_session()

        data = list_ambiguous_requests(db_session)

        DatabaseManager().return_session(db_session)
        if len(data) > 0:
            return make_response(
                jsonify({
                    "ambiguous_requests": data,
                    "status": "done"
                }), 200)
        else:
            return api_error(message="Unable to get ambiguous request entries",
                             errors="No ambiguous entries in the SQL db",
                             status_code=404)
コード例 #19
0
    def _run_development(self):
        DatabaseManager().create_db_tables()

        Logger() \
            .event(category="runnable", action="run development") \
            .server(ip=self.host, port=self.port) \
            .out(severity=Severity.INFO)

        # CORS only in development
        @self.application.after_request
        def _after_request(response):
            response.headers.add('Access-Control-Allow-Origin', '*')
            response.headers.add('Access-Control-Allow-Headers',
                                 'Content-Type,Authorization')
            response.headers.add('Access-Control-Max-Age', 21600)
            return response

        # Start metric server
        start_metrics_server()

        # Run application
        self.application.run(
            **{
                "host": self.host,
                "port": self.port,
                "debug": True,
                "use_reloader": False
            })
コード例 #20
0
    def test_update_redirect_rule(self, database_populated):
        """
        Starts with a populated database with 5 Redirect Rule entries
        Test the update_redirect_rule() function.
        Expected behaviour:
            1. Update the rule correctly
            2. Returns None if rule with that id does not exist
        """
        # Get session
        from redirectory.libs_int.database import DatabaseManager
        db_session = DatabaseManager().get_session()

        # Import needed functions and classes
        from redirectory.libs_int.database import update_redirect_rule
        from redirectory.models import RedirectRule

        # Update rule 1
        updated_rule = update_redirect_rule(db_session, 1, 'updated.com',
                                            False, '/new/update', False,
                                            'https://google.com', False, 100)

        # Get the rule from the DB not from the method
        rule: RedirectRule = db_session.query(RedirectRule).get(1)

        # Check
        assert updated_rule is not None
        assert rule.domain_rule.rule == 'updated.com'
        assert rule.path_rule.rule == '/new/update'
        assert rule.destination_rule.destination_url == 'https://google.com'

        # Check non existent rule
        updated_rule = update_redirect_rule(db_session, 1001, '', False, '',
                                            False, '', False, 100)
        assert isinstance(updated_rule, int)
        assert updated_rule == 2

        # Check validation for rewrite rule
        updated_rule = update_redirect_rule(
            db_session, 1, 'asdasasdasd.com', False,
            '/wrong/pattern??[<asd>]>??', True,
            'the_best_destination_but_the_wrong_one{aa}?', True, 100)
        assert isinstance(updated_rule, int)
        assert updated_rule == 1

        # Return session
        DatabaseManager().return_session(db_session)
コード例 #21
0
ファイル: hs_actions.py プロジェクト: kumina/k8s-redirectory
def get_hs_db_version() -> Tuple[Optional[str], Optional[str]]:
    """
    Queries the database for the HsDbVersion table which only
    has one entry at all times. Return the two numbers which
    represent the old_version and the current_version of the
    Hyperscan database.

    Returns:
        tuple of old_version and new_version of the Hyperscan Database
    """
    db_session = DatabaseManager().get_session()
    try:
        db_version: HsDbVersion = db_session.query(HsDbVersion).one()
        return db_version.old_version, db_version.current_version
    except NoResultFound:
        return None, None
    finally:
        DatabaseManager().return_session(db_session)
コード例 #22
0
    def test_get_usage_count(self, database_populated):
        """
        Starts with a populated database with 5 Redirect Rule entries
        Test the get_usage_count() function.
        Expected behaviour:
            1. Gets the correct usage of model in the Redirect Rule table
        """
        # Get session
        from redirectory.libs_int.database import DatabaseManager
        db_session = DatabaseManager().get_session()

        # Import needed functions and classes
        from redirectory.libs_int.database import get_usage_count, add_redirect_rule
        from redirectory.models import PathRule, DomainRule, DestinationRule

        # Check
        usage = get_usage_count(db_session, DomainRule, 1)
        assert usage == 1

        usage = get_usage_count(db_session, PathRule, 1)
        assert usage == 1

        usage = get_usage_count(db_session, DestinationRule, 1)
        assert usage == 1

        # Add new rules
        add_redirect_rule(db_session, 'ggg.test.kumina.nl', False,
                          '/test/path', False, 'https://kumina.nl', False, 100)
        add_redirect_rule(db_session, 'blabla.test.kumina.nl', False,
                          '/test/path', False, 'https://kumina.nl', False, 100)

        # Check again
        usage = get_usage_count(db_session, DomainRule, 2)
        assert usage == 2

        usage = get_usage_count(db_session, PathRule, 1)
        assert usage == 3

        usage = get_usage_count(db_session, DestinationRule, 3)
        assert usage == 3

        # Return session
        DatabaseManager().return_session(db_session)
コード例 #23
0
    def import_into_db(self):
        """
        Imports all the rules in the given csv file into the database as RedirectRules.
        If a rule is a duplicate it will be skipped.
        If there is an error in parsing the csv then all the changes will
        be roll backed and the whole import will be marked as fail.
        """
        db_session = DatabaseManager().get_session()

        try:
            row_counter = 1
            for row in self.csv_reader:
                row_counter += 1
                assert len(row) == len(self.columns), f"Entry at line: {row_counter} has different amount of " \
                    f"arguments than expected. Expected: {len(self.columns)} instead got: {len(row)}"

                self.data_template["domain"] = row[0]
                self.data_template["domain_is_regex"] = self._get_bool_from_str(row[1])
                self.data_template["path"] = row[2]
                self.data_template["path_is_regex"] = self._get_bool_from_str(row[3])
                self.data_template["destination"] = row[4]
                self.data_template["destination_is_rewrite"] = self._get_bool_from_str(row[5])
                self.data_template["weight"] = int(row[6])

                result = add_redirect_rule(db_session, **self.data_template, commit=False)
                if isinstance(result, int) and result == 2:  # 2 means already exists
                    raise AssertionError
        except AssertionError as e:
            Logger() \
                .event(category="import", action="import failed") \
                .error(message=str(e)) \
                .out(severity=Severity.ERROR)
            db_session.rollback()
        except Exception as e:
            Logger() \
                .event(category="import", action="import failed") \
                .error(message=str(e)) \
                .out(severity=Severity.CRITICAL)
            db_session.rollback()
        else:
            Logger() \
                .event(category="import", action="import successful",
                       dataset=f"Rules added from import: {row_counter - 1}")
            db_session.commit()

        DatabaseManager().return_session(db_session)
コード例 #24
0
    def test_encode_model(self, database_populated):
        """
        Starts with an populated database with 5 redirect rules.
        Test the encode_model() function.
        Expected behaviour:
            1. Converts a model to a JSON dict correctly
            2. When the expand settings is used it actually expands the models
        """
        # Get session
        from redirectory.libs_int.database import DatabaseManager
        db_session = DatabaseManager().get_session()

        # Import needed functions and classes
        from redirectory.libs_int.database import db_encode_model
        from redirectory.models import RedirectRule

        # Get a model and encode to Json (dict)
        model = db_session.query(RedirectRule).get(1)
        encoded = db_encode_model(model)

        # Check
        assert 'id' in encoded
        assert encoded['id'] == 1
        assert 'weight' in encoded
        assert encoded['weight'] == 100
        assert 'domain_rule' not in encoded

        # Encode with the expand property
        encoded_expanded = db_encode_model(model, expand=True)

        # Check
        assert 'domain_rule' in encoded_expanded
        assert encoded_expanded['domain_rule']['rule'] == 'asd.test.kumina.nl'
        assert 'path_rule' in encoded_expanded
        assert encoded_expanded['path_rule']['rule'] == '/test/path'
        assert 'destination_rule' in encoded_expanded
        assert encoded_expanded['destination_rule'][
            'destination_url'] == 'https://google.com'

        # Return session
        DatabaseManager().return_session(db_session)
コード例 #25
0
    def test_paginate(self, database_populated):
        """
        Starts with a populated database with 5 Redirect Rule entries.
        Test the paginate() function.
        Expected behaviour:
            1. Gathers the correct amount of entries into a Page
            2. Calculates correctly if there are other pages
            3. Calculates the total correctly
        """
        # Get session
        from redirectory.libs_int.database import DatabaseManager
        db_session = DatabaseManager().get_session()

        # Import needed functions and classes
        from redirectory.libs_int.database import paginate, Page
        from redirectory.models import RedirectRule

        # Check
        query = db_session.query(RedirectRule)
        page: Page = paginate(query, 1, 3)
        assert isinstance(page, Page)
        assert len(page.items) == 3
        assert page.next_page == 2
        assert page.has_next
        assert not page.has_previous
        assert page.pages == 2
        assert page.total == 5

        # Check
        query = db_session.query(RedirectRule)
        page: Page = paginate(query, 1, 5)
        assert isinstance(page, Page)
        assert len(page.items) == 5
        assert page.next_page is None
        assert not page.has_next
        assert not page.has_previous
        assert page.pages == 1
        assert page.total == 5

        # Return session
        DatabaseManager().return_session(db_session)
コード例 #26
0
    def worker_sync_files(self):
        """
        This function gets called when the application is in worker mode.
        Stops the readiness checks from succeeding.
        Downloads and reloads the databases.
        After that it logs a couple of metrics and also logging.
        """
        Logger() \
            .event(category="synchronizer", action="synchronizer sync started") \
            .out(severity=Severity.INFO)

        db_manager = DatabaseManager()
        hs_manager = HsManager()

        # Mark hs database as not loaded which causes ready check to fail
        hs_manager.database.is_loaded = False

        # Download sync files and write to disc
        sync_zip_file = self.management_get_sync_files()
        if sync_zip_file is None:
            return

        self.util_save_sync_zip_file(sync_zip_file)

        # Reload sql
        db_manager.reload()

        # Reload hs
        hs_manager.database.reload_database()
        new_hs_db_version = hs_manager.database.db_version

        # Metrics
        HYPERSCAN_DB_RELOADED_TOTAL.labels(self.configuration.node_type).inc()
        HYPERSCAN_DB_VERSION.labels(
            self.configuration.node_type).set(new_hs_db_version)

        # Log
        Logger() \
            .event(category="synchronizer", action="synchronizer sync complete",
                   dataset=f"New hs db version: {new_hs_db_version}") \
            .out(severity=Severity.INFO)
コード例 #27
0
    def post(self):
        rule_id = request.get_json()["rule_id"]
        db_session = DatabaseManager().get_session()

        redirect_instance = get_model_by_id(db_session, RedirectRule, rule_id)

        if redirect_instance:
            redirect_rule_data = db_encode_model(redirect_instance,
                                                 expand=True)

            DatabaseManager().return_session(db_session)
            return make_response(
                jsonify({
                    "rule": redirect_rule_data,
                    "status": "done"
                }), 200)
        else:
            DatabaseManager().return_session(db_session)
            return api_error(message="Unable to get redirect rule",
                             errors=f"Redirect rule with id: {rule_id} do",
                             status_code=404)
コード例 #28
0
ファイル: add.py プロジェクト: kumina/k8s-redirectory
    def post(self):
        new_request_url = request.get_json()["request"]
        db_session = DatabaseManager().get_session()

        # Delete the ambiguous entry and get bool if successful
        new_entry = add_ambiguous_request(db_session, new_request_url)

        # Release session
        DatabaseManager().return_session(db_session)

        if new_entry is not None:
            return make_response(
                jsonify({
                    "new_ambiguous_request": db_encode_model(new_entry),
                    "status": "done"
                }), 200)
        else:
            return api_error(
                message="Unable to add new ambiguous request entry",
                errors=f"Ambiguous request like this already exist",
                status_code=400)
コード例 #29
0
    def test_get_and_update_hs_db_version(self, database_empty):
        """
        Starts with an empty database.
        Test the get_hs_db_version() function.
        Test the update_hs_db_version() function.
        Expected behaviour:
            1. The get hs db version returns correctly. Including when there is no entry!
            2. Updates new version correctly and moves the old one into the old column in the db.
        """
        # Get session
        from redirectory.libs_int.database import DatabaseManager
        db_session = DatabaseManager().get_session()

        # Import needed functions and classes
        from redirectory.libs_int.hyperscan import get_hs_db_version, update_hs_db_version, get_timestamp

        # Check
        result = get_hs_db_version()
        assert isinstance(result, tuple)
        assert result[0] is None
        assert result[1] is None

        version_1 = get_timestamp()
        update_hs_db_version(version_1)
        result = get_hs_db_version()
        assert isinstance(result, tuple)
        assert result[0] is None
        assert result[1] == version_1

        version_2 = get_timestamp()
        update_hs_db_version(version_2)
        result = get_hs_db_version()
        assert isinstance(result, tuple)
        assert result[0] == version_1
        assert result[1] == version_2

        # Return session
        DatabaseManager().return_session(db_session)
コード例 #30
0
    def post(self):
        rule_id = request.get_json()["rule_id"]
        db_session = DatabaseManager().get_session()

        # Delete the rule and get bool if successful
        is_deleted = delete_redirect_rule(db_session, rule_id)

        # Release session
        DatabaseManager().return_session(db_session)

        # Metrics
        metric_update_rules_total()

        if is_deleted:
            return make_response(jsonify({
                "status": "done"
            }), 200)
        else:
            return api_error(
                message="Unable to delete redirect rule",
                errors=f"Redirect rule with id: {rule_id} does not exist",
                status_code=404
            )