Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to call RegisterWithActivationKeys() with enable_content #3488

Merged
merged 3 commits into from
Jan 24, 2025

Conversation

jirihnidek
Copy link
Contributor

  • 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
  • Some refactoring and bug fixes in separate commits

* 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.
* Converted method to static method
* Deleted dead code
* Deleted empty lines
Copy link

github-actions bot commented Jan 6, 2025

Coverage

Coverage (computed on Fedora latest) •
FileStmtsMissCoverMissing
rhsmlib/dbus/objects
   register.py2287268%36, 97–98, 111–112, 122–123, 125–126, 136–137, 139, 153, 156, 169, 172, 230, 239, 279–280, 287, 307, 314, 357–359, 361–362, 382–386, 388–390, 392, 394, 396, 415–418, 420–422, 424, 426, 428, 443, 447, 464–469, 471–473, 475, 477, 489–493, 495–497, 499, 501
TOTAL17445448274% 

Tests Skipped Failures Errors Time
2418 14 💤 0 ❌ 0 🔥 31.330s ⏱️

@jirihnidek jirihnidek force-pushed the jhnidek/register_activation_keys_and_enable_content_opt branch from 54329a3 to cb97911 Compare January 6, 2025 16:41
@Archana-PandeyM
Copy link

In case the implementation is done- I tried using this subman build on rhel 10 with rhc copr builds that support this feature. I hit the same error as before when using activation key. Sharing the logs from rhsm.log-
e found for domain: 'rhsm' 2025-01-09T08:12:27.625+01:00 [ERROR] rhsm-service:32088:MainThread @util.py:72 - Unknown arguments: dict_keys(['enable_content']) Traceback (most recent call last): File "/usr/lib64/python3.12/site-packages/rhsmlib/dbus/util.py", line 70, in dbus_handle_exceptions return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib64/python3.12/site-packages/rhsmlib/dbus/objects/register.py", line 499, in RegisterWithActivationKeys dbus_sender.set_cmd_line(sender=self.sender, cmd_line=self.cmd_line) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib64/python3.12/site-packages/rhsmlib/dbus/objects/register.py", line 265, in register_with_activation_keys enable_content: bool = self._remove_enable_content_option(register_options, default=True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib64/python3.12/site-packages/rhsmlib/services/register.py", line 67, in register # signature we want to consider that an error. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ rhsmlib.services.exceptions.ValidationError: Unknown arguments: dict_keys(['enable_content'])

@jirihnidek
Copy link
Contributor Author

jirihnidek commented Jan 13, 2025

In case the implementation is done- I tried using this subman build on rhel 10 with rhc copr builds that support this feature. I hit the same error as before when using activation key. Sharing the logs from rhsm.log- e found for domain: 'rhsm' 2025-01-09T08:12:27.625+01:00 [ERROR] rhsm-service:32088:MainThread @util.py:72 - Unknown arguments: dict_keys(['enable_content']) Traceback (most recent call last): File "/usr/lib64/python3.12/site-packages/rhsmlib/dbus/util.py", line 70, in dbus_handle_exceptions return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib64/python3.12/site-packages/rhsmlib/dbus/objects/register.py", line 499, in RegisterWithActivationKeys dbus_sender.set_cmd_line(sender=self.sender, cmd_line=self.cmd_line) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib64/python3.12/site-packages/rhsmlib/dbus/objects/register.py", line 265, in register_with_activation_keys enable_content: bool = self._remove_enable_content_option(register_options, default=True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib64/python3.12/site-packages/rhsmlib/services/register.py", line 67, in register # signature we want to consider that an error. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ rhsmlib.services.exceptions.ValidationError: Unknown arguments: dict_keys(['enable_content'])

@Archana-PandeyM Did you manually restart rhsm.service before testing this PR? It is still necessary to restart rhsm.service after installing update of subscription-manager, because this PR: #3485 is still WIP. If rhsm.service was running before installing update of subscription-manager, then you can get false negative results after installing subscription-manager, because old rhsm.service process is still running.

@jirihnidek
Copy link
Contributor Author

I can confirm that it works as expected, when service is restarted.

@Archana-PandeyM
Copy link

Thanks George! I checked too it works after the service is restarted.

@jirihnidek jirihnidek requested a review from m-horky January 15, 2025 14:37
@jirihnidek
Copy link
Contributor Author

To test this change you can use library.sh from this repository https://github.com/jirihnidek/rhsm-dbus-examples/ and following bash script:

#!/bin/bash

source ./library.sh

export org_id="donaldduck"
export activation_key="awesome_os_pool"

start_register_server

# Try to register using activation key and org
echo "Registering using activation key and org..."
dbus-send --address=${my_addr} --print-reply --dest='com.redhat.RHSM1.Register' \
       '/com/redhat/RHSM1/Register' \
       com.redhat.RHSM1.Register.RegisterWithActivationKeys \
       string:"${org_id}" \
       array:string:"${activation_key}" \
       dict:string:string:"enable_content","0" \
       dict:string:string:"","" \
       string:"" | gawk '/string/{ $1 = ""; print $0; }' | sed 's/\"//' | sed 's/\(.*\)\"/\1/' > /root/register_output.json

print_registration_result $?

stop_register_server

# unregister

@Glutexo
Copy link
Contributor

Glutexo commented Jan 22, 2025

I was able to reproduce that without this patch, the enable_content parameter is not recognized. Also that with the changes applied, the parameter is accepted. I ran into the same issue as Archana. Thanks to your comments, I was able to fix it by restarting DBus.

Now, I’m going to verify that argument does what it’s intended to do. Also, I‘ll check the code.


Tampering with the dbus-send command, I encountered variations on this error:

Error org.freedesktop.DBus.Python.TypeError: Traceback (most recent call last):
  File "/usr/lib64/python3.9/site-packages/dbus/service.py", line 715, in _message_cb
    retval = candidate_method(self, *args, **keywords)
TypeError: RegisterWithActivationKeys() missing 3 required positional arguments: 'options', 'connection_options', and 'locale'

Subscription Manager should probably handle missing DBus parameter more gracefully.

@jirihnidek
Copy link
Contributor Author

Tampering with the dbus-send command, I encountered variations on this error:

Error org.freedesktop.DBus.Python.TypeError: Traceback (most recent call last):
  File "/usr/lib64/python3.9/site-packages/dbus/service.py", line 715, in _message_cb
    retval = candidate_method(self, *args, **keywords)
TypeError: RegisterWithActivationKeys() missing 3 required positional arguments: 'options', 'connection_options', and 'locale'

Subscription Manager should probably handle missing DBus parameter more gracefully.

The error was raised from /usr/lib64/python3.9/site-packages/dbus/service.py as you can see in the traceback. The subscription-manager can hardly do anything with this. The D-Bus itself does not have concept of optional arguments.

Copy link
Contributor

@Glutexo Glutexo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I can approve. I was not successful with running the DBus command, but Archana confirmed that part. I don’t want to block the PR for too long. I looked at the code and it makes sense. I only nitpicked a few boolean expressions, but they are non-blocking comments.

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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enable_content is bool, so if enable_content means effectively the same.

Suggested change
if enable_content is True:
if enable_content:

Comment on lines 299 to 302
if is_true_value(enable_content):
return True
else:
return False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if is_true_value(enable_content):
return True
else:
return False
return is_true_value(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
@jirihnidek jirihnidek force-pushed the jhnidek/register_activation_keys_and_enable_content_opt branch from cb97911 to b5bf151 Compare January 24, 2025 08:24
@jirihnidek jirihnidek requested a review from Glutexo January 24, 2025 09:00
Copy link
Contributor

@Glutexo Glutexo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look at the new tests. I didn’t examine them to the detail, but they make sense to me. I tried to run them and they succeed. Suggested a parametrization, but that’s not necessary. The other changes look perfect.

Two unrelated tests fail when run with a non-en_US locale. I‘ll file tickets for those.

Comment on lines +426 to +448
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two tests only differ in the "enable_content" value. They can be parametrized.

Suggested change
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)
@pytest.mark.parametrize(("enable_content",), [("true",), ("false",)])
def test_RegisterWithActivationKeys__with_enable_content_option(self, enable_content):
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": enable_content},
{"host": "localhost", "port": "8443", "handler": "/candlepin"},
)
self.assertEqual(expected, result)

@Glutexo Glutexo merged commit 0d4a48c into main Jan 24, 2025
22 of 23 checks passed
@Glutexo Glutexo deleted the jhnidek/register_activation_keys_and_enable_content_opt branch January 24, 2025 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants