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

Handle the case when IP has no ASN #32

Merged
merged 3 commits into from
Feb 27, 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
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
Loading