1064ba
From fc4ca923d02886669f2f0e0916732133e1969bb6 Mon Sep 17 00:00:00 2001
1064ba
From: Dave Mulford <dmulford@redhat.com>
1064ba
Date: Mon, 9 Oct 2017 15:28:15 -0500
1064ba
Subject: [PATCH] rh_subscription: Perform null checks for enabled and disabled
1064ba
 repos.
1064ba
1064ba
The rh_subscription module doesn't perform null checks when attempting to
1064ba
iterate on the enabled and disable repos arrays. When only one is
1064ba
specified, cloud-init fails to run.
1064ba
1064ba
(cherry picked from commit 9bc4ce0596544ffa56d9d67245b00e07006a8662)
1064ba
1064ba
Resolves: rhbz#1498974
1064ba
Signed-off-by: Ryan McCabe <rmccabe@redhat.com>
1064ba
---
1064ba
 cloudinit/config/cc_rh_subscription.py  | 46 ++++++++++++++++++++-------------
1064ba
 tests/unittests/test_rh_subscription.py | 15 +++++++++++
1064ba
 2 files changed, 43 insertions(+), 18 deletions(-)
1064ba
1064ba
diff --git a/cloudinit/config/cc_rh_subscription.py b/cloudinit/config/cc_rh_subscription.py
1064ba
index 7f36cf8f..a9d21e78 100644
1064ba
--- a/cloudinit/config/cc_rh_subscription.py
1064ba
+++ b/cloudinit/config/cc_rh_subscription.py
1064ba
@@ -38,14 +38,16 @@ Subscription`` example config.
1064ba
         server-hostname: <hostname>
