gnodet-bot commented on code in PR #26445: URL: https://github.com/apache/camel/pull/26445#discussion_r4013951098
########## 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: ⚠️ **`pool.borrow()` blocks the calling thread indefinitely when the pool is exhausted.** `OpaPolicyPool.borrow()` calls `Semaphore.acquire()` with no timeout. In an authorization component sitting in a high-throughput Camel route, a burst of concurrent exchanges can fill the pool and leave every subsequent Camel worker thread parked on `acquire()` until one of the in-flight evaluations completes. This is not obviously wrong in the normal case — the pool is *meant* to bound concurrency — but: 1. A hung or very slow WASM evaluation (e.g. an infinite loop in a policy, a GC pause during JIT compilation) holds a permit forever. Every thread that later calls `borrow()` parks permanently. There is no route-level timeout that can interrupt a thread blocked inside `Semaphore.acquire()` unless the Camel thread itself is interrupted. 2. The interaction with `doStop()` is subtle: `pool.close()` releases all parked threads via `IllegalStateException`, but between `closeable.close()` returning and `evaluator = null` being written, a concurrent exchange that already passed the null check on `evaluator` and is now blocked in `borrow()` will get the exception rather than a result, which propagates as an evaluation failure rather than a clean shutdown error. The library does not expose a `tryBorrow(long, TimeUnit)` variant as of 1.1.0. Two options: **Option A (preferred):** Implement the pool inline using the same semaphore pattern, adding a configurable `borrowTimeout` (e.g. default 30s). Straightforward since `OpaPolicyPool`'s internals are known and not complex. **Option B:** Document the behaviour explicitly in the `poolSize` Javadoc and the component docs, and note that `poolSize` must be set ≥ the maximum concurrent exchanges the route is expected to see, or threads will park. This is the minimum acceptable bar if Option A is deferred. Either way, `keepsThePoolUsableAfterRepeatedEvaluationFailures` should be joined by a test that actually exercises the blocking path — right now the `@Timeout(60)` guards against a wedged pool, but a test that asserts `tryBorrow` returns within a deadline when exhausted would pin the contract. -- 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]
