Beispiel #1
0
 def handle_config_notify(self, message: Dict) -> NoReturn:
     """
     Consumer for Config-Notify messages that come
     from the configuration service.
     Upon arrival this service updates its running configuration.
     """
     message.ack()
     log.debug("message: {}\npayload: {}".format(message, message.payload))
     signal_loading("detection", True)
     try:
         raw = message.payload
         if raw["timestamp"] > self.timestamp:
             self.timestamp = raw["timestamp"]
             self.rules = raw.get("rules", [])
             self.init_detection()
             # Request ongoing hijacks from DB
             self.producer.publish(
                 self.timestamp,
                 exchange=self.hijack_exchange,
                 routing_key="ongoing-request",
                 priority=1,
                 serializer="ujson",
             )
     except Exception:
         log.exception("Exception")
     finally:
         signal_loading("detection", False)
Beispiel #2
0
        def __init__(self, connection):
            self.connection = connection
            self.prefix_tree = None
            self.autoignore_rules = None
            # https: // docs.celeryproject.org / projects / kombu / en / stable / reference / kombu.asynchronous.timer.html
            # https://docs.celeryproject.org/projects/kombu/en/stable/_modules/kombu/asynchronous/timer.html#Timer
            self.rule_timer_threads = {}
            self.timestamp = -1

            # DB variables
            self.ro_db = DB(
                application_name="autoignore-readonly",
                user=DB_USER,
                password=DB_PASS,
                host=DB_HOST,
                port=DB_PORT,
                database=DB_NAME,
                reconnect=True,
                autocommit=True,
                readonly=True,
            )
            self.wo_db = DB(
                application_name="autoignore-write",
                user=DB_USER,
                password=DB_PASS,
                host=DB_HOST,
                port=DB_PORT,
                database=DB_NAME,
            )

            # EXCHANGES
            self.config_exchange = Exchange("config",
                                            type="direct",
                                            durable=False,
                                            delivery_mode=1)

            # QUEUES
            self.config_queue = Queue(
                "autoignore-config-notify",
                exchange=self.config_exchange,
                routing_key="notify",
                durable=False,
                auto_delete=True,
                max_priority=3,
                consumer_arguments={"x-priority": 3},
            )

            # redis db
            self.redis = redis.Redis(host=REDIS_HOST, port=REDIS_PORT)
            ping_redis(self.redis)

            signal_loading("autoignore", True)
            self.config_request_rpc()
            signal_loading("autoignore", False)
Beispiel #3
0
 def handle_config_request_reply(self, message):
     message.ack()
     signal_loading("autoignore", True)
     log.debug("message: {}\npayload: {}".format(
         message, message.payload))
     if self.correlation_id == message.properties["correlation_id"]:
         config = message.payload
         if config["timestamp"] > self.timestamp:
             self.timestamp = config["timestamp"]
             self.autoignore_rules = config.get("autoignore", [])
             self.build_prefix_tree()
             self.set_rule_timers()
     signal_loading("autoignore", False)
Beispiel #4
0
 def __init__(self, d, fn, connection):
     super().__init__()
     self.connection = connection
     self.correlation_id = None
     signal_loading("observer", True)
     self.response = None
     self.path = "{}/{}".format(d, fn)
     try:
         with open(self.path, "r") as f:
             self.content = f.readlines()
     except Exception:
         log.exception("exception")
     finally:
         signal_loading("observer", False)
Beispiel #5
0
 def handle_config_notify(self, message):
     message.ack()
     log.debug("message: {}\npayload: {}".format(
         message, message.payload))
     signal_loading("mitigation", True)
     try:
         raw = message.payload
         if raw["timestamp"] > self.timestamp:
             self.timestamp = raw["timestamp"]
             self.rules = raw.get("rules", [])
             self.init_mitigation()
     except Exception:
         log.exception("Exception")
     finally:
         signal_loading("mitigation", False)
Beispiel #6
0
 def handle_config_notify(self, message):
     message.ack()
     log.debug("message: {}\npayload: {}".format(
         message, message.payload))
     signal_loading("autoignore", True)
     try:
         config = message.payload
         if config["timestamp"] > self.timestamp:
             self.timestamp = config["timestamp"]
             self.autoignore_rules = config.get("autoignore", {})
             self.build_prefix_tree()
             self.set_rule_timers()
     except Exception:
         log.exception("Exception")
     finally:
         signal_loading("autoignore", False)
Beispiel #7
0
        def __init__(self, connection):
            self.connection = connection
            self.timestamp = -1
            self.rules = None
            self.prefix_tree = None
            self.correlation_id = None

            # EXCHANGES
            self.mitigation_exchange = Exchange(
                "mitigation",
                channel=connection,
                type="direct",
                durable=False,
                delivery_mode=1,
            )
            self.mitigation_exchange.declare()
            self.config_exchange = Exchange("config",
                                            type="direct",
                                            durable=False,
                                            delivery_mode=1)

            # QUEUES
            self.config_queue = Queue(
                "mitigation-config-notify",
                exchange=self.config_exchange,
                routing_key="notify",
                durable=False,
                auto_delete=True,
                max_priority=3,
                consumer_arguments={"x-priority": 3},
            )
            self.mitigate_queue = Queue(
                "mitigation-mitigate",
                exchange=self.mitigation_exchange,
                routing_key="mitigate",
                durable=False,
                auto_delete=True,
                max_priority=2,
                consumer_arguments={"x-priority": 2},
            )

            signal_loading("mitigation", True)
            self.config_request_rpc()
            signal_loading("mitigation", False)
