From d0b353cc86f0430b6a96e768ee48aede3dff5e03 Mon Sep 17 00:00:00 2001 From: Kevin Nasto Date: Mon, 6 May 2024 12:48:39 -0500 Subject: [PATCH 1/2] Added flag to skip sending a registration request and minor fixes --- landscape/client/configuration.py | 15 +++- landscape/client/tests/test_configuration.py | 87 ++++++++++++++++++-- 2 files changed, 94 insertions(+), 8 deletions(-) diff --git a/landscape/client/configuration.py b/landscape/client/configuration.py index f3ba91a55..cad586ba9 100644 --- a/landscape/client/configuration.py +++ b/landscape/client/configuration.py @@ -7,6 +7,7 @@ import io import os import pwd +import re import shlex import sys import textwrap @@ -111,6 +112,7 @@ class LandscapeSetupConfiguration(BrokerConfiguration): "silent", "ok_no_register", "import_from", + "skip_registration", ) encoding = "utf-8" @@ -247,6 +249,11 @@ def make_parser(self): "registered else returns {}. Displays " "registration info.".format(EXIT_NOT_REGISTERED), ) + parser.add_option( + "--skip-registration", + action="store_true", + help="Don't send a new registration request", + ) return parser @@ -473,7 +480,9 @@ def query_landscape_edition(self): self.landscape_domain = self.prompt_get_input( "Landscape Domain: ", True, - ) + ).strip('/') + self.landscape_domain = re.sub(r'^https?://', '', + self.landscape_domain) self.config.ping_url = f"http://{self.landscape_domain}/ping" self.config.url = f"https://{self.landscape_domain}/message-system" else: @@ -946,6 +955,10 @@ def main(args, print=print): # Attempt to register the client. reactor = LandscapeReactor() + + if config.skip_registration: + return + if config.silent: result = register( config, diff --git a/landscape/client/tests/test_configuration.py b/landscape/client/tests/test_configuration.py index c15d06f0f..5d3503fcc 100644 --- a/landscape/client/tests/test_configuration.py +++ b/landscape/client/tests/test_configuration.py @@ -8,7 +8,6 @@ from twisted.internet.defer import Deferred from twisted.internet.defer import fail from twisted.internet.defer import succeed -from twisted.python.compat import iteritems from landscape.client.broker.registration import Identity from landscape.client.broker.registration import RegistrationError @@ -384,6 +383,56 @@ def test_prompt_landscape_edition( ) self.assertEqual(self.config.account_name, "standalone") + @mock.patch( + "landscape.client.configuration.input", + return_value="http://landscape.hello.com", + ) + @mock.patch( + "landscape.client.configuration.prompt_yes_no", + return_value=True, + ) + @mock.patch("landscape.client.configuration.show_help") + def test_prompt_landscape_edition_strip_http( + self, + mock_help, + prompt_yes_no, + mock_input, + ): + self.script.query_landscape_edition() + self.assertEqual( + self.config.ping_url, + "http://landscape.hello.com/ping", + ) + self.assertEqual( + self.config.url, + "https://landscape.hello.com/message-system", + ) + + @mock.patch( + "landscape.client.configuration.input", + return_value="https://landscape.hello.com", + ) + @mock.patch( + "landscape.client.configuration.prompt_yes_no", + return_value=True, + ) + @mock.patch("landscape.client.configuration.show_help") + def test_prompt_landscape_edition_strip_https( + self, + mock_help, + prompt_yes_no, + mock_input, + ): + self.script.query_landscape_edition() + self.assertEqual( + self.config.ping_url, + "http://landscape.hello.com/ping", + ) + self.assertEqual( + self.config.url, + "https://landscape.hello.com/message-system", + ) + @mock.patch( "landscape.client.configuration.input", return_value="landscape.hello.com", @@ -760,7 +809,9 @@ def get_content(self, config): try: config.config = config_file config.write() - return open(config.config).read().strip() + "\n" + with open(config.config) as fh: + text = fh.read().strip() + "\n" + return text finally: config.config = original_config @@ -788,7 +839,7 @@ def side_effect_input(prompt): "Will you be using your own " "Self-Hosted Landscape installation? [y/N]": "n", } - for key, value in iteritems(fixtures): + for key, value in fixtures.items(): if key in prompt: return value raise KeyError(f"Couldn't find answer for {prompt}") @@ -798,7 +849,7 @@ def side_effect_getpass(prompt): "(Optional) Registration Key:": "New Password", "Please confirm:": "New Password", } - for key, value in iteritems(fixtures): + for key, value in fixtures.items(): if key in prompt: return value raise KeyError(f"Couldn't find answer for {prompt}") @@ -870,7 +921,7 @@ def side_effect_input(prompt): "Will you be using your own " "Self-Hosted Landscape installation? [y/N]": "n", } - for key, value in iteritems(fixtures): + for key, value in fixtures.items(): if key in prompt: return value @@ -879,7 +930,7 @@ def side_effect_getpass(prompt): "(Optional) Registration Key:": "New Password", "Please confirm:": "New Password", } - for key, value in iteritems(fixtures): + for key, value in fixtures.items(): if key in prompt: return value @@ -1231,6 +1282,27 @@ def test_main_no_registration(self, mock_setup, mock_register, mock_input): "\nRequest a new registration for this computer now? [Y/n]: ", ) + @mock.patch("landscape.client.configuration.input") + @mock.patch("landscape.client.configuration.register") + @mock.patch("landscape.client.configuration.setup") + def test_skip_registration(self, mock_setup, mock_register, mock_input): + """ + Registration and input asking user to register is not called + when flag on + """ + main(["-c", self.make_working_config(), "--skip-registration"], + print=noop_print) + mock_register.assert_not_called() + mock_input.assert_not_called() + + @mock.patch("landscape.client.configuration.register") + @mock.patch("landscape.client.configuration.setup") + def test_main_no_registration_silent(self, mock_setup, mock_register): + """Skip registration works in silent mode""" + main(["-c", self.make_working_config(), "--skip-registration", + "--silent"], print=noop_print) + mock_register.assert_not_called() + @mock.patch( "landscape.client.configuration.register", return_value="success", @@ -2139,7 +2211,8 @@ def test_base64_ssl_public_key_is_exported_to_file( mock_print_text.assert_called_once_with( f"Writing SSL CA certificate to {key_filename}...", ) - self.assertEqual("Hi there!", open(key_filename).read()) + with open(key_filename) as fh: + self.assertEqual("Hi there!", fh.read()) options = ConfigParser() options.read(config_filename) From cda9564b969d78a284df9626ddc2321c8c4b55aa Mon Sep 17 00:00:00 2001 From: Kevin Nasto Date: Thu, 25 Jul 2024 15:56:17 -0500 Subject: [PATCH 2/2] Update configuration.py --- landscape/client/configuration.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/landscape/client/configuration.py b/landscape/client/configuration.py index cad586ba9..1ae887a25 100644 --- a/landscape/client/configuration.py +++ b/landscape/client/configuration.py @@ -480,8 +480,8 @@ def query_landscape_edition(self): self.landscape_domain = self.prompt_get_input( "Landscape Domain: ", True, - ).strip('/') - self.landscape_domain = re.sub(r'^https?://', '', + ).strip("/") + self.landscape_domain = re.sub(r"^https?://", "", self.landscape_domain) self.config.ping_url = f"http://{self.landscape_domain}/ping" self.config.url = f"https://{self.landscape_domain}/message-system"