| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# (c) 2015, Toshio Kuratomi <[email protected]> |
| 3 |
# |
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
from ansible.module_utils.basic import heuristic_log_sanitize |
| 22 |
|
| 23 |
|
| 24 |
class TestHeuristicLogSanitize: |
| 25 |
def setup_method(self): |
| 26 |
self.URL_SECRET = 'http://username:pas:[email protected]/data' |
| 27 |
self.SSH_SECRET = 'username:pas:[email protected]/data' |
| 28 |
self.clean_data = repr(self._gen_data(3, True, True, 'no_secret_here')) |
| 29 |
self.url_data = repr(self._gen_data(3, True, True, self.URL_SECRET)) |
| 30 |
self.ssh_data = repr(self._gen_data(3, True, True, self.SSH_SECRET)) |
| 31 |
|
| 32 |
def _gen_data(self, records, per_rec, top_level, secret_text): |
| 33 |
hostvars = {'hostvars': {}} |
| 34 |
for i in range(1, records, 1): |
| 35 |
host_facts = { |
| 36 |
'host%s' % i: { |
| 37 |
'pstack': { |
| 38 |
'running': '875.1', |
| 39 |
'symlinked': '880.0', |
| 40 |
'tars': [], |
| 41 |
'versions': ['885.0'] |
| 42 |
}, |
| 43 |
} |
| 44 |
} |
| 45 |
if per_rec: |
| 46 |
host_facts['host%s' % i]['secret'] = secret_text |
| 47 |
hostvars['hostvars'].update(host_facts) |
| 48 |
if top_level: |
| 49 |
hostvars['secret'] = secret_text |
| 50 |
return hostvars |
| 51 |
|
| 52 |
def test_did_not_hide_too_much(self): |
| 53 |
assert heuristic_log_sanitize(self.clean_data) == self.clean_data |
| 54 |
|
| 55 |
def test_hides_url_secrets(self): |
| 56 |
url_output = heuristic_log_sanitize(self.url_data) |
| 57 |
# Basic functionality: Successfully hid the password |
| 58 |
assert 'pas:word' not in url_output |
| 59 |
|
| 60 |
# Slightly more advanced, we hid all of the password despite the ":" |
| 61 |
assert 'pas' not in url_output |
| 62 |
|
| 63 |
# In this implementation we replace the password with 8 "*" which is |
| 64 |
# also the length of our password. The url fields should be able to |
| 65 |
# accurately detect where the password ends so the length should be |
| 66 |
# the same: |
| 67 |
assert len(url_output) == len(self.url_data) |
| 68 |
|
| 69 |
def test_hides_ssh_secrets(self): |
| 70 |
ssh_output = heuristic_log_sanitize(self.ssh_data) |
| 71 |
assert 'pas:word' not in ssh_output |
| 72 |
|
| 73 |
# Slightly more advanced, we hid all of the password despite the ":" |
| 74 |
assert 'pas' not in ssh_output |
| 75 |
|
| 76 |
# ssh checking is harder as the heuristic is overzealous in many |
| 77 |
# cases. Since the input will have at least one ":" present before |
| 78 |
# the password we can tell some things about the beginning and end of |
| 79 |
# the data, though: |
| 80 |
assert ssh_output.startswith("{'") |
| 81 |
assert ssh_output.endswith("}") |
| 82 |
assert ":********@foo.com/data'" in ssh_output |
| 83 |
|
| 84 |
def test_hides_parameter_secrets(self): |
| 85 |
output = heuristic_log_sanitize('token="secret", user="person", token_entry="test=secret"', frozenset(['secret'])) |
| 86 |
assert 'secret' not in output |
| 87 |
|
| 88 |
def test_no_password(self): |
| 89 |
assert heuristic_log_sanitize('foo@bar') == 'foo@bar' |