Skip to content

Commit

Permalink
DB2 exception
Browse files Browse the repository at this point in the history
Remove will_be_mocked

fix: auto fix ibm_db import issue (Yelp#239)

Supports https://github.ibm.com/git-defenders/detect-secrets-discuss/issues/306

* fix: support python 2

* fix: cov
  • Loading branch information
justineyster committed Sep 9, 2020
1 parent 8be693b commit e28a7f8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 32 deletions.
1 change: 1 addition & 0 deletions detect_secrets/core/secrets_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 39 additions & 3 deletions detect_secrets/plugins/db2.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions tests/plugins/db2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import textwrap

import pytest
import requests
from mock import MagicMock
from mock import patch

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
26 changes: 0 additions & 26 deletions will_be_mocked

This file was deleted.

0 comments on commit e28a7f8

Please sign in to comment.