From 8cab05fb2cba4b5efd1e04bc157ae2fd8ea9607e Mon Sep 17 00:00:00 2001 From: PiranhaSa Date: Thu, 2 Nov 2023 11:01:51 +0100 Subject: [PATCH] feature/emitting contact_names instead of contact_name --- agent/result_parser.py | 6 +--- tests/whois_domain_agent_test.py | 51 +++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/agent/result_parser.py b/agent/result_parser.py index 956414b..11baf00 100644 --- a/agent/result_parser.py +++ b/agent/result_parser.py @@ -31,7 +31,6 @@ def parse_results(results: whois.parser.WhoisCom) -> Iterator[Dict[str, Any]]: if name is not None: names.add(name.lower()) - contact_name = scan_output_dict.pop("name", "") for name in names: if name != "": output = { @@ -52,10 +51,7 @@ def parse_results(results: whois.parser.WhoisCom) -> Iterator[Dict[str, Any]]: "name_servers": get_list_from_string( scan_output_dict.get("name_servers", "") ), - # TODO(ticket: os-3017): change the proto of v3.asset.domain_name.whois to send contact names. - "contact_name": contact_name[0] - if isinstance(contact_name, list) - else contact_name, + "contact_names": get_list_from_string(scan_output_dict.get("name", "")), "dnssec": get_list_from_string(scan_output_dict.get("dnssec", "")), } for field in OPTIONAL_FIELDS: diff --git a/tests/whois_domain_agent_test.py b/tests/whois_domain_agent_test.py index dfd3825..223d52d 100644 --- a/tests/whois_domain_agent_test.py +++ b/tests/whois_domain_agent_test.py @@ -110,6 +110,25 @@ "email": ["externaldnssupport@marks-and-spencer.com", "ivan.sly@iptwins.com"], } +SCAN_OUTPUT_NO_CONTACT_NAMES = { + "domain_name": "marksandspencer.at", + "registrar": "Key-Systems GmbH ( https://nic.at/registrar/404 )", + "name": [], + "org": ["Marks And Spencer P.l.c.", "IP TWINS S.A.S."], + "address": ["Waterside House", "35 North Wharf Road", "78 rue de Turbigo"], + "registrant_postal_code": ["W2 1NW", "75003"], + "city": ["London", "PARIS"], + "country": ["United Kingdom of Great Britain and Northern Ireland (the)", "France"], + "phone": ["+442087186494", "+33142789312"], + "fax": "+440207487267", + "updated_date": [ + datetime.datetime(2021, 6, 23, 10, 10, 57), + datetime.datetime(2021, 6, 23, 10, 7, 2), + datetime.datetime(2023, 1, 4, 19, 30, 24), + ], + "email": ["externaldnssupport@marks-and-spencer.com", "ivan.sly@iptwins.com"], +} + def testAgentWhois_whenDomainNameAsset_emitsMessages( scan_message: message.Message, @@ -163,7 +182,37 @@ def testAgentWhois_whenMultipleContactNames_emitsMessages( "externaldnssupport@marks-and-spencer.com", "ivan.sly@iptwins.com", ] - assert agent_mock[0].data["contact_name"] == "Catherine Shapiro" + assert agent_mock[0].data["contact_names"] == ["Catherine Shapiro", "Ivan SLY"] + + +def testAgentWhois_whenNoContactNames_emitsMessages( + scan_message: message.Message, + test_agent: whois_domain_agent.AgentWhoisDomain, + agent_persist_mock: Any, + mocker: plugin.MockerFixture, + agent_mock: List[message.Message], +) -> None: + """Tests running the agent and emitting vulnerabilities.""" + del agent_persist_mock + + mock_whois = mocker.patch("whois.whois", return_value=SCAN_OUTPUT_NO_CONTACT_NAMES) + test_agent.start() + test_agent.process(scan_message) + mock_whois.assert_called_once() + + assert len(agent_mock) > 0 + assert agent_mock[0].selector == "v3.asset.domain_name.whois" + assert agent_mock[0].data["name"] == "marksandspencer.at" + assert agent_mock[0].data["updated_date"] == [ + "2021-06-23T10:10:57", + "2021-06-23T10:07:02", + "2023-01-04T19:30:24", + ] + assert agent_mock[0].data["emails"] == [ + "externaldnssupport@marks-and-spencer.com", + "ivan.sly@iptwins.com", + ] + assert "contact_names" not in agent_mock[0].data def testAgentWhois_whenDomainNameInputIsEmpty_NotEmitsMessages(