diff --git a/extensions/oidc-client/runtime/src/main/java/io/quarkus/oidc/client/runtime/OidcClientRecorder.java b/extensions/oidc-client/runtime/src/main/java/io/quarkus/oidc/client/runtime/OidcClientRecorder.java index f5bad6c4b90b9..1302da20e6349 100644 --- a/extensions/oidc-client/runtime/src/main/java/io/quarkus/oidc/client/runtime/OidcClientRecorder.java +++ b/extensions/oidc-client/runtime/src/main/java/io/quarkus/oidc/client/runtime/OidcClientRecorder.java @@ -1,10 +1,11 @@ package io.quarkus.oidc.client.runtime; import java.io.IOException; +import java.net.ConnectException; import java.net.URI; +import java.time.Duration; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.CompletionException; import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -35,6 +36,7 @@ public class OidcClientRecorder { private static final Logger LOG = Logger.getLogger(OidcClientRecorder.class); private static final String DEFAULT_OIDC_CLIENT_ID = "Default"; + private static final Duration INITIAL_BACKOFF_DURATION = Duration.ofSeconds(2); public OidcClients setup(OidcClientsConfig oidcClientsConfig, TlsConfig tlsConfig, Supplier vertx) { @@ -157,41 +159,12 @@ private static void setGrantClientParams(OidcClientConfig oidcConfig, MultiMap g } private static Uni discoverTokenRequestUri(WebClient client, String authServerUrl, OidcClientConfig oidcConfig) { + final String discoveryUrl = authServerUrl + "/.well-known/openid-configuration"; final long connectionRetryCount = OidcCommonUtils.getConnectionRetryCount(oidcConfig); + final long expireInDelay = OidcCommonUtils.getConnectionDelayInMillis(oidcConfig); if (connectionRetryCount > 1) { LOG.infof("Connecting to IDP for up to %d times every 2 seconds", connectionRetryCount); } - - for (long i = 0; i < connectionRetryCount; i++) { - try { - if (oidcConfig.discoveryEnabled) { - return discoverTokenEndpoint(client, authServerUrl); - } - break; - } catch (Throwable throwable) { - while (throwable instanceof CompletionException && throwable.getCause() != null) { - throwable = throwable.getCause(); - } - if (throwable instanceof OidcClientException) { - if (i + 1 < connectionRetryCount) { - try { - Thread.sleep(2000); - } catch (InterruptedException iex) { - // continue connecting - } - } else { - throw (OidcClientException) throwable; - } - } else { - throw new OidcClientException(throwable); - } - } - } - return Uni.createFrom().nullItem(); - } - - private static Uni discoverTokenEndpoint(WebClient client, String authServerUrl) { - String discoveryUrl = authServerUrl + "/.well-known/openid-configuration"; return client.getAbs(discoveryUrl).send().onItem().transform(resp -> { if (resp.statusCode() == 200) { JsonObject json = resp.bodyAsJsonObject(); @@ -200,7 +173,10 @@ private static Uni discoverTokenEndpoint(WebClient client, String authSe LOG.tracef("Discovery has failed, status code: %d", resp.statusCode()); return null; } - }); + }).onFailure(ConnectException.class) + .retry() + .withBackOff(INITIAL_BACKOFF_DURATION, INITIAL_BACKOFF_DURATION) + .expireIn(expireInDelay); } protected static OidcClientException toOidcClientException(String authServerUrlString, Throwable cause) { diff --git a/extensions/oidc-common/runtime/src/main/java/io/quarkus/oidc/common/runtime/OidcCommonUtils.java b/extensions/oidc-common/runtime/src/main/java/io/quarkus/oidc/common/runtime/OidcCommonUtils.java index c3099f85e5752..6670dfd5e80c5 100644 --- a/extensions/oidc-common/runtime/src/main/java/io/quarkus/oidc/common/runtime/OidcCommonUtils.java +++ b/extensions/oidc-common/runtime/src/main/java/io/quarkus/oidc/common/runtime/OidcCommonUtils.java @@ -93,6 +93,10 @@ private static long getConnectionDelay(OidcCommonConfig oidcConfig) { : 0; } + public static long getConnectionDelayInMillis(OidcCommonConfig oidcConfig) { + return getConnectionDelay(oidcConfig) * 1000; + } + public static Optional toProxyOptions(OidcCommonConfig.Proxy proxyConfig) { // Proxy is enabled if (at least) "host" is configured. if (!proxyConfig.host.isPresent()) {