Skip to content

Commit

Permalink
Merge pull request #215 from boxine/make-anonymize-case-insensitive
Browse files Browse the repository at this point in the history
Make anonymize case insensitive
  • Loading branch information
phihag authored Sep 24, 2024
2 parents aa211e7 + 2ce5a79 commit a833c44
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 251 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ bx_py_utils = {path = ".", editable = true}

[dev-packages]
bx_py_utils = {path = ".", editable = true, extras = ["dev"]}
parameterized = "*"
513 changes: 265 additions & 248 deletions Pipfile.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Please take a look into the sources and tests for deeper informations.
### bx_py_utils.anonymize

* [`anonymize()`](https://github.com/boxine/bx_py_utils/blob/master/bx_py_utils/anonymize.py#L17-L47) - Anonymize the given string with special handling for eMail addresses and the possibility to truncate the output.
* [`anonymize_dict()`](https://github.com/boxine/bx_py_utils/blob/master/bx_py_utils/anonymize.py#L50-L70) - Returns a new dict with anonymized values for keys containing one of the given keywords.
* [`anonymize_dict()`](https://github.com/boxine/bx_py_utils/blob/master/bx_py_utils/anonymize.py#L50-L75) - Returns a new dict with anonymized values for keys containing one of the given keywords.

### bx_py_utils.auto_doc

Expand Down
2 changes: 1 addition & 1 deletion bx_py_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '103'
__version__ = '104'
7 changes: 6 additions & 1 deletion bx_py_utils/anonymize.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,18 @@ def anonymize_dict(
>>> anonymize_dict({'client_id': '123', 'client_secret': 'This is really secret'})
{'client_id': '123', 'client_secret': 'Txxx_xx_xxxxxx_xxxxxt'}
>>> anonymize_dict({'client_id': '123', 'client_secret': 'This is really secret'}, frozenset({'Client_Secret'}))
{'client_id': '123', 'client_secret': 'Txxx_xx_xxxxxx_xxxxxt'}
>>> anonymize_dict({'client_id': '123', 'client_secret': 'This is really secret'}, frozenset({'Secret'}))
{'client_id': '123', 'client_secret': 'Txxx_xx_xxxxxx_xxxxxt'}
>>> anonymize_dict({'client_id': '123', 'client_secret': 'This is really secret'}, max_length=10)
{'client_id': '123', 'client_secret': 'Txxx_xx_x…'}
"""
anonymized_data = data.copy() # Don't modify the original data!
lowercase_secret_keys = [key.lower() for key in secret_keys]
for key, value in anonymized_data.items():
if value:
if isinstance(value, str) and any(k in key.lower() for k in secret_keys):
if isinstance(value, str) and any(k in key.lower() for k in lowercase_secret_keys):
anonymized_data[key] = anonymize(value, max_length=max_length)
elif isinstance(value, dict):
anonymized_data[key] = anonymize_dict(value, secret_keys, max_length=max_length)
Expand Down
27 changes: 27 additions & 0 deletions bx_py_utils_tests/tests/test_anonymize.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import unittest

from parameterized import parameterized

from bx_py_utils.anonymize import anonymize_dict


Expand Down Expand Up @@ -40,6 +42,31 @@ def test_anonymize_dict(self):
},
)

@parameterized.expand(
[
('Authorization', 'Authorization'),
('Authorization', 'authorization'),
('authorization', 'Authorization'),
('authorization', 'authorization'),
('UserAuthorization', 'Authorization'),
('UserAuthorization', 'authorization'),
('user_authorization', 'Authorization'),
('user_authorization', 'authorization'),
]
)
def test_anonymize_dict_ignore_case(self, key, secret_key):
data = {
'lang': 'es',
key: 'secret',
}
self.assertEqual(
anonymize_dict(data, frozenset({secret_key})),
{
'lang': 'es',
key: 'sxxxxt',
},
)

def test_anonymize_dict_truncating_output(self):
data = {
'short_safe': 'SAFE',
Expand Down

0 comments on commit a833c44

Please sign in to comment.