Skip to content
Snippets Groups Projects
Commit 182d6978 authored by delacal's avatar delacal
Browse files

Removed junk files and cleaned and formatted code

parent 053968fd
No related branches found
No related tags found
2 merge requests!142Release TeraFlowSDN 2.1,!128Fixes on L3 Cybersecurity components
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
INFO:__main__:Starting...
DEBUG:l3_centralizedattackdetector.service.l3_centralizedattackdetectorService:Starting Service (tentative endpoint: 0.0.0.0:10001, max_workers: 10)...
INFO:l3_centralizedattackdetector.service.l3_centralizedattackdetectorServiceServicerImpl:Creating Centralized Attack Detector Service
INFO:l3_centralizedattackdetector.service.l3_centralizedattackdetectorServiceServicerImpl:Cryptomining Detector Features: [3.0, 5.0, 7.0, 8.0, 9.0, 17.0, 19.0, 21.0, 22.0, 23.0]
INFO:l3_centralizedattackdetector.service.l3_centralizedattackdetectorServiceServicerImpl:Batch size: 10
DEBUG:monitoring.client.MonitoringClient:Creating channel to 10.152.183.227:7070...
DEBUG:monitoring.client.MonitoringClient:Channel created
DEBUG:l3_attackmitigator.client.l3_attackmitigatorClient:Creating channel to l3-attackmitigatorservice:10002...
DEBUG:l3_attackmitigator.client.l3_attackmitigatorClient:Channel created
INFO:l3_centralizedattackdetector.service.l3_centralizedattackdetectorServiceServicerImpl:This replica's identifier is: 84c48a95-dedc-4d31-a2f8-358a0237f320
INFO:l3_centralizedattackdetector.service.l3_centralizedattackdetectorService:Listening on 0.0.0.0:10001...
DEBUG:l3_centralizedattackdetector.service.l3_centralizedattackdetectorService:Service started
......@@ -34,7 +34,7 @@ from common.method_wrappers.Decorator import MetricsPool, safe_and_metered_rpc_m
LOGGER = logging.getLogger(__name__)
METRICS_POOL = MetricsPool('l3_attackmitigator', 'RPC')
METRICS_POOL = MetricsPool("l3_attackmitigator", "RPC")
class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer):
......@@ -65,9 +65,6 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer):
service_id.context_id.context_uuid.uuid = context_uuid
service_id.service_uuid.uuid = service_uuid
# Get service form Context
# context_client = ContextClient()
try:
_service: Service = self.context_client.GetService(service_id)
except:
......@@ -88,11 +85,9 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer):
# Set RuleSet for this ACL ConfigRule
acl_rule_set = acl_config_rule.acl.rule_set
# TODO: update the following parameters; for instance, add them as parameters of the method configure_acl_rule
# acl_rule_set.name = "DROP-HTTPS"
acl_rule_set.name = "DROP-TCP"
acl_rule_set.type = AclRuleTypeEnum.ACLRULETYPE_IPV4
# acl_rule_set.description = "DROP undesired HTTPS traffic"
acl_rule_set.description = "DROP undesired TCP traffic"
# Add ACLEntry to the ACLRuleSet
......@@ -108,7 +103,7 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer):
acl_entry.match.dst_address = "{}/32".format(dst_ip)
acl_entry.match.src_port = int(src_port)
acl_entry.match.dst_port = int(dst_port)
# TODO: update the following parameters; for instance, add them as parameters of the method configure_acl_rule
acl_entry.action.forward_action = AclForwardActionEnum.ACLFORWARDINGACTION_DROP
acl_entry.action.log_action = AclLogActionEnum.ACLLOGACTION_NOLOG
......@@ -119,15 +114,13 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer):
self.configured_acl_config_rules.append(acl_config_rule)
# Update the Service with the new ACL RuleSet
# service_client = ServiceClient()
service_reply: ServiceId = self.service_client.UpdateService(service_request)
# TODO: Log the service_reply details
LOGGER.info("Service reply: %s", service_reply)
if service_reply != service_request.service_id: # pylint: disable=no-member
raise Exception("Service update failed. Wrong ServiceId was returned")
@safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
def PerformMitigation(self, request, context):
last_value = request.confidence
......@@ -186,7 +179,6 @@ class l3_attackmitigatorServiceServicerImpl(L3AttackmitigatorServicer):
return Empty(message=f"OK, received values: {last_tag} with confidence {last_value}.")
@safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
def GetConfiguredACLRules(self, request, context):
acl_rules = ACLRules()
......
......@@ -103,8 +103,8 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto
self.label_name = self.cryptomining_detector_model.get_outputs()[0].name
self.prob_name = self.cryptomining_detector_model.get_outputs()[1].name
# Kpi values
self.l3_security_status = 0 # unnecessary
# KPI values
self.l3_security_status = 0
self.l3_ml_model_confidence = 0
self.l3_inferences_in_interval_counter = 0
......@@ -235,19 +235,11 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto
-output: None
"""
def create_kpis(self, service_id, device_id, endpoint_id):
def create_kpis(self, service_id):
LOGGER.info("Creating KPIs for service {}".format(service_id))
# for now, all the KPIs are created for all the services from which requests are received
# all the KPIs are created for all the services from which requests are received
for kpi in self.monitored_kpis:
# generate random slice_id
slice_id = SliceId()
slice_id.slice_uuid.uuid = str(uuid.uuid4())
# generate random connection_id
connection_id = ConnectionId()
connection_id.connection_uuid.uuid = str(uuid.uuid4())
created_kpi = self.create_kpi(
service_id,
kpi,
......@@ -270,17 +262,9 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto
if non_empty_time_interval:
for service_id in monitor_service_ids:
LOGGER.debug("service_id: {}".format(service_id))
self.monitor_compute_l3_kpi(service_id, monitor_inference_results)
# Demo mode inference results are erased
"""if DEMO_MODE:
# Delete fist half of the inference results
LOGGER.debug("inference_results len: {}".format(len(self.inference_results)))
self.inference_results = self.inference_results[len(self.inference_results)//2:]
LOGGER.debug("inference_results len after erase: {}".format(len(self.inference_results)))"""
# end = time.time()
# LOGGER.debug("Time to process inference results with erase: {}".format(end - start))
LOGGER.debug("KPIs sent to monitoring server")
else:
LOGGER.debug("No KPIs sent to monitoring server")
......@@ -325,7 +309,7 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto
LOGGER.debug("time_interval_start: {}".format(self.time_interval_start))
LOGGER.debug("time_interval_end: {}".format(self.time_interval_end))
def monitor_compute_l3_kpi(self, service_id, monitor_inference_results):
def monitor_compute_l3_kpi(self,):
# L3 security status
kpi_security_status = Kpi()
kpi_security_status.kpi_id.kpi_id.CopyFrom(self.monitored_kpis["l3_security_status"]["kpi_id"])
......@@ -400,11 +384,6 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto
# Get batch size
batch_size = x_data.shape[0]
# Print batch size
LOGGER.debug("batch_size: {}".format(batch_size))
LOGGER.debug("x_data.shape: {}".format(x_data.shape))
inference_time_start = time.time()
# Perform inference
......@@ -573,15 +552,8 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto
# Perform inference with the data sent in the request
if len(self.active_requests) == 0:
self.first_batch_request_time = time.time()
self.active_requests.append(request)
LOGGER.debug("active_requests length: {}".format(len(self.active_requests)))
LOGGER.debug("BATCH_SIZE: {}".format(BATCH_SIZE))
LOGGER.debug(len(self.active_requests) == BATCH_SIZE)
LOGGER.debug("type(len(self.active_requests)): {}".format(type(len(self.active_requests))))
LOGGER.debug("type(BATCH_SIZE): {}".format(type(BATCH_SIZE)))
if len(self.active_requests) >= BATCH_SIZE:
LOGGER.debug("Performing inference... {}".format(self.replica_uuid))
......@@ -598,12 +570,10 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto
for i, req in enumerate(self.active_requests):
service_id = req.connection_metadata.service_id
device_id = req.connection_metadata.endpoint_id.device_id
endpoint_id = req.connection_metadata.endpoint_id
# Check if a request of a new service has been received and, if so, create the monitored KPIs for that service
if service_id not in self.service_ids:
self.create_kpis(service_id, device_id, endpoint_id)
self.create_kpis(service_id)
self.service_ids.append(service_id)
monitor_kpis_start = time.time()
......@@ -661,8 +631,6 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto
self.false_positives += 1
self.total_predictions += 1
# if False:
notification_time_start = time.time()
LOGGER.debug("Crypto attack detected")
......@@ -675,7 +643,10 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto
try:
logging.info("Sending the connection information to the Attack Mitigator component...")
message = L3AttackmitigatorOutput(**cryptomining_detector_output[i])
response = self.attackmitigator_client.PerformMitigation(message)
am_response = self.attackmitigator_client.PerformMitigation(message)
LOGGER.debug("AM response: {}".format(am_response))
notification_time_end = time.time()
self.am_notification_times.append(notification_time_end - notification_time_start)
......@@ -706,11 +677,8 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto
f.write("Std notification time: {}\n".format(std_notification_time))
f.write("Median notification time: {}\n".format(median_notification_time))
# logging.info("Attack Mitigator notified and received response: ", response.message) # FIX No message received
logging.info("Attack Mitigator notified")
# return Empty(message="OK, information received and mitigator notified abou the attack")
except Exception as e:
logging.error("Error notifying the Attack Mitigator component about the attack: ", e)
logging.error("Couldn't find l3_attackmitigator")
......@@ -728,8 +696,6 @@ class l3_centralizedattackdetectorServiceServicerImpl(L3Centralizedattackdetecto
self.total_predictions += 1
# return Empty(message="Ok, information received (no attack detected)")
self.active_requests = []
self.last_batch_request_time = time.time()
......
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