178 lines
7.4 KiB
Python
178 lines
7.4 KiB
Python
"""Cloudflare zone discovery and DNS record upserts."""
|
|||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import unittest
|
||
|
|
|
||
|
|
from proxdomain.cloudflare import CloudflareClient
|
||
|
|
from proxdomain.errors import ProxdomainError
|
||
|
|
from tests.support import FakeTransport, cf, make_config
|
||
|
|
|
||
|
|
|
||
|
|
def client(transport: FakeTransport, **cloudflare) -> CloudflareClient:
|
||
|
|
config = make_config(cloudflare=cloudflare or None)
|
||
|
|
return CloudflareClient(config.cloudflare, transport=transport)
|
||
|
|
|
||
|
|
|
||
|
|
class ZoneTests(unittest.TestCase):
|
||
|
|
def test_base_domain_zone(self) -> None:
|
||
|
|
transport = FakeTransport()
|
||
|
|
transport.add("GET", "/client/v4/zones", cf([{"id": "z1", "name": "example.com"}]))
|
||
|
|
zone = client(transport).find_zone("web01.example.com", "example.com")
|
||
|
|
self.assertEqual(zone.id, "z1")
|
||
|
|
self.assertEqual(transport.calls[0].param("name"), "example.com")
|
||
|
|
|
||
|
|
def test_suffix_walk_when_base_domain_does_not_match(self) -> None:
|
||
|
|
transport = FakeTransport()
|
||
|
|
transport.add(
|
||
|
|
"GET",
|
||
|
|
"/client/v4/zones",
|
||
|
|
lambda call: cf([{"id": "z2", "name": "example.com"}])
|
||
|
|
if call.param("name") == "example.com"
|
||
|
|
else cf([]),
|
||
|
|
)
|
||
|
|
zone = client(transport, api_token="t").find_zone("app.example.com", "other.com")
|
||
|
|
self.assertEqual(zone.name, "example.com")
|
||
|
|
|
||
|
|
def test_zone_lookup_is_cached(self) -> None:
|
||
|
|
transport = FakeTransport()
|
||
|
|
transport.add("GET", "/client/v4/zones", cf([{"id": "z1", "name": "example.com"}]))
|
||
|
|
cf_client = client(transport)
|
||
|
|
cf_client.find_zone("a.example.com", "example.com")
|
||
|
|
cf_client.find_zone("b.example.com", "example.com")
|
||
|
|
self.assertEqual(len(transport.calls), 1)
|
||
|
|
|
||
|
|
def test_missing_zone(self) -> None:
|
||
|
|
transport = FakeTransport()
|
||
|
|
transport.add("GET", "/client/v4/zones", cf([]))
|
||
|
|
with self.assertRaises(ProxdomainError) as ctx:
|
||
|
|
client(transport).find_zone("a.example.com", "example.com")
|
||
|
|
self.assertIn("no Cloudflare zone", str(ctx.exception))
|
||
|
|
|
||
|
|
def test_verify_token(self) -> None:
|
||
|
|
transport = FakeTransport()
|
||
|
|
transport.add("GET", "/client/v4/user/tokens/verify", cf({"status": "active"}))
|
||
|
|
self.assertEqual(client(transport).verify_token(), "active")
|
||
|
|
|
||
|
|
|
||
|
|
class RecordTests(unittest.TestCase):
|
||
|
|
def test_create_a_record(self) -> None:
|
||
|
|
transport = FakeTransport()
|
||
|
|
transport.add("GET", "/client/v4/zones", cf([{"id": "z1", "name": "example.com"}]))
|
||
|
|
transport.add("GET", "/client/v4/zones/z1/dns_records", cf([]))
|
||
|
|
transport.add(
|
||
|
|
"POST",
|
||
|
|
"/client/v4/zones/z1/dns_records",
|
||
|
|
lambda call: cf({"id": "new", **call.body}),
|
||
|
|
)
|
||
|
|
result = client(transport).upsert_address_record("z1", "web01.example.com", "10.0.0.5")
|
||
|
|
self.assertEqual(result.action, "created")
|
||
|
|
self.assertEqual(result.record.id, "new")
|
||
|
|
body = transport.calls_to("POST", "/client/v4/zones/z1/dns_records")[0].body
|
||
|
|
self.assertEqual(body["type"], "A")
|
||
|
|
self.assertEqual(body["content"], "10.0.0.5")
|
||
|
|
self.assertTrue(body["proxied"])
|
||
|
|
self.assertEqual(body["ttl"], 1)
|
||
|
|
self.assertEqual(body["comment"], "managed by proxdomain")
|
||
|
|
|
||
|
|
def test_aaaa_for_ipv6(self) -> None:
|
||
|
|
transport = FakeTransport()
|
||
|
|
transport.add("GET", "/client/v4/zones/z1/dns_records", cf([]))
|
||
|
|
transport.add(
|
||
|
|
"POST",
|
||
|
|
"/client/v4/zones/z1/dns_records",
|
||
|
|
lambda call: cf({"id": "new", **call.body}),
|
||
|
|
)
|
||
|
|
result = client(transport).upsert_address_record("z1", "v6.example.com", "2001:db8::5")
|
||
|
|
self.assertEqual(result.record.type, "AAAA")
|
||
|
|
|
||
|
|
def test_unchanged_record_is_not_written(self) -> None:
|
||
|
|
existing = [
|
||
|
|
{
|
||
|
|
"id": "r1",
|
||
|
|
"name": "web01.example.com",
|
||
|
|
"type": "A",
|
||
|
|
"content": "10.0.0.5",
|
||
|
|
"proxied": True,
|
||
|
|
"ttl": 1,
|
||
|
|
}
|
||
|
|
]
|
||
|
|
transport = FakeTransport()
|
||
|
|
transport.add("GET", "/client/v4/zones/z1/dns_records", cf(existing))
|
||
|
|
transport.add("PATCH", "/client/v4/zones/z1/dns_records/r1", cf({"id": "r1"}))
|
||
|
|
result = client(transport).upsert_address_record("z1", "web01.example.com", "10.0.0.5")
|
||
|
|
self.assertEqual(result.action, "unchanged")
|
||
|
|
self.assertEqual(transport.count("PATCH", "/client/v4/zones/z1/dns_records/r1"), 0)
|
||
|
|
self.assertEqual(transport.count("POST", "/client/v4/zones/z1/dns_records"), 0)
|
||
|
|
|
||
|
|
def test_update_when_address_changes(self) -> None:
|
||
|
|
existing = [
|
||
|
|
{
|
||
|
|
"id": "r1",
|
||
|
|
"name": "web01.example.com",
|
||
|
|
"type": "A",
|
||
|
|
"content": "10.0.0.4",
|
||
|
|
"proxied": True,
|
||
|
|
"ttl": 1,
|
||
|
|
}
|
||
|
|
]
|
||
|
|
transport = FakeTransport()
|
||
|
|
transport.add("GET", "/client/v4/zones/z1/dns_records", cf(existing))
|
||
|
|
transport.add(
|
||
|
|
"PATCH",
|
||
|
|
"/client/v4/zones/z1/dns_records/r1",
|
||
|
|
lambda call: cf({"id": "r1", **call.body}),
|
||
|
|
)
|
||
|
|
result = client(transport).upsert_address_record("z1", "web01.example.com", "10.0.0.5")
|
||
|
|
self.assertEqual(result.action, "updated")
|
||
|
|
self.assertEqual(result.previous.content, "10.0.0.4")
|
||
|
|
body = transport.calls_to("PATCH", "/client/v4/zones/z1/dns_records/r1")[0].body
|
||
|
|
self.assertEqual(body["content"], "10.0.0.5")
|
||
|
|
|
||
|
|
def test_filters_records_by_name_and_type(self) -> None:
|
||
|
|
payload = [
|
||
|
|
{"id": "r1", "name": "other.example.com", "type": "A", "content": "1.1.1.1"},
|
||
|
|
{"id": "r2", "name": "web01.example.com", "type": "A", "content": "10.0.0.5"},
|
||
|
|
]
|
||
|
|
transport = FakeTransport()
|
||
|
|
transport.add("GET", "/client/v4/zones/z1/dns_records", cf(payload))
|
||
|
|
records = client(transport).list_records("z1", name="web01.example.com", rtype="A")
|
||
|
|
self.assertEqual([r.id for r in records], ["r2"])
|
||
|
|
|
||
|
|
def test_proxied_record_uses_automatic_ttl(self) -> None:
|
||
|
|
existing = [
|
||
|
|
{
|
||
|
|
"id": "r1",
|
||
|
|
"name": "web01.example.com",
|
||
|
|
"type": "A",
|
||
|
|
"content": "10.0.0.5",
|
||
|
|
"proxied": True,
|
||
|
|
"ttl": 1,
|
||
|
|
}
|
||
|
|
]
|
||
|
|
transport = FakeTransport()
|
||
|
|
transport.add("GET", "/client/v4/zones/z1/dns_records", cf(existing))
|
||
|
|
transport.add("PATCH", "/client/v4/zones/z1/dns_records/r1", cf({"id": "r1"}))
|
||
|
|
cf_client = client(transport, ttl=300, proxied=True)
|
||
|
|
result = cf_client.upsert_address_record("z1", "web01.example.com", "10.0.0.5")
|
||
|
|
self.assertEqual(result.action, "unchanged")
|
||
|
|
self.assertEqual(transport.count("PATCH", "/client/v4/zones/z1/dns_records/r1"), 0)
|
||
|
|
|
||
|
|
def test_delete_missing_record_is_not_an_error(self) -> None:
|
||
|
|
transport = FakeTransport()
|
||
|
|
transport.add(
|
||
|
|
"DELETE",
|
||
|
|
"/client/v4/zones/z1/dns_records/gone",
|
||
|
|
{"success": False, "errors": [{"code": 81044, "message": "not found"}]},
|
||
|
|
status=404,
|
||
|
|
)
|
||
|
|
self.assertFalse(client(transport).delete_record("z1", "gone"))
|
||
|
|
|
||
|
|
def test_delete_record(self) -> None:
|
||
|
|
transport = FakeTransport()
|
||
|
|
transport.add("DELETE", "/client/v4/zones/z1/dns_records/r1", cf({"id": "r1"}))
|
||
|
|
self.assertTrue(client(transport).delete_record("z1", "r1"))
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|