gnodet-bot commented on code in PR #26445:
URL: https://github.com/apache/camel/pull/26445#discussion_r4014278785


##########
components/camel-opa/src/main/java/org/apache/camel/component/opa/OpaWasmEvaluator.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Map;
+import java.util.concurrent.TimeoutException;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.styra.opa.wasm.OpaPolicy;
+import org.apache.camel.CamelContext;
+import org.apache.camel.support.ResourceHelper;
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
+import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
+import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
+
+/**
+ * Evaluates the policy in-process from a WebAssembly bundle produced by 
{@code opa build -t wasm}.
+ * <p/>
+ * No OPA server is involved, so there is no network hop and no unreachable 
policy decision point - at the cost of the
+ * policy being a build-time artefact rather than something a server 
distributes and updates.
+ */
+public class OpaWasmEvaluator extends OpaPolicyEvaluator implements 
AutoCloseable {
+
+    private static final ObjectMapper MAPPER = new ObjectMapper();
+    private static final String POLICY_WASM = "policy.wasm";
+    private static final String DATA_JSON = "data.json";
+
+    private final OpaWasmPolicyPool pool;
+    private final String entrypoint;
+    private final String data;
+
+    public OpaWasmEvaluator(byte[] wasm, String data, String entrypoint, int 
poolSize, long borrowTimeout,
+                            String policyPath, String allowKey, String 
includeHeaders, String includeProperties,
+                            boolean includeBody, boolean failOpen) {
+        super(policyPath, allowKey, includeHeaders, includeProperties, 
includeBody, failOpen);
+        this.entrypoint = entrypoint;
+        this.data = data;
+        // OpaPolicy carries mutable input/data and is not thread-safe, while 
a Camel producer is invoked
+        // concurrently - so each exchange borrows its own instance rather 
than sharing one
+        this.pool = new OpaWasmPolicyPool(() -> 
OpaPolicy.builder().withPolicy(wasm).build(), poolSize, borrowTimeout);
+        // fail at startup rather than on the first exchange: the OpaPolicy 
constructor is what rejects a module
+        // that is not a valid OPA bundle, and the pool creates instances 
lazily
+        try (OpaWasmPolicyPool.Lease warmup = pool.borrow()) {
+            prepare(warmup.policy());
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            throw new IllegalStateException("Interrupted while loading the 
WebAssembly policy", e);
+        } catch (TimeoutException e) {
+            throw new IllegalStateException("Timed out loading the WebAssembly 
policy", e);
+        }
+    }
+
+    /**
+     * Applies the entrypoint and data to a borrowed instance.
+     * <p/>
+     * This has to happen on every borrow, not once when the instance is 
built: returning a {@link OpaPolicyPool.Loan}
+     * calls {@code OpaPolicy.reset()}, which clears the data and sets the 
entrypoint back to 0. An instance configured
+     * only at creation would therefore evaluate whatever rule happens to be 
entrypoint 0 from its second use onwards -
+     * a different policy deciding, silently.

Review Comment:
   💡 **Nit: `http:` `policyBundle` has no connection timeout at startup.**
   
   `ResourceHelper.resolveMandatoryResourceAsInputStream` for `http:` URLs uses 
the JDK's default `URLConnection`, which has no read or connect timeout. A 
policy server that is unreachable or slow stalls the endpoint startup thread — 
and since startup runs on Camel's bootstrap thread, it stalls the entire 
`CamelContext` start.
   
   This is a pre-existing Camel pattern shared by many components 
(camel-groovy, camel-velocity, etc.) and not introduced by this PR, so it's not 
a blocker here. Worth documenting in the `policyBundle` option description: 
*"When using an `http:` location, note that no connection timeout is applied; a 
slow or unavailable server will stall the endpoint start. Prefer `classpath:` 
or `file:` for bundles distributed with the application."* Alternatively, a 
follow-up JIRA (per the pattern of CAMEL-24742/24743) tracking a dedicated 
resource-loading timeout could cover this for all affected components.



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