mirror of
https://github.com/ansible/awx.git
synced 2026-05-24 00:57:48 -02:30
Ported old redact.py tests
This commit is contained in:
@@ -1,11 +1,9 @@
|
|||||||
|
|
||||||
import textwrap
|
import textwrap
|
||||||
|
import re
|
||||||
|
|
||||||
# AWX
|
# AWX
|
||||||
from awx.main.redact import UriCleaner
|
from awx.main.redact import UriCleaner
|
||||||
from awx.main.tests.base import BaseTest, URI
|
from awx.main.tests.base import URI
|
||||||
|
|
||||||
__all__ = ['UriCleanTests']
|
|
||||||
|
|
||||||
TEST_URIS = [
|
TEST_URIS = [
|
||||||
URI('no host', scheme='https', username='myusername', password='mypass', host=None),
|
URI('no host', scheme='https', username='myusername', password='mypass', host=None),
|
||||||
@@ -80,59 +78,89 @@ TEST_CLEARTEXT.append({
|
|||||||
'host_occurrences' : 4
|
'host_occurrences' : 4
|
||||||
})
|
})
|
||||||
|
|
||||||
class UriCleanTests(BaseTest):
|
|
||||||
|
|
||||||
# should redact sensitive usernames and passwords
|
def check_found(string, substr, count=-1, description=None, word_boundary=False):
|
||||||
def test_uri_scm_simple_redacted(self):
|
if word_boundary:
|
||||||
for uri in TEST_URIS:
|
count_actual = len(re.findall(r'\b%s\b' % re.escape(substr), string))
|
||||||
redacted_str = UriCleaner.remove_sensitive(str(uri))
|
else:
|
||||||
if uri.username:
|
count_actual = string.count(substr)
|
||||||
self.check_not_found(redacted_str, uri.username, uri.description)
|
|
||||||
if uri.password:
|
|
||||||
self.check_not_found(redacted_str, uri.password, uri.description)
|
|
||||||
|
|
||||||
# should replace secret data with safe string, UriCleaner.REPLACE_STR
|
msg = ''
|
||||||
def test_uri_scm_simple_replaced(self):
|
if description:
|
||||||
for uri in TEST_URIS:
|
msg = 'Test "%s".\n' % description
|
||||||
redacted_str = UriCleaner.remove_sensitive(str(uri))
|
if count == -1:
|
||||||
self.check_found(redacted_str, UriCleaner.REPLACE_STR, uri.get_secret_count())
|
assert count_actual > 0
|
||||||
|
else:
|
||||||
|
msg += 'Found %d occurances of "%s" instead of %d in: "%s"' % (count_actual, substr, count, string)
|
||||||
|
if count_actual != count:
|
||||||
|
raise Exception(msg)
|
||||||
|
|
||||||
# should redact multiple uris in text
|
def check_not_found(string, substr, description=None, word_boundary=False):
|
||||||
def test_uri_scm_multiple(self):
|
if word_boundary:
|
||||||
cleartext = ''
|
count = len(re.findall(r'\b%s\b' % re.escape(substr), string))
|
||||||
for uri in TEST_URIS:
|
else:
|
||||||
cleartext += str(uri) + ' '
|
count = string.find(substr)
|
||||||
for uri in TEST_URIS:
|
if count == -1:
|
||||||
cleartext += str(uri) + '\n'
|
count = 0
|
||||||
|
|
||||||
|
msg = ''
|
||||||
|
if description:
|
||||||
|
msg = 'Test "%s".\n' % description
|
||||||
|
msg += '"%s" found in: "%s"' % (substr, string)
|
||||||
|
if count != 0:
|
||||||
|
raise Exception(msg)
|
||||||
|
|
||||||
|
|
||||||
|
# should redact sensitive usernames and passwords
|
||||||
|
def test_uri_scm_simple_redacted():
|
||||||
|
for uri in TEST_URIS:
|
||||||
redacted_str = UriCleaner.remove_sensitive(str(uri))
|
redacted_str = UriCleaner.remove_sensitive(str(uri))
|
||||||
if uri.username:
|
if uri.username:
|
||||||
self.check_not_found(redacted_str, uri.username, uri.description)
|
check_not_found(redacted_str, uri.username, uri.description)
|
||||||
if uri.password:
|
if uri.password:
|
||||||
self.check_not_found(redacted_str, uri.password, uri.description)
|
check_not_found(redacted_str, uri.password, uri.description)
|
||||||
|
|
||||||
# should replace multiple secret data with safe string
|
# should replace secret data with safe string, UriCleaner.REPLACE_STR
|
||||||
def test_uri_scm_multiple_replaced(self):
|
def test_uri_scm_simple_replaced():
|
||||||
cleartext = ''
|
for uri in TEST_URIS:
|
||||||
find_count = 0
|
redacted_str = UriCleaner.remove_sensitive(str(uri))
|
||||||
for uri in TEST_URIS:
|
check_found(redacted_str, UriCleaner.REPLACE_STR, uri.get_secret_count())
|
||||||
cleartext += str(uri) + ' '
|
|
||||||
find_count += uri.get_secret_count()
|
|
||||||
|
|
||||||
for uri in TEST_URIS:
|
# should redact multiple uris in text
|
||||||
cleartext += str(uri) + '\n'
|
def test_uri_scm_multiple():
|
||||||
find_count += uri.get_secret_count()
|
cleartext = ''
|
||||||
|
for uri in TEST_URIS:
|
||||||
|
cleartext += str(uri) + ' '
|
||||||
|
for uri in TEST_URIS:
|
||||||
|
cleartext += str(uri) + '\n'
|
||||||
|
|
||||||
redacted_str = UriCleaner.remove_sensitive(cleartext)
|
redacted_str = UriCleaner.remove_sensitive(str(uri))
|
||||||
self.check_found(redacted_str, UriCleaner.REPLACE_STR, find_count)
|
if uri.username:
|
||||||
|
check_not_found(redacted_str, uri.username, uri.description)
|
||||||
|
if uri.password:
|
||||||
|
check_not_found(redacted_str, uri.password, uri.description)
|
||||||
|
|
||||||
# should redact and replace multiple secret data within a complex cleartext blob
|
# should replace multiple secret data with safe string
|
||||||
def test_uri_scm_cleartext_redact_and_replace(self):
|
def test_uri_scm_multiple_replaced():
|
||||||
for test_data in TEST_CLEARTEXT:
|
cleartext = ''
|
||||||
uri = test_data['uri']
|
find_count = 0
|
||||||
redacted_str = UriCleaner.remove_sensitive(test_data['text'])
|
for uri in TEST_URIS:
|
||||||
self.check_not_found(redacted_str, uri.username, uri.description)
|
cleartext += str(uri) + ' '
|
||||||
self.check_not_found(redacted_str, uri.password, uri.description)
|
find_count += uri.get_secret_count()
|
||||||
# Ensure the host didn't get redacted
|
|
||||||
self.check_found(redacted_str, uri.host, test_data['host_occurrences'], uri.description)
|
|
||||||
|
|
||||||
|
for uri in TEST_URIS:
|
||||||
|
cleartext += str(uri) + '\n'
|
||||||
|
find_count += uri.get_secret_count()
|
||||||
|
|
||||||
|
redacted_str = UriCleaner.remove_sensitive(cleartext)
|
||||||
|
check_found(redacted_str, UriCleaner.REPLACE_STR, find_count)
|
||||||
|
|
||||||
|
# should redact and replace multiple secret data within a complex cleartext blob
|
||||||
|
def test_uri_scm_cleartext_redact_and_replace():
|
||||||
|
for test_data in TEST_CLEARTEXT:
|
||||||
|
uri = test_data['uri']
|
||||||
|
redacted_str = UriCleaner.remove_sensitive(test_data['text'])
|
||||||
|
check_not_found(redacted_str, uri.username, uri.description)
|
||||||
|
check_not_found(redacted_str, uri.password, uri.description)
|
||||||
|
# Ensure the host didn't get redacted
|
||||||
|
check_found(redacted_str, uri.host, test_data['host_occurrences'], uri.description)
|
||||||
Reference in New Issue
Block a user