Commit bb3651b5 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

L3 CyberSecurity:

- Replace custom Empty message by StatusMessage
- Pre-merge code cleanup
parent 776c7647
Loading
Loading
Loading
Loading
+3 −4
Original line number Original line Diff line number Diff line
@@ -13,15 +13,14 @@
// limitations under the License.
// limitations under the License.


syntax = "proto3";
syntax = "proto3";
package l3_attackmitigator;


import "context.proto";
import "context.proto";
import "l3_centralizedattackdetector.proto";


service L3Attackmitigator{
service L3Attackmitigator{
  // Perform Mitigation
  rpc PerformMitigation (L3AttackmitigatorOutput) returns (l3_centralizedattackdetector.StatusMessage) {}
  rpc PerformMitigation (L3AttackmitigatorOutput) returns (context.Empty) {}
  // Get Mitigation
  rpc GetMitigation (context.Empty) returns (context.Empty) {}
  rpc GetMitigation (context.Empty) returns (context.Empty) {}
  // Get Configured ACL Rules
  rpc GetConfiguredACLRules (context.Empty) returns (ACLRules) {}
  rpc GetConfiguredACLRules (context.Empty) returns (ACLRules) {}
}
}


+4 −2
Original line number Original line Diff line number Diff line
@@ -13,6 +13,7 @@
// limitations under the License.
// limitations under the License.


syntax = "proto3";
syntax = "proto3";
package l3_centralizedattackdetector;


import "context.proto";
import "context.proto";


@@ -26,8 +27,9 @@ service L3Centralizedattackdetector {
  // Get the list of features used by the ML model in the CAD component
  // Get the list of features used by the ML model in the CAD component
  rpc GetFeaturesIds (context.Empty) returns (AutoFeatures) {}
  rpc GetFeaturesIds (context.Empty) returns (AutoFeatures) {}


  // Sets the list of attack IPs in order to be used to compute the prediction accuracy of the ML model in the CAD component in case of testing the ML model
  // Sets the list of attack IPs in order to be used to compute the prediction accuracy of the
  rpc SetAttackIPs (AttackIPs) returns (Empty) {}
  // ML model in the CAD component in case of testing the ML model.
  rpc SetAttackIPs (AttackIPs) returns (context.Empty) {}
}
}