1064ba
 """
1064ba
 
1064ba
+from cloudinit import log as logging
1064ba
 from cloudinit import util
1064ba
 
1064ba
+LOG = logging.getLogger(__name__)
1064ba
+
1064ba
 distros = ['fedora', 'rhel']
1064ba
 
1064ba
 
1064ba
 def handle(name, cfg, _cloud, log, _args):
1064ba
-    sm = SubscriptionManager(cfg)
1064ba
-    sm.log = log
1064ba
+    sm = SubscriptionManager(cfg, log=log)
1064ba
     if not sm.is_configured():
1064ba
         log.debug("%s: module not configured.", name)
1064ba
         return None
1064ba
@@ -86,10 +88,9 @@ def handle(name, cfg, _cloud, log, _args):
1064ba
                 if not return_stat:
1064ba
                     raise SubscriptionError("Unable to attach pools {0}"
1064ba
                                             .format(sm.pools))
1064ba
-            if (sm.enable_repo is not None) or (sm.disable_repo is not None):
1064ba
-                return_stat = sm.update_repos(sm.enable_repo, sm.disable_repo)
1064ba
-                if not return_stat:
1064ba
-                    raise SubscriptionError("Unable to add or remove repos")
1064ba
+            return_stat = sm.update_repos()
1064ba
+            if not return_stat:
1064ba
+                raise SubscriptionError("Unable to add or remove repos")
1064ba
             sm.log_success("rh_subscription plugin completed successfully")
1064ba
         except SubscriptionError as e:
1064ba
             sm.log_warn(str(e))
1064ba
@@ -108,7 +109,10 @@ class SubscriptionManager(object):
1064ba
                      'rhsm-baseurl', 'server-hostname',
1064ba
                      'auto-attach', 'service-level']
1064ba
 
1064ba
-    def __init__(self, cfg):
1064ba
+    def __init__(self, cfg, log=None):
1064ba
+        if log is None:
1064ba
+            log = LOG
1064ba
+        self.log = log
1064ba
         self.cfg = cfg
1064ba
         self.rhel_cfg = self.cfg.get('rh_subscription', {})
1064ba
         self.rhsm_baseurl = self.rhel_cfg.get('rhsm-baseurl')
1064ba
@@ -130,7 +134,7 @@ class SubscriptionManager(object):
1064ba
 
1064ba
     def log_warn(self, msg):
1064ba
         '''Simple wrapper for logging warning messages. Useful for unittests'''
1064ba
-        self.log.warn(msg)
1064ba
+        self.log.warning(msg)
1064ba
 
1064ba
     def _verify_keys(self):
1064ba
         '''
1064ba
@@ -245,7 +249,7 @@ class SubscriptionManager(object):
1064ba
             return False
1064ba
 
1064ba
         reg_id = return_out.split("ID: ")[1].rstrip()
1064ba
-        self.log.debug("Registered successfully with ID {0}".format(reg_id))
1064ba
+        self.log.debug("Registered successfully with ID %s", reg_id)
1064ba
         return True
1064ba
 
1064ba
     def _set_service_level(self):
1064ba
@@ -347,7 +351,7 @@ class SubscriptionManager(object):
1064ba
             try:
1064ba
                 self._sub_man_cli(cmd)
1064ba
                 self.log.debug("Attached the following pools to your "
1064ba
-                               "system: %s" % (", ".join(pool_list))
1064ba
+                               "system: %s", (", ".join(pool_list))
1064ba
                                .replace('--pool=', ''))
1064ba
                 return True
1064ba
             except util.ProcessExecutionError as e:
1064ba
@@ -355,18 +359,24 @@ class SubscriptionManager(object):
1064ba
                               "due to {1}".format(pool, e))
1064ba
                 return False
1064ba
 
1064ba
-    def update_repos(self, erepos, drepos):
1064ba
+    def update_repos(self):
1064ba
         '''
1064ba
         Takes a list of yum repo ids that need to be disabled or enabled; then
1064ba
         it verifies if they are already enabled or disabled and finally
1064ba
         executes the action to disable or enable
1064ba
         '''
1064ba
 
1064ba
-        if (erepos is not None) and (not isinstance(erepos, list)):
1064ba
+        erepos = self.enable_repo
1064ba
+        drepos = self.disable_repo
1064ba
+        if erepos is None:
1064ba
+            erepos = []
1064ba
+        if drepos is None:
1064ba
+            drepos = []
1064ba
+        if not isinstance(erepos, list):
1064ba
             self.log_warn("Repo IDs must in the format of a list.")
1064ba
             return False
1064ba
 
1064ba
-        if (drepos is not None) and (not isinstance(drepos, list)):
1064ba
+        if not isinstance(drepos, list):
1064ba
             self.log_warn("Repo IDs must in the format of a list.")
1064ba
             return False
1064ba
 
1064ba
@@ -399,14 +409,14 @@ class SubscriptionManager(object):
1064ba
             for fail in enable_list_fail:
1064ba
                 # Check if the repo exists or not
1064ba
                 if fail in active_repos:
1064ba
-                    self.log.debug("Repo {0} is already enabled".format(fail))
1064ba
+                    self.log.debug("Repo %s is already enabled", fail)
1064ba
                 else:
1064ba
                     self.log_warn("Repo {0} does not appear to "
1064ba
                                   "exist".format(fail))
1064ba
         if len(disable_list_fail) > 0:
1064ba
             for fail in disable_list_fail:
1064ba
-                self.log.debug("Repo {0} not disabled "
1064ba
-                               "because it is not enabled".format(fail))
1064ba
+                self.log.debug("Repo %s not disabled "
1064ba
+                               "because it is not enabled", fail)
1064ba
 
1064ba
         cmd = ['repos']
1064ba
         if len(disable_list) > 0:
1064ba
@@ -422,10 +432,10 @@ class SubscriptionManager(object):
1064ba
             return False
1064ba
 
1064ba
         if len(enable_list) > 0:
1064ba
-            self.log.debug("Enabled the following repos: %s" %
1064ba
+            self.log.debug("Enabled the following repos: %s",
1064ba
                            (", ".join(enable_list)).replace('--enable=', ''))
1064ba
         if len(disable_list) > 0:
1064ba
-            self.log.debug("Disabled the following repos: %s" %
1064ba
+            self.log.debug("Disabled the following repos: %s",
1064ba
                            (", ".join(disable_list)).replace('--disable=', ''))
1064ba
         return True
1064ba
 
1064ba
diff --git a/tests/unittests/test_rh_subscription.py b/tests/unittests/test_rh_subscription.py
1064ba
index ca14cd46..7b35b9d0 100644
1064ba
--- a/tests/unittests/test_rh_subscription.py
1064ba
+++ b/tests/unittests/test_rh_subscription.py
1064ba
@@ -2,6 +2,7 @@
1064ba
 
1064ba
 """Tests for registering RHEL subscription via rh_subscription."""
1064ba
 
1064ba
+import copy
1064ba
 import logging
1064ba
 
1064ba
 from cloudinit.config import cc_rh_subscription
1064ba
@@ -68,6 +69,20 @@ class GoodTests(TestCase):
1064ba
         self.assertEqual(self.SM.log_success.call_count, 1)
1064ba
         self.assertEqual(self.SM._sub_man_cli.call_count, 2)
1064ba
 
1064ba
+    @mock.patch.object(cc_rh_subscription.SubscriptionManager, "_getRepos")
1064ba
+    @mock.patch.object(cc_rh_subscription.SubscriptionManager, "_sub_man_cli")
1064ba
+    def test_update_repos_disable_with_none(self, m_sub_man_cli, m_get_repos):
1064ba
+        cfg = copy.deepcopy(self.config)
1064ba
+        m_get_repos.return_value = ([], ['repo1'])
1064ba
+        m_sub_man_cli.return_value = (b'', b'')
1064ba
+        cfg['rh_subscription'].update(
1064ba
+            {'enable-repo': ['repo1'], 'disable-repo': None})
1064ba
+        mysm = cc_rh_subscription.SubscriptionManager(cfg)
1064ba
+        self.assertEqual(True, mysm.update_repos())
1064ba
+        m_get_repos.assert_called_with()
1064ba
+        self.assertEqual(m_sub_man_cli.call_args_list,
1064ba
+                         [mock.call(['repos', '--enable=repo1'])])
1064ba
+
1064ba
     def test_full_registration(self):
1064ba
         '''
1064ba
         Registration with auto-attach, service-level, adding pools,
1064ba
-- 
1064ba
2.13.6
1064ba