diff --git a/landscape/client/configuration.py b/landscape/client/configuration.py index 246099b0..16857ead 100644 --- a/landscape/client/configuration.py +++ b/landscape/client/configuration.py @@ -736,6 +736,7 @@ def attempt_registration( :returns: an exit code based on the registration result. """ + client_info = ClientRegistrationInfo.from_identity(identity) for retry in range(retries): @@ -750,6 +751,7 @@ def attempt_registration( config.url, cainfo=config.ssl_public_key, ) + break except RegistrationException as e: # This is unlikely to be resolved by the time we retry, so we fail @@ -763,7 +765,12 @@ def attempt_registration( # We're finished retrying and haven't succeeded yet. return 2 - set_secure_id(config, registration_info.secure_id) + set_secure_id( + config, + registration_info.secure_id, + registration_info.insecure_id, + ) + print("Registration request sent successfully") restart_client(config) @@ -805,7 +812,7 @@ def registration_info_text(config, registration_status): return text -def set_secure_id(config, new_id): +def set_secure_id(config, new_id, insecure_id=None): """Persists a secure id in the identity data file. This is used to indicate whether we are currently in the process of registering. """ @@ -819,6 +826,8 @@ def set_secure_id(config, new_id): ) identity = Identity(config, persist) identity.secure_id = new_id + if insecure_id is not None: + identity.insecure_id = insecure_id persist.save() @@ -910,6 +919,7 @@ def main(args, print=print): ) exit_code = 0 + if should_register: exit_code = attempt_registration(identity, config) restart_client(config) diff --git a/landscape/client/registration.py b/landscape/client/registration.py index afb584f0..322cd18d 100644 --- a/landscape/client/registration.py +++ b/landscape/client/registration.py @@ -49,6 +49,7 @@ def from_identity( cls: Type["ClientRegistrationInfo"], identity: Identity, ) -> "ClientRegistrationInfo": + return cls( identity.access_group, identity.account_name, diff --git a/landscape/client/tests/test_configuration.py b/landscape/client/tests/test_configuration.py index a8774d62..9dda91fe 100644 --- a/landscape/client/tests/test_configuration.py +++ b/landscape/client/tests/test_configuration.py @@ -26,6 +26,7 @@ from landscape.client.configuration import setup from landscape.client.configuration import show_help from landscape.client.configuration import store_public_key_data +from landscape.client.registration import RegistrationInfo from landscape.client.serviceconfig import ServiceConfigException from landscape.client.tests.helpers import LandscapeTest from landscape.lib.compat import ConfigParser @@ -1496,6 +1497,48 @@ def test_register_silent( mock_register.assert_called_once() mock_input.assert_not_called() + @mock.patch( + "landscape.client.configuration.ClientRegistrationInfo.from_identity", + ) + @mock.patch("landscape.client.configuration.restart_client") + @mock.patch("landscape.client.configuration.input") + @mock.patch("landscape.client.configuration.set_secure_id") + @mock.patch("landscape.client.configuration.register") + @mock.patch("landscape.client.configuration.setup") + def test_register_insecure_id( + self, + mock_setup, + mock_register, + mock_set_secure_id, + mock_input, + mock_restart_client, + mock_client_info, + ): + """ + Tests that silent registration sets insecure id when provided + """ + + mock_register.return_value = RegistrationInfo( + 10, + "fake-secure-id", + "fake-server-uuid", + ) + + self.assertRaises( + SystemExit, + main, + ["--silent", "-c", self.make_working_config()], + print=noop_print, + ) + + mock_setup.assert_called_once() + mock_input.assert_not_called() + mock_set_secure_id.assert_called_once_with( + mock.ANY, + "fake-secure-id", + 10, + ) + @mock.patch("landscape.client.configuration.input") @mock.patch("landscape.client.configuration.attempt_registration") @mock.patch(