This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 8ecaf88b4cb CAMEL-21871: camel-azure-serviebus - TokenCredential is autowired, an… (#17735) 8ecaf88b4cb is described below commit 8ecaf88b4cb5d6064f94b0ed2a3975cc11e8d4c4 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sun Apr 13 16:01:11 2025 +0200 CAMEL-21871: camel-azure-serviebus - TokenCredential is autowired, an… (#17735) * CAMEL-21871: camel-azure-serviebus - TokenCredential is autowired, and ensure if explicit configured on component then that is used. --- .../camel/catalog/components/azure-servicebus.json | 4 +-- .../servicebus/ServiceBusComponentConfigurer.java | 2 +- .../servicebus/ServiceBusEndpointConfigurer.java | 2 +- .../azure/servicebus/azure-servicebus.json | 4 +-- .../azure/servicebus/ServiceBusComponent.java | 29 +++++++++------------- .../azure/servicebus/ServiceBusConfiguration.java | 5 ++-- .../servicebus/client/ServiceBusClientFactory.java | 7 +++++- .../azure/servicebus/ServiceBusEndpointTest.java | 14 +++++++++++ 8 files changed, 40 insertions(+), 27 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/azure-servicebus.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/azure-servicebus.json index e492028d14b..c0e29f7979d 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/azure-servicebus.json +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/azure-servicebus.json @@ -52,7 +52,7 @@ "connectionString": { "index": 25, "kind": "property", "displayName": "Connection String", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConfiguration", "configurationField": "configuration", "description": "Sets the connection string for a Service Bus namespace or a specific Service Bus [...] "credentialType": { "index": 26, "kind": "property", "displayName": "Credential Type", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "org.apache.camel.component.azure.servicebus.CredentialType", "enum": [ "AZURE_IDENTITY", "CONNECTION_STRING", "TOKEN_CREDENTIAL" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "CONNECTION_STRING", "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConfi [...] "fullyQualifiedNamespace": { "index": 27, "kind": "property", "displayName": "Fully Qualified Namespace", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConfiguration", "configurationField": "configuration", "description": "Fully Qualified Namespace of the service bus" }, - "tokenCredential": { "index": 28, "kind": "property", "displayName": "Token Credential", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "com.azure.core.credential.TokenCredential", "deprecated": false, "autowired": false, "secret": true, "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConfiguration", "configurationField": "configuration", "description": "A TokenCredential for Azure AD authentication." } + "tokenCredential": { "index": 28, "kind": "property", "displayName": "Token Credential", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "com.azure.core.credential.TokenCredential", "deprecated": false, "deprecationNote": "", "autowired": true, "secret": true, "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConfiguration", "configurationField": "configuration", "description": "A TokenCredential for Azure AD authe [...] }, "headers": { "CamelAzureServiceBusApplicationProperties": { "index": 0, "kind": "header", "displayName": "", "group": "common", "label": "common", "required": false, "javaType": "Map<String, Object>", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "The application properties (also known as custom properties) on messages sent and received by the producer and consumer, respectively.", "constantName": "org.apache.camel.component.azure.servicebus.Servi [...] @@ -111,6 +111,6 @@ "connectionString": { "index": 26, "kind": "parameter", "displayName": "Connection String", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConfiguration", "configurationField": "configuration", "description": "Sets the connection string for a Service Bus namespace or a specific Service Bus [...] "credentialType": { "index": 27, "kind": "parameter", "displayName": "Credential Type", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "org.apache.camel.component.azure.servicebus.CredentialType", "enum": [ "AZURE_IDENTITY", "CONNECTION_STRING", "TOKEN_CREDENTIAL" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "CONNECTION_STRING", "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConf [...] "fullyQualifiedNamespace": { "index": 28, "kind": "parameter", "displayName": "Fully Qualified Namespace", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConfiguration", "configurationField": "configuration", "description": "Fully Qualified Namespace of the service bus" }, - "tokenCredential": { "index": 29, "kind": "parameter", "displayName": "Token Credential", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "com.azure.core.credential.TokenCredential", "deprecated": false, "autowired": false, "secret": true, "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConfiguration", "configurationField": "configuration", "description": "A TokenCredential for Azure AD authentication." } + "tokenCredential": { "index": 29, "kind": "parameter", "displayName": "Token Credential", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "com.azure.core.credential.TokenCredential", "deprecated": false, "deprecationNote": "", "autowired": true, "secret": true, "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConfiguration", "configurationField": "configuration", "description": "A TokenCredential for Azure AD auth [...] } } diff --git a/components/camel-azure/camel-azure-servicebus/src/generated/java/org/apache/camel/component/azure/servicebus/ServiceBusComponentConfigurer.java b/components/camel-azure/camel-azure-servicebus/src/generated/java/org/apache/camel/component/azure/servicebus/ServiceBusComponentConfigurer.java index 64f26f12ac2..bbd3077ff25 100644 --- a/components/camel-azure/camel-azure-servicebus/src/generated/java/org/apache/camel/component/azure/servicebus/ServiceBusComponentConfigurer.java +++ b/components/camel-azure/camel-azure-servicebus/src/generated/java/org/apache/camel/component/azure/servicebus/ServiceBusComponentConfigurer.java @@ -92,7 +92,7 @@ public class ServiceBusComponentConfigurer extends PropertyConfigurerSupport imp @Override public String[] getAutowiredNames() { - return new String[]{"processorClient", "senderClient"}; + return new String[]{"processorClient", "senderClient", "tokenCredential"}; } @Override diff --git a/components/camel-azure/camel-azure-servicebus/src/generated/java/org/apache/camel/component/azure/servicebus/ServiceBusEndpointConfigurer.java b/components/camel-azure/camel-azure-servicebus/src/generated/java/org/apache/camel/component/azure/servicebus/ServiceBusEndpointConfigurer.java index 4c0952187e0..469ff07db87 100644 --- a/components/camel-azure/camel-azure-servicebus/src/generated/java/org/apache/camel/component/azure/servicebus/ServiceBusEndpointConfigurer.java +++ b/components/camel-azure/camel-azure-servicebus/src/generated/java/org/apache/camel/component/azure/servicebus/ServiceBusEndpointConfigurer.java @@ -86,7 +86,7 @@ public class ServiceBusEndpointConfigurer extends PropertyConfigurerSupport impl @Override public String[] getAutowiredNames() { - return new String[]{"processorClient", "senderClient"}; + return new String[]{"processorClient", "senderClient", "tokenCredential"}; } @Override diff --git a/components/camel-azure/camel-azure-servicebus/src/generated/resources/META-INF/org/apache/camel/component/azure/servicebus/azure-servicebus.json b/components/camel-azure/camel-azure-servicebus/src/generated/resources/META-INF/org/apache/camel/component/azure/servicebus/azure-servicebus.json index e492028d14b..c0e29f7979d 100644 --- a/components/camel-azure/camel-azure-servicebus/src/generated/resources/META-INF/org/apache/camel/component/azure/servicebus/azure-servicebus.json +++ b/components/camel-azure/camel-azure-servicebus/src/generated/resources/META-INF/org/apache/camel/component/azure/servicebus/azure-servicebus.json @@ -52,7 +52,7 @@ "connectionString": { "index": 25, "kind": "property", "displayName": "Connection String", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConfiguration", "configurationField": "configuration", "description": "Sets the connection string for a Service Bus namespace or a specific Service Bus [...] "credentialType": { "index": 26, "kind": "property", "displayName": "Credential Type", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "org.apache.camel.component.azure.servicebus.CredentialType", "enum": [ "AZURE_IDENTITY", "CONNECTION_STRING", "TOKEN_CREDENTIAL" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "CONNECTION_STRING", "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConfi [...] "fullyQualifiedNamespace": { "index": 27, "kind": "property", "displayName": "Fully Qualified Namespace", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConfiguration", "configurationField": "configuration", "description": "Fully Qualified Namespace of the service bus" }, - "tokenCredential": { "index": 28, "kind": "property", "displayName": "Token Credential", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "com.azure.core.credential.TokenCredential", "deprecated": false, "autowired": false, "secret": true, "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConfiguration", "configurationField": "configuration", "description": "A TokenCredential for Azure AD authentication." } + "tokenCredential": { "index": 28, "kind": "property", "displayName": "Token Credential", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "com.azure.core.credential.TokenCredential", "deprecated": false, "deprecationNote": "", "autowired": true, "secret": true, "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConfiguration", "configurationField": "configuration", "description": "A TokenCredential for Azure AD authe [...] }, "headers": { "CamelAzureServiceBusApplicationProperties": { "index": 0, "kind": "header", "displayName": "", "group": "common", "label": "common", "required": false, "javaType": "Map<String, Object>", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "The application properties (also known as custom properties) on messages sent and received by the producer and consumer, respectively.", "constantName": "org.apache.camel.component.azure.servicebus.Servi [...] @@ -111,6 +111,6 @@ "connectionString": { "index": 26, "kind": "parameter", "displayName": "Connection String", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConfiguration", "configurationField": "configuration", "description": "Sets the connection string for a Service Bus namespace or a specific Service Bus [...] "credentialType": { "index": 27, "kind": "parameter", "displayName": "Credential Type", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "org.apache.camel.component.azure.servicebus.CredentialType", "enum": [ "AZURE_IDENTITY", "CONNECTION_STRING", "TOKEN_CREDENTIAL" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "CONNECTION_STRING", "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConf [...] "fullyQualifiedNamespace": { "index": 28, "kind": "parameter", "displayName": "Fully Qualified Namespace", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConfiguration", "configurationField": "configuration", "description": "Fully Qualified Namespace of the service bus" }, - "tokenCredential": { "index": 29, "kind": "parameter", "displayName": "Token Credential", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "com.azure.core.credential.TokenCredential", "deprecated": false, "autowired": false, "secret": true, "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConfiguration", "configurationField": "configuration", "description": "A TokenCredential for Azure AD authentication." } + "tokenCredential": { "index": 29, "kind": "parameter", "displayName": "Token Credential", "group": "security", "label": "security", "required": false, "type": "object", "javaType": "com.azure.core.credential.TokenCredential", "deprecated": false, "deprecationNote": "", "autowired": true, "secret": true, "configurationClass": "org.apache.camel.component.azure.servicebus.ServiceBusConfiguration", "configurationField": "configuration", "description": "A TokenCredential for Azure AD auth [...] } } diff --git a/components/camel-azure/camel-azure-servicebus/src/main/java/org/apache/camel/component/azure/servicebus/ServiceBusComponent.java b/components/camel-azure/camel-azure-servicebus/src/main/java/org/apache/camel/component/azure/servicebus/ServiceBusComponent.java index 0f1af11f787..42a9f930b3f 100644 --- a/components/camel-azure/camel-azure-servicebus/src/main/java/org/apache/camel/component/azure/servicebus/ServiceBusComponent.java +++ b/components/camel-azure/camel-azure-servicebus/src/main/java/org/apache/camel/component/azure/servicebus/ServiceBusComponent.java @@ -17,21 +17,20 @@ package org.apache.camel.component.azure.servicebus; import java.util.Map; -import java.util.Set; -import com.azure.core.credential.TokenCredential; +import com.azure.identity.DefaultAzureCredential; import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.annotations.Component; import org.apache.camel.support.DefaultComponent; -import org.apache.camel.util.ObjectHelper; /** * Azure ServiceBus component */ @Component("azure-servicebus") public class ServiceBusComponent extends DefaultComponent { + @Metadata private ServiceBusConfiguration configuration = new ServiceBusConfiguration(); @@ -44,7 +43,6 @@ public class ServiceBusComponent extends DefaultComponent { @Override protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { - if (remaining == null || remaining.isBlank()) { throw new IllegalArgumentException("A queue or topic name must be specified."); } @@ -57,22 +55,19 @@ public class ServiceBusComponent extends DefaultComponent { final ServiceBusEndpoint endpoint = new ServiceBusEndpoint(uri, this, configuration); setProperties(endpoint, parameters); - setCredentials(configuration); - - return endpoint; - } - - private void setCredentials(final ServiceBusConfiguration configuration) { - if (ObjectHelper.isNotEmpty(configuration.getFullyQualifiedNamespace()) && - ObjectHelper.isEmpty(configuration.getTokenCredential())) { - final Set<TokenCredential> tokenCredentialFromRegistry - = getCamelContext().getRegistry().findByType(TokenCredential.class); - // Find exactly one from the registry or create one - if (tokenCredentialFromRegistry.size() == 1) { - configuration.setTokenCredential(tokenCredentialFromRegistry.stream().findFirst().get()); + // ensure we use default credential type if not configured + if (endpoint.getConfiguration().getTokenCredential() == null) { + if (endpoint.getConfiguration().getCredentialType() == null) { + endpoint.getConfiguration().setCredentialType(CredentialType.CONNECTION_STRING); } + } else { + boolean azure = endpoint.getConfiguration().getTokenCredential() instanceof DefaultAzureCredential; + endpoint.getConfiguration() + .setCredentialType(azure ? CredentialType.AZURE_IDENTITY : CredentialType.TOKEN_CREDENTIAL); } + + return endpoint; } /** diff --git a/components/camel-azure/camel-azure-servicebus/src/main/java/org/apache/camel/component/azure/servicebus/ServiceBusConfiguration.java b/components/camel-azure/camel-azure-servicebus/src/main/java/org/apache/camel/component/azure/servicebus/ServiceBusConfiguration.java index 143bf7dd748..b1b41141d5b 100644 --- a/components/camel-azure/camel-azure-servicebus/src/main/java/org/apache/camel/component/azure/servicebus/ServiceBusConfiguration.java +++ b/components/camel-azure/camel-azure-servicebus/src/main/java/org/apache/camel/component/azure/servicebus/ServiceBusConfiguration.java @@ -32,8 +32,6 @@ import com.azure.messaging.servicebus.models.SubQueue; import org.apache.camel.RuntimeCamelException; import org.apache.camel.spi.*; -import static org.apache.camel.component.azure.servicebus.CredentialType.CONNECTION_STRING; - @UriParams public class ServiceBusConfiguration implements Cloneable, HeaderFilterStrategyAware { @@ -46,6 +44,7 @@ public class ServiceBusConfiguration implements Cloneable, HeaderFilterStrategyA private String connectionString; @UriParam(label = "security") private String fullyQualifiedNamespace; + @Metadata(autowired = true) @UriParam(label = "security", secret = true) private TokenCredential tokenCredential; @UriParam(label = "common") @@ -89,7 +88,7 @@ public class ServiceBusConfiguration implements Cloneable, HeaderFilterStrategyA private boolean binary; @UriParam(label = "security", enums = "AZURE_IDENTITY,CONNECTION_STRING,TOKEN_CREDENTIAL", defaultValue = "CONNECTION_STRING") - private CredentialType credentialType = CONNECTION_STRING; + private CredentialType credentialType; // New fields for session support @UriParam(label = "consumer", defaultValue = "false", description = "Enable session support") private boolean sessionEnabled; diff --git a/components/camel-azure/camel-azure-servicebus/src/main/java/org/apache/camel/component/azure/servicebus/client/ServiceBusClientFactory.java b/components/camel-azure/camel-azure-servicebus/src/main/java/org/apache/camel/component/azure/servicebus/client/ServiceBusClientFactory.java index a245ea2af3a..aed3c0c603c 100644 --- a/components/camel-azure/camel-azure-servicebus/src/main/java/org/apache/camel/component/azure/servicebus/client/ServiceBusClientFactory.java +++ b/components/camel-azure/camel-azure-servicebus/src/main/java/org/apache/camel/component/azure/servicebus/client/ServiceBusClientFactory.java @@ -21,6 +21,7 @@ import java.util.function.Consumer; import com.azure.core.credential.TokenCredential; import com.azure.identity.DefaultAzureCredentialBuilder; import com.azure.messaging.servicebus.*; +import org.apache.camel.component.azure.servicebus.CredentialType; import org.apache.camel.component.azure.servicebus.ServiceBusConfiguration; import org.apache.camel.component.azure.servicebus.ServiceBusType; @@ -36,7 +37,11 @@ public final class ServiceBusClientFactory { String fullyQualifiedNamespace = configuration.getFullyQualifiedNamespace(); TokenCredential credential = configuration.getTokenCredential(); - switch (configuration.getCredentialType()) { + CredentialType type = configuration.getCredentialType(); + if (type == null) { + type = CredentialType.CONNECTION_STRING; + } + switch (type) { case CONNECTION_STRING -> builder.connectionString(configuration.getConnectionString()); case TOKEN_CREDENTIAL -> builder.credential(fullyQualifiedNamespace, credential); case AZURE_IDENTITY -> builder.credential(fullyQualifiedNamespace, new DefaultAzureCredentialBuilder().build()); diff --git a/components/camel-azure/camel-azure-servicebus/src/test/java/org/apache/camel/component/azure/servicebus/ServiceBusEndpointTest.java b/components/camel-azure/camel-azure-servicebus/src/test/java/org/apache/camel/component/azure/servicebus/ServiceBusEndpointTest.java index fdc10d7b7b2..c4a87039ce9 100644 --- a/components/camel-azure/camel-azure-servicebus/src/test/java/org/apache/camel/component/azure/servicebus/ServiceBusEndpointTest.java +++ b/components/camel-azure/camel-azure-servicebus/src/test/java/org/apache/camel/component/azure/servicebus/ServiceBusEndpointTest.java @@ -23,8 +23,10 @@ import com.azure.core.credential.AccessToken; import com.azure.core.credential.TokenCredential; import com.azure.core.credential.TokenRequestContext; import com.azure.identity.DefaultAzureCredentialBuilder; +import com.azure.messaging.servicebus.ServiceBusSenderClient; import org.apache.camel.FailedToCreateProducerException; import org.apache.camel.ResolveEndpointFailedException; +import org.apache.camel.component.azure.servicebus.client.ServiceBusClientFactory; import org.apache.camel.test.junit5.CamelTestSupport; import org.junit.jupiter.api.Test; import reactor.core.publisher.Mono; @@ -168,6 +170,7 @@ class ServiceBusEndpointTest extends CamelTestSupport { assertEquals(10, endpoint.getConfiguration().getPrefetchCount()); assertEquals(fullyQualifiedNamespace, endpoint.getConfiguration().getFullyQualifiedNamespace()); assertEquals(credential, endpoint.getConfiguration().getTokenCredential()); + assertEquals(CredentialType.AZURE_IDENTITY, endpoint.getConfiguration().getCredentialType()); } @Test @@ -192,4 +195,15 @@ class ServiceBusEndpointTest extends CamelTestSupport { assertEquals(fullyQualifiedNamespace, endpoint.getConfiguration().getFullyQualifiedNamespace()); assertNull(endpoint.getConfiguration().getTokenCredential()); } + + @Test + void testCreateBaseServiceBusClientWithNoCredentialType() throws Exception { + ServiceBusConfiguration configuration = new ServiceBusConfiguration(); + configuration.setConnectionString("Endpoint=sb://camel.apache.org/;SharedAccessKeyName=test;SharedAccessKey=test"); + configuration.setTopicOrQueueName("myQueue"); + ServiceBusClientFactory factory = new ServiceBusClientFactory(); + ServiceBusSenderClient senderClient = factory.createServiceBusSenderClient(configuration); + assertNotNull(senderClient); + senderClient.close(); + } }