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
This commit is contained in:
parent
188615d3eb
commit
e44594df2e
4
.gitreview
Normal file
4
.gitreview
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
[gerrit]
|
||||||
|
host=review.openstack.org
|
||||||
|
port=29418
|
||||||
|
project=stackforge/murano-common.git
|
14
muranocommon/helpers/__init__.py
Normal file
14
muranocommon/helpers/__init__.py
Normal file
@ -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.
|
69
muranocommon/helpers/token_sanitizer.py
Normal file
69
muranocommon/helpers/token_sanitizer.py
Normal file
@ -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
|
14
muranocommon/tests/__init__.py
Normal file
14
muranocommon/tests/__init__.py
Normal file
@ -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.
|
14
muranocommon/tests/helpers/__init__.py
Normal file
14
muranocommon/tests/helpers/__init__.py
Normal file
@ -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.
|
48
muranocommon/tests/helpers/token_sanitizer_tests.py
Normal file
48
muranocommon/tests/helpers/token_sanitizer_tests.py
Normal file
@ -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')
|
@ -12,3 +12,5 @@ mock
|
|||||||
sphinx>=1.1.2
|
sphinx>=1.1.2
|
||||||
testrepository>=0.0.13
|
testrepository>=0.0.13
|
||||||
testtools>=0.9.26
|
testtools>=0.9.26
|
||||||
|
unittest2
|
||||||
|
nose
|
Loading…
x
Reference in New Issue
Block a user