Search code examples
apache-camelazureservicebus

How do I use camel-azure-servicebus with ClientID & Token


I am running Camel inside ActiveMQ trying to connect to an Azure ServiceBus Queue, but org.apache.camel.component.azure.servicebus.ServiceBusConfiguration appears only to accept a connectionString property. Sadly I don't have and can't get a connection string.

I have been allocated a ClientID, Tenant ID, Secret instead. I can't work out how to implement this with my existing xml based configuration. I have tried to craft a connection string from the details I have but I guess this is not possible.

    <endpoint id="azureQueueEndpoint" uri="azureServiceBusComponent:iris.705d3ce1-xxxx-4fdf-acb3-xxxxxx" />
    
    <route id="Elexon_IRIS_Route">
        <from uri="azureQueueEndpoint" />
        <to uri="localAMQ:topic:IRIS-Elexon"/>
    </route>
    
    <bean id="azureServiceBusComponent" class="org.apache.camel.component.azure.servicebus.ServiceBusComponent">
            
        <property name="configuration">
            <bean class="org.apache.camel.component.azure.servicebus.ServiceBusConfiguration">
                <property name="connectionString" value="Endpoint=https://elexon-iris.servicebus.windows.net/iris.705d3ce1-xxxx-4fdf-acb3-xxxxxx;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=?????;EntityPath=iris.705d3ce1-xxxx-4fdf-acb3-xxxxxx" />
            </bean>
        </property>
    </bean>

I'm not great with Java and can only really understand xml based config. Currently I get the following error when starting the route;

WARN | {"az.sdk.message":"Non-retryable error occurred in AMQP receive link.","exception":"status-code: 401, status-description: InvalidSignature: The token has an invalid signature., errorContext[NAMESPACE: elexon-iris.servicebus.windows.net. ERROR CONTEXT: N/A, PATH: $cbs, REFERENCE_ID: cbs:receiver, LINK_CREDIT: 0]","linkName":"n/a","entityPath":"n/a"}
ERROR | Errors occurred upstream.

I am trying to connect to the ServiceBus and bridge the message feed to an ActiveMQ queue. The is using data from Elexon-IRIS (free service)


Solution

  • Somehow I have been able to resolve the connection issue. In part thanks to Chat-GPT for providing an example based on the source code from the com.azure.identity jar.

    I will not list all the dependant .jars as there are many...

    <endpoint id="azureQueueEndpoint" uri="azureServiceBusComponent:iris.705d3ce1-xxxx-4fdf-acb3-xxxxxxxx" />
    
    <route id="Elexon_IRIS_Route">
        <from uri="azureQueueEndpoint" />
        <to uri="localAMQ:topic:IRIS-Elexon"/>
    </route>
    
    <bean id="azureServiceBusComponent" class="org.apache.camel.component.azure.servicebus.ServiceBusComponent">    
        <property name="configuration">
            <bean class="org.apache.camel.component.azure.servicebus.ServiceBusConfiguration">
                <property name="tokenCredential" ref="azauth" />
                <property name="credentialType" value="TOKEN_CREDENTIAL" />
                <property name="fullyQualifiedNamespace" value="elexon-iris.servicebus.windows.net" />
            </bean>
        </property>
    </bean>
    
    <bean id="azauth" class="com.azure.identity.ClientSecretCredential">
        <constructor-arg value="AZURE_TENANT_ID"/>
        <constructor-arg value="CLIENT_ID"/>
        <constructor-arg value="CLIENT_SECRET"/>
            <constructor-arg>
                <bean class="com.azure.identity.implementation.IdentityClientOptions"/>
        </constructor-arg>
    </bean>
    

    On connection I now get a valid token;

     INFO | Azure Identity => getToken() result for scopes [https://servicebus.azure.net/.default]: SUCCESS
     INFO | {"az.sdk.message":"Scheduling refresh token task.","scopes":"https://servicebus.azure.net/.default"}
     INFO | {"az.sdk.message":"Creating a new receiver link.","connectionId":"MF_77e428_1686394354332","sessionName":"iris.705d3ce1-xxxx-4fdf-acb3-xxxxxxxx","linkName":"iris.705d3ce1-xxxx-4fdf-acb3-xxxxxxxx_76bf6f_1686394354961"}
     INFO | {"az.sdk.message":"Setting next AMQP receive link.","linkName":"iris.705d3ce1-xxxx-4fdf-acb3-0fcb836d2bc9_76bf6f_1686394354961","entityPath":"iris.705d3ce1-xxxx-4fdf-acb3-xxxxxxxx"}
     INFO | {"az.sdk.message":"Returning existing receive link.","connectionId":"MF_77e428_1686394354332","linkName":"iris.705d3ce1-690e-4fdf-acb3-0fcb836d2bc9_76bf6f_1686394354961","entityPath":"iris.705d3ce1-xxxx-4fdf-acb3-xxxxxxxx"}
     INFO | {"az.sdk.message":"onLinkRemoteOpen","connectionId":"MF_77e428_1686394354332","entityPath":"iris.705d3ce1-xxxx-4fdf-acb3-xxxxxxxx","linkName":"iris.705d3ce1-xxxx-4fdf-acb3-xxxxxxxx_76bf6f_1686394354961","remoteSource":"Source{address='iris.705d3ce1-xxxx-4fdf-acb3-xxxxxxxx', durable=NONE, expiryPolicy=SESSION_END, timeout=0, dynamic=false, dynamicNodeProperties=null, distributionMode=null, filter=null, defaultOutcome=null, outcomes=null, capabilities=null}"}
    

    I have other problems now though; I have a conflict with the ASM packages. The azure stuff requires asm-1.0.2 which seem to break the embedded jetty server with;

     WARN | Failed startup of context o.e.j.w.WebAppContext@25de8898{/admin,file:///root/apache-activemq-5.18.1/webapps/admin/,UNAVAILABLE}
    java.lang.ExceptionInInitializerError: null
            at org.eclipse.jetty.annotations.AnnotationConfiguration.createAnnotationParser(AnnotationConfiguration.java:502) ~[jetty-annotations-9.4.51.v20230217.jar:9.4.51.v20230217]
            at org.eclipse.jetty.annotations.AnnotationConfiguration.scanForAnnotations(AnnotationConfiguration.java:416) ~[jetty-annotations-9.4.51.v20230217.jar:9.4.51.v20230217]
            at org.eclipse.jetty.annotations.AnnotationConfiguration.configure(AnnotationConfiguration.java:346) ~[jetty-annotations-9.4.51.v20230217.jar:9.4.51.v20230217]
    

    This was fixed by removing the asm-1.0.2.jar and old json-smart.jar

    Though the broker starts. The biggest problem (for another post) is this error I now get presumably when receiving a message and trying to send it to AMQ:

    WARN | Cannot determine specific JmsMessage type to use from body class. Will use generic JmsMessage. Body class: com.azure.core.util.BinaryData. If you want to send a POJO then your class might need to implement java.io.Serializable, or you can force a specific type by setting the jmsMessageType option on the JMS endpoint.
    

    Which was fixed by adding <convertBodyTo type="java.lang.String"/> to the route like and turning into valid JSON;

    <route id="Elexon_IRIS_Route">
        <from uri="azureQueueEndpoint" />
        <convertBodyTo type="java.lang.String"/>
        <unmarshal><json/></unmarshal>
        <to uri="localAMQ:queue:IRIS-Elexon"/>
    </route>