oscerd commented on code in PR #26436:
URL: https://github.com/apache/camel/pull/26436#discussion_r4024391472


##########
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:
   Good catch, and it is worse than a nit — fixed here, with a test.
   
   Confirmed the mechanism rather than taking it on trust:
   
   - `OpaHealthProbe` builds `URI.create(serverUrl + "/health")`, so 
`http://opa:8181/` gives `//health`.
   - The shared client is 
`HttpClient.newBuilder().connectTimeout(TIMEOUT).build()` — no 
`followRedirects`, so it is `Redirect.NEVER` by default.
   - So a healthy server comes back as `statusCode != 200` and the probe 
reports **DOWN** with a message blaming the server.
   
   That is the bad shape for this particular bug: a DOWN that looks like a real 
outage on a server that is fine, from a configuration difference an operator 
would never suspect. And you are right that the shared probe is the place — it 
now covers the producer check and the policy check in one line.
   
   `FileUtil.stripTrailingSeparator` applied as suggested; it loops, so 
`http://opa:8181///` is handled too.
   
   I also checked whether the same trailing slash breaks the actual policy 
calls — it does not. `OpaPolicyEvaluator.createClient` hands `serverUrl` 
straight to the Styra `OPAClient`, so the probe is the only place building a 
path by concatenation. The blast radius really is just the health checks.
   
   `toleratesATrailingSlashOnTheServerUrl` pins it, and it goes red with the 
fix reverted. Worth having, because every other test in that class uses a 
slash-free URL — which is exactly how this survived CAMEL-24644.
   
   _Claude Code on behalf of @oscerd_



-- 
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]

Reply via email to