Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed non-working delay for oidc-client #14976

Merged
merged 1 commit into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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