From bb53032020178adfa1bdd7196c7796d17012f203 Mon Sep 17 00:00:00 2001 From: Carlos Natalino Date: Mon, 22 Jan 2024 17:08:35 +0000 Subject: [PATCH 01/12] Updating version bounds of dependencies of the webui and compute component. --- src/compute/requirements.in | 10 +++++----- src/webui/requirements.in | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/compute/requirements.in b/src/compute/requirements.in index 08bbf281a..20f877aa8 100644 --- a/src/compute/requirements.in +++ b/src/compute/requirements.in @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -Flask==2.1.3 -Flask-HTTPAuth==4.5.0 -Flask-RESTful==0.3.9 -jsonschema==4.4.0 -requests==2.27.1 +Flask<3 +Flask-HTTPAuth==4.8.0 +Flask-RESTful==0.3.10 +jsonschema==4.21.1 +requests==2.31.0 diff --git a/src/webui/requirements.in b/src/webui/requirements.in index b4a158d39..325f4ec9e 100644 --- a/src/webui/requirements.in +++ b/src/webui/requirements.in @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -Flask==2.1.3 -Flask-WTF==1.0.0 -flask-healthz==0.0.3 -flask-unittest==0.1.2 +Flask<3 +Flask-WTF<2 +flask-healthz<2 +flask-unittest==0.1.3 lorem-text==2.1 -- GitLab From f3f6a68abab8fa6366f912d37a826a77a3b1c3b4 Mon Sep 17 00:00:00 2001 From: Carlos Natalino Date: Mon, 5 Feb 2024 14:12:43 +0000 Subject: [PATCH 02/12] Replacing the custom mock by a standard one. --- src/opticalattackdetector/tests/test_unitary.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/opticalattackdetector/tests/test_unitary.py b/src/opticalattackdetector/tests/test_unitary.py index 6c7579238..ecd59b917 100644 --- a/src/opticalattackdetector/tests/test_unitary.py +++ b/src/opticalattackdetector/tests/test_unitary.py @@ -58,12 +58,12 @@ def test_detect_attack( message = dbscan.DetectionResponse() message.cluster_indices.extend([0, 1, -1, -1, -1]) - monitoring_mock = MockServicerImpl_Monitoring(queue_samples = queue.Queue()) + # monitoring_mock = MockServicerImpl_Monitoring(queue_samples = queue.Queue()) + # TODO: remove the monitoring mock with patch( "opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.attack_mitigator_client" ) as mitigator, patch( - "opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.monitoring_client", - monitoring_mock, + "opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.monitoring_client.IncludeKpi", ) as monitoring, patch( "opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.dbscanserving_client.Detect", # TODO: return dumb object with "cluster_indices" attribute @@ -77,5 +77,5 @@ def test_detect_attack( request.kpi_id.kpi_id.uuid = "1" optical_attack_detector_client.DetectAttack(request) dbscanserving.assert_called_once() - monitoring.IncludeKpi.assert_called_once() + monitoring.assert_called_once() mitigator.NotifyAttack.assert_called() -- GitLab From 7793c8716ab1d17aedc51cea9816dfccc1824f2a Mon Sep 17 00:00:00 2001 From: Carlos Natalino Date: Mon, 5 Feb 2024 16:28:51 +0000 Subject: [PATCH 03/12] Removing unnecessary import. --- src/opticalattackdetector/tests/test_unitary.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/opticalattackdetector/tests/test_unitary.py b/src/opticalattackdetector/tests/test_unitary.py index ecd59b917..cab888d59 100644 --- a/src/opticalattackdetector/tests/test_unitary.py +++ b/src/opticalattackdetector/tests/test_unitary.py @@ -21,7 +21,6 @@ import pytest from common.proto import dbscanserving_pb2 as dbscan from common.proto.optical_attack_detector_pb2 import DetectionRequest -from common.tests.MockServicerImpl_Monitoring import MockServicerImpl_Monitoring from opticalattackdetector.client.OpticalAttackDetectorClient import \ OpticalAttackDetectorClient @@ -58,8 +57,6 @@ def test_detect_attack( message = dbscan.DetectionResponse() message.cluster_indices.extend([0, 1, -1, -1, -1]) - # monitoring_mock = MockServicerImpl_Monitoring(queue_samples = queue.Queue()) - # TODO: remove the monitoring mock with patch( "opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.attack_mitigator_client" ) as mitigator, patch( -- GitLab From 7671bdeac261d80a0ab07d4bccf41e6e67774a69 Mon Sep 17 00:00:00 2001 From: Carlos Natalino Date: Tue, 13 Feb 2024 13:45:01 +0000 Subject: [PATCH 04/12] Setting a fixed localhost address to the service. --- src/opticalattackdetector/tests/test_unitary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/opticalattackdetector/tests/test_unitary.py b/src/opticalattackdetector/tests/test_unitary.py index cab888d59..97e5a68d2 100644 --- a/src/opticalattackdetector/tests/test_unitary.py +++ b/src/opticalattackdetector/tests/test_unitary.py @@ -43,7 +43,7 @@ def optical_attack_detector_service(): @pytest.fixture(scope="session") def optical_attack_detector_client(optical_attack_detector_service: OpticalAttackDetectorService): _client = OpticalAttackDetectorClient( - host=optical_attack_detector_service.bind_address, + host="127.0.0.1", port=optical_attack_detector_service.bind_port, ) yield _client -- GitLab From 2f4aa10226b833fa9d852ad37db257c5183e692d Mon Sep 17 00:00:00 2001 From: Carlos Natalino Date: Wed, 14 Feb 2024 11:02:34 +0000 Subject: [PATCH 05/12] Improving the assertion of calls. --- src/opticalattackdetector/tests/test_unitary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/opticalattackdetector/tests/test_unitary.py b/src/opticalattackdetector/tests/test_unitary.py index 97e5a68d2..9a4e23c8f 100644 --- a/src/opticalattackdetector/tests/test_unitary.py +++ b/src/opticalattackdetector/tests/test_unitary.py @@ -73,6 +73,6 @@ def test_detect_attack( request.service_id.service_uuid.uuid = str(uuid.uuid4()) request.kpi_id.kpi_id.uuid = "1" optical_attack_detector_client.DetectAttack(request) - dbscanserving.assert_called_once() + dbscanserving.assert_called() monitoring.assert_called_once() mitigator.NotifyAttack.assert_called() -- GitLab From 2d53cea64683faa49f9a9a9266401fb6eb37173d Mon Sep 17 00:00:00 2001 From: Carlos Natalino Date: Wed, 14 Feb 2024 11:03:00 +0000 Subject: [PATCH 06/12] Improving the assertions of calls. --- src/opticalattackdetector/tests/test_unitary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/opticalattackdetector/tests/test_unitary.py b/src/opticalattackdetector/tests/test_unitary.py index 9a4e23c8f..c1db52f03 100644 --- a/src/opticalattackdetector/tests/test_unitary.py +++ b/src/opticalattackdetector/tests/test_unitary.py @@ -74,5 +74,5 @@ def test_detect_attack( request.kpi_id.kpi_id.uuid = "1" optical_attack_detector_client.DetectAttack(request) dbscanserving.assert_called() - monitoring.assert_called_once() + monitoring.assert_called() mitigator.NotifyAttack.assert_called() -- GitLab From e6cf4df7b6f1f2b553dfe2aa27863e4d73b38294 Mon Sep 17 00:00:00 2001 From: Carlos Natalino Date: Thu, 15 Feb 2024 11:15:25 +0000 Subject: [PATCH 07/12] Adjusting the tests to match the cluster indices required. --- src/opticalattackdetector/tests/test_unitary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/opticalattackdetector/tests/test_unitary.py b/src/opticalattackdetector/tests/test_unitary.py index c1db52f03..139e89890 100644 --- a/src/opticalattackdetector/tests/test_unitary.py +++ b/src/opticalattackdetector/tests/test_unitary.py @@ -55,7 +55,7 @@ def test_detect_attack( optical_attack_detector_client: OpticalAttackDetectorClient, ): message = dbscan.DetectionResponse() - message.cluster_indices.extend([0, 1, -1, -1, -1]) + message.cluster_indices.extend([0, 1] * 5 + [-1] * 10) with patch( "opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.attack_mitigator_client" -- GitLab From 14bd2c134d8330d06c7102decab0e695d0a3c728 Mon Sep 17 00:00:00 2001 From: Carlos Natalino Date: Thu, 15 Feb 2024 11:18:18 +0000 Subject: [PATCH 08/12] Fixing the conditional that triggers response. --- .../service/OpticalAttackDetectorServiceServicerImpl.py | 2 +- src/opticalattackdetector/tests/test_unitary.py | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/opticalattackdetector/service/OpticalAttackDetectorServiceServicerImpl.py b/src/opticalattackdetector/service/OpticalAttackDetectorServiceServicerImpl.py index 11016037e..d8e8b649c 100644 --- a/src/opticalattackdetector/service/OpticalAttackDetectorServiceServicerImpl.py +++ b/src/opticalattackdetector/service/OpticalAttackDetectorServiceServicerImpl.py @@ -221,7 +221,7 @@ class OpticalAttackDetectorServiceServicerImpl(OpticalAttackDetectorServiceServi monitoring_client.IncludeKpi(kpi) # if -1 in response.cluster_indices: # attack detected - if kpi.kpi_value.int32Val == -1: + if kpi.kpi_value.int32Val == 1: attack = AttackDescription() attack.cs_id.uuid = request.service_id.service_uuid.uuid with HISTOGRAM_DURATION.labels(step="mitigation", **METRIC_LABELS).time(): diff --git a/src/opticalattackdetector/tests/test_unitary.py b/src/opticalattackdetector/tests/test_unitary.py index 139e89890..3ed8be463 100644 --- a/src/opticalattackdetector/tests/test_unitary.py +++ b/src/opticalattackdetector/tests/test_unitary.py @@ -63,8 +63,6 @@ def test_detect_attack( "opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.monitoring_client.IncludeKpi", ) as monitoring, patch( "opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.dbscanserving_client.Detect", - # TODO: return dumb object with "cluster_indices" attribute - # idea: create new response object return_value=message, ) as dbscanserving: for _ in range(10): -- GitLab From bd9705009d6cc0884e29001f143a44dc156bbf78 Mon Sep 17 00:00:00 2001 From: Carlos Natalino Date: Thu, 15 Feb 2024 11:56:31 +0000 Subject: [PATCH 09/12] Removing unnecessary asserts. --- src/opticalattackdetector/tests/test_unitary.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/opticalattackdetector/tests/test_unitary.py b/src/opticalattackdetector/tests/test_unitary.py index 3ed8be463..4be13d529 100644 --- a/src/opticalattackdetector/tests/test_unitary.py +++ b/src/opticalattackdetector/tests/test_unitary.py @@ -65,12 +65,10 @@ def test_detect_attack( "opticalattackdetector.service.OpticalAttackDetectorServiceServicerImpl.dbscanserving_client.Detect", return_value=message, ) as dbscanserving: - for _ in range(10): - request: DetectionRequest = DetectionRequest() - request.service_id.context_id.context_uuid.uuid = str(uuid.uuid4()) - request.service_id.service_uuid.uuid = str(uuid.uuid4()) - request.kpi_id.kpi_id.uuid = "1" - optical_attack_detector_client.DetectAttack(request) - dbscanserving.assert_called() - monitoring.assert_called() - mitigator.NotifyAttack.assert_called() + request: DetectionRequest = DetectionRequest() + request.service_id.context_id.context_uuid.uuid = str(uuid.uuid4()) + request.service_id.service_uuid.uuid = str(uuid.uuid4()) + request.kpi_id.kpi_id.uuid = "1" + optical_attack_detector_client.DetectAttack(request) + dbscanserving.assert_called() + monitoring.assert_called() -- GitLab From 5f73d5476414cf345c4624e9b4c1feda67014ef0 Mon Sep 17 00:00:00 2001 From: gifrerenom Date: Thu, 15 Feb 2024 15:47:08 +0000 Subject: [PATCH 10/12] Optical Attack Detector component: - Fix Ci/CD pipeline --- src/opticalattackdetector/.gitlab-ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/opticalattackdetector/.gitlab-ci.yml b/src/opticalattackdetector/.gitlab-ci.yml index 2a89b5c54..7f2b669a3 100644 --- a/src/opticalattackdetector/.gitlab-ci.yml +++ b/src/opticalattackdetector/.gitlab-ci.yml @@ -72,7 +72,9 @@ unit_test opticalattackdetector: - sleep 5 - docker ps -a - docker logs $IMAGE_NAME - - docker exec -i $IMAGE_NAME bash -c "coverage run -m pytest --log-level=DEBUG --verbose $IMAGE_NAME/tests/test_unitary.py --junitxml=/home/${IMAGE_NAME}/results/${IMAGE_NAME}_report.xml; coverage xml -o /home/${IMAGE_NAME}/results/${IMAGE_NAME}_coverage.xml; coverage report --include='${IMAGE_NAME}/*' --show-missing" + - docker exec -i $IMAGE_NAME bash -c "coverage run -m pytest --log-level=DEBUG --verbose $IMAGE_NAME/tests/test_unitary.py --junitxml=/home/${IMAGE_NAME}/results/${IMAGE_NAME}_report.xml" + - docker exec -i $IMAGE_NAME bash -c "coverage xml -o /home/${IMAGE_NAME}/results/${IMAGE_NAME}_coverage.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: - docker rm -f $IMAGE_NAME -- GitLab From 4eb475857997c1e70d55f3d9a9b0316ef5ef6d91 Mon Sep 17 00:00:00 2001 From: gifrerenom Date: Thu, 15 Feb 2024 16:07:47 +0000 Subject: [PATCH 11/12] Optical Attack Detector component: - Fix CI/CD pipeline --- src/opticalattackdetector/.gitlab-ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/opticalattackdetector/.gitlab-ci.yml b/src/opticalattackdetector/.gitlab-ci.yml index 7f2b669a3..9c064f118 100644 --- a/src/opticalattackdetector/.gitlab-ci.yml +++ b/src/opticalattackdetector/.gitlab-ci.yml @@ -73,6 +73,9 @@ unit_test opticalattackdetector: - docker ps -a - docker logs $IMAGE_NAME - docker exec -i $IMAGE_NAME bash -c "coverage run -m pytest --log-level=DEBUG --verbose $IMAGE_NAME/tests/test_unitary.py --junitxml=/home/${IMAGE_NAME}/results/${IMAGE_NAME}_report.xml" + - docker logs redis + - docker logs dbscanserving + - docker logs $IMAGE_NAME - docker exec -i $IMAGE_NAME bash -c "coverage xml -o /home/${IMAGE_NAME}/results/${IMAGE_NAME}_coverage.xml" - docker exec -i $IMAGE_NAME bash -c "coverage report --include='${IMAGE_NAME}/*' --show-missing" coverage: '/TOTAL\s+\d+\s+\d+\s+(\d+%)/' -- GitLab From ad051b2f99f49e8e1420778817bc119ef51d5474 Mon Sep 17 00:00:00 2001 From: Carlos Natalino Date: Mon, 19 Feb 2024 13:36:27 +0000 Subject: [PATCH 12/12] Including sleep during initialization of the service. --- src/opticalattackdetector/tests/test_unitary.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/opticalattackdetector/tests/test_unitary.py b/src/opticalattackdetector/tests/test_unitary.py index 4be13d529..0112ec75c 100644 --- a/src/opticalattackdetector/tests/test_unitary.py +++ b/src/opticalattackdetector/tests/test_unitary.py @@ -15,6 +15,7 @@ import logging import uuid import queue +import time from unittest.mock import patch import pytest @@ -36,6 +37,7 @@ LOGGER = logging.getLogger(__name__) def optical_attack_detector_service(): _service = OpticalAttackDetectorService() _service.start() + time.sleep(2) yield _service _service.stop() -- GitLab