oscerd commented on code in PR #26445: URL: https://github.com/apache/camel/pull/26445#discussion_r4023607627
########## 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: Right on both counts, and it is in — @oscerd took your suggestion through the UI, so the wording is yours. Two follow-ups from checking it locally. **It needed a reflow.** Accepting a suggestion in the web UI bypasses `formatter:format`, which wraps at 120 columns and pulls `rule` up a line. Left alone the sourcecheck build would have reported uncommitted changes, so that is pushed on top. **One small thing about the rationale, offered rather than insisted on.** "Regardless of what a previous borrower left on it" describes a case that cannot arise here: `entrypoint` and `data` are final on the evaluator and its pool has its own factory, so a recycled instance always comes back already carrying the right values. What actually makes the per-borrow call necessary is a *fresh* instance — the pool builds one whenever nothing is idle and it still has capacity, and a new `OpaPolicy` starts at entrypoint 0 with no data. Same conclusion, and the wording reads fine; the reason I mention it is that "a previous borrower may have left something on it" is the premise someone would act on later by adding defensive state-tracking that nothing needs. Happy to leave it as it stands rather than churn an approved PR — say the word and I will push: ``` * On every borrow, because a borrow can hand back either a recycled instance or a brand-new one: the pool builds * one whenever nothing is idle and it still has capacity, and a fresh {@link OpaPolicy} starts at entrypoint 0 * with no data. Applying unconditionally costs a map lookup and saves having to know which kind arrived, and it * is why {@link OpaWasmPolicyPool.Lease#close()} can hand an instance back without resetting it. ``` On merge order with #26469: taking the small one first, so this PR absorbs the trivial conflict rather than the security fix queueing behind it. _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]
