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


##########
components/camel-opa/src/main/java/org/apache/camel/component/opa/OpaWasmEvaluator.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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 com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.styra.opa.wasm.OpaPolicy;
+import com.styra.opa.wasm.OpaPolicyPool;
+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 OpaPolicyPool pool;
+    private final String entrypoint;
+    private final String data;
+
+    public OpaWasmEvaluator(byte[] wasm, String data, String entrypoint, int 
poolSize, 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 = OpaPolicyPool.create(() -> 
OpaPolicy.builder().withPolicy(wasm).build(), poolSize);
+        // 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 (OpaPolicyPool.Loan warmup = pool.borrow()) {
+            prepare(warmup.policy());
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            throw new IllegalStateException("Interrupted while 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.
+     */
+    private void prepare(OpaPolicy policy) {
+        policy.entrypoint(entrypoint);
+        if (data != null) {
+            policy.data(data);
+        }
+    }
+
+    /**
+     * Loads a WebAssembly policy from a Camel resource location.
+     * <p/>
+     * {@code opa build} emits a {@code bundle.tar.gz} holding {@code 
/policy.wasm} alongside the source and a manifest,
+     * so that is what an operator will actually have to hand; a bare {@code 
.wasm} is accepted too.
+     */
+    public static Bundle loadPolicy(CamelContext camelContext, String 
location) throws Exception {
+        try (InputStream in = 
ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, location)) {
+            byte[] content = in.readAllBytes();
+            return isGzip(content) ? extractFromBundle(content, location) : 
new Bundle(content, null);
+        }
+    }
+
+    /**
+     * A loaded policy: the WebAssembly module, and the data document that 
{@code opa build} packed beside it when the
+     * source was a bundle. A policy that reads {@code data.*} needs the 
latter to decide the same way it would against
+     * a server that had loaded the same bundle.
+     *
+     * @param wasm the WebAssembly module
+     * @param data the bundle's data document as JSON, or null when there was 
none
+     */
+    public record Bundle(byte[] wasm, String data) {
+    }
+
+    private static boolean isGzip(byte[] content) {
+        return content.length > 1 && (content[0] & 0xff) == 0x1f && 
(content[1] & 0xff) == 0x8b;
+    }
+
+    private static Bundle extractFromBundle(byte[] bundle, String location) 
throws Exception {
+        byte[] wasm = null;
+        String data = null;
+        try (TarArchiveInputStream tar
+                = new TarArchiveInputStream(new GzipCompressorInputStream(new 
ByteArrayInputStream(bundle)))) {
+            TarArchiveEntry entry;
+            while ((entry = tar.getNextEntry()) != null) {
+                if (entry.isDirectory()) {
+                    continue;
+                }
+                if (entry.getName().endsWith(POLICY_WASM)) {
+                    wasm = tar.readAllBytes();
+                } else if (entry.getName().endsWith(DATA_JSON)) {
+                    data = new String(tar.readAllBytes(), 
StandardCharsets.UTF_8);
+                }
+            }
+        }
+        if (wasm == null) {
+            throw new IllegalArgumentException(
+                    "No " + POLICY_WASM + " inside the bundle at " + location
+                                               + ". Build it with: opa build 
-t wasm -e <entrypoint> <policy.rego>");
+        }
+        return new Bundle(wasm, data);
+    }
+
+    @Override
+    protected Object evaluateDecision(Map<String, Object> input) throws 
Exception {
+        OpaPolicyPool.Loan loan = pool.borrow();

Review Comment:
   Taking Option A — the pool is now camel-owned, in `OpaWasmPolicyPool`.
   
   Two corrections first, both from decompiling 1.1.0 rather than reading its 
javadoc:
   
   **`close()` does not release parked threads.** It is `if 
(closed.compareAndSet(false, true)) { idle.clear(); }` — nothing touches the 
semaphore. The `IllegalStateException` you describe is thrown at *entry* to 
`borrow()`, before `acquire()`, so a thread already parked stays parked; there 
is no window and no race with `evaluator = null`. Worse in one way, simpler in 
another.
   
   **"Straightforward since `OpaPolicyPool`'s internals are known and not 
complex" nearly wasn't.** `OpaPolicy.reset()` is **package-private**, so a pool 
outside `com.styra.opa.wasm` cannot call it — which would leave a replacement 
unable to recycle instances at all. It turns out not to need to: `data(String)` 
frees the old data value and rewinds the heap to `baseHeapPtr`, and 
`input(String)` — which `evaluate(String)` calls — rewinds to `dataHeapPtr` on 
*every* evaluation. Since this evaluator sets the entrypoint and data on every 
borrow anyway, nothing accumulates across reuses. That is recorded on 
`Lease.close()` so the next reader does not have to redo the bytecode.
   
   What the replacement gives us:
   
   - `tryAcquire(borrowTimeout, MILLISECONDS)`, default 30s, configurable. The 
timeout message names `poolSize` and `borrowTimeout` and says which to raise.
   - A lease that ends **exactly once**: the guard is claimed with a CAS 
*before* any work, so returning and discarding are mutually exclusive by 
construction. That is the double-release from the other thread made 
unrepresentable rather than avoided by care at the call site — 
`evaluateDecision` goes back to an unconditional `discard()` in its `catch`.
   - `borrow()` restores the interrupt flag that the interruptible wait clears.
   
   And one thing neither of us had spotted, which your comment led to: 
`InterruptedException` was landing in the base class's `catch (Exception e)`, 
so with **`failOpen=true` a shutdown interrupt became an `allow`**. Nothing had 
decided that exchange was permitted. `OpaPolicyEvaluator.evaluate` now catches 
it ahead of the `failOpen` branch, restores the flag and always fails closed — 
`failOpen` is for "the decision point is unavailable", not for "we are being 
torn down".
   
   New `OpaWasmPolicyPoolTest` pins the contract directly, without a WASM 
module: bounded wait (and that it really waited), a waiter freed as soon as a 
lease ends, discard-after-close not double-releasing, reuse vs. replacement 
after discard, refusal once closed, and the interrupt flag. 59 tests in the 
module.
   
   _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