davsclaus commented on code in PR #26445: URL: https://github.com/apache/camel/pull/26445#discussion_r4012011044
########## components/camel-opa/src/main/java/org/apache/camel/component/opa/OpaWasmEvaluator.java: ########## @@ -0,0 +1,117 @@ +/* + * 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.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 final OpaPolicyPool pool; + + public OpaWasmEvaluator(byte[] wasm, String entrypoint, int poolSize, String policyPath, String allowKey, + String includeHeaders, String includeProperties, boolean includeBody, boolean failOpen) { + super(policyPath, allowKey, includeHeaders, includeProperties, includeBody, failOpen); + // 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().entrypoint(entrypoint), + poolSize); + } + + /** + * 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 byte[] loadPolicy(CamelContext camelContext, String location) throws Exception { + try (InputStream in = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, location)) { + byte[] content = in.readAllBytes(); + return isGzip(content) ? extractFromBundle(content, location) : content; + } + } + + private static boolean isGzip(byte[] content) { + return content.length > 1 && (content[0] & 0xff) == 0x1f && (content[1] & 0xff) == 0x8b; + } + + private static byte[] extractFromBundle(byte[] bundle, String location) throws Exception { + try (TarArchiveInputStream tar + = new TarArchiveInputStream(new GzipCompressorInputStream(new ByteArrayInputStream(bundle)))) { + TarArchiveEntry entry; + while ((entry = tar.getNextEntry()) != null) { + if (!entry.isDirectory() && entry.getName().endsWith(POLICY_WASM)) { Review Comment: ⚠️ Only `policy.wasm` is taken from the bundle. `opa build` also packs `/data.json` (and any `data.yaml`) next to it, and a policy referencing `data.roles` etc. will silently evaluate against empty data here. Either load it and apply `policy.data(...)` per borrow (note `reset()` clears data too, so it has to be re-applied each time), or state the limitation prominently in the docs and the `policyBundle` description. ########## components/camel-opa/src/main/java/org/apache/camel/component/opa/OpaWasmEvaluator.java: ########## @@ -0,0 +1,117 @@ +/* + * 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.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 final OpaPolicyPool pool; + + public OpaWasmEvaluator(byte[] wasm, String entrypoint, int poolSize, String policyPath, String allowKey, + String includeHeaders, String includeProperties, boolean includeBody, boolean failOpen) { + super(policyPath, allowKey, includeHeaders, includeProperties, includeBody, failOpen); + // 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().entrypoint(entrypoint), + poolSize); + } + + /** + * 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 byte[] loadPolicy(CamelContext camelContext, String location) throws Exception { + try (InputStream in = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, location)) { + byte[] content = in.readAllBytes(); + return isGzip(content) ? extractFromBundle(content, location) : content; + } + } + + private static boolean isGzip(byte[] content) { + return content.length > 1 && (content[0] & 0xff) == 0x1f && (content[1] & 0xff) == 0x8b; + } + + private static byte[] extractFromBundle(byte[] bundle, String location) throws Exception { + try (TarArchiveInputStream tar + = new TarArchiveInputStream(new GzipCompressorInputStream(new ByteArrayInputStream(bundle)))) { + TarArchiveEntry entry; + while ((entry = tar.getNextEntry()) != null) { + if (!entry.isDirectory() && entry.getName().endsWith(POLICY_WASM)) { + return tar.readAllBytes(); + } + } + } + throw new IllegalArgumentException( + "No " + POLICY_WASM + " inside the bundle at " + location + + ". Build it with: opa build -t wasm -e <entrypoint> <policy.rego>"); + } + + @Override + protected Object evaluateDecision(Map<String, Object> input) throws Exception { + try (OpaPolicyPool.Loan loan = pool.borrow()) { + return unwrap(loan.policy().evaluate(MAPPER.writeValueAsString(input))); Review Comment: Non-blocking: on an exception the loan is closed and the instance goes back into the pool. The library javadoc on `Loan` says to call `discard()` instead when a processing error may have left the policy in a bad state (a WASM trap mid-eval qualifies). Worth catching and discarding rather than returning. ########## components/camel-opa/src/main/java/org/apache/camel/component/opa/OpaWasmEvaluator.java: ########## @@ -0,0 +1,117 @@ +/* + * 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.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 final OpaPolicyPool pool; + + public OpaWasmEvaluator(byte[] wasm, String entrypoint, int poolSize, String policyPath, String allowKey, + String includeHeaders, String includeProperties, boolean includeBody, boolean failOpen) { + super(policyPath, allowKey, includeHeaders, includeProperties, includeBody, failOpen); + // 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().entrypoint(entrypoint), Review Comment: 🔴 The entrypoint is lost after the first exchange. `OpaPolicyPool.Loan.close()` calls `OpaPolicy.reset()`, and in 1.1.0 that does: ```java void reset() { ... dataAddr = -1; inputAddr = -1; entrypoint = 0; } ``` So the entrypoint set here in the factory only survives the *first* borrow; every later exchange served by that instance evaluates entrypoint 0 — whichever rule that is in the bundle. In an authorization component that is the wrong rule deciding. The tests don't see it because all multi-message tests use `authz/allow` (index 0) and both `authz/decision` tests send exactly one message each (fresh instance). Fix is to set it per borrow in `evaluateDecision` — `findEntrypoint` is a map lookup so it's free: ```java try (OpaPolicyPool.Loan loan = pool.borrow()) { return unwrap(loan.policy().entrypoint(entrypoint).evaluate(MAPPER.writeValueAsString(input))); } ``` and add a test that sends two or more messages to a non-zero entrypoint (e.g. `authz/decision` twice, asserting the decision object both times). ########## components/camel-opa/src/main/java/org/apache/camel/component/opa/OpaEndpoint.java: ########## @@ -57,16 +60,40 @@ public OpaEndpoint(final String uri, final Component component, final OpaConfigu @Override protected void doStart() throws Exception { super.doStart(); - opaClient = configuration.getOpaClient() != null - ? configuration.getOpaClient() - : OpaPolicyEvaluator.createClient(configuration.getServerUrl(), configuration.getBearerToken()); - evaluator = new OpaPolicyEvaluator( - opaClient, policyPath, configuration.getAllowKey(), configuration.getIncludeHeaders(), - configuration.getIncludeProperties(), configuration.isIncludeBody(), configuration.isFailOpen()); + if (WASM_MODE.equalsIgnoreCase(configuration.getEvaluationMode())) { Review Comment: +1 to the earlier comment: an unknown `evaluationMode` should fail at start rather than silently become `rest` and ignore `policyBundle`. ########## components/camel-opa/src/main/java/org/apache/camel/component/opa/OpaEndpoint.java: ########## @@ -57,16 +60,40 @@ public OpaEndpoint(final String uri, final Component component, final OpaConfigu @Override protected void doStart() throws Exception { super.doStart(); - opaClient = configuration.getOpaClient() != null - ? configuration.getOpaClient() - : OpaPolicyEvaluator.createClient(configuration.getServerUrl(), configuration.getBearerToken()); - evaluator = new OpaPolicyEvaluator( - opaClient, policyPath, configuration.getAllowKey(), configuration.getIncludeHeaders(), - configuration.getIncludeProperties(), configuration.isIncludeBody(), configuration.isFailOpen()); + if (WASM_MODE.equalsIgnoreCase(configuration.getEvaluationMode())) { + evaluator = createWasmEvaluator(); + } else { + opaClient = configuration.getOpaClient() != null + ? configuration.getOpaClient() + : OpaRestEvaluator.createClient(configuration.getServerUrl(), configuration.getBearerToken()); + evaluator = new OpaRestEvaluator( + opaClient, policyPath, configuration.getAllowKey(), configuration.getIncludeHeaders(), + configuration.getIncludeProperties(), configuration.isIncludeBody(), configuration.isFailOpen()); + } + } + + private OpaPolicyEvaluator createWasmEvaluator() throws Exception { + if (ObjectHelper.isEmpty(configuration.getPolicyBundle())) { + throw new IllegalArgumentException( + "policyBundle is required when evaluationMode=wasm; build one with" + + " opa build -t wasm -e <entrypoint> <policy.rego>"); + } + // the entrypoint is fixed at build time and is not the same thing as a data path, but opa build names it + // after the rule, so the policy path is the right default + String entrypoint = ObjectHelper.isNotEmpty(configuration.getEntrypoint()) + ? configuration.getEntrypoint() : policyPath; + byte[] wasm = OpaWasmEvaluator.loadPolicy(getCamelContext(), configuration.getPolicyBundle()); Review Comment: Non-blocking: the pool is lazy, so a bad or ABI-incompatible module (the `OpaPolicy` constructor throws on the version check) is only discovered on the first exchange. Borrowing and returning one instance here would make a broken bundle fail startup instead, and warms the first instance. ########## components/camel-opa/pom.xml: ########## @@ -47,6 +47,23 @@ <groupId>org.apache.camel</groupId> <artifactId>camel-health</artifactId> </dependency> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-compress</artifactId> + <version>${commons-compress-version}</version> + </dependency> + <dependency> + <groupId>com.styra.opa</groupId> + <artifactId>opa-java-wasm</artifactId> + <version>${opa-wasm-version}</version> + <exclusions> + <!-- the SDK declares a test engine at runtime scope --> Review Comment: Nit: upstream `opa-java-wasm` 1.1.0 declares `junit-jupiter-engine` at *test* scope (and `run.endive:runtime` likewise), so this exclusion is a no-op. Harmless, but the comment is misleading — drop it or reword. -- 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]
