summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Harlow <jxharlow@godaddy.com>2018-07-17 09:24:53 -0700
committerJoshua Harlow <jxharlow@godaddy.com>2018-07-17 09:29:40 -0700
commit4e2abfeec4ef6fa9d7830b56a49091dec489a376 (patch)
treefcc55d89a484f871a6823edc9f5dd345c9935927
parent2b36107f0b6b03c79d184f995dfee4c761d38350 (diff)
downloadoslo-utils-4e2abfeec4ef6fa9d7830b56a49091dec489a376.tar.gz
Handle non-string keys appropriately
In python dict keys can be anything that is hashable; which includes non-strings such as ints. Currently the code is blowing up with these types of keys with exceptions like: TypeError: argument of type 'int' is not iterable So to fix that handle the case where non-string keys are found. Change-Id: I4f576a089df6f68e43572bf0eee15e99f2b557fe
-rw-r--r--oslo_utils/strutils.py16
-rw-r--r--oslo_utils/tests/test_strutils.py7
2 files changed, 18 insertions, 5 deletions
diff --git a/oslo_utils/strutils.py b/oslo_utils/strutils.py
index ae4d00d..8eaa73d 100644
--- a/oslo_utils/strutils.py
+++ b/oslo_utils/strutils.py
@@ -361,15 +361,21 @@ def mask_dict_password(dictionary, secret="***"): # nosec
continue
# NOTE(jlvillal): Check to see if anything in the dictionary 'key'
# contains any key specified in _SANITIZE_KEYS.
- for sani_key in _SANITIZE_KEYS:
- if sani_key in k:
- out[k] = secret
- break
- else:
+ k_matched = False
+ if isinstance(k, six.string_types):
+ for sani_key in _SANITIZE_KEYS:
+ if sani_key in k:
+ out[k] = secret
+ k_matched = True
+ break
+ if not k_matched:
# We did not find a match for the key name in the
# _SANITIZE_KEYS, so we fall through to here
if isinstance(v, six.string_types):
out[k] = mask_password(v, secret=secret)
+ else:
+ # Just leave it alone.
+ out[k] = v
return out
diff --git a/oslo_utils/tests/test_strutils.py b/oslo_utils/tests/test_strutils.py
index 0560d08..41007cc 100644
--- a/oslo_utils/tests/test_strutils.py
+++ b/oslo_utils/tests/test_strutils.py
@@ -634,6 +634,13 @@ class MaskDictionaryPasswordTestCase(test_base.BaseTestCase):
self.assertEqual(expected,
strutils.mask_dict_password(payload))
+ def test_do_an_int(self):
+ payload = {}
+ payload[1] = 2
+ expected = payload.copy()
+ self.assertEqual(expected,
+ strutils.mask_dict_password(payload))
+
def test_mask_values(self):
payload = {'somekey': 'test = cmd --password my\xe9\x80\x80pass'}
expected = {'somekey': 'test = cmd --password ***'}