Skip to content
Snippets Groups Projects
Commit 6edc9d28 authored by delacal's avatar delacal
Browse files

Changed grpc calls to client in l3_attackmitigator and...

Changed grpc calls to client in l3_attackmitigator and l3_centralizedattackdetector components and added attack mitigator port and name to constants
parent 06d38829
No related branches found
No related tags found
2 merge requests!142Release TeraFlowSDN 2.1,!93Updated L3 components + scalability
...@@ -204,7 +204,7 @@ fi ...@@ -204,7 +204,7 @@ fi
for COMPONENT in $TFS_COMPONENTS; do for COMPONENT in $TFS_COMPONENTS; do
echo "Waiting for '$COMPONENT' component..." echo "Waiting for '$COMPONENT' component..."
kubectl wait --namespace $TFS_K8S_NAMESPACE \ kubectl wait --namespace $TFS_K8S_NAMESPACE \
--for='condition=available' --timeout=300s deployment/${COMPONENT}service --for='condition=available' --timeout=10s deployment/${COMPONENT}service
printf "\n" printf "\n"
done done
......
ssh -L 12345:localhost:80 ubuntu@192.168.165.78
log_am.txt 0 → 100644
This diff is collapsed.
This diff is collapsed.
...@@ -46,6 +46,7 @@ class ServiceNameEnum(Enum): ...@@ -46,6 +46,7 @@ class ServiceNameEnum(Enum):
COMPUTE = 'compute' COMPUTE = 'compute'
CYBERSECURITY = 'cybersecurity' CYBERSECURITY = 'cybersecurity'
INTERDOMAIN = 'interdomain' INTERDOMAIN = 'interdomain'
L3AM = 'l3-attackmitigator'
PATHCOMP = 'pathcomp' PATHCOMP = 'pathcomp'
WEBUI = 'webui' WEBUI = 'webui'
...@@ -61,6 +62,7 @@ DEFAULT_SERVICE_GRPC_PORTS = { ...@@ -61,6 +62,7 @@ DEFAULT_SERVICE_GRPC_PORTS = {
ServiceNameEnum.DLT .value : 8080, ServiceNameEnum.DLT .value : 8080,
ServiceNameEnum.COMPUTE .value : 9090, ServiceNameEnum.COMPUTE .value : 9090,
ServiceNameEnum.CYBERSECURITY.value : 10000, ServiceNameEnum.CYBERSECURITY.value : 10000,
ServiceNameEnum.L3AM .value : 10002,
ServiceNameEnum.INTERDOMAIN .value : 10010, ServiceNameEnum.INTERDOMAIN .value : 10010,
ServiceNameEnum.PATHCOMP .value : 10020, ServiceNameEnum.PATHCOMP .value : 10020,
} }
......
...@@ -13,13 +13,18 @@ ...@@ -13,13 +13,18 @@
# limitations under the License. # limitations under the License.
import grpc, logging import grpc, logging
from common.Constants import ServiceNameEnum
from common.Settings import get_service_host, get_service_port_grpc
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.proto.l3_attackmitigator_pb2_grpc import (
L3AttackmitigatorStub, L3AttackmitigatorStub,
) )
from common.proto.l3_attackmitigator_pb2 import ( from common.proto.l3_attackmitigator_pb2 import (
Output, L3AttackmitigatorOutput,
EmptyMitigator )
from common.proto.context_pb2 import (
Empty
) )
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
...@@ -28,8 +33,10 @@ DELAY_FUNCTION = delay_exponential(initial=0.01, increment=2.0, maximum=5.0) ...@@ -28,8 +33,10 @@ 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') RETRY_DECORATOR = retry(max_retries=MAX_RETRIES, delay_function=DELAY_FUNCTION, prepare_method_name='connect')
class l3_attackmitigatorClient: class l3_attackmitigatorClient:
def __init__(self, address, port): def __init__(self, host=None, port=None):
self.endpoint = "{}:{}".format(address, port) if not host: host = get_service_host(ServiceNameEnum.L3AM)
if not port: port = get_service_port_grpc(ServiceNameEnum.L3AM)
self.endpoint = "{}:{}".format(host, port)
LOGGER.debug("Creating channel to {}...".format(self.endpoint)) LOGGER.debug("Creating channel to {}...".format(self.endpoint))
self.channel = None self.channel = None
self.stub = None self.stub = None
...@@ -47,16 +54,9 @@ class l3_attackmitigatorClient: ...@@ -47,16 +54,9 @@ class l3_attackmitigatorClient:
self.stub = None self.stub = None
@RETRY_DECORATOR @RETRY_DECORATOR
def SendOutput(self, request: Output) -> EmptyMitigator: def SendOutput(self, request: L3AttackmitigatorOutput) -> Empty:
LOGGER.debug('SendOutput request: {}'.format(request)) LOGGER.debug('SendOutput request: {}'.format(request))
response = self.stub.SendOutput(request) response = self.stub.SendOutput(request)
LOGGER.debug('SendOutput result: {}'.format(response)) LOGGER.debug('SendOutput result: {}'.format(response))
return response return response
@RETRY_DECORATOR
def GetMitigation(self, request: EmptyMitigator) -> EmptyMitigator:
LOGGER.debug('GetMitigation request: {}'.format(request))
response = self.stub.GetMitigation(request)
LOGGER.debug('GetMitigation result: {}'.format(response))
return response
...@@ -42,7 +42,6 @@ LOGGER = logging.getLogger(__name__) ...@@ -42,7 +42,6 @@ LOGGER = logging.getLogger(__name__)
CONTEXT_CHANNEL = "192.168.165.78:1010" CONTEXT_CHANNEL = "192.168.165.78:1010"
SERVICE_CHANNEL = "192.168.165.78:3030" SERVICE_CHANNEL = "192.168.165.78:3030"
class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer): class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer):
def __init__(self): def __init__(self):
LOGGER.info("Creating Attack Mitigator Service") LOGGER.info("Creating Attack Mitigator Service")
...@@ -51,6 +50,9 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer): ...@@ -51,6 +50,9 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer):
self.last_tag = 0 self.last_tag = 0
self.sequence_id = 0 self.sequence_id = 0
self.context_client = ContextClient()
self.service_client = ServiceClient()
def GenerateRuleValue(self, ip_o, ip_d, port_o, port_d): def GenerateRuleValue(self, ip_o, ip_d, port_o, port_d):
value = { value = {
"ipv4:source-address": ip_o, "ipv4:source-address": ip_o,
...@@ -108,10 +110,10 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer): ...@@ -108,10 +110,10 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer):
service_id.service_uuid.uuid = service_uuid service_id.service_uuid.uuid = service_uuid
# Get service form Context # Get service form Context
context_client = ContextClient() #context_client = ContextClient()
try: try:
_service: Service = context_client.GetService(service_id) _service: Service = self.context_client.GetService(service_id)
except: except:
raise Exception("Service({:s}) not found".format(grpc_message_to_json_string(service_id))) raise Exception("Service({:s}) not found".format(grpc_message_to_json_string(service_id)))
...@@ -153,8 +155,8 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer): ...@@ -153,8 +155,8 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer):
acl_entry.action.log_action = AclLogActionEnum.ACLLOGACTION_NOLOG acl_entry.action.log_action = AclLogActionEnum.ACLLOGACTION_NOLOG
# Update the Service with the new ACL RuleSet # Update the Service with the new ACL RuleSet
service_client = ServiceClient() #service_client = ServiceClient()
service_reply: ServiceId = service_client.UpdateService(service_request) service_reply: ServiceId = self.service_client.UpdateService(service_request)
# TODO: Log the service_reply details # TODO: Log the service_reply details
...@@ -184,7 +186,8 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer): ...@@ -184,7 +186,8 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer):
while sentinel: while sentinel:
try: try:
service = self.GetService(service_id) #service = self.GetService(service_id)
service = self.context_client.GetService(service_id)
sentinel = False sentinel = False
except Exception as e: except Exception as e:
counter = counter + 1 counter = counter + 1
...@@ -213,28 +216,14 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer): ...@@ -213,28 +216,14 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer):
) )
LOGGER.info("Service with new rule:\n{}".format(service)) LOGGER.info("Service with new rule:\n{}".format(service))
self.UpdateService(service) self.service_client.UpdateService(service)
service2 = self.GetService(service_id) #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)) 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}.") return Empty(message=f"OK, received values: {last_tag} with confidence {last_value}.")
def GetService(self, service_id):
with grpc.insecure_channel(CONTEXT_CHANNEL) as channel:
stub = ContextServiceStub(channel)
return stub.GetService(service_id)
def ListServiceIds(self, context_id):
with grpc.insecure_channel(CONTEXT_CHANNEL) as channel:
stub = ContextServiceStub(channel)
return stub.ListServiceIds(context_id)
def UpdateService(self, service):
with grpc.insecure_channel(SERVICE_CHANNEL) as channel:
stub = ServiceServiceStub(channel)
stub.UpdateService(service)
def GetMitigation(self, request, context): def GetMitigation(self, request, context):
logging.info("Returning mitigation strategy...") logging.info("Returning mitigation strategy...")
k = self.last_value * 2 k = self.last_value * 2
......
...@@ -44,6 +44,10 @@ WORKDIR /var/teraflow/common ...@@ -44,6 +44,10 @@ WORKDIR /var/teraflow/common
COPY src/common/. ./ COPY src/common/. ./
RUN rm -rf proto RUN rm -rf proto
RUN mkdir -p /var/teraflow/l3_attackmitigator
WORKDIR /var/teraflow/l3_attackmitigator
COPY src/l3_attackmitigator/. ./
# Create proto sub-folder, copy .proto files, and generate Python code # Create proto sub-folder, copy .proto files, and generate Python code
RUN mkdir -p /var/teraflow/common/proto RUN mkdir -p /var/teraflow/common/proto
WORKDIR /var/teraflow/common/proto WORKDIR /var/teraflow/common/proto
......
...@@ -36,6 +36,8 @@ from common.proto.monitoring_pb2 import Kpi ...@@ -36,6 +36,8 @@ from common.proto.monitoring_pb2 import Kpi
from common.tools.timestamp.Converters import timestamp_utcnow_to_float from common.tools.timestamp.Converters import timestamp_utcnow_to_float
from common.proto.context_pb2 import Timestamp from common.proto.context_pb2 import Timestamp
from l3_attackmitigator.client.l3_attackmitigatorClient import l3_attackmitigatorClient
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
current_dir = os.path.dirname(os.path.abspath(__file__)) current_dir = os.path.dirname(os.path.abspath(__file__))
MODEL_FILE = os.path.join(current_dir, "ml_model/crypto_5g_rf_spider_features.onnx") MODEL_FILE = os.path.join(current_dir, "ml_model/crypto_5g_rf_spider_features.onnx")
...@@ -56,6 +58,8 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto ...@@ -56,6 +58,8 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto
self.predicted_class_kpi_id = None self.predicted_class_kpi_id = None
self.class_probability_kpi_id = None self.class_probability_kpi_id = None
self.attackmitigator_client = l3_attackmitigatorClient()
def create_predicted_class_kpi(self, client: MonitoringClient, service_id): def create_predicted_class_kpi(self, client: MonitoringClient, service_id):
kpi_description: KpiDescriptor = KpiDescriptor() kpi_description: KpiDescriptor = KpiDescriptor()
kpi_description.kpi_description = "L3 security status of service {}".format(service_id) kpi_description.kpi_description = "L3 security status of service {}".format(service_id)
...@@ -179,13 +183,16 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto ...@@ -179,13 +183,16 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto
) )
try: try:
with grpc.insecure_channel("192.168.165.78:10002") as channel: """with grpc.insecure_channel("192.168.165.78:10002") as channel:
stub = L3AttackmitigatorStub(channel) stub = L3AttackmitigatorStub(channel)
logging.info("Sending the connection information to the Attack Mitigator component...") logging.info("Sending the connection information to the Attack Mitigator component...")
response = stub.SendOutput(output) response = stub.SendOutput(output)"""
logging.info(
"Attack Mitigator notified and received response: ", response.message logging.info("Sending the connection information to the Attack Mitigator component...")
) # FIX No message received response = self.attackmitigator_client.SendOutput(output)
logging.info(
"Attack Mitigator notified and received response: ", response.message
) # FIX No message received
return Empty(message="OK, information received and mitigator notified abou the attack") return Empty(message="OK, information received and mitigator notified abou the attack")
except Exception as e: except Exception as e:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment