From b22c36b59d40a1632cc58dd6f1275297849c4db0 Mon Sep 17 00:00:00 2001 From: Jiri Hnidek Date: Mon, 6 Jan 2025 16:54:35 +0100 Subject: [PATCH 1/3] fix: It is possible to disable content using enable_content option * When D-Bus method Register() was called with enable_content option and the value was "false" or "0", then code evaluated this value as True. * Fixed this issue, because previous behavior was very confusing * Refactored code a little bit. * The method is static method * It is possible to set default value, when enable_content was not set at all. --- src/rhsmlib/dbus/objects/register.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/rhsmlib/dbus/objects/register.py b/src/rhsmlib/dbus/objects/register.py index e2b68d559d..a814aeeba5 100644 --- a/src/rhsmlib/dbus/objects/register.py +++ b/src/rhsmlib/dbus/objects/register.py @@ -31,6 +31,7 @@ from subscription_manager.i18n import Locale from subscription_manager.i18n import ugettext as _ from subscription_manager.entcertlib import EntCertActionInvoker +from subscription_manager.utils import is_true_value if TYPE_CHECKING: from rhsm.connection import UEPConnection @@ -284,15 +285,20 @@ def _on_no_organization(self, username: str) -> None: """ raise NoOrganizationException(username=username) - def _remove_enable_content_option(self, options: dict) -> bool: + @staticmethod + def _remove_enable_content_option(options: dict, default: bool = False) -> bool: """Try to remove enable_content option from options dictionary. :returns: The value of 'enable_content' key. """ if "enable_content" not in options: - return False + return default - return bool(options.pop("enable_content")) + enable_content = options.pop("enable_content") + if is_true_value(enable_content): + return True + else: + return False def _enable_content(self, uep: "UEPConnection", consumer: dict) -> None: """Try to enable content: refresh SCA entitlement certs in SCA mode.""" From c9a37a61e0485900240b82902a0e135bc7b30e65 Mon Sep 17 00:00:00 2001 From: Jiri Hnidek Date: Mon, 6 Jan 2025 17:00:59 +0100 Subject: [PATCH 2/3] refactor: Small changes of _enable_content * Converted method to static method * Deleted dead code * Deleted empty lines --- src/rhsmlib/dbus/objects/register.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/rhsmlib/dbus/objects/register.py b/src/rhsmlib/dbus/objects/register.py index a814aeeba5..0d3c111f5d 100644 --- a/src/rhsmlib/dbus/objects/register.py +++ b/src/rhsmlib/dbus/objects/register.py @@ -300,10 +300,10 @@ def _remove_enable_content_option(options: dict, default: bool = False) -> bool: else: return False - def _enable_content(self, uep: "UEPConnection", consumer: dict) -> None: + @staticmethod + def _enable_content(uep: "UEPConnection", consumer: dict) -> None: """Try to enable content: refresh SCA entitlement certs in SCA mode.""" content_access: str = consumer["owner"]["contentAccessMode"] - enabled_content = None if content_access == "entitlement": log.error("Entitlement content access mode is not supported") @@ -312,13 +312,9 @@ def _enable_content(self, uep: "UEPConnection", consumer: dict) -> None: service = EntitlementService(uep) # TODO: try get anything useful from refresh result. It is not possible atm. service.refresh(remove_cache=False, force=False) - else: log.error(f"Unable to enable content due to unsupported content access mode: '{content_access}'") - if enabled_content is not None: - consumer["enabledContent"] = enabled_content - def _check_force_handling(self, register_options: dict, connection_options: dict) -> None: """ Handles "force=true" in the registration options From cb97911fd88a3f6d542d86539df07be78c8f3404 Mon Sep 17 00:00:00 2001 From: Jiri Hnidek Date: Mon, 6 Jan 2025 17:32:07 +0100 Subject: [PATCH 3/3] feat: Allow to call RegisterWithActivationKeys() with enable_content * Allow to call D-Bus method RegisterWithActivationKeys() with register option enable_content. When this option is not set, then behavior is still the same. It means that content is enabled by default, when system is registered with this D-Bus method. * Added two unit tests --- src/rhsmlib/dbus/objects/register.py | 7 +++--- test/rhsmlib/dbus/test_register.py | 32 +++++++++++++++++++++------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/rhsmlib/dbus/objects/register.py b/src/rhsmlib/dbus/objects/register.py index 0d3c111f5d..af7e4475da 100644 --- a/src/rhsmlib/dbus/objects/register.py +++ b/src/rhsmlib/dbus/objects/register.py @@ -30,7 +30,6 @@ from subscription_manager.i18n import Locale from subscription_manager.i18n import ugettext as _ -from subscription_manager.entcertlib import EntCertActionInvoker from subscription_manager.utils import is_true_value if TYPE_CHECKING: @@ -263,10 +262,12 @@ def register_with_activation_keys( uep: UEPConnection = self.build_uep(connection_options) service = RegisterService(uep) + enable_content: bool = self._remove_enable_content_option(register_options, default=True) consumer: dict = service.register(organization, **register_options) - ent_cert_lib = EntCertActionInvoker() - ent_cert_lib.update() + # When consumer is created, we can try to enable content, if requested. + if enable_content is True: + self._enable_content(uep, consumer) return consumer diff --git a/test/rhsmlib/dbus/test_register.py b/test/rhsmlib/dbus/test_register.py index 3715d17c25..a4bd1194ae 100644 --- a/test/rhsmlib/dbus/test_register.py +++ b/test/rhsmlib/dbus/test_register.py @@ -299,20 +299,12 @@ def setUp(self) -> None: self.patches["is_registered"] = is_registered_patch.start() self.addCleanup(is_registered_patch.stop) - update_patch = mock.patch( - "rhsmlib.dbus.objects.register.EntCertActionInvoker.update", - name="update", - ) - self.patches["update"] = update_patch.start() - self.addCleanup(update_patch.stop) - build_uep_patch = mock.patch( "rhsmlib.dbus.base_object.BaseImplementation.build_uep", name="build_uep", ) self.patches["build_uep"] = build_uep_patch.start() self.addCleanup(build_uep_patch.stop) - self.patches["update"].return_value = None def test_Register(self): expected = json.loads(CONSUMER_CONTENT_JSON_SCA) @@ -430,3 +422,27 @@ def test_RegisterWithActivationKeys__with_force_option(self): {"host": "localhost", "port": "8443", "handler": "/candlepin"}, ) self.assertEqual(expected, result) + + def test_RegisterWithActivationKeys__with_enable_content_option(self): + expected = json.loads(CONSUMER_CONTENT_JSON_SCA) + self.patches["is_registered"].return_value = False + self.patches["register"].return_value = expected + + result = self.impl.register_with_activation_keys( + "username", + {"keys": ["key1", "key2"], "enable_content": "true"}, + {"host": "localhost", "port": "8443", "handler": "/candlepin"}, + ) + self.assertEqual(expected, result) + + def test_RegisterWithActivationKeys__with_disable_content_option(self): + expected = json.loads(CONSUMER_CONTENT_JSON_SCA) + self.patches["is_registered"].return_value = False + self.patches["register"].return_value = expected + + result = self.impl.register_with_activation_keys( + "username", + {"keys": ["key1", "key2"], "enable_content": "false"}, + {"host": "localhost", "port": "8443", "handler": "/candlepin"}, + ) + self.assertEqual(expected, result)