Skip to content

Commit

Permalink
Merge pull request #102 from dtaniwaki/allow-to-set-ca-bundle
Browse files Browse the repository at this point in the history
Set TLS verify
  • Loading branch information
phumpal authored Jun 17, 2021
2 parents ab802ea + d970443 commit 9967377
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
24 changes: 15 additions & 9 deletions airbrake/notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ def __init__(self, project_id=None, api_key=None, host=None, timeout=None,

self._exc_queue = utils.CheckableQueue()

self._session = requests.Session()
if "verify" in config:
self._session.verify = config["verify"]
elif "AIRBRAKE_CA_BUNDLE" in os.environ:
self._session.verify = os.environ["AIRBRAKE_CA_BUNDLE"]

def __repr__(self):
"""Return value for the repr function."""
return ("Airbrake(project_id=%s, api_key=*****, environment=%s)"
Expand Down Expand Up @@ -350,7 +356,7 @@ def notify(self, exception):

headers = {'Content-Type': 'application/json'}
api_key = {'key': self.api_key}
response = requests.post(
response = self._session.post(
self.api_url,
data=json.dumps(payload, cls=utils.FailProofJSONEncoder,
sort_keys=True),
Expand Down Expand Up @@ -378,13 +384,13 @@ def deploy(self, env=None, username=None, repository=None,
headers = {'Content-Type': 'application/json'}
api_key = {'key': self.api_key}

response = requests.post(self.deploy_url,
data=json.dumps(
payload,
cls=utils.FailProofJSONEncoder,
sort_keys=True),
headers=headers,
params=api_key,
timeout=self.timeout)
response = self._session.post(self.deploy_url,
data=json.dumps(
payload,
cls=utils.FailProofJSONEncoder,
sort_keys=True),
headers=headers,
params=api_key,
timeout=self.timeout)
response.raise_for_status()
return response
41 changes: 27 additions & 14 deletions tests/test_notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def __repr__(self):
self.logger.exception(e, extra=extra)

def test_notify_context(self):
with mock.patch('requests.post') as requests_post:
with mock.patch('requests.Session.post') as requests_post:
version = platform.python_version()
plat = platform.platform()
environment = u"testing123"
Expand Down Expand Up @@ -150,7 +150,7 @@ def test_notify_context(self):
self.assertEqual(expected_context, actual_context)

def test_log_with_severity(self):
with mock.patch('requests.post') as requests_post:
with mock.patch('requests.Session.post') as requests_post:
ab = Airbrake(project_id=1234, api_key='fake')
ab.log("this is a test", severity=ErrorLevels.CRITICAL)

Expand Down Expand Up @@ -192,7 +192,7 @@ def test_capture(self):
try:
raise ValueError("oh nos")
except Exception:
with mock.patch('requests.post') as requests_post:
with mock.patch('requests.Session.post') as requests_post:
ab.capture()

exc_info = sys.exc_info()
Expand Down Expand Up @@ -229,7 +229,7 @@ def test_capture(self):
def test_capture_decorator(self):
ab = Airbrake(project_id=1234, api_key='fake')

with mock.patch('requests.post') as requests_post:
with mock.patch('requests.Session.post') as requests_post:

@capture(ab)
def faulty_func(msg):
Expand Down Expand Up @@ -257,7 +257,7 @@ def test_notify_str(self):
exception_type)
self.assertEqual(expected_payload, notice.payload)

with mock.patch('requests.post') as requests_post:
with mock.patch('requests.Session.post') as requests_post:
ab.notify(exception_str)
data = json.loads(requests_post.call_args[1]['data'])
self.assertEqual(expected_payload, data)
Expand All @@ -274,7 +274,7 @@ def test_notify_exception(self):

self.assertEqual(expected_payload, notice.payload)

with mock.patch('requests.post') as requests_post:
with mock.patch('requests.Session.post') as requests_post:
ab.notify(exception)
data = json.loads(requests_post.call_args[1]['data'])
self.assertEqual(expected_payload, data)
Expand Down Expand Up @@ -312,13 +312,13 @@ def test_notify_error(self):

self.assertEqual(expected_payload, notice.payload)

with mock.patch('requests.post') as requests_post:
with mock.patch('requests.Session.post') as requests_post:
ab.notify(error)
data = json.loads(requests_post.call_args[1]['data'])
self.assertEqual(expected_payload, data)

def test_deploy_payload(self):
with mock.patch('requests.post') as requests_post:
with mock.patch('requests.Session.post') as requests_post:
ab = Airbrake(project_id=1234, api_key='fake', environment='test')
ab.deploy('test',
'user1',
Expand All @@ -340,7 +340,7 @@ def test_deploy_payload(self):
self.assertEqual(expected_call_args, requests_post.call_args)

def test_deploy_revision(self):
with mock.patch('requests.post') as requests_post:
with mock.patch('requests.Session.post') as requests_post:
ab = Airbrake(project_id=1234, api_key='fake', environment='test')
ab.deploy('test',
'user1',
Expand All @@ -363,12 +363,12 @@ def check_timeout(self, timeout=None, expected_timeout=None):
api_key='fake',
environment='test')

with mock.patch('requests.post') as requests_post:
with mock.patch('requests.Session.post') as requests_post:
ab.deploy('test', 'user1')
timeout = requests_post.call_args[1]['timeout']
self.assertEqual(expected_timeout, timeout)

with mock.patch('requests.post') as requests_post:
with mock.patch('requests.Session.post') as requests_post:
notice = ab.build_notice("This is a test")
ab.notify(notice)
timeout = requests_post.call_args[1]['timeout']
Expand All @@ -389,7 +389,7 @@ def check_filter(self, ab, expected_params):

notice = ab.build_notice("This is a test", params)

with mock.patch('requests.post') as requests_post:
with mock.patch('requests.Session.post') as requests_post:
ab.notify(notice)
data = json.loads(requests_post.call_args[1]['data'])
self.assertEqual(expected_params, data["params"])
Expand Down Expand Up @@ -444,7 +444,7 @@ def test_notice_severity(self):
self.assertEqual(ErrorLevels.CRITICAL,
notice.payload['context']['severity'])

with mock.patch('requests.post') as requests_post:
with mock.patch('requests.Session.post') as requests_post:
ab.notify(notice)
data = json.loads(requests_post.call_args[1]['data'])
error_level = data['context']['severity']
Expand All @@ -469,7 +469,7 @@ def early_exit_syshook(*exc_info):

ab.excepthook = early_exit_syshook

with mock.patch('requests.post') as requests_post:
with mock.patch('requests.Session.post') as requests_post:
try:
raise Exception("raise to sys level")
except Exception:
Expand All @@ -484,6 +484,19 @@ def early_exit_syshook(*exc_info):
self.assertEqual(ErrorLevels.ERROR, error_level)
self.assertTrue(self.preserved_syshook)

def test_default_verify(self):
ab = Airbrake(project_id=1234, api_key='fake')
self.assertTrue(ab._session.verify)

def test_arg_verify(self):
ab = Airbrake(project_id=1234, api_key='fake', verify="foo")
self.assertEqual("foo", ab._session.verify)

def test_env_verify(self):
with mock.patch.dict(os.environ, {'AIRBRAKE_CA_BUNDLE': 'bar'}):
ab = Airbrake(project_id=1234, api_key='fake')
self.assertEqual("bar", ab._session.verify)


if __name__ == '__main__':
unittest.main()

0 comments on commit 9967377

Please sign in to comment.