|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +import textwrap |
| 4 | + |
| 5 | +import pytest |
| 6 | +from mock import MagicMock |
| 7 | +from mock import patch |
| 8 | + |
| 9 | +from detect_secrets.core.constants import VerifiedResult |
| 10 | +from detect_secrets.core.potential_secret import PotentialSecret |
| 11 | +from detect_secrets.plugins.db2 import DB2Detector |
| 12 | +from detect_secrets.plugins.db2 import get_other_factor |
| 13 | + |
| 14 | + |
| 15 | +DB2_USER = 'fake_user' |
| 16 | +DB2_PASSWORD = 'fake_password' |
| 17 | +DB2_PORT = '1234' |
| 18 | +DB2_HOSTNAME = 'fake.host.name' |
| 19 | +DB2_DATABASE = 'fake_database' |
| 20 | +DB2_CONN_STRING = 'database={DB2_DATABASE};hostname={DB2_HOSTNAME};port={DB2_PORT};' + \ |
| 21 | + 'protocol=tcpip;uid={DB2_USER};pwd={DB2_PASSWORD}' |
| 22 | +DB2_CONN_STRING = DB2_CONN_STRING.format( |
| 23 | + DB2_DATABASE=DB2_DATABASE, |
| 24 | + DB2_HOSTNAME=DB2_HOSTNAME, |
| 25 | + DB2_PORT=DB2_PORT, |
| 26 | + DB2_USER=DB2_USER, |
| 27 | + DB2_PASSWORD=DB2_PASSWORD, |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +class TestGHDetector(object): |
| 32 | + |
| 33 | + @pytest.mark.parametrize( |
| 34 | + 'payload, should_flag', |
| 35 | + [ |
| 36 | + ( |
| 37 | + 'database=test;hostname=host.test.com;' |
| 38 | + 'port=1;protocol=tcpip;uid=testid;pwd=secret', True, |
| 39 | + ), |
| 40 | + ('dbpwd=$omespeci@!ch@r$', True), |
| 41 | + ('db2_password = "astring"', True), |
| 42 | + ('"password": "Iusedb2!"', True), |
| 43 | + ('password = "ilikespaces"', True), |
| 44 | + ('pwd::anothersyntax!', True), |
| 45 | + ('DB2_PASSWORD = "@#!%#"', True), |
| 46 | + ('dashdb-password = "pass"', True), |
| 47 | + ('dashdb_host = notapassword', False), |
| 48 | + ('someotherpassword = "doesnt start right"', False), |
| 49 | + ], |
| 50 | + ) |
| 51 | + def test_analyze_string(self, payload, should_flag): |
| 52 | + logic = DB2Detector() |
| 53 | + |
| 54 | + output = logic.analyze_string(payload, 1, 'mock_filename') |
| 55 | + assert len(output) == int(should_flag) |
| 56 | + |
| 57 | + @patch('detect_secrets.plugins.db2.ibm_db.connect') |
| 58 | + def test_verify_invalid_secret(self, mock_db2_connect): |
| 59 | + mock_db2_connect.return_value = None |
| 60 | + |
| 61 | + potential_secret = PotentialSecret('test db2', 'test filename', DB2_PASSWORD) |
| 62 | + assert DB2Detector().verify( |
| 63 | + DB2_PASSWORD, |
| 64 | + '''user={}, |
| 65 | + password={}, |
| 66 | + database={}, |
| 67 | + host={}, |
| 68 | + port={}'''.format(DB2_USER, DB2_PASSWORD, DB2_DATABASE, DB2_HOSTNAME, DB2_PORT), |
| 69 | + potential_secret, |
| 70 | + ) == VerifiedResult.VERIFIED_FALSE |
| 71 | + |
| 72 | + mock_db2_connect.assert_called_with(DB2_CONN_STRING, '', '') |
| 73 | + |
| 74 | + @patch('detect_secrets.plugins.db2.ibm_db.connect') |
| 75 | + def test_verify_valid_secret(self, mock_db2_connect): |
| 76 | + mock_db2_connect.return_value = MagicMock() |
| 77 | + |
| 78 | + potential_secret = PotentialSecret('test db2', 'test filename', DB2_PASSWORD) |
| 79 | + assert DB2Detector().verify( |
| 80 | + DB2_PASSWORD, |
| 81 | + '''user={}, |
| 82 | + password={}, |
| 83 | + database={}, |
| 84 | + host={}, |
| 85 | + port={}'''.format(DB2_USER, DB2_PASSWORD, DB2_DATABASE, DB2_HOSTNAME, DB2_PORT), |
| 86 | + potential_secret, |
| 87 | + ) == VerifiedResult.VERIFIED_TRUE |
| 88 | + |
| 89 | + mock_db2_connect.assert_called_with(DB2_CONN_STRING, '', '') |
| 90 | + |
| 91 | + @patch('detect_secrets.plugins.db2.ibm_db.connect') |
| 92 | + def test_verify_unverified_secret(self, mock_db2_connect): |
| 93 | + mock_db2_connect.side_effect = Exception('oops') |
| 94 | + |
| 95 | + potential_secret = PotentialSecret('test db2', 'test filename', DB2_PASSWORD) |
| 96 | + assert DB2Detector().verify( |
| 97 | + DB2_PASSWORD, |
| 98 | + '''user={}, |
| 99 | + password={}, |
| 100 | + database={}, |
| 101 | + host={}, |
| 102 | + port={}'''.format(DB2_USER, DB2_PASSWORD, DB2_DATABASE, DB2_HOSTNAME, DB2_PORT), |
| 103 | + potential_secret, |
| 104 | + ) == VerifiedResult.UNVERIFIED |
| 105 | + |
| 106 | + mock_db2_connect.assert_called_with(DB2_CONN_STRING, '', '') |
| 107 | + |
| 108 | + def test_verify_no_other_factors(self): |
| 109 | + assert DB2Detector().verify( |
| 110 | + DB2_PASSWORD, |
| 111 | + 'password={}'.format(DB2_PASSWORD), |
| 112 | + ) == VerifiedResult.UNVERIFIED |
| 113 | + |
| 114 | + |
| 115 | +@pytest.mark.parametrize( |
| 116 | + 'content, factor_keyword_regex, factor_regex, expected_output', |
| 117 | + ( |
| 118 | + ( |
| 119 | + textwrap.dedent(""" |
| 120 | + user = {} |
| 121 | + """)[1:-1].format( |
| 122 | + DB2_USER, |
| 123 | + ), |
| 124 | + DB2Detector().username_keyword_regex, |
| 125 | + DB2Detector().username_regex, |
| 126 | + [DB2_USER], |
| 127 | + ), |
| 128 | + ( |
| 129 | + textwrap.dedent(""" |
| 130 | + port = {} |
| 131 | + """)[1:-1].format( |
| 132 | + DB2_PORT, |
| 133 | + ), |
| 134 | + DB2Detector().port_keyword_regex, |
| 135 | + DB2Detector().port_regex, |
| 136 | + [DB2_PORT], |
| 137 | + ), |
| 138 | + ( |
| 139 | + textwrap.dedent(""" |
| 140 | + database = {} |
| 141 | + """)[1:-1].format( |
| 142 | + DB2_DATABASE, |
| 143 | + ), |
| 144 | + DB2Detector().database_keyword_regex, |
| 145 | + DB2Detector().database_regex, |
| 146 | + [DB2_DATABASE], |
| 147 | + ), |
| 148 | + ( |
| 149 | + textwrap.dedent(""" |
| 150 | + host = {} |
| 151 | + """)[1:-1].format( |
| 152 | + DB2_HOSTNAME, |
| 153 | + ), |
| 154 | + DB2Detector().hostname_keyword_regex, |
| 155 | + DB2Detector().hostname_regex, |
| 156 | + [DB2_HOSTNAME], |
| 157 | + ), |
| 158 | + ), |
| 159 | +) |
| 160 | +def test_get_other_factor(content, factor_keyword_regex, factor_regex, expected_output): |
| 161 | + assert get_other_factor(content, factor_keyword_regex, factor_regex) == expected_output |
0 commit comments