diff --git a/README.md b/README.md index 38e61b2..1975212 100644 --- a/README.md +++ b/README.md @@ -200,10 +200,9 @@ RecordEntity mx = RecordEntity.build("example.com", RecordType.MX, 3600, "mail.e mx.setPriority(10); cfDnsClient.recordCreate(zone, mx); -// SRV record with structured data (keys as in the Cloudflare API) +// SRV record with structured data (keys as in the Cloudflare API), the priority is part of the data RecordEntity srv = RecordEntity.build("_sip._tcp.example.com", RecordType.SRV, 3600, null); -srv.setPriority(10); -srv.setData(Map.of("weight", 5, "port", 5060, "target", "sip.example.com")); +srv.setData(Map.of("priority", 10, "weight", 5, "port", 5060, "target", "sip.example.com")); cfDnsClient.recordCreate(zone, srv); ``` diff --git a/changelog.md b/changelog.md index eec59bb..1af5514 100644 --- a/changelog.md +++ b/changelog.md @@ -12,7 +12,7 @@ - batch DELETE sends only the record IDs, batch PATCH sends all writable attributes which aren't `null` (before: only the content) - `CfDnsClient` uses one shared HTTP client with connection pooling instead of a new one per request and implements `AutoCloseable`, call `close()` to release the connections - `CloudflareApiException` provides the HTTP status code (`getStatusCode()`) and the errors reported by Cloudflare (`getErrors()`) - - `RecordEntity` supports the attributes `priority` (MX, SRV, URI), `data` (e.g. SRV, CAA), `comment` and `tags`; they are also considered by batch operations, as is `proxied` + - `RecordEntity` supports the attributes `priority` (MX, URI), `data` (e.g. SRV incl. its priority, CAA), `comment` and `tags`; they are also considered by batch operations, as is `proxied` - `recordCreate` and `recordUpdate` don't modify the given `RecordEntity` anymore and send only writable attributes - fluent `update(content)` sends only the new content (partial update) - filtering by exactly one record type is done by Cloudflare (`?type=`), unknown record types in responses don't break the client-side filter anymore diff --git a/src/main/java/codes/thischwa/cf/CfDnsClient.java b/src/main/java/codes/thischwa/cf/CfDnsClient.java index e428435..cbfad1f 100644 --- a/src/main/java/codes/thischwa/cf/CfDnsClient.java +++ b/src/main/java/codes/thischwa/cf/CfDnsClient.java @@ -104,6 +104,8 @@ public class CfDnsClient extends CfBasicHttpClient { private final boolean emptyResultThrowsException; + private final int recordsPerPage; + /** * Constructs a new instance of {@code CfDnsClient}. * @@ -114,9 +116,19 @@ public class CfDnsClient extends CfBasicHttpClient { * @param auth The authentication mechanism to use (ApiTokenAuth or EmailKeyAuth) */ CfDnsClient(boolean emptyResultThrowsException, String baseUrl, CfDnsClientBuilder.CfAuth auth) { + this(emptyResultThrowsException, baseUrl, auth, RECORDS_PER_PAGE); + } + + /** + * Constructs a new instance of {@code CfDnsClient} with a custom page size for fetching all DNS + * records. Intended for tests, which need to fetch multiple pages with only a few records. + */ + CfDnsClient(boolean emptyResultThrowsException, String baseUrl, CfDnsClientBuilder.CfAuth auth, + int recordsPerPage) { super(baseUrl, auth); this.responseValidator = new ResponseValidator(emptyResultThrowsException); this.emptyResultThrowsException = emptyResultThrowsException; + this.recordsPerPage = recordsPerPage; } private static String buildFqdn(ZoneEntity zone, String subdomain) { @@ -239,7 +251,7 @@ public class CfDnsClient extends CfBasicHttpClient { String path = CfRequest.RECORD_LIST.buildPath(zone.getId()); if (pagingRequest == null) { return filterAndSetZoneRecords(zone, null, - getAllPages(path, RecordMultipleResponse.class, RECORDS_PER_PAGE)); + getAllPages(path, RecordMultipleResponse.class, recordsPerPage)); } RecordMultipleResponse resp = getRequest(pagingRequest.addQueryString(path), RecordMultipleResponse.class); checkResponse(resp); @@ -265,7 +277,7 @@ public class CfDnsClient extends CfBasicHttpClient { throws CloudflareApiException { String fqdn = buildFqdn(zone, subdomain); String endpoint = addTypeFilter(CfRequest.RECORD_LIST_NAME.buildPath(zone.getId(), fqdn), types); - List recs = getAllPages(endpoint, RecordMultipleResponse.class, RECORDS_PER_PAGE); + List recs = getAllPages(endpoint, RecordMultipleResponse.class, recordsPerPage); return filterAndSetZoneRecords(zone, types, recs); } @@ -283,7 +295,7 @@ public class CfDnsClient extends CfBasicHttpClient { public List recordListByType(ZoneEntity zone, @Nullable RecordType... types) throws CloudflareApiException { String endpoint = addTypeFilter(CfRequest.RECORD_LIST.buildPath(zone.getId()), types); - List recs = getAllPages(endpoint, RecordMultipleResponse.class, RECORDS_PER_PAGE); + List recs = getAllPages(endpoint, RecordMultipleResponse.class, recordsPerPage); return filterAndSetZoneRecords(zone, types, recs); } diff --git a/src/main/java/codes/thischwa/cf/model/RecordEntity.java b/src/main/java/codes/thischwa/cf/model/RecordEntity.java index a2bd5ec..2f8f017 100644 --- a/src/main/java/codes/thischwa/cf/model/RecordEntity.java +++ b/src/main/java/codes/thischwa/cf/model/RecordEntity.java @@ -20,7 +20,7 @@ import org.jetbrains.annotations.Nullable; *
  • Content of the DNS record, such as an IP address. *
  • Flags indicating whether the record is proxiable or proxied. *
  • TTL (Time-To-Live) for the DNS record. - *
  • Priority, required for MX, SRV and URI records. + *
  • Priority, required for MX and URI records (for SRV records it's part of the data). *
  • Structured data, required for e.g. SRV, CAA, LOC or TLSA records instead of content. *
  • An optional comment and tags. *
  • A locked status to indicate the immutability of the record. @@ -43,7 +43,8 @@ public class RecordEntity extends AbstractEntity { private Boolean proxied; private Integer ttl; /** - * Priority of the record, required for MX, SRV and URI records. + * Priority of the record, required for MX and URI records. For SRV records the priority is + * part of {@link #data}. */ @Nullable private Integer priority; @@ -58,7 +59,8 @@ public class RecordEntity extends AbstractEntity { @Nullable private List tags; /** - * Structured content of the record, e.g. for SRV records the keys "weight", "port" and "target". + * Structured content of the record, e.g. for SRV records the keys "priority", "weight", "port" + * and "target". * The keys are the same as in the Cloudflare API. */ @Nullable diff --git a/src/test/java/codes/thischwa/cf/CfClientTest.java b/src/test/java/codes/thischwa/cf/CfClientTest.java index b6a396f..635c4e1 100644 --- a/src/test/java/codes/thischwa/cf/CfClientTest.java +++ b/src/test/java/codes/thischwa/cf/CfClientTest.java @@ -1,8 +1,11 @@ package codes.thischwa.cf; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -13,6 +16,7 @@ import codes.thischwa.cf.model.PagingRequest; import codes.thischwa.cf.model.RecordEntity; import codes.thischwa.cf.model.RecordType; import codes.thischwa.cf.model.ZoneEntity; +import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -36,6 +40,9 @@ public class CfClientTest { private final CfDnsClient client = new CfDnsClientBuilder().withEmptyResultThrowsException(true).withApiTokenAuth(API_TOKEN).build(); + private static final String IP_PREFIX = "130.0.0."; + private static final String UPDATED_IP_PREFIX = "130.1.0."; + @BeforeAll static void checkEnv() { assumeTrue(API_TOKEN != null && !API_TOKEN.isBlank(), "API_TOKEN not set; skipping client tests"); @@ -207,9 +214,14 @@ public class CfClientTest { assertFalse(fluentList.isEmpty()); assertTrue(fluentList.stream().anyMatch(re -> re.getId().equals(createdRe1.getId()))); - // update AAAA record + // update AAAA record, the given record (incl. its timestamps) must not be modified createdRe2.setContent("2a0a:4cc0:c0:2e4::2"); - client.recordUpdate(z, createdRe2); + Instant createdOn = createdRe2.getCreatedOn(); + RecordEntity updatedRe2 = client.recordUpdate(z, createdRe2); + assertEquals(createdOn, createdRe2.getCreatedOn()); + assertNotNull(createdRe2.getModifiedOn()); + assertEquals("2a0a:4cc0:c0:2e4::2", updatedRe2.getContent()); + assertEquals(z.getId(), updatedRe2.getZoneId()); aaaaRecords = client.recordListBySubdomain(z, randomSld, RecordType.AAAA); assertEquals(1, aaaaRecords.size()); r = aaaaRecords.get(0); @@ -246,10 +258,6 @@ public class CfClientTest { assertTrue(exception.getMessage().contains("Must be one of:")); } - private static final String IP_PREFIX = "130.0.0."; - private static final String UPDATED_IP_PREFIX = "130.1.0."; - - @Test void testBatch() throws Exception { // starting point: already existing zone 'mein-d-ns.de' @@ -518,6 +526,15 @@ public class CfClientTest { Set allRecordIds = allRecords.stream().map(RecordEntity::getId).collect(Collectors.toSet()); assertEquals(createdRecords.size(), allRecordIds.size()); assertTrue(allRecordIds.containsAll(generatedRecordIds)); + + // Without PagingRequest all pages must be fetched, also if the page size is small + try (CfDnsClient smallPageClient = new CfDnsClient(true, CfDnsClientBuilder.DEFAULT_BASEURL, + new CfDnsClientBuilder.ApiTokenAuth(API_TOKEN), 5)) { + assertEquals(allRecordIds, smallPageClient.recordList(zone).stream() + .map(RecordEntity::getId).collect(Collectors.toSet())); + assertEquals(createdRecords.size(), smallPageClient.recordListBySubdomain(zone, pagingSld).size()); + assertEquals(createdRecords.size(), smallPageClient.recordListByType(zone, RecordType.A).size()); + } } finally { try { client.recordDeleteTypeIfExists(zone, pagingSld, RecordType.A); @@ -525,4 +542,186 @@ public class CfClientTest { } } + @Test + void testApiErrorDetails() throws Exception { + ZoneEntity zone = client.zoneGet(ZONE_STR); + String sub = "invalid-" + System.currentTimeMillis(); + try { + // an A record with an invalid IP is rejected by Cloudflare + CloudflareApiException e = assertThrows(CloudflareApiException.class, + () -> client.recordCreateSubdomain(zone, sub, TTL, RecordType.A, "not-an-ip")); + assertEquals(CloudflareApiException.class, e.getClass()); + assertEquals(400, e.getStatusCode()); + assertFalse(e.getErrors().isEmpty()); + assertTrue(e.getErrors().get(0).getCode() > 0); + // the Cloudflare error message is part of the exception message, not wrapped + assertTrue(e.getMessage().contains(e.getErrors().get(0).getMessage()), e.getMessage()); + } finally { + try { + client.recordDeleteTypeIfExists(zone, sub, RecordType.A); + } catch (Exception e) { /* ignore */ } + } + } + + @Test + void testRecordDeleteUnknownId() throws Exception { + ZoneEntity zone = client.zoneGet(ZONE_STR); + CloudflareApiException e = assertThrows(CloudflareApiException.class, + () -> client.recordDelete(zone, "0123456789abcdef0123456789abcdef")); + assertInstanceOf(CloudflareNotFoundException.class, e); + assertEquals(404, e.getStatusCode()); + assertFalse(e.getErrors().isEmpty()); + } + + @Test + void testZoneNotFound() throws Exception { + String unknownZone = "not-existing-" + System.currentTimeMillis() + ".de"; + // emptyResultThrowsException = true + assertThrows(CloudflareNotFoundException.class, () -> client.zoneGet(unknownZone)); + assertThrows(CloudflareNotFoundException.class, () -> client.zone(unknownZone)); + + // emptyResultThrowsException = false + try (CfDnsClient lenientClient = new CfDnsClientBuilder().withApiTokenAuth(API_TOKEN).build()) { + assertNull(lenientClient.zoneGet(unknownZone)); + assertThrows(CloudflareNotFoundException.class, () -> lenientClient.zone(unknownZone)); + assertNotNull(lenientClient.zoneGet(ZONE_STR)); + } + } + + @Test + void testClientClose() throws Exception { + CfDnsClient closeableClient = new CfDnsClientBuilder().withApiTokenAuth(API_TOKEN).build(); + // multiple requests with the same client + assertNotNull(closeableClient.zoneGet(ZONE_STR)); + assertFalse(closeableClient.zoneList().isEmpty()); + assertDoesNotThrow(closeableClient::close); + } + + @Test + void testMultiLevelSubdomainAndApex() throws Exception { + ZoneEntity zone = client.zoneGet(ZONE_STR); + String multiLevel = "a.b-" + System.currentTimeMillis(); + String apexContent = "cf-client-test-" + System.currentTimeMillis(); + List created = new ArrayList<>(); + try { + // multi-level subdomain + RecordEntity rec = client.recordCreateSubdomain(zone, multiLevel, TTL, RecordType.A, "130.0.0.10"); + created.add(rec); + assertEquals(multiLevel + "." + ZONE_STR, rec.getName()); + assertEquals(multiLevel, rec.getSubdomain()); + List fetched = client.recordListBySubdomain(zone, multiLevel, RecordType.A); + assertEquals(1, fetched.size()); + assertEquals(multiLevel, fetched.get(0).getSubdomain()); + assertEquals(ZONE_STR, fetched.get(0).getZoneName()); + + // batch with a multi-level name must not cut the name + String batchName = "c.d-" + System.currentTimeMillis() + "." + ZONE_STR; + BatchEntry batchResult = client.recordBatch(zone, + List.of(RecordEntity.build(batchName, RecordType.A, TTL, "130.0.0.11")), null, null, null); + RecordEntity batched = batchResult.getPosts().get(0); + created.add(batched); + assertEquals(batchName, batched.getName()); + + // zone apex (TXT record with unique content, existing apex records stay untouched) + RecordEntity apex = client.recordCreateSubdomain(zone, CfDnsClient.APEX, TTL, RecordType.TXT, + "\"" + apexContent + "\""); + created.add(apex); + assertEquals(ZONE_STR, apex.getName()); + assertEquals(CfDnsClient.APEX, apex.getSubdomain()); + assertTrue(client.recordListBySubdomain(zone, CfDnsClient.APEX, RecordType.TXT).stream() + .anyMatch(r -> r.getId().equals(apex.getId()) && CfDnsClient.APEX.equals(r.getSubdomain()))); + } finally { + deleteQuietly(zone, created); + } + } + + @Test + void testTypeFilter() throws Exception { + ZoneEntity zone = client.zoneGet(ZONE_STR); + String sub = "types-" + System.currentTimeMillis(); + List created = new ArrayList<>(); + try { + created.add(client.recordCreateSubdomain(zone, sub, TTL, RecordType.A, "130.0.0.20")); + created.add(client.recordCreateSubdomain(zone, sub, TTL, RecordType.AAAA, "2001:db8::20")); + created.add(client.recordCreateSubdomain(zone, sub, TTL, RecordType.TXT, "\"type filter\"")); + + // one type: filtered by Cloudflare + List txt = client.recordListBySubdomain(zone, sub, RecordType.TXT); + assertEquals(1, txt.size()); + assertEquals(RecordType.TXT.getType(), txt.get(0).getType()); + assertTrue(client.recordListByType(zone, RecordType.TXT).stream() + .allMatch(r -> RecordType.TXT.getType().equals(r.getType()))); + + // multiple types: filtered by the client + List ip = client.recordListBySubdomain(zone, sub, RecordType.A, RecordType.AAAA); + assertEquals(Set.of("A", "AAAA"), ip.stream().map(RecordEntity::getType).collect(Collectors.toSet())); + + // no type: all records + assertEquals(3, client.recordListBySubdomain(zone, sub).size()); + } finally { + deleteQuietly(zone, created); + } + } + + @Test + void testRecordAttributes() throws Exception { + ZoneEntity zone = client.zoneGet(ZONE_STR); + String sub = "attr-" + System.currentTimeMillis(); + List created = new ArrayList<>(); + Instant start = Instant.now().minusSeconds(60); + try { + // MX record with priority and comment + RecordEntity mx = RecordEntity.build(sub + "." + ZONE_STR, RecordType.MX, TTL, "mail." + ZONE_STR); + mx.setPriority(10); + mx.setComment("cf client test"); + RecordEntity createdMx = client.recordCreate(zone, mx); + created.add(createdMx); + assertEquals(10, createdMx.getPriority()); + assertEquals("cf client test", createdMx.getComment()); + assertNull(mx.getId(), "the given record must not be modified"); + // timestamps are Instants (UTC) + assertTrue(createdMx.getCreatedOn().isAfter(start), createdMx.getCreatedOn().toString()); + assertTrue(createdMx.getCreatedOn().isBefore(Instant.now().plusSeconds(60))); + + RecordEntity fetchedMx = client.recordListBySubdomain(zone, sub, RecordType.MX).get(0); + assertEquals(10, fetchedMx.getPriority()); + assertEquals("cf client test", fetchedMx.getComment()); + + // partial update keeps the attributes which aren't sent + RecordEntity updatedMx = client.recordUpdate(zone, RecordEntity.build(fetchedMx.getId(), "mx2." + ZONE_STR)); + assertEquals("mx2." + ZONE_STR, updatedMx.getContent()); + assertEquals(10, updatedMx.getPriority()); + assertEquals("cf client test", updatedMx.getComment()); + + // batch PATCH changes the priority + fetchedMx = client.recordListBySubdomain(zone, sub, RecordType.MX).get(0); + fetchedMx.setPriority(20); + client.recordBatch(zone, null, null, List.of(fetchedMx), null); + assertEquals(20, client.recordListBySubdomain(zone, sub, RecordType.MX).get(0).getPriority()); + + // SRV record with structured data, for SRV records the priority is part of the data + String srvName = "_sip._tcp." + sub + "." + ZONE_STR; + RecordEntity srv = RecordEntity.build(srvName, RecordType.SRV, TTL, null); + srv.setData(Map.of("priority", 10, "weight", 5, "port", 5060, "target", "sip." + ZONE_STR)); + RecordEntity createdSrv = client.recordCreate(zone, srv); + created.add(createdSrv); + assertEquals(srvName, createdSrv.getName()); + assertEquals(10, ((Number) createdSrv.getData().get("priority")).intValue()); + assertEquals(5060, ((Number) createdSrv.getData().get("port")).intValue()); + assertEquals(5, ((Number) createdSrv.getData().get("weight")).intValue()); + assertEquals("sip." + ZONE_STR, createdSrv.getData().get("target")); + } finally { + deleteQuietly(zone, created); + } + } + + private void deleteQuietly(ZoneEntity zone, List records) { + for (RecordEntity rec : records) { + try { + client.recordDelete(zone, rec.getId()); + } catch (Exception e) { + log.warn("Couldn't delete test record {}: {}", rec.getName(), e.getMessage()); + } + } + } } diff --git a/src/test/java/codes/thischwa/cf/CfDnsClientMockTest.java b/src/test/java/codes/thischwa/cf/CfDnsClientMockTest.java index 6254ab3..a0844dc 100644 --- a/src/test/java/codes/thischwa/cf/CfDnsClientMockTest.java +++ b/src/test/java/codes/thischwa/cf/CfDnsClientMockTest.java @@ -706,7 +706,7 @@ class CfDnsClientMockTest { RecordEntity srv = RecordEntity.build("_sip._tcp.example.com", RecordType.SRV, 300, null); srv.setId("srv1"); srv.setPriority(10); - srv.setData(Map.of("weight", 5, "port", 5060, "target", "sip.example.com")); + srv.setData(Map.of("priority", 10, "weight", 5, "port", 5060, "target", "sip.example.com")); RecordEntity proxied = RecordEntity.build("www.example.com", RecordType.A, 1, "1.2.3.4"); proxied.setId("www1"); proxied.setProxied(true);