Commit 306073ac authored by Luis de la Cal's avatar Luis de la Cal
Browse files

CAD:

- Removed unnecessary imports
- Removed unused parameters in create_kpi and commented code inside this function.
- Updated the documentation of the methods according to the input and output.
AM:
- Removed unnecessary imports
- Removed commented function GetMitigration
- Removed old commented configuration of an acl rule.
parent 0dc7b043
Loading
Loading
Loading
Loading
+1 −28
Original line number Diff line number Diff line
@@ -14,23 +14,14 @@

from __future__ import print_function
import logging
import grpc
import time
import json

from common.proto.l3_centralizedattackdetector_pb2 import Empty
from common.proto.l3_attackmitigator_pb2_grpc import L3AttackmitigatorServicer
from common.proto.context_pb2 import (
    ServiceId,
    ServiceConfig,
    ContextId,
    Uuid,
    ConfigRule,
    ConfigRule_Custom,
    ConfigActionEnum,
)
from common.proto.context_pb2_grpc import ContextServiceStub
from common.proto.service_pb2_grpc import ServiceServiceStub

from common.proto.acl_pb2 import AclForwardActionEnum, AclLogActionEnum, AclRuleTypeEnum
from common.proto.context_pb2 import ConfigActionEnum, Service, ServiceId
@@ -146,7 +137,6 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer):

        while sentinel:
            try:
                #service = self.GetService(service_id)
                service = self.context_client.GetService(service_id)
                sentinel = False
            except Exception as e:
@@ -156,14 +146,6 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer):

        LOGGER.info("Service obtained from ServiceId:\n{}".format(service))

        # Old version
        # config_rule = self.GetConfigRule(ip_o, ip_d, port_o, port_d)

        # service_config = ServiceConfig()
        # service_config.config_rules.extend([config_rule])
        # service.service_config.CopyFrom(service_config)

        # New version
        self.configure_acl_rule(
            context_uuid=service_id.context_id.context_uuid.uuid,
            service_uuid=service_id.service_uuid.uuid,
@@ -178,16 +160,7 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer):
        LOGGER.info("Service with new rule:\n{}".format(service))
        self.service_client.UpdateService(service)

        #service2 = self.GetService(service_id)
        service2 = self.context_client.GetService(service_id)
        LOGGER.info("Service obtained from ServiceId after updating with the new rule:\n{}".format(service2))

        return Empty(message=f"OK, received values: {last_tag} with confidence {last_value}.")
 No newline at end of file

"""
    def GetMitigation(self, request, context):
        logging.info("Returning mitigation strategy...")
        k = self.last_value * 2

        return Empty(message=f"Mitigation with double confidence = {k}")
"""
+2 −25
Original line number Diff line number Diff line
@@ -17,18 +17,15 @@ from datetime import datetime
from datetime import timedelta

import os
import grpc
import numpy as np
import onnxruntime as rt
import logging
from time import sleep
import time

from common.proto.l3_centralizedattackdetector_pb2 import Empty
from common.proto.l3_centralizedattackdetector_pb2_grpc import L3CentralizedattackdetectorServicer

from common.proto.l3_attackmitigator_pb2 import L3AttackmitigatorOutput
from common.proto.l3_attackmitigator_pb2_grpc import L3AttackmitigatorStub

from common.proto.monitoring_pb2 import KpiDescriptor
from common.proto.kpi_sample_types_pb2 import KpiSampleType
@@ -37,17 +34,10 @@ from monitoring.client.MonitoringClient import MonitoringClient
from common.proto.monitoring_pb2 import Kpi

from common.tools.timestamp.Converters import timestamp_utcnow_to_float
from common.proto.context_pb2 import Timestamp, ServiceId, EndPointId, SliceId, ConnectionId
from common.proto.context_pb2 import Timestamp, SliceId, ConnectionId

from l3_attackmitigator.client.l3_attackmitigatorClient import l3_attackmitigatorClient

# from context.client.ContextClient import ContextClient

from multiprocessing import Process, Queue

from google.protobuf.json_format import MessageToJson, Parse
import copy

import uuid


@@ -129,8 +119,8 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto
    """
    Create a monitored KPI for a specific service and add it to the Monitoring Client
        -input: 
            + client: Monitoring Client object where the KPI will be tracked
            + service_id: service ID where the KPI will be monitored
            + kpi_name: name of the KPI
            + kpi_description: description of the KPI
            + kpi_sample_type: KPI sample type of the KPI (it must be defined in the kpi_sample_types.proto file)
        -output: KPI identifier representing the KPI
@@ -139,10 +129,6 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto
    def create_kpi(
        self,
        service_id,
        device_id,
        endpoint_id,
        slice_id,
        connection_id,
        kpi_name,
        kpi_description,
        kpi_sample_type,
@@ -150,10 +136,6 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto
        kpidescriptor = KpiDescriptor()
        kpidescriptor.kpi_description = kpi_description
        kpidescriptor.service_id.service_uuid.uuid = service_id.service_uuid.uuid
        # kpidescriptor.device_id.device_uuid.uuid = device_id.device_uuid.uuid
        # kpidescriptor.endpoint_id.endpoint_uuid.uuid = endpoint_id.endpoint_uuid.uuid
        # kpidescriptor.slice_id.slice_uuid.uuid = slice_id.slice_uuid.uuid
        # kpidescriptor.connection_id.connection_uuid.uuid = connection_id.connection_uuid.uuid
        kpidescriptor.kpi_sample_type = kpi_sample_type
        new_kpi = self.monitoring_client.SetKpi(kpidescriptor)

@@ -173,7 +155,6 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto

        # for now, all the KPIs are created for all the services from which requests are received
        for kpi in self.monitored_kpis:
            # slice_ids_list = self.context_client.ListSliceIds(self.context_id)[0]
            # generate random slice_id
            slice_id = SliceId()
            slice_id.slice_uuid.uuid = str(uuid.uuid4())
@@ -184,10 +165,6 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto

            created_kpi = self.create_kpi(
                service_id,
                device_id,
                endpoint_id,
                slice_id,
                connection_id,
                kpi,
                self.monitored_kpis[kpi]["description"].format(service_id=service_id.service_uuid.uuid),
                self.monitored_kpis[kpi]["kpi_sample_type"],