Skip to content

Commit

Permalink
Merge pull request #7446 from samaloney/bugfix-helio-ssl-crash
Browse files Browse the repository at this point in the history
Fix bug which causes Fido.search to crash if SSL verification fails for HelioClient
  • Loading branch information
nabobalis committed Feb 13, 2024
2 parents cf1360d + ae34052 commit 8349d7f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/7446.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug which caused ``Fido.search`` to crash due to SSL certificate verification error for the `~sunpy.net.helio.HECClient` now returns no results and logs a warning in this case.
12 changes: 11 additions & 1 deletion sunpy/net/helio/hec.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

from lxml import etree
from requests import Session
from requests.exceptions import SSLError
from zeep import Client
from zeep.transports import Transport

from astropy.io.votable.table import parse_single_table

from sunpy import log
from sunpy.net import attrs as a
from sunpy.net.base_client import BaseClient, QueryResponseTable
from sunpy.net.helio import attrs as ha
Expand Down Expand Up @@ -87,7 +89,12 @@ def __init__(self, link=None):
# This is for use in our test suite.
session.verify = not (bool(os.environ.get("NO_VERIFY_HELIO_SSL", 0)))
transport = Transport(session=session)
self.hec_client = Client(link, transport=transport)
try:
self.hec_client = Client(link, transport=transport)
except SSLError:
log.warning('SSL verification error for HEC client.\n'
'Set the \'NO_VERIFY_HELIO_SSL\' environment variable disable SSL verification for Helio.')
self.hec_client = None

@classmethod
def _can_handle_query(cls, *query):
Expand Down Expand Up @@ -137,6 +144,9 @@ def search(self, *args, **kwargs):
<BLANKLINE>
<BLANKLINE>
"""
if self.hec_client is None:
table = HECResponse([], client=self)
return table
qrdict = {}
for elem in args:
if isinstance(elem, a.Time):
Expand Down
11 changes: 11 additions & 0 deletions sunpy/net/helio/tests/test_helio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest import mock

import pytest
from requests.exceptions import SSLError

from sunpy.net import attrs as a
from sunpy.net.helio.hec import HECClient
Expand Down Expand Up @@ -247,6 +248,16 @@ def test_link_test_on_urlerror(mock_link_test):
link_test('') is None


@mock.patch('sunpy.net.helio.parser.webservice_parser', return_value=wsdl_urls())
@mock.patch('sunpy.net.helio.parser.taverna_parser', return_value=some_taverna_urls())
@mock.patch('sunpy.net.helio.parser.link_test', return_value='some text read')
@mock.patch('sunpy.net.helio.hec.Client', side_effect=SSLError('SSL error'))
def test_ssl_verify_error(mock_webservice, mock_taverna, mock_link, mock_zeep, caplog):
client = HECClient()
query = client.search(a.Time('2023/02/03', '2023/02/03'))
assert len(query) == 0
assert "Set the 'NO_VERIFY_HELIO_SSL' environment variable disable SSL verification for Helio." in caplog.text

@pytest.fixture(scope="session")
def client():
try:
Expand Down

0 comments on commit 8349d7f

Please sign in to comment.