diff --git a/sdk/src/main/java/io/opentdf/platform/sdk/DPoPMode.java b/sdk/src/main/java/io/opentdf/platform/sdk/DPoPMode.java index a481f6bb..c2a71e03 100644 --- a/sdk/src/main/java/io/opentdf/platform/sdk/DPoPMode.java +++ b/sdk/src/main/java/io/opentdf/platform/sdk/DPoPMode.java @@ -14,9 +14,9 @@ public enum DPoPMode { * {@code /.well-known/oauth-protected-resource}. Servers that advertise neither get * {@link #LEGACY}. A discovery that gets no usable answer (a network error, timeout, redirect, * or a status other than 200, 404, 405 and 501) uses legacy for 30 seconds before the server is - * asked again. Answers are cached per origin for a few minutes, then rediscovered, so a server - * that is upgraded or reconfigured is picked up without restarting the client. This is the - * default. + * asked again. Answers are cached per origin for a few minutes (see + * {@link SDKBuilder#dpopDiscoveryCacheTtl}), then rediscovered, so a server that is upgraded or + * reconfigured is picked up without restarting the client. This is the default. */ AUTO, /** diff --git a/sdk/src/main/java/io/opentdf/platform/sdk/SDKBuilder.java b/sdk/src/main/java/io/opentdf/platform/sdk/SDKBuilder.java index 77be44c7..b58a0ab3 100644 --- a/sdk/src/main/java/io/opentdf/platform/sdk/SDKBuilder.java +++ b/sdk/src/main/java/io/opentdf/platform/sdk/SDKBuilder.java @@ -50,6 +50,7 @@ import java.nio.file.Path; import java.security.GeneralSecurityException; import java.security.cert.X509Certificate; +import java.time.Duration; import java.util.Collections; import java.util.List; import java.util.UUID; @@ -71,6 +72,7 @@ public class SDKBuilder { private JWK dpopKey; private JWSAlgorithm dpopAlg; private DPoPMode dpopMode = DPoPMode.AUTO; + private Duration dpopDiscoveryCacheTtl = DPoPModeResolver.DEFAULT_CACHE_TTL; private static final Logger logger = LoggerFactory.getLogger(SDKBuilder.class); @@ -268,6 +270,25 @@ public SDKBuilder dpopMode(DPoPMode dpopMode) { return this; } + /** + * How long a server's DPoP style discovered in {@link DPoPMode#AUTO} is reused before it is + * discovered again. Defaults to five minutes. The legacy fallback after a discovery that got no + * usable answer is reused for 30 seconds, or for this TTL if shorter. {@link Duration#ZERO} + * disables the cache, so every request first queries the server's well-known configuration. + * Ignored for the fixed modes. + * + * @param ttl how long to cache each server's discovered style + * @return this builder instance for method chaining + * @throws IllegalArgumentException if ttl is null or negative + */ + public SDKBuilder dpopDiscoveryCacheTtl(Duration ttl) { + if (ttl == null || ttl.isNegative()) { + throw new IllegalArgumentException("DPoP discovery cache TTL must be zero or positive"); + } + this.dpopDiscoveryCacheTtl = ttl; + return this; + } + private AuthInterceptor getAuthInterceptor(JWK dpopJwk, JWSAlgorithm dpopAlgorithm) { if (platformEndpoint == null) { throw new SDKException("cannot build an SDK without specifying the platform endpoint"); @@ -326,7 +347,8 @@ private AuthInterceptor getAuthInterceptor(JWK dpopJwk, JWSAlgorithm dpopAlgorit this.authzGrant = new ClientCredentialsGrant(); } // Discovery reuses the unauthenticated bootstrap client so it never sends credentials. - var dpopModeResolver = new DPoPModeResolver(dpopMode, httpClient); + var dpopModeResolver = new DPoPModeResolver(dpopMode, httpClient, dpopDiscoveryCacheTtl, System::nanoTime, + DPoPModeResolver.DISCOVERY_TIMEOUT); // Reuse the configuration fetched above rather than discovering the platform again. Only // a positive is seeded: RFC 9728 metadata may still advertise. if (DPoPModeResolver.advertisesRfc9449(config.getConfiguration())) { diff --git a/sdk/src/test/java/io/opentdf/platform/sdk/DPoPModeTest.java b/sdk/src/test/java/io/opentdf/platform/sdk/DPoPModeTest.java index b4f90e00..c930d8b1 100644 --- a/sdk/src/test/java/io/opentdf/platform/sdk/DPoPModeTest.java +++ b/sdk/src/test/java/io/opentdf/platform/sdk/DPoPModeTest.java @@ -436,6 +436,18 @@ void seededAnswerExpires() { assertThat(discoveredPaths()).containsExactly(WELL_KNOWN_CONFIGURATION_PROCEDURE, PROTECTED_RESOURCE_PATH); } + @Test + void zeroTtlDisablesTheCache() { + DPoPModeResolver resolver = new DPoPModeResolver(DPoPMode.AUTO, new OkHttpClient(), Duration.ZERO, nanos::get, + DPoPModeResolver.DISCOVERY_TIMEOUT); + assertThat(resolver.modeFor(rpcUrl())).isEqualTo(DPoPMode.LEGACY); + resolver.recordRfc9449(resourceServer.url("/").url()); + assertThat(resolver.modeFor(rpcUrl())).isEqualTo(DPoPMode.LEGACY); + assertThat(discoveredPaths()).containsExactly( + WELL_KNOWN_CONFIGURATION_PROCEDURE, PROTECTED_RESOURCE_PATH, + WELL_KNOWN_CONFIGURATION_PROCEDURE, PROTECTED_RESOURCE_PATH); + } + @Test void bootstrapAdvertisementSeedsPlatformWithoutDiscovery() { Struct algs = Struct.newBuilder().putFields("dpop_signing_alg_values_supported", @@ -553,6 +565,11 @@ void autoRequiresHttpClientAndBuilderRejectsNull() { SDKBuilder builder = SDKBuilder.newBuilder(); assertThatThrownBy(() -> builder.dpopMode(null)) .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> builder.dpopDiscoveryCacheTtl(null)) + .isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> builder.dpopDiscoveryCacheTtl(Duration.ofSeconds(-1))) + .isInstanceOf(IllegalArgumentException.class); + assertThat(builder.dpopDiscoveryCacheTtl(Duration.ZERO)).isSameAs(builder); } private DPoPModeResolver autoResolver() {