diff --git a/strato_mcp.py b/strato_mcp.py index aa31eb0..de9aa95 100644 --- a/strato_mcp.py +++ b/strato_mcp.py @@ -242,25 +242,47 @@ def strato_check_dns(name: str) -> str: except OSError as exc: resolved = {"error": f"not resolvable: {exc.strerror or exc}"} target = expected[0].value.rstrip(".").lower() if expected else None - matches: bool | None = None - if target and "addresses" in resolved: - names = [resolved["canonical_name"], *resolved["aliases"]] - matches = any(n.rstrip(".").lower() == target for n in names) or _resolves_same( - target, resolved["addresses"] - ) - elif target: - matches = False + if not target: + status = "not_in_zone" + elif "addresses" not in resolved: + status = "not_resolvable" + elif any(n.rstrip(".").lower() == target for n in [resolved["canonical_name"], *resolved["aliases"]]): + status = "match" + elif _resolves_same(target, resolved["addresses"]): + status = "match" + elif all(_is_private(ip) for ip in resolved["addresses"]): + status = "local_override" + else: + status = "mismatch" return _json({ "name": host, "zone_cname_target": target, "resolver": resolved, - "matches_zone": matches, - "note": "Uses the MCP host's resolver; local DNS overrides or caches can differ from public DNS.", + "status": status, + "explanation": { + "match": "The resolver follows the STRATO CNAME.", + "local_override": ( + "The resolver answers with a private address instead of following the CNAME " + "(local DNS / split-horizon). Public resolution cannot be judged from here." + ), + "mismatch": "The resolver returns something else than the STRATO CNAME (cache/TTL or wrong record).", + "not_resolvable": "The name does not resolve (yet).", + "not_in_zone": "There is no CNAME for this name in the STRATO zone.", + }[status], }) except Exception as exc: raise _fail(exc) from None +def _is_private(address: str) -> bool: + import ipaddress + + try: + return ipaddress.ip_address(address).is_private + except ValueError: + return False + + def _resolves_same(target: str, addresses: list[str]) -> bool: try: return bool(set(socket.gethostbyname_ex(target)[2]) & set(addresses)) diff --git a/tests/test_strato_changes.py b/tests/test_strato_changes.py index 81de8b2..9016f11 100644 --- a/tests/test_strato_changes.py +++ b/tests/test_strato_changes.py @@ -247,6 +247,19 @@ class McpTicketTests(unittest.TestCase): self.mcp.strato_apply_change("create", "CNAME", "chat", ticket, True, value="gw.example.net") self.assertNotIn("POST", [r.method for r in self.opener.requests]) + def test_check_dns_statuses(self) -> None: + from unittest import mock + + self.opener.responses[:0] = [] + table = { + "media.example.de": ("media.example.de", [], ["192.168.1.2"]), + "proxy.example.net": ("proxy.example.net", [], ["203.0.113.5"]), + } + with mock.patch.object(self.mcp.socket, "gethostbyname_ex", side_effect=lambda h: table[h]): + result = json.loads(self.mcp.strato_check_dns("media")) + self.assertEqual(result["status"], "local_override") + self.assertEqual(result["zone_cname_target"], "proxy.example.net") + def test_errors_are_tool_errors(self) -> None: from mcp.server.fastmcp.exceptions import ToolError