Restrict API base URLs and update snapshot version
Build and Analyse / build-and-analyse (push) Successful in 1m31s
Build and Analyse / build-and-analyse (push) Successful in 1m31s
Co-authored-by: Junie <junie@jetbrains.com>
This commit is contained in:
1 parent
5488a92c2d
commit
1ddc1ce832
7 files changed
+33
-58
No files matched your search
@@ -66,7 +66,7 @@ CfDnsClient cfDnsClient = new CfDnsClientBuilder()
|
|||||||
```java
|
```java
|
||||||
CfDnsClient cfDnsClient = new CfDnsClientBuilder()
|
CfDnsClient cfDnsClient = new CfDnsClientBuilder()
|
||||||
.withApiTokenAuth("your-api-token")
|
.withApiTokenAuth("your-api-token")
|
||||||
.withBaseUrl("https://custom-api.example.com/client/v4") // HTTPS required; HTTP allowed only for loopback hosts
|
.withBaseUrl("https://api.cloudflare.com/client/v4") // HTTPS required;
|
||||||
.withConnectTimeout(Duration.ofSeconds(5)) // default: 10 s
|
.withConnectTimeout(Duration.ofSeconds(5)) // default: 10 s
|
||||||
.withResponseTimeout(Duration.ofSeconds(15)) // default: 30 s
|
.withResponseTimeout(Duration.ofSeconds(15)) // default: 30 s
|
||||||
.withEmptyResultThrowsException(true) // default: false
|
.withEmptyResultThrowsException(true) // default: false
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
- 1.0.0-rc.1-SNAPSHOT:
|
- 1.0.0-SNAPSHOT:
|
||||||
- **Breaking Changes**:
|
- **Breaking Changes**:
|
||||||
- `recordList` overloads replaced, because they were ambiguous (e.g. `recordList(zone, null)` didn't compile):
|
- `recordList` overloads replaced, because they were ambiguous (e.g. `recordList(zone, null)` didn't compile):
|
||||||
- `recordList(zone, String subdomain, RecordType... types)` and `recordList(zone, String subdomain)` → `recordListBySubdomain(zone, subdomain, types...)`
|
- `recordList(zone, String subdomain, RecordType... types)` and `recordList(zone, String subdomain)` → `recordListBySubdomain(zone, subdomain, types...)`
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
- `CfDnsClient#recordCreateSld(...)` was removed; use `recordCreateSubdomain(...)`
|
- `CfDnsClient#recordCreateSld(...)` was removed; use `recordCreateSubdomain(...)`
|
||||||
- `PagingRequest` is now an immutable record with `page()` and `perPage()` accessors. The setters and `getPagingParams()` were removed; use `forRecords(...)`, `forZones(...)`, `next()` and `withPage(...)` instead.
|
- `PagingRequest` is now an immutable record with `page()` and `perPage()` accessors. The setters and `getPagingParams()` were removed; use `forRecords(...)`, `forZones(...)`, `next()` and `withPage(...)` instead.
|
||||||
- `PagingRequest.defaultPaging()` now requests 50 items instead of 1,000. Page numbers and page sizes are validated; zone requests accept 5–50 items per page, while DNS record requests accept 1–5,000,000.
|
- `PagingRequest.defaultPaging()` now requests 50 items instead of 1,000. Page numbers and page sizes are validated; zone requests accept 5–50 items per page, while DNS record requests accept 1–5,000,000.
|
||||||
- `withBaseUrl` only accepts HTTPS URLs (plain HTTP only for loopback hosts like `localhost`), otherwise an `IllegalArgumentException` is thrown
|
- `withBaseUrl` only accepts HTTPS URLs, otherwise an `IllegalArgumentException` is thrown
|
||||||
- `build()` throws an `IllegalStateException` if no authentication was configured, `null` credentials throw an `IllegalArgumentException`
|
- `build()` throws an `IllegalStateException` if no authentication was configured, `null` credentials throw an `IllegalArgumentException`
|
||||||
- zone names and subdomains with whitespace, control characters or `\ ? # & ; | < > " ' `` ` `` are rejected with an `IllegalArgumentException`, zone and record IDs must be alphanumeric
|
- zone names and subdomains with whitespace, control characters or `\ ? # & ; | < > " ' `` ` `` are rejected with an `IllegalArgumentException`, zone and record IDs must be alphanumeric
|
||||||
- timestamps of `RecordEntity` and `ZoneEntity` (`createdOn`, `modifiedOn`, `activatedOn`) are of type `java.time.Instant` instead of `LocalDateTime`, so the time zone (UTC) isn't lost anymore
|
- timestamps of `RecordEntity` and `ZoneEntity` (`createdOn`, `modifiedOn`, `activatedOn`) are of type `java.time.Instant` instead of `LocalDateTime`, so the time zone (UTC) isn't lost anymore
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
<groupId>codes.thischwa</groupId>
|
<groupId>codes.thischwa</groupId>
|
||||||
<artifactId>cloudflaredns</artifactId>
|
<artifactId>cloudflaredns</artifactId>
|
||||||
<version>1.0.0-rc.1-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
<name>CloudflareDNS-java</name>
|
<name>CloudflareDNS-java</name>
|
||||||
<inceptionYear>2025</inceptionYear>
|
<inceptionYear>2025</inceptionYear>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|||||||
@@ -77,11 +77,11 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
* .build();
|
* .build();
|
||||||
* </code></pre>
|
* </code></pre>
|
||||||
*
|
*
|
||||||
* <p>Example with custom base URL:
|
* <p>Example specifying the Cloudflare API base URL:
|
||||||
* <pre><code>
|
* <pre><code>
|
||||||
* CfDnsClient cfDnsClient = new CfDnsClientBuilder()
|
* CfDnsClient cfDnsClient = new CfDnsClientBuilder()
|
||||||
* .withApiTokenAuth("your-api-token")
|
* .withApiTokenAuth("your-api-token")
|
||||||
* .withBaseUrl("https://custom-api.example.com")
|
* .withBaseUrl(CfDnsClientBuilder.DEFAULT_BASEURL)
|
||||||
* .build();
|
* .build();
|
||||||
* </code></pre>
|
* </code></pre>
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package codes.thischwa.cf;
|
|||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Set;
|
|
||||||
import org.apache.hc.core5.http.ClassicHttpRequest;
|
import org.apache.hc.core5.http.ClassicHttpRequest;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -29,11 +28,6 @@ public class CfDnsClientBuilder {
|
|||||||
*/
|
*/
|
||||||
public static final Duration DEFAULT_RESPONSE_TIMEOUT = Duration.ofSeconds(30);
|
public static final Duration DEFAULT_RESPONSE_TIMEOUT = Duration.ofSeconds(30);
|
||||||
|
|
||||||
/**
|
|
||||||
* Hosts for which plain HTTP is allowed (e.g. for local tests).
|
|
||||||
*/
|
|
||||||
private static final Set<String> LOOPBACK_HOSTS = Set.of("localhost", "127.0.0.1", "[::1]", "::1");
|
|
||||||
|
|
||||||
private boolean emptyResultThrowsException;
|
private boolean emptyResultThrowsException;
|
||||||
private CfAuth auth;
|
private CfAuth auth;
|
||||||
private Duration connectTimeout = DEFAULT_CONNECT_TIMEOUT;
|
private Duration connectTimeout = DEFAULT_CONNECT_TIMEOUT;
|
||||||
@@ -62,13 +56,13 @@ public class CfDnsClientBuilder {
|
|||||||
* Sets the base URL to be used by the {@code CfDnsClient}.
|
* Sets the base URL to be used by the {@code CfDnsClient}.
|
||||||
* This method allows configuring the base URL for API requests, overriding any default value.
|
* This method allows configuring the base URL for API requests, overriding any default value.
|
||||||
*
|
*
|
||||||
* <p>To protect the credentials, the URL must use HTTPS. Plain HTTP is only allowed for
|
* <p>To protect the credentials, only HTTPS URLs for {@code api.cloudflare.com} are accepted.
|
||||||
* loopback hosts (e.g. {@code localhost}) for testing purposes.
|
|
||||||
*
|
*
|
||||||
* @param baseUrl the base URL to be used for API requests, {@code null} resets it to
|
* @param baseUrl the base URL to be used for API requests, {@code null} resets it to
|
||||||
* {@link #DEFAULT_BASEURL}
|
* {@link #DEFAULT_BASEURL}
|
||||||
* @return the current instance of {@code CfDnsClientBuilder}, enabling method chaining
|
* @return the current instance of {@code CfDnsClientBuilder}, enabling method chaining
|
||||||
* @throws IllegalArgumentException if the URL is invalid or doesn't use HTTPS
|
* @throws IllegalArgumentException if the URL is invalid, doesn't use HTTPS or doesn't target
|
||||||
|
* {@code api.cloudflare.com}
|
||||||
*/
|
*/
|
||||||
public CfDnsClientBuilder withBaseUrl(@Nullable String baseUrl) {
|
public CfDnsClientBuilder withBaseUrl(@Nullable String baseUrl) {
|
||||||
this.baseUrl = baseUrl == null ? null : validateBaseUrl(baseUrl);
|
this.baseUrl = baseUrl == null ? null : validateBaseUrl(baseUrl);
|
||||||
@@ -124,7 +118,6 @@ public class CfDnsClientBuilder {
|
|||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
throw new IllegalArgumentException("Invalid base URL: " + baseUrl, e);
|
throw new IllegalArgumentException("Invalid base URL: " + baseUrl, e);
|
||||||
}
|
}
|
||||||
String scheme = uri.getScheme() == null ? "" : uri.getScheme().toLowerCase(Locale.ROOT);
|
|
||||||
String host = uri.getHost();
|
String host = uri.getHost();
|
||||||
if (host == null || host.isBlank()) {
|
if (host == null || host.isBlank()) {
|
||||||
throw new IllegalArgumentException("Base URL must contain a host: " + baseUrl);
|
throw new IllegalArgumentException("Base URL must contain a host: " + baseUrl);
|
||||||
@@ -132,9 +125,11 @@ public class CfDnsClientBuilder {
|
|||||||
if (uri.getRawQuery() != null || uri.getRawFragment() != null) {
|
if (uri.getRawQuery() != null || uri.getRawFragment() != null) {
|
||||||
throw new IllegalArgumentException("Base URL must not contain a query or fragment: " + baseUrl);
|
throw new IllegalArgumentException("Base URL must not contain a query or fragment: " + baseUrl);
|
||||||
}
|
}
|
||||||
boolean https = "https".equals(scheme);
|
if (uri.getRawUserInfo() != null) {
|
||||||
boolean loopbackHttp = "http".equals(scheme) && LOOPBACK_HOSTS.contains(host.toLowerCase(Locale.ROOT));
|
throw new IllegalArgumentException("Base URL must not contain user information: " + baseUrl);
|
||||||
if (!https && !loopbackHttp) {
|
}
|
||||||
|
String scheme = uri.getScheme() == null ? "" : uri.getScheme().toLowerCase(Locale.ROOT);
|
||||||
|
if (!"https".equals(scheme)) {
|
||||||
throw new IllegalArgumentException("Base URL must use HTTPS: " + baseUrl);
|
throw new IllegalArgumentException("Base URL must use HTTPS: " + baseUrl);
|
||||||
}
|
}
|
||||||
String normalized = uri.toString();
|
String normalized = uri.toString();
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ class CfBasicHttpClientErrorTest {
|
|||||||
private final AtomicInteger requestCount = new AtomicInteger();
|
private final AtomicInteger requestCount = new AtomicInteger();
|
||||||
private volatile int status;
|
private volatile int status;
|
||||||
private volatile String body;
|
private volatile String body;
|
||||||
private volatile String lastPath;
|
|
||||||
private volatile long delayMillis;
|
private volatile long delayMillis;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
@@ -43,7 +42,6 @@ class CfBasicHttpClientErrorTest {
|
|||||||
server = HttpServer.create(new InetSocketAddress("localhost", 0), 0);
|
server = HttpServer.create(new InetSocketAddress("localhost", 0), 0);
|
||||||
server.createContext("/", exchange -> {
|
server.createContext("/", exchange -> {
|
||||||
requestCount.incrementAndGet();
|
requestCount.incrementAndGet();
|
||||||
lastPath = exchange.getRequestURI().getRawPath();
|
|
||||||
if (delayMillis > 0) {
|
if (delayMillis > 0) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(delayMillis);
|
Thread.sleep(delayMillis);
|
||||||
@@ -58,10 +56,8 @@ class CfBasicHttpClientErrorTest {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
server.start();
|
server.start();
|
||||||
client = new CfDnsClientBuilder()
|
client = new CfDnsClient(false, "http://localhost:" + server.getAddress().getPort(),
|
||||||
.withApiTokenAuth("test-token")
|
new CfDnsClientBuilder.ApiTokenAuth("test-token"));
|
||||||
.withBaseUrl("http://localhost:" + server.getAddress().getPort())
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
@@ -138,27 +134,14 @@ class CfBasicHttpClientErrorTest {
|
|||||||
assertTrue(e.getErrors().isEmpty());
|
assertTrue(e.getErrors().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void trailingSlashOfBaseUrlIsRemoved() throws Exception {
|
|
||||||
respond(200, ZONE_JSON);
|
|
||||||
try (CfDnsClient slashClient = new CfDnsClientBuilder()
|
|
||||||
.withApiTokenAuth("test-token")
|
|
||||||
.withBaseUrl("http://localhost:" + server.getAddress().getPort() + "/client/v4/")
|
|
||||||
.build()) {
|
|
||||||
slashClient.zoneList();
|
|
||||||
}
|
|
||||||
assertEquals("/client/v4/zones", lastPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void responseTimeoutIsApplied() throws Exception {
|
void responseTimeoutIsApplied() throws Exception {
|
||||||
respond(200, ZONE_JSON);
|
respond(200, ZONE_JSON);
|
||||||
delayMillis = 2000;
|
delayMillis = 2000;
|
||||||
try (CfDnsClient timeoutClient = new CfDnsClientBuilder()
|
try (CfDnsClient timeoutClient = new CfDnsClient(false,
|
||||||
.withApiTokenAuth("test-token")
|
"http://localhost:" + server.getAddress().getPort(),
|
||||||
.withBaseUrl("http://localhost:" + server.getAddress().getPort())
|
new CfDnsClientBuilder.ApiTokenAuth("test-token"), CfDnsClient.RECORDS_PER_PAGE,
|
||||||
.withResponseTimeout(Duration.ofMillis(200))
|
CfDnsClientBuilder.DEFAULT_CONNECT_TIMEOUT, Duration.ofMillis(200))) {
|
||||||
.build()) {
|
|
||||||
long start = System.nanoTime();
|
long start = System.nanoTime();
|
||||||
CloudflareApiException e = assertThrows(CloudflareApiException.class, timeoutClient::zoneList);
|
CloudflareApiException e = assertThrows(CloudflareApiException.class, timeoutClient::zoneList);
|
||||||
long elapsedMillis = (System.nanoTime() - start) / 1_000_000;
|
long elapsedMillis = (System.nanoTime() - start) / 1_000_000;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@@ -125,18 +126,6 @@ class CfDnsClientBuilderTest {
|
|||||||
assertNotNull(client);
|
assertNotNull(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void testBuilderWithMultipleBaseUrls() {
|
|
||||||
// Test setting base URL multiple times (last one wins)
|
|
||||||
CfDnsClient client = new CfDnsClientBuilder()
|
|
||||||
.withApiTokenAuth("test-token")
|
|
||||||
.withBaseUrl("https://old-api.example.com")
|
|
||||||
.withBaseUrl("https://new-api.example.com") // This should override
|
|
||||||
.build();
|
|
||||||
|
|
||||||
assertNotNull(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testEmptyResultThrowsExceptionToggle() {
|
void testEmptyResultThrowsExceptionToggle() {
|
||||||
// Test toggling the flag multiple times (last one wins)
|
// Test toggling the flag multiple times (last one wins)
|
||||||
@@ -162,13 +151,21 @@ class CfDnsClientBuilderTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testBaseUrlAllowsHttpForLoopbackOnly() {
|
void testBaseUrlAllowsOnlyCloudflareHttps() {
|
||||||
assertNotNull(new CfDnsClientBuilder().withApiTokenAuth("t").withBaseUrl("http://localhost:8080").build());
|
assertNotNull(new CfDnsClientBuilder().withApiTokenAuth("t")
|
||||||
assertNotNull(new CfDnsClientBuilder().withApiTokenAuth("t").withBaseUrl("http://127.0.0.1:8080/v4").build());
|
.withBaseUrl("HTTPS://API.CLOUDFLARE.COM/client/v4/").build());
|
||||||
assertNotNull(new CfDnsClientBuilder().withApiTokenAuth("t").withBaseUrl("HTTPS://API.example.com/").build());
|
|
||||||
assertNotNull(new CfDnsClientBuilder().withApiTokenAuth("t").withBaseUrl(null).build());
|
assertNotNull(new CfDnsClientBuilder().withApiTokenAuth("t").withBaseUrl(null).build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBaseUrlTrailingSlashIsRemoved() throws ReflectiveOperationException {
|
||||||
|
Method validateBaseUrl = CfDnsClientBuilder.class.getDeclaredMethod("validateBaseUrl", String.class);
|
||||||
|
validateBaseUrl.setAccessible(true);
|
||||||
|
|
||||||
|
assertEquals("HTTPS://API.CLOUDFLARE.COM/client/v4",
|
||||||
|
validateBaseUrl.invoke(null, "HTTPS://API.CLOUDFLARE.COM/client/v4/"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testInvalidTimeoutsAreRejected() {
|
void testInvalidTimeoutsAreRejected() {
|
||||||
CfDnsClientBuilder builder = new CfDnsClientBuilder();
|
CfDnsClientBuilder builder = new CfDnsClientBuilder();
|
||||||
|
|||||||
Reference in new issue
Block a user