diff --git a/osbenchmark/metrics.py b/osbenchmark/metrics.py index 27b06c4e..a316b35c 100644 --- a/osbenchmark/metrics.py +++ b/osbenchmark/metrics.py @@ -258,7 +258,7 @@ def __init__(self, cfg): client_options["basic_auth_user"] = user client_options["basic_auth_password"] = password - # add options for aws user login: + # add options for aws user login: # pass in aws access key id, aws secret access key, aws session token, service and region on command if metrics_amazon_aws_log_in is not None: client_options["amazon_aws_log_in"] = 'client_option' @@ -266,7 +266,7 @@ def __init__(self, cfg): client_options["aws_secret_access_key"] = metrics_aws_secret_access_key client_options["service"] = metrics_aws_service client_options["region"] = metrics_aws_region - + if metrics_aws_session_token: client_options["aws_session_token"] = metrics_aws_session_token diff --git a/tests/client_test.py b/tests/client_test.py index 2c69817f..12f8a3f4 100644 --- a/tests/client_test.py +++ b/tests/client_test.py @@ -259,24 +259,24 @@ def test_create_https_connection_with_aws_creds(self, mocked_load_cert_chain): "region": "us-east-1", "verify_certs": True } - + role_based_client_options = dict(user_based_client_options) role_based_client_options["aws_session_token"] = "dummy_token" - + client_options_list = [ user_based_client_options, role_based_client_options ] - + logger = logging.getLogger("osbenchmark.client") - + for client_options in client_options_list: # make a copy so we can verify later that the factory did not modify it original_client_options = dict(client_options) - + with mock.patch.object(logger, "info") as mocked_info_logger: f = client.OsClientFactory(hosts, client_options) - + mocked_info_logger.assert_has_calls([ mock.call("SSL support: on"), mock.call("SSL certificate verification: on"), @@ -297,13 +297,13 @@ def test_create_https_connection_with_aws_creds(self, mocked_load_cert_chain): self.assertIn("amazon_aws_log_in", f.client_options) self.assertIn("service", f.client_options) self.assertIn("region", f.client_options) - + if "aws_session_token" in original_client_options: self.assertIn("aws_session_token", f.client_options) - + self.assertDictEqual(original_client_options, client_options) - - + + @mock.patch.object(ssl.SSLContext, "load_cert_chain") def test_create_https_connection_unverified_certificate_present_client_certificates(self, mocked_load_cert_chain): hosts = [{"host": "localhost", "port": 9200}] diff --git a/tests/metrics_test.py b/tests/metrics_test.py index 58202322..421d1d1d 100644 --- a/tests/metrics_test.py +++ b/tests/metrics_test.py @@ -239,14 +239,14 @@ def test_config_opts_parsing_aws_creds_with_env(self, client_OsClientfactory): } self.config_opts_parsing_aws_creds("environment", override_datastore=override_config) - - + + # validate client_options when session_token is passed enable_role_access = [False, True] for role_based in enable_role_access: # verify config parsing is successful when all required parameters are present config_opts = self.config_opts_parsing_aws_creds("environment", role_based=role_based) - + expected_client_options = { "use_ssl": True, "timeout": 120, @@ -257,15 +257,15 @@ def test_config_opts_parsing_aws_creds_with_env(self, client_OsClientfactory): "region": config_opts["_datastore_aws_region"], "verify_certs": config_opts["_datastore_verify_certs"] } - + if role_based: expected_client_options["aws_session_token"] = config_opts["_datastore_aws_session_token"] - + client_OsClientfactory.assert_called_with( hosts=[{"host": config_opts["_datastore_host"], "port": config_opts["_datastore_port"]}], client_options=expected_client_options ) - + def config_opts_parsing(self, password_configuration): cfg = config.Config() @@ -327,7 +327,7 @@ def config_opts_parsing_aws_creds(self, configuration_source, override_datastore _datastore_aws_secret_access_key = "".join([random.choice(string.ascii_letters + string.digits) for _ in range(40)]) _datastore_aws_service = random.choice(['es', 'aoss']) _datastore_aws_region = random.choice(['us-east-1', 'eu-west-1']) - + # optional _datastore_aws_session_token = "".join([random.choice(string.ascii_letters + string.digits) for _ in range(752)]) @@ -391,7 +391,7 @@ def config_opts_parsing_aws_creds(self, configuration_source, override_datastore "'environment' or 'config'" assert e.message == missing_aws_credentials_message return - + response = { "_datastore_user": _datastore_user, "_datastore_host": _datastore_host, @@ -403,10 +403,10 @@ def config_opts_parsing_aws_creds(self, configuration_source, override_datastore "_datastore_aws_service": _datastore_aws_service, "_datastore_aws_region": _datastore_aws_region } - + if role_based: response["_datastore_aws_session_token"] = _datastore_aws_session_token - + return response def test_raises_sytem_setup_error_on_connection_problems(self):