From e44594df2ed6a75ea4d8306bdf27c208a5bcd074 Mon Sep 17 00:00:00 2001 From: Serg Melikyan Date: Fri, 23 Aug 2013 17:47:06 +0400 Subject: [PATCH] Cherry-pick commits from release-0.2. * Added ClearToken class ClearToken can be used for replacing some dictionary attribute value by key-name. I39ac36beb277cd517d1b5cd901f57ed064a88556 Change-Id: Ifb867bb624cc9314cdf900b0efa55441ce305e80 --- .gitreview | 4 ++ muranocommon/helpers/__init__.py | 14 ++++ muranocommon/helpers/token_sanitizer.py | 69 +++++++++++++++++++ muranocommon/tests/__init__.py | 14 ++++ muranocommon/tests/helpers/__init__.py | 14 ++++ .../tests/helpers/token_sanitizer_tests.py | 48 +++++++++++++ test-requirements.txt | 2 + 7 files changed, 165 insertions(+) create mode 100644 .gitreview create mode 100644 muranocommon/helpers/__init__.py create mode 100644 muranocommon/helpers/token_sanitizer.py create mode 100644 muranocommon/tests/__init__.py create mode 100644 muranocommon/tests/helpers/__init__.py create mode 100644 muranocommon/tests/helpers/token_sanitizer_tests.py diff --git a/.gitreview b/.gitreview new file mode 100644 index 0000000..9ebe2b2 --- /dev/null +++ b/.gitreview @@ -0,0 +1,4 @@ +[gerrit] +host=review.openstack.org +port=29418 +project=stackforge/murano-common.git diff --git a/muranocommon/helpers/__init__.py b/muranocommon/helpers/__init__.py new file mode 100644 index 0000000..e3f3e8d --- /dev/null +++ b/muranocommon/helpers/__init__.py @@ -0,0 +1,14 @@ +# Copyright (c) 2013 Mirantis Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/muranocommon/helpers/token_sanitizer.py b/muranocommon/helpers/token_sanitizer.py new file mode 100644 index 0000000..eba9b50 --- /dev/null +++ b/muranocommon/helpers/token_sanitizer.py @@ -0,0 +1,69 @@ +# Copyright (c) 2013 Mirantis Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import types + + +class TokenSanitizer(): + """ + Helper class for cleaning some object from different passwords/tokens. + Simply searches attribute with `look a like` name as one of + the token and replace it value with message. + """ + def __init__(self, tokens=('token', 'pass'), message='*** SANITIZED ***'): + """ + :param tokens: iterable with tokens + :param message: string by which each token going to be replaced + """ + self._tokens = tokens + self._message = message + + @property + def tokens(self): + """ + Iterable with tokens that should be sanitized + """ + return self._tokens + + @property + def message(self): + """ + String by which each token going to be replaced + """ + return self._message + + def _contains_token(self, value): + for token in self.tokens: + if token in value.lower(): + return True + return False + + def sanitize(self, obj): + """ + Replaces each token found in object by message + :param obj: types.DictType, types.ListType, types.Tuple, object + :return: Sanitized object + """ + if isinstance(obj, types.DictType): + return dict([self.sanitize(item) for item in obj.iteritems()]) + elif isinstance(obj, types.ListType): + return [self.sanitize(item) for item in obj] + elif isinstance(obj, types.TupleType): + k, v = obj + if self._contains_token(k) and isinstance(v, types.StringTypes): + return k, self.message + return k, self.sanitize(v) + else: + return obj diff --git a/muranocommon/tests/__init__.py b/muranocommon/tests/__init__.py new file mode 100644 index 0000000..e3f3e8d --- /dev/null +++ b/muranocommon/tests/__init__.py @@ -0,0 +1,14 @@ +# Copyright (c) 2013 Mirantis Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/muranocommon/tests/helpers/__init__.py b/muranocommon/tests/helpers/__init__.py new file mode 100644 index 0000000..e3f3e8d --- /dev/null +++ b/muranocommon/tests/helpers/__init__.py @@ -0,0 +1,14 @@ +# Copyright (c) 2013 Mirantis Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/muranocommon/tests/helpers/token_sanitizer_tests.py b/muranocommon/tests/helpers/token_sanitizer_tests.py new file mode 100644 index 0000000..3e3c4a7 --- /dev/null +++ b/muranocommon/tests/helpers/token_sanitizer_tests.py @@ -0,0 +1,48 @@ +# Copyright (c) 2013 Mirantis, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import unittest2 as unittest + +from muranocommon.helpers.token_sanitizer import TokenSanitizer + + +class TokenSanitizerTests(unittest.TestCase): + sanitizer = TokenSanitizer() + + def test_dict_with_one_value(self): + source = {'token': 'value'} + value = self.sanitizer.sanitize(source) + self.assertEqual(value['token'], self.sanitizer.message) + + def test_dict_with_few_value(self): + source = {'token': 'value', 'pass': 'value'} + value = self.sanitizer.sanitize(source) + + self.assertEqual(value['token'], self.sanitizer.message) + self.assertEqual(value['pass'], self.sanitizer.message) + + def test_dict_with_nested_dict(self): + source = {'obj': {'pass': 'value'}} + value = self.sanitizer.sanitize(source) + self.assertEqual(value['obj']['pass'], self.sanitizer.message) + + def test_dict_with_nested_list(self): + source = {'obj': [{'pass': 'value'}]} + value = self.sanitizer.sanitize(source) + self.assertEqual(value['obj'][0]['pass'], self.sanitizer.message) + + def test_leave_out_other_values(self): + source = {'obj': ['value']} + value = self.sanitizer.sanitize(source) + self.assertEqual(value['obj'][0], 'value') diff --git a/test-requirements.txt b/test-requirements.txt index 91ffda1..fac76e4 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -12,3 +12,5 @@ mock sphinx>=1.1.2 testrepository>=0.0.13 testtools>=0.9.26 +unittest2 +nose \ No newline at end of file