Skip to content

Commit

Permalink
Merge pull request #14976 from Yelzhasdev/#14959
Browse files Browse the repository at this point in the history
Fixed non-working delay for oidc-client
  • Loading branch information
sberyozkin authored Feb 11, 2021
2 parents 5c55e5d + e282fd5 commit 4aeba34
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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> vertx) {

Expand Down Expand Up @@ -157,41 +159,12 @@ private static void setGrantClientParams(OidcClientConfig oidcConfig, MultiMap g
}

private static Uni<String> 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<String> 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();
Expand All @@ -200,7 +173,10 @@ private static Uni<String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ private static long getConnectionDelay(OidcCommonConfig oidcConfig) {
: 0;
}

public static long getConnectionDelayInMillis(OidcCommonConfig oidcConfig) {
return getConnectionDelay(oidcConfig) * 1000;
}

public static Optional<ProxyOptions> toProxyOptions(OidcCommonConfig.Proxy proxyConfig) {
// Proxy is enabled if (at least) "host" is configured.
if (!proxyConfig.host.isPresent()) {
Expand Down

0 comments on commit 4aeba34

Please sign in to comment.