def run(self): HsManager().database.load_database() # Add the redirect from redirectory.services import WorkerRedirect self.api.add_resource(WorkerRedirect, "/", "/<path:content>") # Get needed namespaces worker_ns = NamespaceManager().get_namespace("worker") status_ns = NamespaceManager().get_namespace("status") # Add namespaces to api self.api.add_namespace(worker_ns) self.api.add_namespace(status_ns) # Init api with application self.api.init_app(self.application) # Log Logger() \ .event(category="runnable", action="service configured") \ .service(name="worker").out(severity=Severity.INFO) # Run according to configuration if self.config.deployment == "prod": self._run_production(is_worker=True) elif self.config.deployment == "dev": self._run_development() elif self.config.deployment == "test": self._run_test()
def run(self): # Load hyperscan database because of test in UI HsManager().database.load_database() # Get needed namespaces management_ns = NamespaceManager().get_namespace("management") status_ns = NamespaceManager().get_namespace("status") # Add the ui from redirectory.services import ManagementUI self.api.add_resource(ManagementUI, "/", "/<path:path>") # Log ui folder Logger().event( category="ui", action="ui loaded", dataset=self.config.directories.ui).out(severity=Severity.INFO) # Add namespaces to api self.api.add_namespace(management_ns) self.api.add_namespace(status_ns) # Init api with application self.api.init_app(self.application) # Log Logger() \ .event(category="runnable", action="service configured") \ .service(name="management").out(severity=Severity.INFO) # Run according to configuration if self.config.deployment == "prod": self._run_production() elif self.config.deployment == "dev": self._run_development() elif self.config.deployment == "test": self._run_test()
This endpoint provides the management pod with the ability to get information about all of the worker pods. This is done with the use of the Kubernetes API. It returns an array with every worker as an object. If the application is not running in a Kubernetes environment then a 400 will be returned. """ from flask import make_response, jsonify from flask_restplus import Resource from redirectory.libs_int.metrics import REQUESTS_DURATION_SECONDS from redirectory.libs_int.kubernetes import K8sManager from redirectory.libs_int.service import NamespaceManager, api_error K8S_GET_WORKERS_REQUEST_DURATION_SECONDS = REQUESTS_DURATION_SECONDS.labels( "management") api = NamespaceManager().get_namespace("management") @api.route("/kubernetes/get_workers") class ManagementKubernetesGetWorkers(Resource): @api.doc( description="Get a list of all worker nodes currently up and running!") @K8S_GET_WORKERS_REQUEST_DURATION_SECONDS.time() def get(self): try: workers = K8sManager().get_worker_pods() except AssertionError as e: return api_error(message="Unable to get worker pods", errors=str(e), status_code=400)
""" from time import time from flask import make_response, jsonify, request from flask_restplus import Resource, fields from redirectory.libs_int.metrics import REQUESTS_DURATION_SECONDS, metric_update_rules_total from redirectory.models import RedirectRule, DomainRule, PathRule, DestinationRule from redirectory.libs_int.database import DatabaseManager, db_encode_model, paginate, Page, db_sanitize_like_query from redirectory.libs_int.service import NamespaceManager, api_error # Metrics RULES_GET_PAGE_REQUEST_DURATION_SECONDS = REQUESTS_DURATION_SECONDS.labels( "management") # Api Namespace api = NamespaceManager().get_namespace("management") # All possible filters to apply to the page selection filter_args = api.model( "filter", { "redirect_rule_id": fields.Integer(required=False, default=None), "domain_rule_id": fields.Integer(required=False, default=None), "path_rule_id": fields.Integer(required=False, default=None), "destination_rule_id": fields.Integer(required=False, default=None), "weight": fields.Integer(required=False, default=None), "domain":
The Status Get Node Configuration provides the ability to see the configuration of the current Node. After the configuration is loaded (which is one of the first things that the application does) it is in dictionary form and is easily serializable to JSON and returned. """ from flask import make_response, jsonify from flask_restplus import Resource from redirectory.libs_int.metrics import REQUESTS_DURATION_SECONDS from redirectory.libs_int.config import Configuration from redirectory.libs_int.service import NamespaceManager # Metrics STATUS_CONFIG_REQUEST_DURATION_SECONDS = REQUESTS_DURATION_SECONDS.labels("management") # Api Namespace api = NamespaceManager().get_namespace("status") @api.route("/get_node_configuration") class ServiceGetConfiguration(Resource): @api.doc(description="Get the configuration of the service") @STATUS_CONFIG_REQUEST_DURATION_SECONDS.time() def get(self): config = Configuration().values return make_response(jsonify({ "configuration": config }), 200)
If the health status request fails then the worker is considered unreachable and a 400 is returned. If the health status request succeeds then a second reload worker hs db request is send. If the reload worker hs db requests returns 200 then the worker has started updating itself. """ from flask import make_response, jsonify, request from flask_restplus import Resource, fields from kubi_ecs_logger import Logger, Severity from redirectory.libs_int.metrics import REQUESTS_DURATION_SECONDS from redirectory.libs_int.sync import Synchronizer from redirectory.libs_int.kubernetes import WorkerPod from redirectory.libs_int.service import NamespaceManager, api_error DATABASE_RELOAD_WORKER_REQUEST_DURATION_SECONDS = REQUESTS_DURATION_SECONDS.labels( "management") api = NamespaceManager().get_namespace("management") endpoint_args = api.model( "update_worker", { "name": fields.String(required=True, example="redirectory-worker"), "ip": fields.String(required=True, example="127.0.0.1"), "port": fields.Integer(required=True, example=8001), }) @api.route("/database/reload_worker") class ManagementReloadWorkerHsDb(Resource): @api.expect(endpoint_args, validate=True) @api.doc(description= "With the help of kubernetes api update every worker with new db." )
and it will delete Domain Rules, Path Rules and Destination Rules if they are not used by any other Redirect Rule. For more insights on the topic take a look at delete_redirect_rule() function. If no Redirect Rule with the given id exists then a 404 will be returned. """ from flask import make_response, jsonify, request from flask_restplus import Resource, fields from redirectory.libs_int.metrics import REQUESTS_DURATION_SECONDS, metric_update_rules_total from redirectory.libs_int.database import DatabaseManager, delete_redirect_rule from redirectory.libs_int.service import NamespaceManager, api_error # Metrics RULES_DELETE_REQUEST_DURATION_SECONDS = REQUESTS_DURATION_SECONDS.labels("management") # Api Namespace api = NamespaceManager().get_namespace("management") endpoint_args = api.model("del_rule", { "rule_id": fields.Integer(required=True, example=1, min=1) }) @api.route("/rules/delete") class ManagementDeleteRule(Resource): @api.expect(endpoint_args, validate=True) @api.doc(description="Delete an already existing rule from the database") @RULES_DELETE_REQUEST_DURATION_SECONDS.time() def post(self): rule_id = request.get_json()["rule_id"] db_session = DatabaseManager().get_session()
If the Redirect Rule is new then it will be added to the database and a serialized JSON of the new Redirect Rule instance will be returned. """ from flask import make_response, jsonify, request from flask_restplus import Resource, fields from redirectory.models import RedirectRule from redirectory.libs_int.metrics import REQUESTS_DURATION_SECONDS, metric_update_rules_total from redirectory.libs_int.database import DatabaseManager, db_encode_model, add_redirect_rule from redirectory.libs_int.service import NamespaceManager, api_error # Metrics RULES_ADD_REQUEST_DURATION_SECONDS = REQUESTS_DURATION_SECONDS.labels("management") # Api Namespace api = NamespaceManager().get_namespace("management") endpoint_args = api.model("add_rule", { "domain": fields.String(required=True, example="test.com"), "domain_is_regex": fields.Boolean(required=True, example=False, default=False), "path": fields.String(required=True, example="/test/path"), "path_is_regex": fields.Boolean(required=True, example=False, default=False), "destination": fields.String(required=True, example="newtest.com/test/newpath"), "destination_is_rewrite": fields.Boolean(required=True, example=False, default=False), "weight": fields.Integer(required=True, example=100) }) @api.route("/rules/add") class ManagementAddRule(Resource):
- 200: The ambiguous entry has been added - 400: An ambiguous entry like this already exists The Add Ambiguous endpoint provides the ability to add an ambiguous entry to the sqlite database. """ from flask import make_response, jsonify, request from flask_restplus import Resource, fields from redirectory.libs_int.metrics import REQUESTS_DURATION_SECONDS from redirectory.libs_int.database import DatabaseManager, add_ambiguous_request, db_encode_model from redirectory.libs_int.service import NamespaceManager, api_error AMBIGUOUS_ADD_REQUEST_DURATION_SECONDS = REQUESTS_DURATION_SECONDS.labels( "management") api = NamespaceManager().get_namespace("management") endpoint_args = api.model("add_ambiguous", { "request": fields.String(required=True, example="https://example.com/request") }) @api.route("/ambiguous/add") class ManagementAddAmbiguous(Resource): @api.expect(endpoint_args, validate=True) @api.doc(description="Add a new ambiguous request entry to the database") @AMBIGUOUS_ADD_REQUEST_DURATION_SECONDS.time() def post(self): new_request_url = request.get_json()["request"] db_session = DatabaseManager().get_session()
from flask import make_response, jsonify, request from flask_restplus import Resource, fields from kubi_ecs_logger import Logger, Severity from redirectory.models import DomainRule, RedirectRule from redirectory.libs_int.metrics import REQUESTS_DURATION_SECONDS from redirectory.libs_int.hyperscan import HsManager from redirectory.libs_int.service import NamespaceManager, api_error from redirectory.libs_int.database import DatabaseManager, get_model_by_id, db_encode_model # Metrics RULES_TEST_REQUEST_DURATION_SECONDS = REQUESTS_DURATION_SECONDS.labels( "management") # Api Namespace api = NamespaceManager().get_namespace("management") endpoint_args = api.model( "test_request", { "request_url": fields.String(required=True, example="https://example.com/test/path"), }) @api.route("/rules/test") class ManagementTestRequest(Resource): @api.expect(endpoint_args, validate=True) @api.doc( description="Test a request. What would be the response from the system." ) @RULES_TEST_REQUEST_DURATION_SECONDS.time()
the current Hyperscan DB Version that the HsManager() has loaded and is using to run queries. If the worker still has not Hyperscan DB loaded then a 400 is returned. """ from flask import make_response, jsonify from flask_restplus import Resource from redirectory.libs_int.metrics import REQUESTS_TOTAL, REQUESTS_DURATION_SECONDS from redirectory.libs_int.hyperscan import HsManager from redirectory.libs_int.service import NamespaceManager, api_error # Metrics WORKER_GET_HS_DB_VERSION_REQUEST_DURATION_SECONDS = REQUESTS_DURATION_SECONDS.labels( "worker") # Api Namespace api = NamespaceManager().get_namespace("worker") @api.route("/get_hs_db_version") class WorkerGetHsDbVersion(Resource): @api.doc( description= "Get the current hyperscan database version that the worker is using") @WORKER_GET_HS_DB_VERSION_REQUEST_DURATION_SECONDS.time() def get(self): hs_db_version = HsManager().database.db_version if hs_db_version: return make_response(jsonify({"hs_db_version": hs_db_version})) else: return api_error(message="Worker has no Hyperscan Database loaded", errors=[],