From fe8b2a798d08df5b6dc5b00c9866932edf0f9a67 Mon Sep 17 00:00:00 2001 From: Jesse Keating Date: Wed, 19 Oct 2016 10:50:53 -0700 Subject: [PATCH] Avoid string actions on non-string objects When config is coming from a clouds.yaml file, Python's yaml parser may turn the version into an int if it is not quoted in the yaml. Ensure that we set the value to a string before interacting with it. Also fix a possible string action on a None object. Change-Id: Ibf02a85d89c95ba76688b3f5c417f861173008d5 Closes-Bug: #1634986 --- osc_lib/cli/client_config.py | 8 ++++---- osc_lib/tests/cli/test_client_config.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/osc_lib/cli/client_config.py b/osc_lib/cli/client_config.py index f88dcc9..79e0e5f 100644 --- a/osc_lib/cli/client_config.py +++ b/osc_lib/cli/client_config.py @@ -33,7 +33,7 @@ class OSC_Config(config.OpenStackConfig): Migrated from auth.select_auth_plugin() """ - identity_version = config.get('identity_api_version', '') + identity_version = str(config.get('identity_api_version', '')) if config.get('username', None) and not config.get('auth_type', None): if identity_version == '3': @@ -82,8 +82,8 @@ class OSC_Config(config.OpenStackConfig): # NOTE(hieulq): If USER_DOMAIN_NAME, USER_DOMAIN_ID, PROJECT_DOMAIN_ID # or PROJECT_DOMAIN_NAME is present and API_VERSION is 2.0, then # ignore all domain related configs. - if (config.get('identity_api_version', '').startswith('2') and - config.get('auth_type', None).endswith('password')): + if (str(config.get('identity_api_version', '')).startswith('2') and + config.get('auth_type').endswith('password')): domain_props = [ 'project_domain_id', 'project_domain_name', @@ -102,7 +102,7 @@ class OSC_Config(config.OpenStackConfig): Migrated from clientmanager.setup_auth() """ - identity_version = config.get('identity_api_version', '') + identity_version = str(config.get('identity_api_version', '')) auth_type = config.get('auth_type', None) # TODO(mordred): This is a usability improvement that's broadly useful diff --git a/osc_lib/tests/cli/test_client_config.py b/osc_lib/tests/cli/test_client_config.py index 4200180..cb538cc 100644 --- a/osc_lib/tests/cli/test_client_config.py +++ b/osc_lib/tests/cli/test_client_config.py @@ -46,6 +46,15 @@ class TestOSCConfig(utils.TestCase): self.assertEqual('v2password', ret_config['auth_type']) self.assertEqual('fred', ret_config['username']) + def test_auth_select_default_plugin_password_v2_int(self): + config = { + 'identity_api_version': 2, + 'username': 'fred', + } + ret_config = self.cloud._auth_select_default_plugin(config) + self.assertEqual('v2password', ret_config['auth_type']) + self.assertEqual('fred', ret_config['username']) + def test_auth_select_default_plugin_password_v3(self): config = { 'identity_api_version': '3', @@ -55,6 +64,15 @@ class TestOSCConfig(utils.TestCase): self.assertEqual('v3password', ret_config['auth_type']) self.assertEqual('fred', ret_config['username']) + def test_auth_select_default_plugin_password_v3_int(self): + config = { + 'identity_api_version': 3, + 'username': 'fred', + } + ret_config = self.cloud._auth_select_default_plugin(config) + self.assertEqual('v3password', ret_config['auth_type']) + self.assertEqual('fred', ret_config['username']) + def test_auth_select_default_plugin_token(self): config = { 'token': 'subway',