From e28a7f8f733e000a121579c369bc464ed1c1357e Mon Sep 17 00:00:00 2001 From: Justin Eyster Date: Mon, 13 Jan 2020 15:12:58 -0500 Subject: [PATCH] DB2 exception Remove will_be_mocked fix: auto fix ibm_db import issue (#239) Supports https://github.ibm.com/git-defenders/detect-secrets-discuss/issues/306 * fix: support python 2 * fix: cov --- detect_secrets/core/secrets_collection.py | 1 + detect_secrets/plugins/db2.py | 42 +++++++++++++++++++++-- tests/plugins/db2_test.py | 5 ++- will_be_mocked | 26 -------------- 4 files changed, 42 insertions(+), 32 deletions(-) delete mode 100644 will_be_mocked diff --git a/detect_secrets/core/secrets_collection.py b/detect_secrets/core/secrets_collection.py index 243e4adf7..395343b76 100644 --- a/detect_secrets/core/secrets_collection.py +++ b/detect_secrets/core/secrets_collection.py @@ -146,6 +146,7 @@ def load_baseline_from_dict(cls, data): secret='will be replaced', lineno=item['line_number'], is_secret=item.get('is_secret'), + output_raw=result.output_raw, ) secret.secret_hash = item['hashed_secret'] result.data[filename][secret] = secret diff --git a/detect_secrets/plugins/db2.py b/detect_secrets/plugins/db2.py index c06e991bd..43b1f24d4 100644 --- a/detect_secrets/plugins/db2.py +++ b/detect_secrets/plugins/db2.py @@ -1,9 +1,45 @@ from __future__ import absolute_import +import os import re +import subprocess +import sys + +try: + import ibm_db +except ImportError as ie: # pragma: no cover + # Try to fix the ibm_db dynamic lib import issue + if 'darwin' not in sys.platform: + raise ie + + if hasattr(ie, 'msg'): + message = ie.msg + else: + # python 2 + message = ie.message + + if 'Library not loaded: libdb2.dylib' not in message: + raise ie + + if hasattr(ie, 'path'): + ibm_db_darwin_so = ie.path + else: + # python 2 + so_file_search = re.search(r'dlopen\((.*),', message, re.IGNORECASE) + if so_file_search: + ibm_db_darwin_so = so_file_search.group(1) + + if not ibm_db_darwin_so: + raise ie + + site_packages = os.path.dirname(ibm_db_darwin_so) + libdb2_dylib = os.path.join(site_packages, 'clidriver/lib/libdb2.dylib') + if os.path.exists(libdb2_dylib): + subprocess.call( + ['install_name_tool', '-change', 'libdb2.dylib', libdb2_dylib, ibm_db_darwin_so], + ) -import ibm_db -import requests + import ibm_db from .base import classproperty from .base import RegexBasedDetector @@ -135,7 +171,7 @@ def verify_db2_credentials( return VerifiedResult.VERIFIED_TRUE else: return VerifiedResult.VERIFIED_FALSE - except requests.exceptions.RequestException as e: + except Exception as e: if 'Timeout' in str(e): return VerifiedResult.UNVERIFIED else: diff --git a/tests/plugins/db2_test.py b/tests/plugins/db2_test.py index 3e3a5225b..6e54810c4 100644 --- a/tests/plugins/db2_test.py +++ b/tests/plugins/db2_test.py @@ -3,7 +3,6 @@ import textwrap import pytest -import requests from mock import MagicMock from mock import patch @@ -78,7 +77,7 @@ def test_verify_invalid_connect_returns_none(self, mock_db2_connect): @patch('detect_secrets.plugins.db2.ibm_db.connect') def test_verify_invalid_connect_throws_exception(self, mock_db2_connect): - mock_db2_connect.side_effect = requests.exceptions.RequestException('oops') + mock_db2_connect.side_effect = Exception('oops') potential_secret = PotentialSecret('test db2', 'test filename', DB2_PASSWORD) assert Db2Detector().verify( @@ -180,7 +179,7 @@ def test_verify_from_url(self, mock_db2_connect): @patch('detect_secrets.plugins.db2.ibm_db.connect') def test_verify_times_out(self, mock_db2_connect): - mock_db2_connect.side_effect = requests.exceptions.RequestException('Timeout') + mock_db2_connect.side_effect = Exception('Timeout') potential_secret = PotentialSecret('test db2', 'test filename', DB2_PASSWORD) assert Db2Detector().verify( diff --git a/will_be_mocked b/will_be_mocked deleted file mode 100644 index 546f98962..000000000 --- a/will_be_mocked +++ /dev/null @@ -1,26 +0,0 @@ -{ - "exclude": { - "files": "", - "lines": "" - }, - "generated_at": "2020-01-13T18:02:29Z", - "plugins_used": [ - { - "base64_limit": 4.5, - "name": "Base64HighEntropyString" - }, - { - "hex_limit": 3, - "name": "HexHighEntropyString" - }, - { - "name": "PrivateKeyDetector" - } - ], - "results": {}, - "version": "0.13.0-ibm.7-dss", - "word_list": { - "file": null, - "hash": null - } -}