0
votes

I am trying to send messages to a service bus queue through an APIM I have go through a couple of articles like the ones below with no luck https://connectedcircuits.blog/2018/09/25/exposing-azure-service-bus-through-apim-generating-a-sas-token-and-setting-the-session-id/ https://www.serverlessnotes.com/docs/load-article/docs/en/expose-service-bus-queue-through-api-management

The issue right now is that when I attempt to send the message, instead of the typical 201 created, I just receive a 200 OK and no message arrives to the queue. Notice that it works just fine going directly againts the Service Bus in question.

Below is the policy that I am currently using

<policies>
<inbound>
<set-header name="Authorization" exists-action="override">
<value>@{
// Load variables
string resourceUri = "xxxxx.servicebus.windows.net/apimqueue/messages";
string sasKeyName = "";
string sasKey = "";
// Set the token lifespan
System.TimeSpan sinceEpoch = System.DateTime.UtcNow.Subtract(new System.DateTime(1970, 1, 1));
var expiry = System.Convert.ToString((int)sinceEpoch.TotalSeconds + 60); //1 minute
string stringToSign = System.Uri.EscapeDataString(resourceUri) + "\n" + expiry;
System.Security.Cryptography.HMACSHA256 hmac = new System.Security.Cryptography.HMACSHA256(System.Text.Encoding.UTF8.GetBytes(sasKey));
var signature = System.Convert.ToBase64String(hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(stringToSign)));
// Format the sas token
var sasToken = String.Format("SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
System.Uri.EscapeDataString(resourceUri), System.Uri.EscapeDataString(signature), expiry, sasKeyName);
return sasToken;
}</value>
</set-header>
</inbound>
<backend>
<!-- base: Begin Product scope -->
<!-- base: Begin Global scope -->
<forward-request />
<!-- base: End Global scope -->
<!-- base: End Product scope -->
</backend>
<outbound />
<on-error />
</policies>

Response I get while doing a POST call againts https://apimtestbus.azure-api.net/azurebustest21243.servicebus.windows.net/apimqueue/messages

HTTP/1.1 200 OK

content-length: 0

date: Sun, 23 May 2021 06:02:25 GMT ocp-apim-apiid: servicebus ocp-apim-operationid: sendingmessages ocp-apim-subscriptionid: master ocp-apim-trace-location: https://apimstdebros05i5mtudswoa.blob.core.windows.net/apiinspectorcontainer/cHcJa9krxa3PhOTGYwgFgw2-37?sv=2019-07-07&sr=b&sig=uh6Y%2BVekeFU%2BZ5G1t8dXt6gVp6sQku5Mp25OTagJUts%3D&se=2021-05-24T06%3A02%3A26Z&sp=r&traceId=8097b06294bd49a0bc95cf1979ac7448 vary: Origin

Any help here would be appreciated

1
Have you seen any error on servicebus dashboard ?Thomas
No errors, in fact the request doesn't even appear to be reaching the service bus as I nothing the request/message dashboard when testing, so not sure why the APIM would be replying with a 200 OK if something is not going wellJdresc
I'm missing entries for rewrite-uri template and set-backend-service. If this is not the complete Policy, I guess the request was already read. Therefore context.Request.Body.As<string>(preserveContent: true); will helpMarkus Meyer
Thanks @MarkusMeyer by any chance do you have a sample policy that I can try ?Jdresc

1 Answers

0
votes

The issue was that I was confiruring a policy at the API level but another policy at the operation level was overriding it. After removing the operation level policy it worked.