message Feature {
message Feature {
+8 −6
Original line number Original line Diff line number Diff line
# l3_attackmitigator
# L3 Attack Mitigator
- Receives detected attacks from the Centralized Attack Detector component and performs the necessary mitigations.

- Functions: 
Receives detected attacks from the Centralized Attack Detector component and performs the necessary mitigations.
  - PerformMitigation(self, request: L3AttackmitigatorOutput)

  - GetMitigation(self, request: Empty)
## Functions:
  - GetConfiguredACLRules(self, request: Empty)
- PerformMitigation(L3AttackmitigatorOutput) -> StatusMessage
- GetMitigation(Empty) -> Empty
- GetConfiguredACLRules(Empty) -> ACLRules
+13 −19
Original line number Original line Diff line number Diff line
@@ -15,17 +15,12 @@
import grpc, logging
import grpc, logging
from common.Constants import ServiceNameEnum
from common.Constants import ServiceNameEnum
from common.Settings import get_service_host, get_service_port_grpc
from common.Settings import get_service_host, get_service_port_grpc
from common.proto.context_pb2 import Empty
from common.proto.l3_attackmitigator_pb2 import L3AttackmitigatorOutput, ACLRules
from common.proto.l3_attackmitigator_pb2_grpc import L3AttackmitigatorStub
from common.proto.l3_centralizedattackdetector_pb2 import StatusMessage
from common.tools.client.RetryDecorator import retry, delay_exponential
from common.tools.client.RetryDecorator import retry, delay_exponential
from common.proto.l3_attackmitigator_pb2_grpc import (
from common.tools.grpc.Tools import grpc_message_to_json_string
    L3AttackmitigatorStub,
)
from common.proto.l3_attackmitigator_pb2 import (
    L3AttackmitigatorOutput, ACLRules
)

from common.proto.context_pb2 import (
    Empty
)


LOGGER = logging.getLogger(__name__)
LOGGER = logging.getLogger(__name__)
MAX_RETRIES = 15
MAX_RETRIES = 15
@@ -37,7 +32,7 @@ class l3_attackmitigatorClient:
        if not host: host = get_service_host(ServiceNameEnum.L3_AM)
        if not host: host = get_service_host(ServiceNameEnum.L3_AM)
        if not port: port = get_service_port_grpc(ServiceNameEnum.L3_AM)
        if not port: port = get_service_port_grpc(ServiceNameEnum.L3_AM)
        self.endpoint = "{}:{}".format(host, port)
        self.endpoint = "{}:{}".format(host, port)
        LOGGER.debug("Creating channel to {}...".format(self.endpoint))
        LOGGER.debug("Creating channel to {:s}...".format(self.endpoint))
        self.channel = None
        self.channel = None
        self.stub = None
        self.stub = None
        self.connect()
        self.connect()
@@ -54,23 +49,22 @@ class l3_attackmitigatorClient:
        self.stub = None
        self.stub = None


    @RETRY_DECORATOR
    @RETRY_DECORATOR
    def PerformMitigation(self, request: L3AttackmitigatorOutput) -> Empty:
    def PerformMitigation(self, request: L3AttackmitigatorOutput) -> StatusMessage:
        LOGGER.debug('PerformMitigation request: {}'.format(request))
        LOGGER.debug('PerformMitigation request: {:s}'.format(grpc_message_to_json_string(request)))
        response = self.stub.PerformMitigation(request)
        response = self.stub.PerformMitigation(request)
        LOGGER.debug('PerformMitigation result: {}'.format(response))
        LOGGER.debug('PerformMitigation result: {:s}'.format(grpc_message_to_json_string(response)))
        return response
        return response
    
    
    @RETRY_DECORATOR
    @RETRY_DECORATOR
    def GetMitigation(self, request: Empty) -> Empty:
    def GetMitigation(self, request: Empty) -> Empty:
        LOGGER.debug('GetMitigation request: {}'.format(request))
        LOGGER.debug('GetMitigation request: {:s}'.format(grpc_message_to_json_string(request)))
        response = self.stub.GetMitigation(request)
        response = self.stub.GetMitigation(request)
        LOGGER.debug('GetMitigation result: {}'.format(response))
        LOGGER.debug('GetMitigation result: {:s}'.format(grpc_message_to_json_string(response)))
        return response
        return response
    
    
    @RETRY_DECORATOR
    @RETRY_DECORATOR
    def GetConfiguredACLRules(self, request: Empty) -> ACLRules:
    def GetConfiguredACLRules(self, request: Empty) -> ACLRules:
        LOGGER.debug('GetConfiguredACLRules request: {}'.format(request))
        LOGGER.debug('GetConfiguredACLRules request: {:s}'.format(grpc_message_to_json_string(request)))
        response = self.stub.GetConfiguredACLRules(request)
        response = self.stub.GetConfiguredACLRules(request)
        LOGGER.debug('GetConfiguredACLRules result: {}'.format(response))
        LOGGER.debug('GetConfiguredACLRules result: {:s}'.format(grpc_message_to_json_string(response)))
        return response
        return response
+12 −14
Original line number Original line Diff line number Diff line
@@ -13,24 +13,21 @@
# limitations under the License.
# limitations under the License.


from __future__ import print_function
from __future__ import print_function

import grpc
import logging
import logging
import time
import time


from common.proto.l3_centralizedattackdetector_pb2 import Empty
from common.method_wrappers.Decorator import MetricsPool, safe_and_metered_rpc_method
from common.proto.l3_attackmitigator_pb2_grpc import L3AttackmitigatorServicer
from common.proto.l3_attackmitigator_pb2 import ACLRules
from common.proto.context_pb2 import (
    ServiceId,
    ConfigActionEnum,
)

from common.proto.acl_pb2 import AclForwardActionEnum, AclLogActionEnum, AclRuleTypeEnum
from common.proto.acl_pb2 import AclForwardActionEnum, AclLogActionEnum, AclRuleTypeEnum
from common.proto.context_pb2 import ConfigActionEnum, Service, ServiceId, ConfigRule
from common.proto.context_pb2 import ConfigActionEnum, Empty, Service, ServiceId
from common.proto.l3_attackmitigator_pb2 import ACLRules, L3AttackmitigatorOutput
from common.proto.l3_attackmitigator_pb2_grpc import L3AttackmitigatorServicer
from common.proto.l3_centralizedattackdetector_pb2 import StatusMessage
from common.tools.grpc.Tools import grpc_message_to_json_string
from common.tools.grpc.Tools import grpc_message_to_json_string
from context.client.ContextClient import ContextClient
from context.client.ContextClient import ContextClient
from service.client.ServiceClient import ServiceClient
from service.client.ServiceClient import ServiceClient


from common.method_wrappers.Decorator import MetricsPool, safe_and_metered_rpc_method


LOGGER = logging.getLogger(__name__)
LOGGER = logging.getLogger(__name__)
METRICS_POOL = MetricsPool("l3_attackmitigator", "RPC")
METRICS_POOL = MetricsPool("l3_attackmitigator", "RPC")
@@ -150,7 +147,7 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer):
            raise Exception("Service update failed. Wrong ServiceId was returned")
            raise Exception("Service update failed. Wrong ServiceId was returned")


    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    def PerformMitigation(self, request, context):
    def PerformMitigation(self, request : L3AttackmitigatorOutput, context : grpc.ServicerContext) -> StatusMessage:
        """
        """
        Performs mitigation on an attack by configuring an ACL rule to block undesired TCP traffic.
        Performs mitigation on an attack by configuring an ACL rule to block undesired TCP traffic.


@@ -159,7 +156,8 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer):
            context (Empty): The context of the request.
            context (Empty): The context of the request.


        Returns:
        Returns:
            Empty: An empty response indicating that the attack mitigation information was received and processed.
            StatusMessage: A response with a message indicating that the attack mitigation information
                was received and processed.
        """
        """


        last_value = request.confidence
        last_value = request.confidence
@@ -217,10 +215,10 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer):
            )
            )
        )
        )


        return Empty(message=f"OK, received values: {last_tag} with confidence {last_value}.")
        return StatusMessage(message=f"OK, received values: {last_tag} with confidence {last_value}.")


    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    def GetConfiguredACLRules(self, request, context):
    def GetConfiguredACLRules(self, request : Empty, context : grpc.ServicerContext) -> ACLRules:
        """
        """
        Returns the configured ACL rules.
        Returns the configured ACL rules.


Loading