davsclaus commented on code in PR #26445: URL: https://github.com/apache/camel/pull/26445#discussion_r4017474202
########## 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: Nit: this javadoc still explains the per-borrow re-apply in terms of the SDK's `OpaPolicyPool.Loan` calling `reset()`, but that pool is no longer used (and `OpaPolicyPool` is not imported, so the `{@link}` does not resolve). With the camel-owned pool nothing resets an instance on return; the correct rationale is already in `OpaWasmPolicyPool.Lease#close()`. Something like: ```suggestion * Applies the entrypoint and data to a borrowed instance. * <p/> * Done on every borrow rather than once at creation, so that a pooled instance always evaluates the configured * rule against the bundle's data regardless of what a previous borrower left on it. See * {@link OpaWasmPolicyPool.Lease#close()} for why nothing is reset on return. ``` -- 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]
