Commit 4c99980b authored by Carlos Natalino's avatar Carlos Natalino
Browse files

Intermediate commit to save current state.

parent 053e0177
Loading
Loading
Loading
Loading
+13 −1
Original line number Diff line number Diff line
import logging

# General settings
LOG_LEVEL = logging.WARNING
LOG_LEVEL = logging.DEBUG

# gRPC settings
GRPC_SERVICE_PORT = 10005
GRPC_MAX_WORKERS  = 10
GRPC_GRACE_PERIOD = 60

# service settings
MONITORING_INTERVAL = 2  # monitoring interval in seconds
#TODO: adjust the addresses below for the specific case
# MONITORING_SERVICE_ADDRESS = 'monitoring'  # address/name of the monitoring service
MONITORING_SERVICE_ADDRESS = 'localhost'  # address/name of the monitoring service
# CONTEXT_SERVICE_ADDRESS = 'context'  # address/name of the monitoring service
CONTEXT_SERVICE_ADDRESS = '10.103.238.143'  # address/name of the monitoring service
# SERVICE_SERVICE_ADDRESS = 'service'  # address/name of the service service
SERVICE_SERVICE_ADDRESS = '10.98.1.146'  # address/name of the service service
# INFERENCE_SERVICE_ADDRESS = 'dbscanserving'  # address/name of the inference service
INFERENCE_SERVICE_ADDRESS = 'localhost'  # address/name of the inference service

# Prometheus settings
METRICS_PORT = 9192
+3 −0
Original line number Diff line number Diff line
@@ -29,6 +29,9 @@ RUN python3 -m pip install -r opticalcentralizedattackdetector/requirements.txt

# Add files into working directory
COPY common/. common
COPY context/. context
COPY monitoring/. monitoring
COPY service/. service
COPY opticalcentralizedattackdetector/. opticalcentralizedattackdetector

# Start opticalcentralizedattackdetector service
+53 −2
Original line number Diff line number Diff line
import logging, signal, sys, threading
import logging, signal, sys, time, threading, multiprocessing
from prometheus_client import start_http_server
from common.Settings import get_setting
from opticalcentralizedattackdetector.Config import (
    GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD, LOG_LEVEL, METRICS_PORT)
    GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD, LOG_LEVEL, METRICS_PORT,
    MONITORING_INTERVAL, CONTEXT_SERVICE_ADDRESS, SERVICE_SERVICE_ADDRESS, INFERENCE_SERVICE_ADDRESS, MONITORING_SERVICE_ADDRESS)
from context.Config import GRPC_SERVICE_PORT as CONTEXT_GRPC_SERVICE_PORT
from context.client.ContextClient import ContextClient
from opticalcentralizedattackdetector.proto.context_pb2 import (Empty,
    Context,  ContextId,  ContextIdList,  ContextList,
    Service,  ServiceId,  ServiceIdList,  ServiceList
)
# from monitoring.Config import GRPC_SERVICE_PORT as MONITORING_GRPC_SERVICE_PORT
# from monitoring.client.monitoring_client import MonitoringClient
from service.Config import GRPC_SERVICE_PORT as SERVICE_GRPC_SERVICE_PORT
from service.client.ServiceClient import ServiceClient
from opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorService import OpticalCentralizedAttackDetectorService

terminate = threading.Event()
@@ -12,6 +23,40 @@ def signal_handler(signal, frame): # pylint: disable=redefined-outer-name
    LOGGER.warning('Terminate signal received')
    terminate.set()

def detect_attack(monitoring_interval):
    LOGGER.info("Starting the attack detection loop")
    context_client: ContextClient = ContextClient(address=CONTEXT_SERVICE_ADDRESS, port=CONTEXT_GRPC_SERVICE_PORT)
    # monitoring_client: MonitoringClient = MonitoringClient(address=MONITORING_SERVICE_ADDRESS, port=MONITORING_GRPC_SERVICE_PORT)
    service_client: ServiceClient = ServiceClient(address=SERVICE_SERVICE_ADDRESS, port=SERVICE_GRPC_SERVICE_PORT)
    while True:  # infinite loop that runs until the terminate is set
        if terminate.is_set():  # if terminate is set
            LOGGER.warning("Stopping execution...")
            context_client.close()
            service_client.close()
            break  # break the while and stop execution
        
        # retrieve list with current contexts
        # import pdb; pdb.set_trace()
        context_ids: ContextIdList = context_client.ListContextIds(Empty())

        # for each context, retrieve list of current services
        service_uuids = []
        for context_id in context_ids.context_ids:
            LOGGER.warning(context_id.context_uuid.uuid)
            service_ids: ServiceIdList = context_client.ListServiceIds(context_id)
            for service_id in service_ids.service_ids:
                service_uuids.append((service_id.context_id.context_uuid.uuid, service_id.service_uuid.uuid))

        # get monitoring data for each of the current services

        # run attack detection for every service

        # if attack is detected, run the attack mitigator

        # sleep
        LOGGER.debug("Sleeping for {} seconds...".format(monitoring_interval))
        time.sleep(monitoring_interval)

def main():
    global LOGGER # pylint: disable=global-statement

@@ -20,6 +65,7 @@ def main():
    grace_period = get_setting('GRACE_PERIOD',                                              default=GRPC_GRACE_PERIOD)
    log_level    = get_setting('LOG_LEVEL',                                                 default=LOG_LEVEL        )
    metrics_port = get_setting('METRICS_PORT',                                              default=METRICS_PORT     )
    monitoring_interval = get_setting('MONITORING_INTERVAL',                                              default=MONITORING_INTERVAL     )

    logging.basicConfig(level=log_level)
    LOGGER = logging.getLogger(__name__)
@@ -37,11 +83,16 @@ def main():
        port=service_port, max_workers=max_workers, grace_period=grace_period)
    grpc_service.start()

    # p = multiprocessing.Process(target=detect_attack, args=(monitoring_interval, ))
    # p.start()
    detect_attack(monitoring_interval)

    # Wait for Ctrl+C or termination signal
    while not terminate.wait(timeout=0.1): pass

    LOGGER.info('Terminating...')
    grpc_service.stop()
    # p.kill()

    LOGGER.info('Bye')
    return 0