Beispiel #8
0
        def __init__(self, connection):
            self.connection = connection
            # Time in secs to gather entries to perform a bulk operation
            self.time_to_wait = float(os.getenv("BULK_TIMER", 1))
            self.correlation_id = None

            self.db_clock_exchange = Exchange(
                "db-clock",
                type="direct",
                channel=connection,
                durable=False,
                delivery_mode=1,
            )
            self.db_clock_exchange.declare()

            signal_loading("clock", True)
            log.info("started")
            signal_loading("clock", False)
            self._db_clock_send()
Beispiel #9
0
        def __init__(self, connection):
            self.connection = connection
            self.timestamp = -1
            self.prefix_tree = None
            self.process_ids = []
            self.rules = None
            self.prefixes = set()
            self.prefix_file = "/root/monitor_prefixes.json"
            self.monitors = None
            self.flag = True
            self.redis = redis.Redis(host=REDIS_HOST, port=REDIS_PORT)
            ping_redis(self.redis)
            self.correlation_id = None

            # EXCHANGES
            self.config_exchange = Exchange("config",
                                            type="direct",
                                            durable=False,
                                            delivery_mode=1)

            # QUEUES
            self.config_queue = Queue(
                "monitor-config-notify",
                exchange=self.config_exchange,
                routing_key="notify",
                durable=False,
                auto_delete=True,
                max_priority=2,
                consumer_arguments={"x-priority": 2},
            )

            signal_loading("monitor", True)
            self.config_request_rpc()

            # setup Redis monitor listeners
            self.setup_redis_mon_listeners()

            log.info("started")
            signal_loading("monitor", False)
Beispiel #10
0
        def __init__(self, connection: Connection) -> NoReturn:
            self.connection = connection
            self.timestamp = -1
            self.rules = None
            self.prefix_tree = None
            self.mon_num = 1
            self.correlation_id = None

            # EXCHANGES
            self.update_exchange = Exchange(
                "bgp-update",
                channel=connection,
                type="direct",
                durable=False,
                delivery_mode=1,
            )
            self.update_exchange.declare()

            self.hijack_exchange = Exchange(
                "hijack-update",
                channel=connection,
                type="direct",
                durable=False,
                delivery_mode=1,
            )
            self.hijack_exchange.declare()

            self.hijack_hashing = Exchange(
                "hijack-hashing",
                channel=connection,
                type="x-consistent-hash",
                durable=False,
                delivery_mode=1,
            )
            self.hijack_hashing.declare()

            self.handled_exchange = Exchange(
                "handled-update",
                channel=connection,
                type="direct",
                durable=False,
                delivery_mode=1,
            )

            self.config_exchange = Exchange(
                "config",
                channel=connection,
                type="direct",
                durable=False,
                delivery_mode=1,
            )

            self.pg_amq_bridge = Exchange(
                "amq.direct", type="direct", durable=True, delivery_mode=1
            )

            # QUEUES
            self.update_queue = Queue(
                "detection-update-update",
                exchange=self.pg_amq_bridge,
                routing_key="update-insert",
                durable=False,
                auto_delete=True,
                max_priority=1,
                consumer_arguments={"x-priority": 1},
            )
            self.hijack_ongoing_queue = Queue(
                "detection-hijack-ongoing",
                exchange=self.hijack_exchange,
                routing_key="ongoing",
                durable=False,
                auto_delete=True,
                max_priority=1,
                consumer_arguments={"x-priority": 1},
            )
            self.config_queue = Queue(
                "detection-config-notify-{}".format(uuid()),
                exchange=self.config_exchange,
                routing_key="notify",
                durable=False,
                auto_delete=True,
                max_priority=3,
                consumer_arguments={"x-priority": 3},
            )

            signal_loading("detection", True)

            setattr(self, "publish_hijack_fun", self.publish_hijack_result_production)
            if TEST_ENV == "true":
                setattr(self, "publish_hijack_fun", self.publish_hijack_result_test)

            self.redis = redis.Redis(host=REDIS_HOST, port=REDIS_PORT)
            ping_redis(self.redis)

            self.rtrmanager = None
            if RPKI_VALIDATOR_ENABLED == "true":
                from rtrlib import RTRManager

                while True:
                    try:
                        self.rtrmanager = RTRManager(
                            RPKI_VALIDATOR_HOST, RPKI_VALIDATOR_PORT
                        )
                        self.rtrmanager.start()
                        log.info(
                            "Connected to RPKI VALIDATOR '{}:{}'".format(
                                RPKI_VALIDATOR_HOST, RPKI_VALIDATOR_PORT
                            )
                        )
                        break
                    except Exception:
                        log.info(
                            "Could not connect to RPKI VALIDATOR '{}:{}'".format(
                                RPKI_VALIDATOR_HOST, RPKI_VALIDATOR_PORT
                            )
                        )
                        log.info("Retrying RTR connection in 30 seconds...")
                        time.sleep(30)

            self.config_request_rpc()
            log.info("started")
            signal_loading("detection", False)