diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index ffbf9e9f5f02bd0514d4584f6f2ad32761b6264f..9a998c644ccf1fec223d3d14ab6eb8bcfc1244d4 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -32,7 +32,7 @@ include: - local: '/src/service/.gitlab-ci.yml' - local: '/src/dbscanserving/.gitlab-ci.yml' - local: '/src/opticalattackmitigator/.gitlab-ci.yml' - - local: '/src/opticalcentralizedattackdetector/.gitlab-ci.yml' + - local: '/src/opticalattackdetector/.gitlab-ci.yml' - local: '/src/automation/.gitlab-ci.yml' - local: '/src/policy/.gitlab-ci.yml' - local: '/src/webui/.gitlab-ci.yml' diff --git a/proto/optical_centralized_attack_detector.proto b/proto/optical_attack_detector.proto similarity index 93% rename from proto/optical_centralized_attack_detector.proto rename to proto/optical_attack_detector.proto index d480054f2ca3b1cf32cbb40c51df51c562f9b4d5..9e4176f12a378651a6793e4e852e196b4b4b31b4 100644 --- a/proto/optical_centralized_attack_detector.proto +++ b/proto/optical_attack_detector.proto @@ -14,12 +14,12 @@ // protocol buffers documentation: https://developers.google.com/protocol-buffers/docs/proto3 syntax = "proto3"; -package centralized_attack_detector; +package optical_attack_detector; import "context.proto"; import "monitoring.proto"; -service OpticalCentralizedAttackDetectorService { +service OpticalAttackDetectorService { rpc NotifyServiceUpdate (context.Service ) returns (context.Empty) {} // rpc that triggers the attack detection loop diff --git a/src/opticalattackdetector/.gitlab-ci.yml b/src/opticalattackdetector/.gitlab-ci.yml index c3d91aec6e10c7450cfda0b74d0bc3bbe613558b..8382edb40e5cf6b57ec6e2fb0ff99a6630bb7dac 100644 --- a/src/opticalattackdetector/.gitlab-ci.yml +++ b/src/opticalattackdetector/.gitlab-ci.yml @@ -51,10 +51,10 @@ unit test opticalcentralizedattackdetector: - if docker container ls | grep $IMAGE_NAME; then docker rm -f $IMAGE_NAME; else echo "$IMAGE_NAME image is not in the system"; fi script: - docker pull "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG" - - docker run --name $IMAGE_NAME -d -p 10005:10005 -v "$PWD/src/$IMAGE_NAME/tests:/opt/results" --network=teraflowbridge --rm $CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG + - docker run --name $IMAGE_NAME -d -p 10005:10005 -v "$PWD/src/$IMAGE_NAME/tests:/home/${IMAGE_NAME}/results" --network=teraflowbridge --rm $CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG - sleep 5 - docker ps -a - - docker exec -i $IMAGE_NAME bash -c "coverage run -m pytest --log-level=INFO --verbose $IMAGE_NAME/tests/test_unitary.py --junitxml=/opt/results/${IMAGE_NAME}_report.xml" + - docker exec -i $IMAGE_NAME bash -c "coverage run -m pytest --log-level=INFO --verbose $IMAGE_NAME/tests/test_unitary.py --junitxml=/home/${IMAGE_NAME}/results/${IMAGE_NAME}_report.xml" - docker exec -i $IMAGE_NAME bash -c "coverage report --include='${IMAGE_NAME}/*' --show-missing" coverage: '/TOTAL\s+\d+\s+\d+\s+(\d+%)/' after_script: diff --git a/src/opticalattackdetector/Dockerfile b/src/opticalattackdetector/Dockerfile index 98fc664b2906cde5b35b5e6be6ccdc544700a1a3..0b5adc3c5e322c146b510d495852a972594f215a 100644 --- a/src/opticalattackdetector/Dockerfile +++ b/src/opticalattackdetector/Dockerfile @@ -21,6 +21,7 @@ RUN apt-get --yes --quiet --quiet update && \ # Set Python to show logs as they occur ENV PYTHONUNBUFFERED=0 +ENV PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python # Download the gRPC health probe RUN GRPC_HEALTH_PROBE_VERSION=v0.2.0 && \ @@ -71,18 +72,16 @@ WORKDIR /home/opticalattackdetector/teraflow # Get Python packages per module COPY --chown=opticalattackdetector:opticalattackdetector src/opticalattackdetector/requirements.in opticalattackdetector/requirements.in -RUN pip-compile --output-file=opticalattackdetector/requirements.txt opticalattackdetector/requirements.in +RUN pip-compile --quiet --output-file=opticalattackdetector/requirements.txt opticalattackdetector/requirements.in RUN python3 -m pip install -r opticalattackdetector/requirements.txt # Add files into working directory -COPY src/context/. context -COPY src/monitoring/. monitoring -COPY src/service/. service -COPY src/dbscanserving/. dbscanserving -COPY src/opticalattackmitigator/. opticalattackmitigator -COPY src/opticalattackdetector/. opticalattackdetector - -ENV PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python +COPY --chown=opticalattackdetector:opticalattackdetector src/context/. context +COPY --chown=opticalattackdetector:opticalattackdetector src/monitoring/. monitoring +COPY --chown=opticalattackdetector:opticalattackdetector src/service/. service +COPY --chown=opticalattackdetector:opticalattackdetector src/dbscanserving/. dbscanserving +COPY --chown=opticalattackdetector:opticalattackdetector src/opticalattackmitigator/. opticalattackmitigator +COPY --chown=opticalattackdetector:opticalattackdetector src/opticalattackdetector/. opticalattackdetector # Start opticalattackdetector service ENTRYPOINT ["python", "-m", "opticalattackdetector.service"] diff --git a/src/opticalattackdetector/client/OpticalCentralizedAttackDetectorClient.py b/src/opticalattackdetector/client/OpticalAttackDetectorClient.py similarity index 86% rename from src/opticalattackdetector/client/OpticalCentralizedAttackDetectorClient.py rename to src/opticalattackdetector/client/OpticalAttackDetectorClient.py index cfb78b41ec6a6d5385692813e1a839566d1c90e7..846bcce6e5a77d14ca1735a45b456e8c3865bc5b 100644 --- a/src/opticalattackdetector/client/OpticalCentralizedAttackDetectorClient.py +++ b/src/opticalattackdetector/client/OpticalAttackDetectorClient.py @@ -14,16 +14,16 @@ import grpc, logging from common.tools.client.RetryDecorator import retry, delay_exponential -from opticalcentralizedattackdetector.proto.context_pb2 import Empty, Service -from opticalcentralizedattackdetector.proto.monitoring_pb2 import KpiList -from opticalcentralizedattackdetector.proto.optical_centralized_attack_detector_pb2_grpc import OpticalCentralizedAttackDetectorServiceStub +from common.proto.context_pb2 import Empty, Service +from common.proto.monitoring_pb2 import KpiList +from common.proto.optical_attack_detector_pb2_grpc import OpticalAttackDetectorServiceStub LOGGER = logging.getLogger(__name__) MAX_RETRIES = 15 DELAY_FUNCTION = delay_exponential(initial=0.01, increment=2.0, maximum=5.0) RETRY_DECORATOR = retry(max_retries=MAX_RETRIES, delay_function=DELAY_FUNCTION, prepare_method_name='connect') -class OpticalCentralizedAttackDetectorClient: +class OpticalAttackDetectorClient: def __init__(self, address, port): self.endpoint = '{:s}:{:s}'.format(str(address), str(port)) LOGGER.debug('Creating channel to {:s}...'.format(str(self.endpoint))) @@ -34,7 +34,7 @@ class OpticalCentralizedAttackDetectorClient: def connect(self): self.channel = grpc.insecure_channel(self.endpoint) - self.stub = OpticalCentralizedAttackDetectorServiceStub(self.channel) + self.stub = OpticalAttackDetectorServiceStub(self.channel) def close(self): if(self.channel is not None): self.channel.close() diff --git a/src/opticalattackdetector/service/OpticalCentralizedAttackDetectorService.py b/src/opticalattackdetector/service/OpticalAttackDetectorService.py similarity index 77% rename from src/opticalattackdetector/service/OpticalCentralizedAttackDetectorService.py rename to src/opticalattackdetector/service/OpticalAttackDetectorService.py index e29566833a781afc8bf85b41b7d5dd10f2a4a2f5..357ef2ab8eef00ce894808634be7eca85f26b22d 100644 --- a/src/opticalattackdetector/service/OpticalCentralizedAttackDetectorService.py +++ b/src/opticalattackdetector/service/OpticalAttackDetectorService.py @@ -18,16 +18,16 @@ from concurrent import futures from grpc_health.v1.health import HealthServicer, OVERALL_HEALTH from grpc_health.v1.health_pb2 import HealthCheckResponse from grpc_health.v1.health_pb2_grpc import add_HealthServicer_to_server -from opticalcentralizedattackdetector.proto.optical_centralized_attack_detector_pb2_grpc import ( - add_OpticalCentralizedAttackDetectorServiceServicer_to_server) -from opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl import ( - OpticalCentralizedAttackDetectorServiceServicerImpl) -from opticalcentralizedattackdetector.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD +from common.proto.optical_attack_detector_pb2_grpc import ( + add_OpticalAttackDetectorServiceServicer_to_server) +from opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl import ( + OpticalAttackDetectorServiceServicerImpl) +from opticalattackdetector.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD BIND_ADDRESS = '0.0.0.0' LOGGER = logging.getLogger(__name__) -class OpticalCentralizedAttackDetectorService: +class OpticalAttackDetectorService: def __init__( self, address=BIND_ADDRESS, port=GRPC_SERVICE_PORT, max_workers=GRPC_MAX_WORKERS, grace_period=GRPC_GRACE_PERIOD): @@ -37,7 +37,7 @@ class OpticalCentralizedAttackDetectorService: self.endpoint = None self.max_workers = max_workers self.grace_period = grace_period - self.centralized_attack_detector_servicer = None + self.attack_detector_servicer = None self.health_servicer = None self.pool = None self.server = None @@ -50,8 +50,8 @@ class OpticalCentralizedAttackDetectorService: self.pool = futures.ThreadPoolExecutor(max_workers=self.max_workers) self.server = grpc.server(self.pool) # , interceptors=(tracer_interceptor,)) - self.centralized_attack_detector_servicer = OpticalCentralizedAttackDetectorServiceServicerImpl() - add_OpticalCentralizedAttackDetectorServiceServicer_to_server(self.centralized_attack_detector_servicer, self.server) + self.attack_detector_servicer = OpticalAttackDetectorServiceServicerImpl() + add_OpticalAttackDetectorServiceServicer_to_server(self.attack_detector_servicer, self.server) self.health_servicer = HealthServicer( experimental_non_blocking=True, experimental_thread_pool=futures.ThreadPoolExecutor(max_workers=1)) diff --git a/src/opticalattackdetector/service/OpticalCentralizedAttackDetectorServiceServicerImpl.py b/src/opticalattackdetector/service/OpticalAttackDetectorServiceServicerImpl.py similarity index 89% rename from src/opticalattackdetector/service/OpticalCentralizedAttackDetectorServiceServicerImpl.py rename to src/opticalattackdetector/service/OpticalAttackDetectorServiceServicerImpl.py index d4c71476f016081f7d230a3cfe87e73b35654987..d6fd1787bf93222457eb44f3bb7ee7d649c776c7 100644 --- a/src/opticalattackdetector/service/OpticalCentralizedAttackDetectorServiceServicerImpl.py +++ b/src/opticalattackdetector/service/OpticalAttackDetectorServiceServicerImpl.py @@ -18,26 +18,26 @@ from common.rpc_method_wrapper.Decorator import create_metrics, safe_and_metered from context.client.ContextClient import ContextClient from monitoring.client.MonitoringClient import MonitoringClient from service.client.ServiceClient import ServiceClient -from dbscanserving.proto.dbscanserving_pb2 import DetectionRequest, DetectionResponse, Sample +from common.proto.dbscanserving_pb2 import DetectionRequest, DetectionResponse, Sample from dbscanserving.client.DbscanServingClient import DbscanServingClient from dbscanserving.Config import GRPC_SERVICE_PORT as DBSCANSERVING_GRPC_SERVICE_PORT from opticalattackmitigator.client.OpticalAttackMitigatorClient import OpticalAttackMitigatorClient -from opticalattackmitigator.proto.optical_attack_mitigator_pb2 import AttackDescription, AttackResponse +from common.proto.optical_attack_mitigator_pb2 import AttackDescription, AttackResponse from opticalattackmitigator.Config import GRPC_SERVICE_PORT as ATTACK_MITIGATOR_GRPC_SERVICE_PORT -from opticalcentralizedattackdetector.proto.context_pb2 import (Empty, +from common.proto.context_pb2 import (Empty, Context, ContextId, ContextIdList, ContextList, Service, ServiceId, ServiceIdList, ServiceList ) -from opticalcentralizedattackdetector.proto.monitoring_pb2 import KpiList -from opticalcentralizedattackdetector.proto.optical_centralized_attack_detector_pb2_grpc import ( - OpticalCentralizedAttackDetectorServiceServicer) -from opticalcentralizedattackdetector.Config import ( +from common.proto.monitoring_pb2 import KpiList +from common.proto.optical_attack_detector_pb2_grpc import ( + OpticalAttackDetectorServiceServicer) +from opticalattackdetector.Config import ( INFERENCE_SERVICE_ADDRESS, MONITORING_SERVICE_ADDRESS, ATTACK_MITIGATOR_SERVICE_ADDRESS) LOGGER = logging.getLogger(__name__) -SERVICE_NAME = 'OpticalCentralizedAttackDetector' +SERVICE_NAME = 'OpticalAttackDetector' METHOD_NAMES = ['NotifyServiceUpdate', 'DetectAttack', 'ReportSummarizedKpi', 'ReportKpi'] METRICS = create_metrics(SERVICE_NAME, METHOD_NAMES) @@ -57,7 +57,7 @@ attack_mitigator_client: OpticalAttackMitigatorClient = OpticalAttackMitigatorCl address=ATTACK_MITIGATOR_SERVICE_ADDRESS, port=ATTACK_MITIGATOR_GRPC_SERVICE_PORT) -class OpticalCentralizedAttackDetectorServiceServicerImpl(OpticalCentralizedAttackDetectorServiceServicer): +class OpticalAttackDetectorServiceServicerImpl(OpticalAttackDetectorServiceServicer): def __init__(self): LOGGER.debug('Creating Servicer...') diff --git a/src/opticalattackdetector/service/__main__.py b/src/opticalattackdetector/service/__main__.py index d21b96cf8adc6f9d9968f3553c9249a3b8cedf31..c57a75e8dc47f1a23b387492054cee83fbfb3189 100644 --- a/src/opticalattackdetector/service/__main__.py +++ b/src/opticalattackdetector/service/__main__.py @@ -15,21 +15,20 @@ import os, 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, - MONITORING_INTERVAL) -from opticalcentralizedattackdetector.proto.context_pb2 import (Empty, +from common.Settings import get_log_level, get_metrics_port, get_setting +from opticalattackdetector.Config import ( + GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD, MONITORING_INTERVAL) +from common.proto.context_pb2 import (Empty, Context, ContextId, ContextIdList, ContextList, Service, ServiceId, ServiceIdList, ServiceList ) -from opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorService import OpticalCentralizedAttackDetectorService -from opticalcentralizedattackdetector.client.OpticalCentralizedAttackDetectorClient import OpticalCentralizedAttackDetectorClient +from opticalattackdetector.service.OpticalAttackDetectorService import OpticalAttackDetectorService +from opticalattackdetector.client.OpticalAttackDetectorClient import OpticalAttackDetectorClient terminate = threading.Event() LOGGER = None -client: OpticalCentralizedAttackDetectorClient = None +client: OpticalAttackDetectorClient = None def signal_handler(signal, frame): # pylint: disable=redefined-outer-name LOGGER.warning('Terminate signal received') @@ -38,7 +37,7 @@ def signal_handler(signal, frame): # pylint: disable=redefined-outer-name def detect_attack(monitoring_interval): time.sleep(10) # wait for the service to start LOGGER.info("Starting the attack detection loop") - client = OpticalCentralizedAttackDetectorClient(address='localhost', port=GRPC_SERVICE_PORT) + client = OpticalAttackDetectorClient(address='localhost', port=GRPC_SERVICE_PORT) client.connect() while True: # infinite loop that runs until the terminate is set if terminate.is_set(): # if terminate is set @@ -53,26 +52,26 @@ def detect_attack(monitoring_interval): def main(): global LOGGER # pylint: disable=global-statement - service_port = get_setting('OPTICALCENTRALIZEDATTACKDETECTORSERVICE_SERVICE_PORT_GRPC', default=GRPC_SERVICE_PORT) - max_workers = get_setting('MAX_WORKERS', default=GRPC_MAX_WORKERS ) - 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 ) - + log_level = get_log_level() logging.basicConfig(level=log_level) LOGGER = logging.getLogger(__name__) + service_port = get_setting('OPTICALATTACKDETECTORSERVICE_SERVICE_PORT_GRPC', default=GRPC_SERVICE_PORT) + max_workers = get_setting('MAX_WORKERS', default=GRPC_MAX_WORKERS ) + grace_period = get_setting('GRACE_PERIOD', default=GRPC_GRACE_PERIOD) + monitoring_interval = get_setting('MONITORING_INTERVAL', default=MONITORING_INTERVAL ) + signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) LOGGER.info('Starting...') # Start metrics server + metrics_port = get_metrics_port() start_http_server(metrics_port) # Starting CentralizedCybersecurity service - grpc_service = OpticalCentralizedAttackDetectorService( + grpc_service = OpticalAttackDetectorService( port=service_port, max_workers=max_workers, grace_period=grace_period) grpc_service.start() diff --git a/src/opticalattackdetector/tests/example_objects.py b/src/opticalattackdetector/tests/example_objects.py index 3c5a26b6d0bde888560741f052906e0d2694c91d..642cba009e6dba5d7609c3941b0b43f3d20e27d4 100644 --- a/src/opticalattackdetector/tests/example_objects.py +++ b/src/opticalattackdetector/tests/example_objects.py @@ -14,14 +14,14 @@ from copy import deepcopy from common.Constants import DEFAULT_CONTEXT_UUID, DEFAULT_TOPOLOGY_UUID -from context.proto.context_pb2 import ( +from common.proto.context_pb2 import ( ConfigActionEnum, DeviceDriverEnum, DeviceOperationalStatusEnum, ServiceStatusEnum, ServiceTypeEnum) # Some example objects to be used by the tests # Helper methods -def config_rule(action, resource_key, resource_value): - return {'action': action, 'resource_key': resource_key, 'resource_value': resource_value} +def config_rule(action, resource_value): + return {'action': action, 'resource_value': resource_value} def endpoint_id(topology_id, device_id, endpoint_uuid): return {'topology_id': deepcopy(topology_id), 'device_id': deepcopy(device_id), @@ -61,9 +61,9 @@ DEVICE1 = { 'device_id': deepcopy(DEVICE1_ID), 'device_type': 'packet-router', 'device_config': {'config_rules': [ - config_rule(ConfigActionEnum.CONFIGACTION_SET, 'dev/rsrc1/value', 'value1'), - config_rule(ConfigActionEnum.CONFIGACTION_SET, 'dev/rsrc2/value', 'value2'), - config_rule(ConfigActionEnum.CONFIGACTION_SET, 'dev/rsrc3/value', 'value3'), + config_rule(ConfigActionEnum.CONFIGACTION_SET, 'value1'), + config_rule(ConfigActionEnum.CONFIGACTION_SET, 'value2'), + config_rule(ConfigActionEnum.CONFIGACTION_SET, 'value3'), ]}, 'device_operational_status': DeviceOperationalStatusEnum.DEVICEOPERATIONALSTATUS_ENABLED, 'device_drivers': [DeviceDriverEnum.DEVICEDRIVER_OPENCONFIG, DeviceDriverEnum.DEVICEDRIVER_P4], @@ -154,10 +154,10 @@ SERVICE_DEV1_DEV2 = { endpoint_id(TOPOLOGY_ID, DEVICE1_ID, 'EP100'), endpoint_id(TOPOLOGY_ID, DEVICE2_ID, 'EP100'), ], - 'service_constraints': [ - {'constraint_type': 'latency_ms', 'constraint_value': '15.2'}, - {'constraint_type': 'jitter_us', 'constraint_value': '1.2'}, - ], + # 'service_constraints': [ + # {'constraint_type': 'latency_ms', 'constraint_value': '15.2'}, + # {'constraint_type': 'jitter_us', 'constraint_value': '1.2'}, + # ], 'service_status': {'service_status': ServiceStatusEnum.SERVICESTATUS_ACTIVE}, 'service_config': {'config_rules': [ config_rule(ConfigActionEnum.CONFIGACTION_SET, 'svc/rsrc1/value', 'value7'), @@ -178,10 +178,10 @@ SERVICE_DEV1_DEV3 = { endpoint_id(TOPOLOGY_ID, DEVICE1_ID, 'EP100'), endpoint_id(TOPOLOGY_ID, DEVICE3_ID, 'EP100'), ], - 'service_constraints': [ - {'constraint_type': 'latency_ms', 'constraint_value': '5.8'}, - {'constraint_type': 'jitter_us', 'constraint_value': '0.1'}, - ], + # 'service_constraints': [ + # {'constraint_type': 'latency_ms', 'constraint_value': '5.8'}, + # {'constraint_type': 'jitter_us', 'constraint_value': '0.1'}, + # ], 'service_status': {'service_status': ServiceStatusEnum.SERVICESTATUS_ACTIVE}, 'service_config': {'config_rules': [ config_rule(ConfigActionEnum.CONFIGACTION_SET, 'svc/rsrc1/value', 'value7'), @@ -202,10 +202,10 @@ SERVICE_DEV2_DEV3 = { endpoint_id(TOPOLOGY_ID, DEVICE2_ID, 'EP100'), endpoint_id(TOPOLOGY_ID, DEVICE3_ID, 'EP100'), ], - 'service_constraints': [ - {'constraint_type': 'latency_ms', 'constraint_value': '23.1'}, - {'constraint_type': 'jitter_us', 'constraint_value': '3.4'}, - ], + # 'service_constraints': [ + # {'constraint_type': 'latency_ms', 'constraint_value': '23.1'}, + # {'constraint_type': 'jitter_us', 'constraint_value': '3.4'}, + # ], 'service_status': {'service_status': ServiceStatusEnum.SERVICESTATUS_ACTIVE}, 'service_config': {'config_rules': [ config_rule(ConfigActionEnum.CONFIGACTION_SET, 'svc/rsrc1/value', 'value7'), diff --git a/src/opticalattackdetector/tests/opticalattackdetector_report.xml b/src/opticalattackdetector/tests/opticalattackdetector_report.xml new file mode 100644 index 0000000000000000000000000000000000000000..e1e82bb1c4679be1c3707cc7185e99aca0f5657e --- /dev/null +++ b/src/opticalattackdetector/tests/opticalattackdetector_report.xml @@ -0,0 +1,201 @@ +message_descriptor = <google.protobuf.descriptor.Descriptor object at 0x7f3746d949a0> +field_name = 'resource_key' + + def _GetFieldByName(message_descriptor, field_name): + """Returns a field descriptor by field name. + + Args: + message_descriptor: A Descriptor describing all fields in message. + field_name: The name of the field to retrieve. + Returns: + The field descriptor associated with the field name. + """ + try: +> return message_descriptor.fields_by_name[field_name] +E KeyError: 'resource_key' + +../venv/lib/python3.9/site-packages/google/protobuf/internal/python_message.py:577: KeyError + +During handling of the above exception, another exception occurred: + +optical_attack_detector_client = <opticalattackdetector.client.OpticalAttackDetectorClient.OpticalAttackDetectorClient object at 0x7f374670a4f0> + + def test_detect_attack_with_service(optical_attack_detector_client: OpticalAttackDetectorClient,): + with patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.context_client') as context, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.influxdb_client') as influxdb, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.dbscanserving_client') as dbscan: + + # setting up the mock + cid_list = ContextIdList() + cid_list.context_ids.append(ContextId(**CONTEXT_ID)) + context.ListContextIds.return_value = cid_list + + service_list = ServiceList() +> service_list.services.append(Service(**SERVICE_DEV1_DEV2)) + +opticalattackdetector/tests/test_unitary.py:111: +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ +../venv/lib/python3.9/site-packages/google/protobuf/internal/python_message.py:548: in init + new_val = field.message_type._concrete_class(**field_value) +../venv/lib/python3.9/site-packages/google/protobuf/internal/python_message.py:535: in init + copy.add(**val) +../venv/lib/python3.9/site-packages/google/protobuf/internal/containers.py:276: in add + new_element = self._message_descriptor._concrete_class(**kwargs) +../venv/lib/python3.9/site-packages/google/protobuf/internal/python_message.py:516: in init + field = _GetFieldByName(message_descriptor, field_name) +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ + +message_descriptor = <google.protobuf.descriptor.Descriptor object at 0x7f3746d949a0> +field_name = 'resource_key' + + def _GetFieldByName(message_descriptor, field_name): + """Returns a field descriptor by field name. + + Args: + message_descriptor: A Descriptor describing all fields in message. + field_name: The name of the field to retrieve. + Returns: + The field descriptor associated with the field name. + """ + try: + return message_descriptor.fields_by_name[field_name] + except KeyError: +> raise ValueError('Protocol message %s has no "%s" field.' % + (message_descriptor.name, field_name)) +E ValueError: Protocol message ConfigRule has no "resource_key" field. + +../venv/lib/python3.9/site-packages/google/protobuf/internal/python_message.py:579: ValueErrormessage_descriptor = <google.protobuf.descriptor.Descriptor object at 0x7f3746d949a0> +field_name = 'resource_key' + + def _GetFieldByName(message_descriptor, field_name): + """Returns a field descriptor by field name. + + Args: + message_descriptor: A Descriptor describing all fields in message. + field_name: The name of the field to retrieve. + Returns: + The field descriptor associated with the field name. + """ + try: +> return message_descriptor.fields_by_name[field_name] +E KeyError: 'resource_key' + +../venv/lib/python3.9/site-packages/google/protobuf/internal/python_message.py:577: KeyError + +During handling of the above exception, another exception occurred: + +optical_attack_detector_client = <opticalattackdetector.client.OpticalAttackDetectorClient.OpticalAttackDetectorClient object at 0x7f374670a4f0> + + def test_detect_attack_no_attack(optical_attack_detector_client: OpticalAttackDetectorClient,): + with patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.context_client') as context, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.influxdb_client') as influxdb, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.dbscanserving_client') as dbscan, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.attack_mitigator_client') as mitigator: + + # setting up the mock + cid_list = ContextIdList() + cid_list.context_ids.append(ContextId(**CONTEXT_ID)) + context.ListContextIds.return_value = cid_list + + service_list = ServiceList() +> service_list.services.append(Service(**SERVICE_DEV1_DEV2)) + +opticalattackdetector/tests/test_unitary.py:138: +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ +../venv/lib/python3.9/site-packages/google/protobuf/internal/python_message.py:548: in init + new_val = field.message_type._concrete_class(**field_value) +../venv/lib/python3.9/site-packages/google/protobuf/internal/python_message.py:535: in init + copy.add(**val) +../venv/lib/python3.9/site-packages/google/protobuf/internal/containers.py:276: in add + new_element = self._message_descriptor._concrete_class(**kwargs) +../venv/lib/python3.9/site-packages/google/protobuf/internal/python_message.py:516: in init + field = _GetFieldByName(message_descriptor, field_name) +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ + +message_descriptor = <google.protobuf.descriptor.Descriptor object at 0x7f3746d949a0> +field_name = 'resource_key' + + def _GetFieldByName(message_descriptor, field_name): + """Returns a field descriptor by field name. + + Args: + message_descriptor: A Descriptor describing all fields in message. + field_name: The name of the field to retrieve. + Returns: + The field descriptor associated with the field name. + """ + try: + return message_descriptor.fields_by_name[field_name] + except KeyError: +> raise ValueError('Protocol message %s has no "%s" field.' % + (message_descriptor.name, field_name)) +E ValueError: Protocol message ConfigRule has no "resource_key" field. + +../venv/lib/python3.9/site-packages/google/protobuf/internal/python_message.py:579: ValueErrormessage_descriptor = <google.protobuf.descriptor.Descriptor object at 0x7f3746d949a0> +field_name = 'resource_key' + + def _GetFieldByName(message_descriptor, field_name): + """Returns a field descriptor by field name. + + Args: + message_descriptor: A Descriptor describing all fields in message. + field_name: The name of the field to retrieve. + Returns: + The field descriptor associated with the field name. + """ + try: +> return message_descriptor.fields_by_name[field_name] +E KeyError: 'resource_key' + +../venv/lib/python3.9/site-packages/google/protobuf/internal/python_message.py:577: KeyError + +During handling of the above exception, another exception occurred: + +optical_attack_detector_client = <opticalattackdetector.client.OpticalAttackDetectorClient.OpticalAttackDetectorClient object at 0x7f374670a4f0> + + def test_detect_attack_with_attack(optical_attack_detector_client: OpticalAttackDetectorClient,): + with patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.context_client') as context, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.influxdb_client') as influxdb, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.dbscanserving_client') as dbscan, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.attack_mitigator_client') as mitigator: + + # setting up the mock + cid_list = ContextIdList() + cid_list.context_ids.append(ContextId(**CONTEXT_ID)) + context.ListContextIds.return_value = cid_list + + service_list = ServiceList() +> service_list.services.append(Service(**SERVICE_DEV1_DEV2)) + +opticalattackdetector/tests/test_unitary.py:167: +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ +../venv/lib/python3.9/site-packages/google/protobuf/internal/python_message.py:548: in init + new_val = field.message_type._concrete_class(**field_value) +../venv/lib/python3.9/site-packages/google/protobuf/internal/python_message.py:535: in init + copy.add(**val) +../venv/lib/python3.9/site-packages/google/protobuf/internal/containers.py:276: in add + new_element = self._message_descriptor._concrete_class(**kwargs) +../venv/lib/python3.9/site-packages/google/protobuf/internal/python_message.py:516: in init + field = _GetFieldByName(message_descriptor, field_name) +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ + +message_descriptor = <google.protobuf.descriptor.Descriptor object at 0x7f3746d949a0> +field_name = 'resource_key' + + def _GetFieldByName(message_descriptor, field_name): + """Returns a field descriptor by field name. + + Args: + message_descriptor: A Descriptor describing all fields in message. + field_name: The name of the field to retrieve. + Returns: + The field descriptor associated with the field name. + """ + try: + return message_descriptor.fields_by_name[field_name] + except KeyError: +> raise ValueError('Protocol message %s has no "%s" field.' % + (message_descriptor.name, field_name)) +E ValueError: Protocol message ConfigRule has no "resource_key" field. + +../venv/lib/python3.9/site-packages/google/protobuf/internal/python_message.py:579: ValueError \ No newline at end of file diff --git a/src/opticalattackdetector/tests/test_unitary.py b/src/opticalattackdetector/tests/test_unitary.py index da0f4ca9f78478073354d8645cf0b455a5f7a874..9c05d05f679ea73d40967c3e881aeceb1b3769bf 100644 --- a/src/opticalattackdetector/tests/test_unitary.py +++ b/src/opticalattackdetector/tests/test_unitary.py @@ -14,11 +14,11 @@ import logging, pytest from unittest.mock import patch -from opticalcentralizedattackdetector.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD -from opticalcentralizedattackdetector.client.OpticalCentralizedAttackDetectorClient import OpticalCentralizedAttackDetectorClient -from opticalcentralizedattackdetector.proto.context_pb2 import ContextIdList, ContextId, Empty, Service, ContextId, ServiceList -from opticalcentralizedattackdetector.proto.monitoring_pb2 import Kpi, KpiList -from opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorService import OpticalCentralizedAttackDetectorService +from opticalattackdetector.Config import GRPC_SERVICE_PORT, GRPC_MAX_WORKERS, GRPC_GRACE_PERIOD +from opticalattackdetector.client.OpticalAttackDetectorClient import OpticalAttackDetectorClient +from common.proto.context_pb2 import ContextIdList, ContextId, Empty, Service, ContextId, ServiceList +from common.proto.monitoring_pb2 import Kpi, KpiList +from opticalattackdetector.service.OpticalAttackDetectorService import OpticalAttackDetectorService from .example_objects import CONTEXT_ID, CONTEXT_ID_2, SERVICE_DEV1_DEV2 port = 10000 + GRPC_SERVICE_PORT # avoid privileged ports @@ -27,13 +27,13 @@ LOGGER = logging.getLogger(__name__) LOGGER.setLevel(logging.DEBUG) @pytest.fixture(scope='session') -def optical_centralized_attack_detector_service(): - _service = OpticalCentralizedAttackDetectorService( +def optical_attack_detector_service(): + _service = OpticalAttackDetectorService( port=port, max_workers=GRPC_MAX_WORKERS, grace_period=GRPC_GRACE_PERIOD) - # mocker_context_client = mock.patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.context_client') + # mocker_context_client = mock.patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.context_client') # mocker_context_client.start() - # mocker_influx_db = mock.patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.influxdb_client') + # mocker_influx_db = mock.patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.influxdb_client') # mocker_influx_db.start() _service.start() @@ -43,27 +43,27 @@ def optical_centralized_attack_detector_service(): # mocker_influx_db.stop() @pytest.fixture(scope='session') -def optical_centralized_attack_detector_client(optical_centralized_attack_detector_service): - _client = OpticalCentralizedAttackDetectorClient(address='127.0.0.1', port=port) +def optical_attack_detector_client(optical_attack_detector_service): + _client = OpticalAttackDetectorClient(address='127.0.0.1', port=port) yield _client _client.close() -def test_notify_service_update(optical_centralized_attack_detector_client: OpticalCentralizedAttackDetectorClient): +def test_notify_service_update(optical_attack_detector_client: OpticalAttackDetectorClient): service = Service() - optical_centralized_attack_detector_client.NotifyServiceUpdate(service) + optical_attack_detector_client.NotifyServiceUpdate(service) -def test_detect_attack_no_contexts(optical_centralized_attack_detector_client: OpticalCentralizedAttackDetectorClient): - with patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.context_client') as context, \ - patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.influxdb_client') as influxdb: +def test_detect_attack_no_contexts(optical_attack_detector_client: OpticalAttackDetectorClient): + with patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.context_client') as context, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.influxdb_client') as influxdb: request = Empty() - optical_centralized_attack_detector_client.DetectAttack(request) + optical_attack_detector_client.DetectAttack(request) context.ListContextIds.assert_called_once() influxdb.query.assert_called_once() context.ListServices.assert_not_called() -def test_detect_attack_with_context(optical_centralized_attack_detector_client: OpticalCentralizedAttackDetectorClient,): - with patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.context_client') as context, \ - patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.influxdb_client') as influxdb: +def test_detect_attack_with_context(optical_attack_detector_client: OpticalAttackDetectorClient,): + with patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.context_client') as context, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.influxdb_client') as influxdb: # setting up the mock cid_list = ContextIdList() cid_list.context_ids.append(ContextId(**CONTEXT_ID)) @@ -71,16 +71,16 @@ def test_detect_attack_with_context(optical_centralized_attack_detector_client: # making the test request = Empty() - optical_centralized_attack_detector_client.DetectAttack(request) + optical_attack_detector_client.DetectAttack(request) # checking behavior context.ListContextIds.assert_called_once() context.ListServices.assert_called_with(cid_list.context_ids[0]) influxdb.query.assert_called_once() -def test_detect_attack_with_contexts(optical_centralized_attack_detector_client: OpticalCentralizedAttackDetectorClient,): - with patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.context_client') as context, \ - patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.influxdb_client') as influxdb: +def test_detect_attack_with_contexts(optical_attack_detector_client: OpticalAttackDetectorClient,): + with patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.context_client') as context, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.influxdb_client') as influxdb: # setting up the mock cid_list = ContextIdList() cid_list.context_ids.append(ContextId(**CONTEXT_ID)) @@ -89,7 +89,7 @@ def test_detect_attack_with_contexts(optical_centralized_attack_detector_client: # making the test request = Empty() - optical_centralized_attack_detector_client.DetectAttack(request) + optical_attack_detector_client.DetectAttack(request) # checking behavior context.ListContextIds.assert_called_once() @@ -97,10 +97,10 @@ def test_detect_attack_with_contexts(optical_centralized_attack_detector_client: context.ListServices.assert_any_call(cid_list.context_ids[1]) influxdb.query.assert_called_once() -def test_detect_attack_with_service(optical_centralized_attack_detector_client: OpticalCentralizedAttackDetectorClient,): - with patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.context_client') as context, \ - patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.influxdb_client') as influxdb, \ - patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.dbscanserving_client') as dbscan: +def test_detect_attack_with_service(optical_attack_detector_client: OpticalAttackDetectorClient,): + with patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.context_client') as context, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.influxdb_client') as influxdb, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.dbscanserving_client') as dbscan: # setting up the mock cid_list = ContextIdList() @@ -115,7 +115,7 @@ def test_detect_attack_with_service(optical_centralized_attack_detector_client: # making the test request = Empty() - optical_centralized_attack_detector_client.DetectAttack(request) + optical_attack_detector_client.DetectAttack(request) # checking behavior context.ListContextIds.assert_called_once() @@ -123,11 +123,11 @@ def test_detect_attack_with_service(optical_centralized_attack_detector_client: influxdb.query.assert_called_once() dbscan.Detect.assert_called() -def test_detect_attack_no_attack(optical_centralized_attack_detector_client: OpticalCentralizedAttackDetectorClient,): - with patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.context_client') as context, \ - patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.influxdb_client') as influxdb, \ - patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.dbscanserving_client') as dbscan, \ - patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.attack_mitigator_client') as mitigator: +def test_detect_attack_no_attack(optical_attack_detector_client: OpticalAttackDetectorClient,): + with patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.context_client') as context, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.influxdb_client') as influxdb, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.dbscanserving_client') as dbscan, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.attack_mitigator_client') as mitigator: # setting up the mock cid_list = ContextIdList() @@ -143,7 +143,7 @@ def test_detect_attack_no_attack(optical_centralized_attack_detector_client: Opt # making the test request = Empty() - optical_centralized_attack_detector_client.DetectAttack(request) + optical_attack_detector_client.DetectAttack(request) # checking behavior context.ListContextIds.assert_called_once() @@ -152,11 +152,11 @@ def test_detect_attack_no_attack(optical_centralized_attack_detector_client: Opt dbscan.Detect.assert_called() mitigator.NotifyAttack.assert_not_called() -def test_detect_attack_with_attack(optical_centralized_attack_detector_client: OpticalCentralizedAttackDetectorClient,): - with patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.context_client') as context, \ - patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.influxdb_client') as influxdb, \ - patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.dbscanserving_client') as dbscan, \ - patch('opticalcentralizedattackdetector.service.OpticalCentralizedAttackDetectorServiceServicerImpl.attack_mitigator_client') as mitigator: +def test_detect_attack_with_attack(optical_attack_detector_client: OpticalAttackDetectorClient,): + with patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.context_client') as context, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.influxdb_client') as influxdb, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.dbscanserving_client') as dbscan, \ + patch('opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.attack_mitigator_client') as mitigator: # setting up the mock cid_list = ContextIdList() @@ -172,7 +172,7 @@ def test_detect_attack_with_attack(optical_centralized_attack_detector_client: O # making the test request = Empty() - optical_centralized_attack_detector_client.DetectAttack(request) + optical_attack_detector_client.DetectAttack(request) # checking behavior context.ListContextIds.assert_called_once() @@ -181,10 +181,10 @@ def test_detect_attack_with_attack(optical_centralized_attack_detector_client: O dbscan.Detect.assert_called() mitigator.NotifyAttack.assert_called() -def test_report_summarized_kpi(optical_centralized_attack_detector_client: OpticalCentralizedAttackDetectorClient): +def test_report_summarized_kpi(optical_attack_detector_client: OpticalAttackDetectorClient): kpi_list = KpiList() - optical_centralized_attack_detector_client.ReportSummarizedKpi(kpi_list) + optical_attack_detector_client.ReportSummarizedKpi(kpi_list) -def test_report_kpi(optical_centralized_attack_detector_client: OpticalCentralizedAttackDetectorClient): +def test_report_kpi(optical_attack_detector_client: OpticalAttackDetectorClient): kpi_list = KpiList() - optical_centralized_attack_detector_client.ReportKpi(kpi_list) + optical_attack_detector_client.ReportKpi(kpi_list)