davsclaus commented on code in PR #26436: URL: https://github.com/apache/camel/pull/26436#discussion_r4024253023
########## components/camel-opa/src/main/java/org/apache/camel/component/opa/OpaHealthProbe.java: ########## @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.opa; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Duration; + +import org.apache.camel.health.HealthCheckResultBuilder; +import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.URISupport; + +/** + * The OPA readiness probe, shared by the producer health check and the one on {@code OpaSecurityPolicy}. + * <p/> + * Both ask the same question of the same endpoint, so the probe lives in one place: a fix here - a changed timeout, a + * new failure mode to report - applies to both rather than to whichever was remembered. + */ +public final class OpaHealthProbe { + + private static final Duration TIMEOUT = Duration.ofSeconds(5); + + /** + * Shared across every OPA health check in the JVM. {@link HttpClient} only became {@link AutoCloseable} in Java 21, + * so on the Java 17 baseline one client per check would leak its selector thread with no way to shut it down. The + * client is immutable and thread-safe, so sharing is free; the per-request URL and token live on the + * {@link HttpRequest}. + */ + private static final HttpClient HTTP_CLIENT = HttpClient.newBuilder().connectTimeout(TIMEOUT).build(); + + private OpaHealthProbe() { + } + + /** + * Probes the OPA server's health endpoint and records the outcome on the builder. + * <p/> + * An unreachable server and a server answering with an error are reported differently, so an outage is never + * mistaken for a policy that denied. + * + * @param builder the result to populate + * @param serverUrl base URL of the OPA server, without the /v1/data suffix + * @param bearerToken token for OPA API authentication, or null when OPA does not require one + * @param policyPath the policy this check is reporting for, recorded as a detail + */ + public static void probe( + HealthCheckResultBuilder builder, String serverUrl, String bearerToken, String policyPath) { + builder.detail("opa.serverUrl", URISupport.sanitizeUri(serverUrl)); Review Comment: Non-blocking, and pre-existing from CAMEL-24644 — but since the probe is now shared, fixing it here covers both checks. A `serverUrl` with a trailing slash (`http://opa:8181/`) produces `//health`. OPA's Go router answers a non-canonical path with a redirect, and `java.net.http.HttpClient` defaults to `Redirect.NEVER`, so a healthy server would be reported DOWN with "HTTP 301". ```suggestion .uri(URI.create(FileUtil.stripTrailingSeparator(serverUrl) + "/health")) ``` (`org.apache.camel.util.FileUtil` — it strips a trailing `/` or `\`.) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
