Skip to content

Commit

Permalink
Address review feedback.
Browse files Browse the repository at this point in the history
1) Use the new cert location as the fallback path.

2) Log a warning when no certs are present in the cert location,
   checking in system being migrated, and after the bind mount of
   that path into the ISO runtime environment.
  • Loading branch information
rtamalin committed Feb 2, 2024
1 parent 58364da commit 13cf5e7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
19 changes: 18 additions & 1 deletion suse_migration_services/units/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with suse-migration-services. If not, see <http://www.gnu.org/licenses/>
#
import glob
import logging
import os
import shutil
Expand Down Expand Up @@ -67,7 +68,7 @@ def main():
'/etc/pki/trust/anchors/'
]
cache_cloudregister_path = '/var/cache/cloudregister'
cloud_register_certs_path_fallback = '/var/lib/regionService/certs'
cloud_register_certs_path_fallback = '/usr/lib/regionService/certs'

cloud_register_metadata = ""

Expand All @@ -87,6 +88,9 @@ def main():
suse_cloud_regionsrv_setup,
cloud_register_certs_path_fallback
)
warn_if_regionsrv_certs_not_found(
root_path + cloud_register_certs_path, log
)
if os.path.exists(hosts_setup):
shutil.copy(
hosts_setup, '/etc/hosts'
Expand Down Expand Up @@ -214,6 +218,9 @@ def main():
cloud_register_certs_path
]
)
warn_if_regionsrv_certs_not_found(
cloud_register_certs_path, log
)
update_smt_cache = '/usr/sbin/updatesmtcache'
if os.path.isfile(update_smt_cache):
log.info('Updating SMT cache')
Expand Down Expand Up @@ -261,6 +268,16 @@ def get_regionsrv_certs_path(suse_cloud_regionsrv_setup, fallback):
return regionsrv_setup.get('server', 'certlocation', fallback=fallback)


def warn_if_regionsrv_certs_not_found(certs_path, log):
"""
Log a warning if no PEM files are found at the specified certs path
in the system being migrated.
"""
if not glob.glob(os.path.join(certs_path, '*.pem')):
log.warning("No certs found under specified certs path: %s",
repr(certs_path))


def update_regionsrv_setup(root_path, suse_cloud_regionsrv_setup):
"""
Note: This method is specific to the SUSE Public Cloud Infrastructure
Expand Down
35 changes: 33 additions & 2 deletions test/unit/units/prepare_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import os
import shutil
from tempfile import NamedTemporaryFile
from tempfile import (
NamedTemporaryFile,
mkdtemp
)
from configparser import ConfigParser
from unittest.mock import (
patch, call, Mock, MagicMock, mock_open
Expand All @@ -9,7 +13,7 @@

from suse_migration_services.units.prepare import (
main, update_regionsrv_setup, get_regionsrv_client_file_location,
get_regionsrv_certs_path
get_regionsrv_certs_path, warn_if_regionsrv_certs_not_found
)

from suse_migration_services.suse_connect import SUSEConnect
Expand Down Expand Up @@ -457,3 +461,30 @@ def test_get_regionsrv_certs_path_certlocation(
tmp_regionserverclnt.name,
'foo'
) == '/var/lib/regionService/certs'

def test_warn_if_regionsrv_certs_not_found(
self,
mock_Command_run,
mock_Fstab,
mock_logger_setup,
mock_update_regionsrv_setup
):
tmp_certs_dir = mkdtemp()

log = Mock(spec=['warning'])

# verify that log.warning() is called when dir has no pem files
warn_if_regionsrv_certs_not_found(tmp_certs_dir, log)
assert len(log.warning.call_args_list) == 1

# reset the log mock object
log.reset_mock()

# verify that log.warning() is not called when dir has pem files
tmp_pem_path = os.path.join(tmp_certs_dir, 'dummy_cert.pem')
open(tmp_pem_path, 'w').close()
warn_if_regionsrv_certs_not_found(tmp_certs_dir, log)
assert len(log.warning.call_args_list) == 0

# cleanup tmp_certs_dir
shutil.rmtree(tmp_certs_dir, ignore_errors=True)

0 comments on commit 13cf5e7

Please sign in to comment.