Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch ASN Parse error more gracefully #626

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions apps/greencheck/domain_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import tld
from django.utils import timezone
from ipwhois.asn import IPASN
from ipwhois.exceptions import IPDefinedError
from ipwhois.exceptions import IPDefinedError, ASNParseError
from ipwhois.net import Net

from .choices import GreenlistChoice
Expand Down Expand Up @@ -309,7 +309,10 @@ def check_for_matching_asn(self, ip_address):

try:
asn_result = self.asn_from_ip(ip_address)
except IPDefinedError:
except (ASNParseError, IPDefinedError) as ex:
logger.warning(
f"Unable to parse ASN for IP: {ip_address} - error type: {type(ex).__name__} {ex}"
)
return False
except Exception as err:
logger.exception(err)
Expand Down
15 changes: 14 additions & 1 deletion apps/greencheck/tests/test_domain_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ def test_with_green_domain_by_asn_double(self, green_asn, checker):
assert isinstance(res, legacy_workers.SiteCheck)
assert res.hosting_provider_id == green_asn.hostingprovider.id

def test_asn_from_ip_fails_gracefully_with_bad_asn_lookup(self, checker, caplog):
"""
Sometimes calling lookup() on an IP address raises a ASNParseError.
Do we catch this exception and log it?
"""

checker.asn_from_ip = mock.MagicMock(side_effect=domain_check.ASNParseError)
with caplog.at_level(logging.WARNING):
res = checker.check_for_matching_asn("23.32.24.203")

assert res is False
logged_error = caplog.text
assert "ASNParseError" in logged_error

def test_with_green_domain_by_non_resolving_asn(self, green_asn, checker):
"""
Sometimes the service we use for resolving ASNs returns
Expand Down Expand Up @@ -210,7 +224,6 @@ class TestDomainCheckByCarbonTxt:
def test_lookup_green_domain(
self, green_domain_factory, green_ip_factory, mocker, checker
):

# mock our network lookup, so we get a consistent response when
# looking up our domains
green_ip = green_ip_factory.create()
Expand Down