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


##########
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:
   ⚠️ **`poolSize` is not validated.** A user setting `poolSize=0` or a 
negative value passes straight through to `OpaPolicyPool.create(factory, 
poolSize)`. The `OpaPolicyPool` javadoc does not specify what happens with a 
zero or negative capacity — it could deadlock on the first `borrow()` (if it's 
a semaphore with 0 permits), throw later at runtime, or silently pool nothing.
   
   Fail fast at configuration time:
   
   ```suggestion
       public void setPoolSize(int poolSize) {
           if (poolSize < 1) {
               throw new IllegalArgumentException("poolSize must be at least 1, 
got: " + poolSize);
           }
           this.poolSize = poolSize;
   ```



##########
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:
   ⚠️ **`loan.close()` is called inside the `try` block, not via 
try-with-resources.** If `close()` itself throws (e.g. the pool implementation 
rejects a return), the `catch` fires and calls `loan.discard()` on a loan that 
is already mid-`close()`. Whether `discard()` is safe to call after a partial 
`close()` depends on `OpaPolicyPool` internals — the library does not document 
this as safe.
   
   The conventional pattern for a resource with two lifecycle exits is 
try-with-resources for the normal path plus explicit `discard()` in a 
`catch`-before-TWR-close, but Java's TWR only allows one. The safest rewrite 
keeps the loan outside TWR and uses a flag:
   
   ```suggestion
               Object decision = 
unwrap(loan.policy().evaluate(MAPPER.writeValueAsString(input)));
               loan.close();
               return decision;
           } catch (Exception e) {
               try {
                   loan.discard();
               } catch (Exception suppressed) {
                   e.addSuppressed(suppressed);
               }
               throw e;
   ```
   
   Alternatively, if `close()` cannot throw in practice, a comment stating that 
would be sufficient.



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