Skip to content

Commit

Permalink
Merge pull request #32 from Ostorlab/fix/asn_error
Browse files Browse the repository at this point in the history
Handle the case when IP has no ASN
  • Loading branch information
3asm authored Feb 27, 2024
2 parents 671fbbb + 00bafc1 commit b9ff647
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions agent/whois_ip_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def _process_dns_record(self, message: m.Message) -> None:
except (
ipwhois.exceptions.IPDefinedError,
ipwhois.exceptions.HTTPLookupError,
ipwhois.exceptions.ASNRegistryError,
):
# Case where of the loopback address.
logger.warning(
Expand Down
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,15 @@ def scan_message_ipv_with_incorrect_version() -> message.Message:
"version": 5,
}
return message.Message.from_data(selector, data=msg_data)


@pytest.fixture()
def scan_message_global_ipv4_with_mask32() -> message.Message:
"""Creates a message of type v3.asset.ip with global IP address"""
selector = "v3.asset.ip"
msg_data = {
"host": "41.0.0.0",
"mask": "32",
"version": 4,
}
return message.Message.from_data(selector, data=msg_data)
24 changes: 23 additions & 1 deletion tests/whois_ip_agent_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Unittests for WhoisIP agent."""
from typing import List, Dict

import ipwhois
import pytest
from ostorlab.agent.message import message
from pytest_mock import plugin
import pytest

from agent import whois_ip_agent

Expand Down Expand Up @@ -281,3 +282,24 @@ def testWhoisIP_whenIPAssetHasIncorrectVersion_raiseValueError(
"""Test the CIDR Limit in case IP has incorrect version."""
with pytest.raises(ValueError, match="Incorrect ip version 5."):
test_agent.process(scan_message_ipv_with_incorrect_version)


def testWhoisIP_whenIPHasNoASN_doesNotCrash(
test_agent: whois_ip_agent.WhoisIPAgent,
agent_mock: List[message.Message],
mocker: plugin.MockerFixture,
scan_message_global_ipv4_with_mask32: message.Message,
) -> None:
"""Test the CIDR Limit in case IP has no ASN."""
mocker.patch(
"ostorlab.agent.mixins.agent_persist_mixin.AgentPersistMixin.add_ip_network",
return_value=True,
)
mocker.patch(
"agent.whois_ip_agent._get_whois_record",
side_effect=ipwhois.exceptions.ASNRegistryError,
)

test_agent.process(scan_message_global_ipv4_with_mask32)

assert len(agent_mock) == 0

0 comments on commit b9ff647

Please sign in to comment.