[Resolved] Java - Azure Messaging Service Bus Error: Illegal connection string parameter: SharedAccessSignature sr

Ana Rodríguez González 6 Reputation points
2022-08-24T14:17:52.093+00:00

I am working with the Azure Messaging Service Bus Library version 7.10.0 on my project. I have created some integration tests to verify its functionality and with SaK authentication works like a charm, but with SaS I get the next error:

java.lang.IllegalArgumentException: Illegal connection string parameter name: SharedAccessSignature sr

IllegalArgumentException

Npxhs.png

/**  
 * Creates a new Synchronous Service Bus receiver.  
 * @param subscriptionName the name of the subscription to use.  
 * @return Service Bus receiver client for topics and subscriptions.  
 * @throws AuthorizationException if the token is invalid or has expired when the creation request is done.  
 * @throws ChannelException if the token is pointing to a non-existing, disabled, or invalid channel.  
 * @throws ConnectionException if the token is pointing to a non-existing or non-AMQP server. Also, if there are connectivity issues on client side.  
 * @throws InterruptedThreadException if the thread was interrupted while the client was being created.  
 * @throws InvalidTokenException if the token is malformed due to abnormal manipulations when the creation request is done.  
 * @throws QuotaExceededException if the server has reached the maximum limit of concurrent connections or operations.  
 * @throws RequestTimeoutException if the request timed out while was being processed.  
 * @throws ServerBusyException if the server is temporarily overloaded and is rejecting new requests.  
 * @throws UnexpectedDisconnectionException if the connection was closed abnormally while the client was being created.  
 * @throws MessageHubException if there were other problems creating the Service Bus receiver.  
 */  
@Override  
public ServiceBusReceiverClient createSbReceiver(String subscriptionName) throws MessageHubException {  
    try {  
        return new ServiceBusClientBuilder()  
                .connectionString(TokenHelper.getConnectionStringBuilder(authToken))  
                .receiver()  
                .topicName(authToken.getEntityPath())  
                .subscriptionName(subscriptionName)  
                .receiveMode(ServiceBusReceiveMode.PEEK_LOCK)  
                .buildClient();  
        } catch (Throwable exception) {  
        throw ExceptionHandler.generateClientCreationException(exception, "Error creating receiver client");  
    }  
}  

If we see the method TokenHelper.getConnectionStringBuilder():

/**  
 * Returns a String using a valid authentication token.  
 * @param authToken Source authentication token.  
 * @return String.  
 * @throws InvalidTokenException if the encoding of the authentication token is not supported  
 */  
public static String getConnectionStringBuilder (IAuthenticationProvider authToken) throws MessageHubException {  
    return authToken.getServiceBusToken();  
}  

And getServiceBusToken():

/**  
 * Returns Service Bus Sas token.  
 * @return Service Bus Sas token.  
 */  
@Override  
public String getServiceBusToken()  
{  
    String resourceUri = String.format("%s/%s",getEndpointAddress(),entityPath);  
    String sasToken = null;  
    try {  
        String stringToSign = URLEncoder.encode(resourceUri, "UTF-8") + "\n" + expirationDate;  
        String signature = getHMAC256(runtimeSignature, stringToSign);  
        sasToken = "SharedAccessSignature sr=" + URLEncoder.encode(resourceUri, "UTF-8") +"&sig=" +  
                signature + "&se=" + expirationDate + "&skn=" + policyName;  
    } catch (UnsupportedEncodingException e) {  
        e.printStackTrace();  
    }  
  
    return sasToken;  
}  
  
  
public static String getHMAC256(String key, String input) {  
    Mac sha256_HMAC;  
    String hash = null;  
    try {  
        sha256_HMAC = Mac.getInstance("HmacSHA256");  
        SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256");  
        sha256_HMAC.init(secret_key);  
        Base64.Encoder encoder = Base64.getEncoder();  
  
        hash = new String(encoder.encode(sha256_HMAC.doFinal(input.getBytes(StandardCharsets.UTF_8))));  
  
    } catch (InvalidKeyException | NoSuchAlgorithmException | IllegalStateException e) {  
        e.printStackTrace();  
    }  
  
    return hash;  
}  

And the Microsoft documentation that I followed to do the method:

0XUL3.png

I have tried different ways to build the SaS connection String to meet the requirements but I didn't get the right Connection String.

rqKTv.png

Could somebody give me some clue about what is wrong with the SaS Connection String? Thank you in advance!

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
700 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JananiRamesh-MSFT 29,261 Reputation points
    2022-09-06T07:15:03.23+00:00

    Hi @Ana Rodríguez González Thanks for reaching out. Connection string should contain both Endpoint name and the Shared access key.
    Please refer the sample format below,
    Endpoint=sb://<namesapce name>.servicebus.windows.net/;SharedAccessKey=<SAS token>;

    can you try creating the connection string as above and let me know if this works?

    return new ServiceBusClientBuilder()
    .credential(getEndpointAddress(), new AzureSasCredential(TokenHelper.getConnectionStringBuilder(authToken)))

    let me know incase of further queries, I would be happy to assist you.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.