From ffee660727620a5d5c1e069ac28c09850ac57299 Mon Sep 17 00:00:00 2001 From: Guillermo Calvino Date: Fri, 19 Aug 2022 13:01:06 +0200 Subject: [PATCH] Get VIM certificates from DB Change-Id: Iaf11da954dd8faedbc27e0055bdf134433ea680f Signed-off-by: Guillermo Calvino --- NG-RO/osm_ng_ro/ns_thread.py | 19 +++-- NG-RO/osm_ng_ro/tests/test_ns_thread.py | 71 ++++++++++++++++++- ..._10946_Add_VIM_certificates_to_OSM_DB.yaml | 22 ++++++ 3 files changed, 101 insertions(+), 11 deletions(-) create mode 100644 releasenotes/notes/feature_10946_Add_VIM_certificates_to_OSM_DB.yaml diff --git a/NG-RO/osm_ng_ro/ns_thread.py b/NG-RO/osm_ng_ro/ns_thread.py index 6e9f1043..71944464 100644 --- a/NG-RO/osm_ng_ro/ns_thread.py +++ b/NG-RO/osm_ng_ro/ns_thread.py @@ -27,7 +27,8 @@ A ro_task can contain several 'tasks', each one with a target, where to store th from copy import deepcopy from http import HTTPStatus import logging -from os import mkdir +from os import makedirs +from os import path import queue from shutil import rmtree import threading @@ -39,7 +40,8 @@ from unittest.mock import Mock from importlib_metadata import entry_points from osm_common.dbbase import DbException from osm_ng_ro.vim_admin import LockRenew -from osm_ro_plugin import sdnconn, vimconn +from osm_ro_plugin import sdnconn +from osm_ro_plugin import vimconn from osm_ro_plugin.sdn_dummy import SdnDummyConnector from osm_ro_plugin.vim_dummy import VimDummyConnector import yaml @@ -1587,7 +1589,7 @@ class NsWorker(threading.Thread): self.task_lock.release() return False - def _process_vim_config(self, target_id, db_vim): + def _process_vim_config(self, target_id: str, db_vim: dict) -> None: """ Process vim config, creating vim configuration files as ca_cert :param target_id: vim/sdn/wim + id @@ -1598,17 +1600,14 @@ class NsWorker(threading.Thread): return file_name = "" + work_dir = "/app/osm_ro/certs" try: if db_vim["config"].get("ca_cert_content"): - file_name = "{}:{}".format(target_id, self.worker_index) + file_name = f"{work_dir}/{target_id}:{self.worker_index}" - try: - mkdir(file_name) - except FileExistsError: - self.logger.exception( - "FileExistsError occured while processing vim_config." - ) + if not path.isdir(file_name): + makedirs(file_name) file_name = file_name + "/ca_cert" diff --git a/NG-RO/osm_ng_ro/tests/test_ns_thread.py b/NG-RO/osm_ng_ro/tests/test_ns_thread.py index b1209015..3c73a7d4 100644 --- a/NG-RO/osm_ng_ro/tests/test_ns_thread.py +++ b/NG-RO/osm_ng_ro/tests/test_ns_thread.py @@ -17,12 +17,13 @@ import logging import unittest -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock, Mock, mock_open, patch from osm_common.dbmemory import DbMemory from osm_ng_ro.ns_thread import ( ConfigValidate, NsWorker, + NsWorkerException, VimInteractionAffinityGroup, VimInteractionMigration, VimInteractionNet, @@ -257,6 +258,74 @@ class TestNsWorker(unittest.TestCase): len(self.get_disabled_tasks(db, "DONE")), disabled_tasks_count ) + @patch("osm_ng_ro.ns_thread.makedirs", return_value="") + def test_create_file_cert(self, mock_makedirs): + vim_config = {"config": {"ca_cert_content": "test"}} + target_id = "1234" + db = Mock() + + with patch("builtins.open", mock_open()) as mocked_file: + nsw = NsWorker(self.worker_index, self.config, self.plugins, db) + nsw._process_vim_config(target_id, vim_config) + mocked_file.assert_called_once_with( + f"/app/osm_ro/certs/{target_id}:{self.worker_index}/ca_cert", "w" + ) + assert ( + vim_config["config"]["ca_cert"] + == f"/app/osm_ro/certs/{target_id}:{self.worker_index}/ca_cert" + ) + + @patch("osm_ng_ro.ns_thread.makedirs") + @patch("osm_ng_ro.ns_thread.path") + def test_create_file_cert_exists(self, mock_path, mock_makedirs): + vim_config = {"config": {"ca_cert_content": "test"}} + target_id = "1234" + db = Mock() + mock_path.isdir.return_value = True + + with patch("builtins.open", mock_open()) as mocked_file: + nsw = NsWorker(self.worker_index, self.config, self.plugins, db) + nsw._process_vim_config(target_id, vim_config) + mock_makedirs.assert_not_called() + mocked_file.assert_called_once_with( + f"/app/osm_ro/certs/{target_id}:{self.worker_index}/ca_cert", "w" + ) + assert ( + vim_config["config"]["ca_cert"] + == f"/app/osm_ro/certs/{target_id}:{self.worker_index}/ca_cert" + ) + + @patch("osm_ng_ro.ns_thread.path") + @patch("osm_ng_ro.ns_thread.makedirs", side_effect=Exception) + def test_create_file_cert_makedirs_except(self, mock_makedirs, mock_path): + vim_config = {"config": {"ca_cert_content": "test"}} + target_id = "1234" + db = Mock() + mock_path.isdir.return_value = False + + with patch("builtins.open", mock_open()) as mocked_file: + nsw = NsWorker(self.worker_index, self.config, self.plugins, db) + with self.assertRaises(NsWorkerException): + nsw._process_vim_config(target_id, vim_config) + mocked_file.assert_not_called() + assert vim_config["config"]["ca_cert_content"] == "test" + + @patch("osm_ng_ro.ns_thread.makedirs", return_value="") + def test_create_file_cert_open_excepts(self, mock_makedirs): + vim_config = {"config": {"ca_cert_content": "test"}} + target_id = "1234" + db = Mock() + + with patch("builtins.open", mock_open()) as mocked_file: + mocked_file.side_effect = Exception + nsw = NsWorker(self.worker_index, self.config, self.plugins, db) + with self.assertRaises(NsWorkerException): + nsw._process_vim_config(target_id, vim_config) + mocked_file.assert_called_once_with( + f"/app/osm_ro/certs/{target_id}:{self.worker_index}/ca_cert", "w" + ) + assert vim_config["config"]["ca_cert_content"] == "test" + class TestVimInteractionNet(unittest.TestCase): def setUp(self): diff --git a/releasenotes/notes/feature_10946_Add_VIM_certificates_to_OSM_DB.yaml b/releasenotes/notes/feature_10946_Add_VIM_certificates_to_OSM_DB.yaml new file mode 100644 index 00000000..0938f625 --- /dev/null +++ b/releasenotes/notes/feature_10946_Add_VIM_certificates_to_OSM_DB.yaml @@ -0,0 +1,22 @@ +####################################################################################### +# Copyright ETSI Contributors and Others. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. +####################################################################################### +--- +features: + - | + Feature 10946 - Add VIM certificates to OSM DB + RO and MON will get the VIM certificates from the MongoDB database, + instead of depending on the certificates which are manually created. -- 2.25.1