Added ClearToken class

ClearToken can be used for replacing some dictionary
attribute value by key-name.

Change-Id: I39ac36beb277cd517d1b5cd901f57ed064a88556
This commit is contained in:
Serg Melikyan 2013-08-23 17:47:06 +04:00
parent 6d6c29f7b5
commit b2fd1ea2ee
10 changed files with 167 additions and 2 deletions

4
.gitreview Normal file
View File

@ -0,0 +1,4 @@
[gerrit]
host=review.openstack.org
port=29418
project=stackforge/murano-common.git

View 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.

View 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

View File

@ -17,4 +17,4 @@ from message import Message
from subscription import Subscription
from mqclient import MqClient
__all__ = ['Message', 'Subscription', 'MqClient']
__all__ = ['Message', 'Subscription', 'MqClient']

View File

@ -32,7 +32,6 @@ class Message(object):
self.body = None
log.exception(e)
@property
def body(self):
return self._body

View 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.

View 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.

View 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')

View File

@ -1,5 +1,6 @@
[metadata]
name = muranocommon
version = 0.2
summary = Shared library for Murano
description-file =
README.rst

View File

@ -12,3 +12,5 @@ mock
sphinx>=1.1.2
testrepository>=0.0.13
testtools>=0.9.26
unittest2
nose