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


##########
components/camel-opa/src/main/java/org/apache/camel/component/opa/OpaWasmEvaluator.java:
##########
@@ -0,0 +1,176 @@
+/*
+ * 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();
+        try {
+            prepare(loan.policy());
+            Object decision = 
unwrap(loan.policy().evaluate(MAPPER.writeValueAsString(input)));
+            loan.close();

Review Comment:
   Good catch, and it is worse than "depends on internals" — I decompiled 
`OpaPolicyPool` 1.1.0 to check, and the two exits are not symmetric:
   
   ```java
   private void release(OpaPolicy p) {
       try { p.reset(); if (!closed.get()) idle.offerFirst(p); }
       finally { permits.release(); }          // <- always
   }
   Loan.close()   { if (policy != null) { pool.release(policy); policy = null; 
} }
   Loan.discard() { if (policy != null) { pool.discard();       policy = null; 
} }   // pool.discard() == permits.release()
   ```
   
   The permit is released from a `finally`, so it is gone the moment `close()` 
is entered. But `close()` nulls `policy` only *after* `release()` returns, so a 
throwing `reset()` leaves the loan looking un-returned — and my `catch` then 
called `discard()`, releasing the **same permit a second time**. A semaphore 
that gains permits stops bounding anything: the pool would quietly allow more 
live `OpaPolicy` instances than `poolSize`, which is the one property it exists 
to provide.
   
   So the fix is not to guard `discard()` against throwing — it is to not call 
it at all once `close()` has been entered:
   
   ```java
   boolean returned = false;
   try {
       ...
       returned = true;        // before close(), not after
       loan.close();
       return decision;
   } catch (Exception e) {
       if (!returned) {
           loan.discard();
       }
       throw e;
   }
   ```
   
   A throwing `close()` is then correct on both counts: the permit was released 
by that `finally`, and the instance was never re-added to `idle` (the 
`offerFirst` sits after `reset()` in the `try`), which is exactly discard 
semantics.
   
   `keepsThePoolUsableAfterRepeatedEvaluationFailures` locks the reachable half 
— five failed evaluations through a `poolSize=1` pool, then a successful one — 
under a `@Timeout` so a wedged semaphore fails instead of hanging the build.
   
   _Claude Code on behalf of @oscerd_



##########
components/camel-opa/src/main/java/org/apache/camel/component/opa/OpaConfiguration.java:
##########
@@ -131,6 +143,60 @@ public void setBearerToken(String bearerToken) {
         this.bearerToken = bearerToken;
     }
 
+    public String getEvaluationMode() {
+        return evaluationMode;
+    }
+
+    /**
+     * How the policy is evaluated. {@code rest} (the default) calls a running 
OPA server over its Data API.
+     * {@code wasm} evaluates a WebAssembly bundle in-process, with no server 
involved - so there is no network hop and
+     * no unreachable decision point, at the cost of the policy being a 
build-time artefact rather than something a
+     * server distributes and updates. {@code serverUrl}, {@code bearerToken} 
and {@code failOpen} do not apply in
+     * {@code wasm} mode.
+     */
+    public void setEvaluationMode(String evaluationMode) {
+        this.evaluationMode = evaluationMode;
+    }
+
+    public String getPolicyBundle() {
+        return policyBundle;
+    }
+
+    /**
+     * The WebAssembly policy to evaluate in {@code wasm} mode, as produced by 
{@code opa build -t wasm}. Accepts a
+     * {@code file:}, {@code classpath:} or {@code http:} location holding 
either the {@code bundle.tar.gz} that
+     * {@code opa build} emits or a bare {@code .wasm} module. Required when 
{@code evaluationMode=wasm}. Prefer the
+     * bundle: it also carries the data document the policy reads as {@code 
data.*}, which a bare module does not.
+     */
+    public void setPolicyBundle(String policyBundle) {
+        this.policyBundle = policyBundle;
+    }
+
+    public String getEntrypoint() {
+        return entrypoint;
+    }
+
+    /**
+     * The compiled entrypoint to evaluate in {@code wasm} mode. This is not 
the same thing as the policy path: an
+     * entrypoint is fixed when the bundle is built, with {@code opa build 
-e}. Defaults to the endpoint's policy path,
+     * which is the name {@code opa build} gives it.
+     */
+    public void setEntrypoint(String entrypoint) {
+        this.entrypoint = entrypoint;
+    }
+
+    public int getPoolSize() {
+        return poolSize;
+    }
+
+    /**
+     * How many WebAssembly policy instances to pool in {@code wasm} mode. An 
instance carries mutable state and is not
+     * thread-safe, so each exchange borrows one; this bounds how many 
exchanges evaluate at once.
+     */
+    public void setPoolSize(int poolSize) {
+        this.poolSize = poolSize;

Review Comment:
   Adding the check, but the premise needs correcting: `OpaPolicyPool.create` 
already rejects it.
   
   ```java
   public static OpaPolicyPool create(Supplier<OpaPolicy> factory, int maxSize) 
{
       if (maxSize <= 0) {
           throw new IllegalArgumentException("maxSize must be positive, got: " 
+ maxSize);
       }
   ```
   
   So `poolSize=0` fails fast today — no deadlock, no silent empty pool. I 
assumed the hang too and wrote the test expecting one; reverting the check made 
it fail in 0.5s rather than time out, which is what sent me to the bytecode.
   
   It is still worth checking here, for the message: the library names 
`maxSize`, its own constructor parameter, on a component where `poolSize` is 
the only pool setting the operator has ever seen. The check lives in 
`createWasmEvaluator` next to the `policyBundle is required` one rather than in 
the setter, so all the wasm-mode validation is in one place and surfaces as the 
same `ResolveEndpointFailedException`.
   
   `rejectsAPoolSizeBelowOne` asserts on the message, not just the type — 
without the check the type still matches, so only the message distinguishes 
them.
   
   _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