This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27219 in repository https://gitbox.apache.org/repos/asf/camel.git
commit c74b7fc5a768bf2c7f54b0e8e736a8b3e4368355 Author: Claus Ibsen <[email protected]> AuthorDate: Thu Nov 19 07:41:05 2026 +0100 CAMEL-27219: camel-ai-tool - a configuration bean combines with the options set on the component or the endpoint An AiToolConfiguration bean given as the configuration option no longer replaces the options set beside it. On the component, the options win over the values of the bean in whatever order they are bound. On an endpoint, the endpoint options win over the bean, and the values the bean does not set come from the component, such as its authorizationPolicy and allowedRoles. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../camel/catalog/docs/ai-tool-component.adoc | 20 ++ .../src/main/docs/ai-tool-component.adoc | 20 ++ .../camel/component/ai/tool/AiToolComponent.java | 17 ++ .../component/ai/tool/AiToolConfiguration.java | 59 ++++++ .../ai/tool/AiToolConfigurationBeanTest.java | 214 +++++++++++++++++++++ .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 12 ++ 6 files changed, 342 insertions(+) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/ai-tool-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/ai-tool-component.adoc index a656f564715b..8dcca187f591 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/ai-tool-component.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/ai-tool-component.adoc @@ -590,6 +590,26 @@ check: a value that names no role, such as an empty one, refuses every call. The its start names the tool. Only an option that is not set takes the component's value; that includes an optional placeholder (`{{?name}}`) whose property is absent. +=== With a configuration bean + +The options can also come from an `AiToolConfiguration` bean, given as the `configuration` option of the component or +of an endpoint. The bean combines with the options set beside it, so `authorizationPolicy` and `allowedRoles` apply +whether they are set as an option or in the bean: + +* On the component, an option set on the component wins over the value of the bean, in whatever order the two are + set. An option that is not set takes the value of the bean. +* On an endpoint, an option set on the endpoint wins over the value of the bean. An option that neither the endpoint + nor the bean sets takes the value of the component. An endpoint with `configuration=#toolDefaults` is therefore + still guarded by the `authorizationPolicy` and the `allowedRoles` of the component, unless the bean or the endpoint + sets its own. + +[source,properties] +---- +camel.component.ai-tool.configuration = #toolDefaults +# kept, and applied to every tool route +camel.component.ai-tool.authorization-policy = #myAuthorizationPolicy +---- + === Observing denials Because the guard runs in front of the route, a denied call is caught before the route's unit of work and so fires no diff --git a/components/camel-ai/camel-ai-tool/src/main/docs/ai-tool-component.adoc b/components/camel-ai/camel-ai-tool/src/main/docs/ai-tool-component.adoc index a656f564715b..8dcca187f591 100644 --- a/components/camel-ai/camel-ai-tool/src/main/docs/ai-tool-component.adoc +++ b/components/camel-ai/camel-ai-tool/src/main/docs/ai-tool-component.adoc @@ -590,6 +590,26 @@ check: a value that names no role, such as an empty one, refuses every call. The its start names the tool. Only an option that is not set takes the component's value; that includes an optional placeholder (`{{?name}}`) whose property is absent. +=== With a configuration bean + +The options can also come from an `AiToolConfiguration` bean, given as the `configuration` option of the component or +of an endpoint. The bean combines with the options set beside it, so `authorizationPolicy` and `allowedRoles` apply +whether they are set as an option or in the bean: + +* On the component, an option set on the component wins over the value of the bean, in whatever order the two are + set. An option that is not set takes the value of the bean. +* On an endpoint, an option set on the endpoint wins over the value of the bean. An option that neither the endpoint + nor the bean sets takes the value of the component. An endpoint with `configuration=#toolDefaults` is therefore + still guarded by the `authorizationPolicy` and the `allowedRoles` of the component, unless the bean or the endpoint + sets its own. + +[source,properties] +---- +camel.component.ai-tool.configuration = #toolDefaults +# kept, and applied to every tool route +camel.component.ai-tool.authorization-policy = #myAuthorizationPolicy +---- + === Observing denials Because the guard runs in front of the route, a denied call is caught before the route's unit of work and so fires no diff --git a/components/camel-ai/camel-ai-tool/src/main/java/org/apache/camel/component/ai/tool/AiToolComponent.java b/components/camel-ai/camel-ai-tool/src/main/java/org/apache/camel/component/ai/tool/AiToolComponent.java index 007bcc115696..c8da477d1e58 100644 --- a/components/camel-ai/camel-ai-tool/src/main/java/org/apache/camel/component/ai/tool/AiToolComponent.java +++ b/components/camel-ai/camel-ai-tool/src/main/java/org/apache/camel/component/ai/tool/AiToolComponent.java @@ -62,7 +62,15 @@ public class AiToolComponent extends DefaultComponent { final String toolName = remaining; + // the configuration bean of the endpoint is taken out of the parameters before the options are bound, so the + // options set on the endpoint win over the values of the bean, whatever the order they are given in, and the + // values that the bean does not set come from the component (such as its authorization policy) + AiToolConfiguration endpointConfiguration + = resolveAndRemoveReferenceParameter(parameters, "configuration", AiToolConfiguration.class); AiToolConfiguration config = this.configuration.copy(); + if (endpointConfiguration != null) { + config.overrideWith(endpointConfiguration); + } Map<String, Object> toolParameters = PropertiesHelper.extractProperties(parameters, "parameter."); if (!toolParameters.isEmpty()) { @@ -86,7 +94,16 @@ public class AiToolComponent extends DefaultComponent { return configuration; } + /** + * Sets the component configuration. The options already set on the component are kept, and win over the values of + * the given configuration: when the configuration and the options are bound together (such as from + * {@code camel.component.ai-tool.*} properties) the result does not depend on the order they are bound in, and an + * option such as {@code authorizationPolicy} is never dropped. + */ public void setConfiguration(AiToolConfiguration configuration) { + if (configuration != null && this.configuration != null && configuration != this.configuration) { + configuration.overrideWith(this.configuration); + } this.configuration = configuration; } } diff --git a/components/camel-ai/camel-ai-tool/src/main/java/org/apache/camel/component/ai/tool/AiToolConfiguration.java b/components/camel-ai/camel-ai-tool/src/main/java/org/apache/camel/component/ai/tool/AiToolConfiguration.java index 64526f5bdcb9..7edf0f2a4b44 100644 --- a/components/camel-ai/camel-ai-tool/src/main/java/org/apache/camel/component/ai/tool/AiToolConfiguration.java +++ b/components/camel-ai/camel-ai-tool/src/main/java/org/apache/camel/component/ai/tool/AiToolConfiguration.java @@ -287,6 +287,65 @@ public class AiToolConfiguration implements Cloneable { this.allRolesRequired = allRolesRequired; } + /** + * Copies onto this configuration every value that is set on the other one, and keeps the values of this + * configuration that the other one does not set. A value is set when it is not null, or, for a boolean option whose + * default is false, when it is true. + */ + void overrideWith(AiToolConfiguration other) { + if (other.tags != null) { + tags = other.tags; + } + if (other.description != null) { + description = other.description; + } + if (other.parameters != null) { + parameters = new HashMap<>(other.parameters); + } + if (other.argSchema != null) { + argSchema = other.argSchema; + } + if (other.argumentsAsBody) { + argumentsAsBody = true; + } + if (other.outputParameters != null) { + outputParameters = new HashMap<>(other.outputParameters); + } + if (other.outputSchema != null) { + outputSchema = other.outputSchema; + } + if (other.title != null) { + title = other.title; + } + if (other.readOnlyHint != null) { + readOnlyHint = other.readOnlyHint; + } + if (other.destructiveHint != null) { + destructiveHint = other.destructiveHint; + } + if (other.idempotentHint != null) { + idempotentHint = other.idempotentHint; + } + if (other.openWorldHint != null) { + openWorldHint = other.openWorldHint; + } + if (other.returnDirect != null) { + returnDirect = other.returnDirect; + } + if (other.searchable) { + searchable = true; + } + if (other.authorizationPolicy != null) { + authorizationPolicy = other.authorizationPolicy; + } + if (other.allowedRoles != null) { + allowedRoles = other.allowedRoles; + } + if (other.allRolesRequired) { + allRolesRequired = true; + } + } + public AiToolConfiguration copy() { try { AiToolConfiguration copy = (AiToolConfiguration) super.clone(); diff --git a/components/camel-ai/camel-ai-tool/src/test/java/org/apache/camel/component/ai/tool/AiToolConfigurationBeanTest.java b/components/camel-ai/camel-ai-tool/src/test/java/org/apache/camel/component/ai/tool/AiToolConfigurationBeanTest.java new file mode 100644 index 000000000000..ddb71bc98e0b --- /dev/null +++ b/components/camel-ai/camel-ai-tool/src/test/java/org/apache/camel/component/ai/tool/AiToolConfigurationBeanTest.java @@ -0,0 +1,214 @@ +/* + * 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.ai.tool; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.camel.CamelAuthorizationException; +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.NamedNode; +import org.apache.camel.Processor; +import org.apache.camel.Route; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.spi.AuthorizationPolicy; +import org.apache.camel.support.DefaultExchange; +import org.apache.camel.support.PropertyBindingSupport; +import org.apache.camel.test.junit6.CamelTestSupport; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * A {@code configuration} bean, on the component or on an endpoint, combines with the options set at the same level and + * with the component's values: an option that is set wins over the bean's value, and a value the bean does not set is + * taken from the option or from the component. The authorization guard of a tool is therefore the same whether its + * options come from a configuration bean or not. + */ +class AiToolConfigurationBeanTest extends CamelTestSupport { + + static final class SubjectPolicy implements AuthorizationPolicy { + private final String subject; + + SubjectPolicy(String subject) { + this.subject = subject; + } + + @Override + public void beforeWrap(Route route, NamedNode definition) { + } + + @Override + public Processor wrap(Route route, Processor processor) { + return exchange -> { + if (!subject.equals(exchange.getProperty("subject", String.class))) { + throw new CamelAuthorizationException("caller is not authorized", exchange); + } + processor.process(exchange); + }; + } + } + + private final AiToolConfiguration componentDefaults = new AiToolConfiguration(); + private final AiToolConfiguration endpointDefaults = new AiToolConfiguration(); + private final AiToolConfiguration endpointDefaultsWithPolicy = new AiToolConfiguration(); + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext ctx = super.createCamelContext(); + ctx.getRegistry().bind("alicePolicy", new SubjectPolicy("alice")); + ctx.getRegistry().bind("carolPolicy", new SubjectPolicy("carol")); + + componentDefaults.setTags("t"); + ctx.getRegistry().bind("componentDefaults", componentDefaults); + + endpointDefaults.setTags("t"); + endpointDefaults.setDescription("From the bean"); + ctx.getRegistry().bind("endpointDefaults", endpointDefaults); + + endpointDefaultsWithPolicy.setTags("t"); + endpointDefaultsWithPolicy.setAuthorizationPolicy(new SubjectPolicy("carol")); + ctx.getRegistry().bind("endpointDefaultsWithPolicy", endpointDefaultsWithPolicy); + + // bind the component options as Camel Main does for camel.component.ai-tool.* + bindComponent(ctx, Map.of("configuration", "#componentDefaults", "authorizationPolicy", "#alicePolicy")); + return ctx; + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + from("ai-tool:componentBean?description=Component bean") + .setBody(constant("ok")); + from("ai-tool:endpointBean?configuration=#endpointDefaults") + .setBody(constant("ok")); + from("ai-tool:endpointBeanAndPolicy?configuration=#endpointDefaults&authorizationPolicy=#carolPolicy") + .setBody(constant("ok")); + from("ai-tool:endpointBeanWithPolicy?configuration=#endpointDefaultsWithPolicy&description=Bean policy") + .setBody(constant("ok")); + from("ai-tool:endpointBeanAndRoles?configuration=#endpointDefaults&allowedRoles=finance") + .setBody(constant("ok")); + from("ai-tool:endpointBeanAndDescription?configuration=#endpointDefaults&description=From the endpoint") + .setBody(constant("ok")); + } + }; + } + + @Test + void componentPolicyIsKeptWithComponentConfigurationBean() { + AiToolSpec spec = findSpec("componentBean"); + assertThat(call(spec, "bob")).isInstanceOf(AiToolResult.AuthorizationDenied.class); + assertThat(call(spec, "alice")).isInstanceOf(AiToolResult.Success.class); + } + + @Test + void componentPolicyIsKeptWithEndpointConfigurationBean() { + AiToolSpec spec = findSpec("endpointBean"); + assertThat(spec.getDescription()).isEqualTo("From the bean"); + assertThat(call(spec, "bob")).isInstanceOf(AiToolResult.AuthorizationDenied.class); + assertThat(call(spec, "alice")).isInstanceOf(AiToolResult.Success.class); + } + + @Test + void endpointPolicyIsKeptWithEndpointConfigurationBean() { + AiToolSpec spec = findSpec("endpointBeanAndPolicy"); + assertThat(call(spec, "alice")).isInstanceOf(AiToolResult.AuthorizationDenied.class); + assertThat(call(spec, "carol")).isInstanceOf(AiToolResult.Success.class); + } + + @Test + void policyOfEndpointConfigurationBeanReplacesComponentPolicy() { + AiToolSpec spec = findSpec("endpointBeanWithPolicy"); + assertThat(spec.getDescription()).isEqualTo("Bean policy"); + assertThat(call(spec, "alice")).isInstanceOf(AiToolResult.AuthorizationDenied.class); + assertThat(call(spec, "carol")).isInstanceOf(AiToolResult.Success.class); + } + + @Test + void endpointAllowedRolesIsKeptWithEndpointConfigurationBean() { + AiToolSpec spec = findSpec("endpointBeanAndRoles"); + // alice passes the component policy, but has not the role + assertThat(call(spec, "alice")).isInstanceOf(AiToolResult.AuthorizationDenied.class); + + Exchange finance = new DefaultExchange(context); + finance.setProperty("subject", "alice"); + finance.setProperty(AiToolConstants.SECURITY_PRINCIPAL_ROLES_PROPERTY, List.of("finance")); + assertThat(AiToolExecutor.execute(spec, Map.of(), finance)).isInstanceOf(AiToolResult.Success.class); + } + + @Test + void endpointOptionWinsOverEndpointConfigurationBean() { + assertThat(findSpec("endpointBeanAndDescription").getDescription()).isEqualTo("From the endpoint"); + // the bean is shared by several endpoints, and none of them changes it + assertThat(endpointDefaults.getDescription()).isEqualTo("From the bean"); + assertThat(endpointDefaults.getAuthorizationPolicy()).isNull(); + assertThat(endpointDefaults.getAllowedRoles()).isNull(); + } + + @Test + void componentOptionsWinOverComponentConfigurationBeanInAnyOrder() throws Exception { + try (CamelContext other = new DefaultCamelContext()) { + SubjectPolicy policy = new SubjectPolicy("alice"); + SubjectPolicy beanPolicy = new SubjectPolicy("carol"); + AiToolConfiguration bean = new AiToolConfiguration(); + bean.setTags("fromBean"); + bean.setDescription("From the bean"); + bean.setAuthorizationPolicy(beanPolicy); + other.getRegistry().bind("bean", bean); + other.getRegistry().bind("policy", policy); + + // a value that is not a reference is bound before the configuration, and a reference after it + AiToolComponent component = bindComponent(other, + Map.of("configuration", "#bean", "authorizationPolicy", "#policy", "tags", "fromOption", + "allowedRoles", "finance")); + + AiToolConfiguration config = component.getConfiguration(); + assertThat(config.getAuthorizationPolicy()).isSameAs(policy); + assertThat(config.getTags()).isEqualTo("fromOption"); + assertThat(config.getAllowedRoles()).isEqualTo("finance"); + assertThat(config.getDescription()).isEqualTo("From the bean"); + } + } + + private static AiToolComponent bindComponent(CamelContext ctx, Map<String, Object> options) { + AiToolComponent component = ctx.getComponent("ai-tool", AiToolComponent.class); + PropertyBindingSupport.build() + .withCamelContext(ctx) + .withTarget(component) + .withProperties(new HashMap<>(options)) + .withIgnoreCase(true) + .bind(); + return component; + } + + private Object call(AiToolSpec spec, String subject) { + Exchange exchange = new DefaultExchange(context); + exchange.setProperty("subject", subject); + return AiToolExecutor.execute(spec, Map.of(), exchange); + } + + private AiToolSpec findSpec(String toolName) { + return AiToolRegistry.getOrCreate(context).getToolsByTag("t").stream() + .filter(s -> toolName.equals(s.getName())) + .findFirst() + .orElseThrow(() -> new AssertionError("Tool not found: " + toolName)); + } +} diff --git a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc index d552fbdb2309..5e178c664053 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc @@ -417,6 +417,18 @@ call to the suspended tool or read of the suspended resource is refused, and res resource again. When an `ai-tool` or `ai-resource` route is stopped, or Camel shuts down gracefully, tool calls and resource reads in flight now complete instead of failing. +=== camel-ai-tool - a configuration bean combines with the options set at the same level + +An `AiToolConfiguration` bean given as the `configuration` option no longer replaces the options set beside it: + +- On the component, the options set on the component (such as `camel.component.ai-tool.authorization-policy`) are + kept when `camel.component.ai-tool.configuration` is also set, and win over the values of the bean, in whatever + order they are bound. Previously the bean could replace an option, depending on the order of binding. +- On an endpoint, `configuration=#myBean` gives the values that the bean sets. The other options of the endpoint win + over them, and the values that the bean does not set come from the component, such as its `authorizationPolicy` and + `allowedRoles`. Previously the bean replaced the options of the endpoint and the values of the component. The bean + itself is no longer changed by the options of the endpoints that use it. + === camel-wordpress - each endpoint uses its own site URL and credentials Each WordPress endpoint now has a client of its own, set up from the `url`, `apiVersion`, `user` and `password`
