|
| 1 | +package org.keycloak.testframework.server; |
| 2 | + |
| 3 | +import java.net.HttpURLConnection; |
| 4 | +import java.net.URI; |
| 5 | +import java.security.cert.X509Certificate; |
| 6 | +import java.time.Duration; |
| 7 | +import java.util.concurrent.TimeUnit; |
| 8 | +import java.util.function.IntFunction; |
| 9 | +import javax.net.ssl.HttpsURLConnection; |
| 10 | +import javax.net.ssl.SSLContext; |
| 11 | +import javax.net.ssl.TrustManager; |
| 12 | +import javax.net.ssl.X509TrustManager; |
| 13 | + |
| 14 | +/** |
| 15 | + * Polls the server's health endpoint until it reports ready, supporting both HTTP and HTTPS connections. |
| 16 | + */ |
| 17 | +public final class ReadinessProbe { |
| 18 | + |
| 19 | + private static final long STARTUP_TIMEOUT_MILLIS = Duration.ofMinutes(5).toMillis(); |
| 20 | + private static final int CONNECTION_TIMEOUT_MILLIS = Math.toIntExact(Duration.ofSeconds(5).toMillis()); |
| 21 | + private static final long POLL_INTERVAL_MILLIS = Duration.ofMillis(500).toMillis(); |
| 22 | + |
| 23 | + private ReadinessProbe() { |
| 24 | + } |
| 25 | + |
| 26 | + public static void waitUntilReady(String baseManagementUrl) { |
| 27 | + var url = baseManagementUrl + "/health/ready"; |
| 28 | + var deadline = System.currentTimeMillis() + STARTUP_TIMEOUT_MILLIS; |
| 29 | + var sslContext = createTrustAllSslContext(); |
| 30 | + waitUntilReady(url, sslContext, deadline); |
| 31 | + } |
| 32 | + |
| 33 | + public static void waitUntilReady(IntFunction<String> baseManagementUrlFunction, int range) { |
| 34 | + var deadline = System.currentTimeMillis() + STARTUP_TIMEOUT_MILLIS; |
| 35 | + var sslContext = createTrustAllSslContext(); |
| 36 | + for (int i = 0; i < range; i++) { |
| 37 | + var url = baseManagementUrlFunction.apply(i) + "/health/ready"; |
| 38 | + waitUntilReady(url, sslContext, deadline); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private static void waitUntilReady(String url, SSLContext sslContext, long deadline) { |
| 43 | + while (System.currentTimeMillis() < deadline) { |
| 44 | + try { |
| 45 | + HttpURLConnection connection = (HttpURLConnection) URI.create(url).toURL().openConnection(); |
| 46 | + if (connection instanceof HttpsURLConnection https) { |
| 47 | + https.setSSLSocketFactory(sslContext.getSocketFactory()); |
| 48 | + https.setHostnameVerifier((hostname, session) -> true); |
| 49 | + } |
| 50 | + connection.setConnectTimeout(CONNECTION_TIMEOUT_MILLIS); |
| 51 | + connection.setReadTimeout(CONNECTION_TIMEOUT_MILLIS); |
| 52 | + connection.setRequestMethod("GET"); |
| 53 | + |
| 54 | + try { |
| 55 | + if (connection.getResponseCode() == 200) { |
| 56 | + return; |
| 57 | + } |
| 58 | + } finally { |
| 59 | + connection.disconnect(); |
| 60 | + } |
| 61 | + } catch (Exception e) { |
| 62 | + // server not yet available, retry |
| 63 | + } |
| 64 | + |
| 65 | + try { |
| 66 | + //noinspection BusyWait |
| 67 | + Thread.sleep(POLL_INTERVAL_MILLIS); |
| 68 | + } catch (InterruptedException e) { |
| 69 | + Thread.currentThread().interrupt(); |
| 70 | + throw new RuntimeException("Interrupted while waiting for server readiness", e); |
| 71 | + } |
| 72 | + } |
| 73 | + throw new IllegalStateException("Server did not become ready within " + TimeUnit.MILLISECONDS.toSeconds(STARTUP_TIMEOUT_MILLIS) + " seconds: " + url); |
| 74 | + } |
| 75 | + |
| 76 | + private static SSLContext createTrustAllSslContext() { |
| 77 | + try { |
| 78 | + TrustManager[] trustAll = new TrustManager[]{ |
| 79 | + new X509TrustManager() { |
| 80 | + public X509Certificate[] getAcceptedIssuers() { |
| 81 | + return new X509Certificate[0]; |
| 82 | + } |
| 83 | + |
| 84 | + public void checkClientTrusted(X509Certificate[] certs, String authType) { |
| 85 | + } |
| 86 | + |
| 87 | + public void checkServerTrusted(X509Certificate[] certs, String authType) { |
| 88 | + } |
| 89 | + } |
| 90 | + }; |
| 91 | + SSLContext ctx = SSLContext.getInstance("TLS"); |
| 92 | + ctx.init(null, trustAll, null); |
| 93 | + return ctx; |
| 94 | + } catch (Exception e) { |
| 95 | + throw new RuntimeException("Failed to create trust-all SSLContext", e); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments