How to receive messages from azure queue subscription using go - azure

I trying to pull messages from azure service bus queue using Go.Queue topic name,subscription name,service name and shared access key value are the credentials.I'm not getting proper sample code for this.Please help me!!

The Go Cloud Development Kit Pub/Sub API is still a work in progress (it's one of our newer APIs). As of 2019-01-30, there is a pull request out for review that adds support, so stay tuned!

from azure.servicebus.control_client
import ServiceBusService
bus_service=ServiceBusService(service_namespace='<namespace>',
shared_access_key_name='<key_name>',
shared_access_key_value='<acess_key>')
topic_name = "<topic_name>"
subscription_name = "<subscription_name>"
message = bus_service.receive_subscription_message(topic_name,
subscription_name, peek_lock=True)

Related

How to properly add health check for azure service bus when using managed identity (and rebus)

I have recently added managed identity support to Azure Service Bus and struggling how to properly add the health check. Prior to using managed identity, the health check was working fine, but now I am facing issues. The code to add the health check is the following:
builder.AddAzureServiceBusQueue(
"myservicebus.servicebus.windows.net",
"myqueue",
new DefaultAzureCredential(),
name: $"Azure Service Bus Health Check");
With this code, I am receiving the following error:
Put token failed. status-code: 404, status-description: The messaging entity 'sb://myservicebus.servicebus.windows.net/myqueue/$management' could not be found. To know more visit https://aka.ms/sbResourceMgrExceptions. TrackingId:6bd6fac5-3fbd-4889-a89b-a4854b8299f9_G13, SystemTracker:myservicegus.servicebus.windows.net:myqueue/$management, Timestamp:2022-12-23T02:21:08. (MessagingEntityNotFound). For troubleshooting information, see https://aka.ms/azsdk/net/servicebus/exceptions/troubleshoot.
I am not sure what the $management is all about or why it is needed. I thought perhaps the role associated with managed identity was missing a capability. To test that, I granted the role "Azure Service Bus Data Owner" and "Owner" to the resource, but still receiving the above error.
Also, not sure if it matters, but I am using Rebus to handle the publishing and subscribing of events (with managed identity) and that is working fine.
Any help is appreciated.
Thanks,
Eric
This is not really an answer, but here's some hopefully helpful information for anyone running into a similar issue.
The main mistake I had made was specifiying an incorrect queue name. It seems that when trying to perform the operation that health check is performing, something like:
var receiver = client.CreateReceiver("the_invalid_queue");
_ = await receiver.PeekMessageAsync().ConfigureAwait(false);
And when the queue name specified does not exist, we get the error as described in the question, mentioning the missing entity.
So just need to make sure to specify an existing queue name and all should work OK.
Eric

Authorization failed using Azure Service Bus API to manage queues

I'm trying to use the Azure Service Bus api to purge messages from certain queues.
According to the documentation (https://learn.microsoft.com/en-us/rest/api/storageservices/clear-messages) I would do this using the following request:
https://myaccount.queue.core.windows.net/myqueue/messages
This seems simple enough, but the problem is I just cannot get this request to authorize correctly.
(result = 401 Unauthorized)
Apparently the signing process is rather elaborate and described here (https://learn.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key)
I had many attempts, like the following code:
var httpClient = new HttpClient();
using (HMACSHA256 hmac = new HMACSHA256())
{
var StringToSign = $"GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:{DateTime.UtcNow.ToString("o")}\nx-ms-version:2015-02-21\n\n\n\n";
var stringToSignHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(StringToSign));
var base64StringToSign = Convert.ToBase64String(stringToSignHash);
var signature = $"{base64StringToSign}, {MySecretKey}";
using (HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Delete, "https://myaccount.servicebus.windows.net/myqueue/messages"))
{
//requestMessage.Headers.Add("x-ms-date", DateTime.UtcNow.ToString());
requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("SharedKey", signature);
var result = httpClient.SendAsync(requestMessage).GetAwaiter().GetResult();
}
}
For MySecretKey I used my shared access policy key. I tried using as is, and also decoded base64 to ascii.
Does anyone have more success with this?
Are there more simple ways to access the api?
Thanks.
You can leverage the code here in different languages to generate the SAS token.
Updated:
Can you please confirm which API you are trying to execute? As per the code, you are calling the Delete method on myaccount.servicebus.windows.net (i.e. service bus resource). Delete API operation completes the processing of a locked message and deletes it from the queue or subscription. The samples/reference article that you have shared is for the delete messages from the storage queue. If your requirement is to purge all messages from the service bus queue then you need to use Recieve and Delete API. Alternative you can also use service bus explorer to purge the messages.
As an alternative you can use Service Bus Cloud Explorer which runs in the browser with logged-in user Azure account by default and have a purge functionality.
Keep in mind that it will take time to clear up queues or subscriptions. More messages it needs to purge; the more time it would take.

The Azure Alerts using webhook to Slack is not working

I'm using Azure Alerts to use webhooks. I added the webhook in the action group. The URL I used is the one from Slack.
I also added a Notification Type Email. The notification email gets triggered but I am not receiving any message in the Slack channel.
I was able to push messages to the webhooks using postman hence I know my webhook is correct. But Azure Alert is not able to trigger the webhook. I am not receiving any messages in the Slack channel.
Azure Monitor sends a notification with its own schema definition. Slack doesn't recognize it, so you cannot see any notifications in the channel. It is possible to create a Logic App or Azure Function to transform the default schema.
Here the more information about it:
https://learn.microsoft.com/en-us/azure/azure-monitor/alerts/alerts-common-schema
Slack expect a json object when using webhooks. When sending directly from azure alert you will get bad request.
Every slack channel has an e-mail adress. You can post messages to the channel using that email in azure alerts
Anyone looking for using Slack and Azure Alerts. This seems to work fine:
...
webhook_receiver {
name = "Slack"
service_uri = "https://hooks.slack.com/services/secret"
use_common_alert_schema = false
}
Then in the scheduled query rules alert:
...
action {
action_group = [data.azurerm_monitor_action_group.actiongroup.id]
email_subject = "Disk storage at less than 10%"
custom_webhook_payload = "{ \"text\" : \"Warning: Storage has less than 10% diskspace remaining!\"}"
}

How to publish Event Hub events to Event Grid Topic with Azure SDK?

I would like to set an Event Hub to publish events to an Event Grid Topic using Azure SDK.
This can be done in Azure Portal straight from the Event Hub Namespace, creating an Event Grid System Topic.
However, I can't seem to find a proper way using Azure SDK to either create an Event Grid System Topic, or create an Event Grid custom Topic and setting it as an endpoint for the Event Hub Namespace.
Any ideas?
The documentation is available, perhaps a bit scattered. If you navigate to the Event Grid documentation site, the Reference node in the menu tree has all the supported languages/SDKs. For .NET, it will bring you to the article on how to publish and subscribe (link). To manage topics, you would need the management SDK and the samples are available here.
Note that the samples are on the slightly outdated version of the management SDK (related issue) but you should be able to up the versions and use the samples.
Eventually used REST API to make HTTP request for creating an Event Grid System Topic.
Documentation for the API request:
https://learn.microsoft.com/en-us/rest/api/eventgrid/version2020-04-01-preview/systemtopics/createorupdate
Phew !! This issue screwed my mind for 3 days & I had almost lost hope but finally it worked.
Reference:
https://learn.microsoft.com/en-us/java/api/overview/azure/eventgrid?view=azure-java-stable
Code
final String clientId = "clientId";
final String tenantId = "tenantId";
final String clientSecret = "clientSecret";
final String subscriptionId = "subscriptionId";
ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(clientId, tenantId, clientSecret,
AzureEnvironment.AZURE);
credentials.withDefaultSubscriptionId(subscriptionId);
EventGridManager eventGridManager = EventGridManager.configure().authenticate(credentials,
credentials.defaultSubscriptionId());
eventGridManager.eventSubscriptions().define("subscription")
.withScope("/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/microsoft.storage/storageaccounts/{storageAccountName}")
.withEventDeliverySchema(EventDeliverySchema.EVENT_GRID_SCHEMA)
.withDestination(new WebHookEventSubscriptionDestination().withEndpointUrl("{valid https url}"))
.create();
This creates a system topic with a subscription

Building SOAP Listener with Azure Functions

I am using Azure Functions to build some integrations between various systems. I new requirement is to respond to record updates in Salesforce. Some quick research yielded what seems like a good solution from the Salesforce side. Use Outbound messaging which can send SOAP requests on record modifications.
How to create Salesforce application that will send record to external web service when record created/changed(https://salesforce.stackexchange.com/questions/73425/how-to-create-salesforce-application-that-will-send-record-to-external-web-servi)
The challenge now is to be able create a SOAP listener in Azure Function. I have created basic HTTP Triggers for my other listeners. Is there anything "built-in" to Azure Functions that would allow me to easily consume the incoming SOAP request?
Salesforce has the basics for a solution based on a more traditional web service and an ASMX file but I am not sure if or how that can be applied in Azure Functions. (https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_om_outboundmessaging_listener.htm)
That notification is just a SOAP request that is made over HTTP, so really not too different than a regular HTTP trigger request.
Although you could just treat that as a plain request and parse the contents yourself, Azure Functions does expose the great WebHook support we get from ASP.NET WebHooks, and luckily, there is a Salesforce receiver that significantly simplifies this task.
DISCLAIMER: It's worth noting that although the receiver is technically enabled in Azure Functions, there's no official support for it yet, so you won't find a lot of documentation and help will be limited to what you get on SO and Forums. Official support to this and other receivers will hopefully be coming soon, which means documentation, templates and UI support will become available.
To get started, you need the following:
Create a new function, selecting the GenericWebHook - CSharp template (this works for node as well, but I'll focus on C# here.
Follow the steps outlined on the ASP.NET WebHooks integration with Salesforce post in order to create the outbound message. Here you want to use the Function Url given to you by the portal WITHOUT THE CODE QUERY STRING (having the code there wouldn't hurt, but the receiver does not use that information).
IMPORTANT: Get your Salesforce Organization ID, which will be used for authentication and is located under Administer > Company Profile > Company Information > Salesforce.com Organization ID and back in the Azure Functions portal, open the Keys panel, delete your default function key (not host key) and create a new key, named default (this name is important) using the Organization ID value you got from Salesforce.
Go to Integrate
On the integration page, select Advanced Editor on the upper right (as mentioned, there's no official support, so the UI does not expose this. We're putting our explorer hats on and venturing into a more advanced workflow here :) )
Change the webHookType property value to sfsoap and save the configuration. Your function.json config should look like the following:
function.json:
{
"bindings": [
{
"type": "httpTrigger",
"direction": "in",
"webHookType": "sfsoap",
"name": "req"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
],
"disabled": false
}
Switch to the Develop tab. We're ready to write our code.
This is where the ASP.NET WebHooks receiver shines! It will parse the notification for you, exposing strong typed objects you can work with. All you need to do is modify the method/function signature you get withe template to use the SalesforceNotifications type, making sure you're referencing the required assembly (Microsoft.AspNet.WebHooks.Receivers.Salesforce, which is made available to you, so no need for package reference) and namespace reference (Microsoft.AspNet.WebHooks).
Here is a full sample of a function that will receive the request and log the Organization ID, Action ID, grab the first notification and log all of its properties:
#r "Microsoft.AspNet.WebHooks.Receivers.Salesforce"
using Microsoft.AspNet.WebHooks;
public static void Run(SalesforceNotifications req, TraceWriter log)
{
log.Info($"Salesforce webhook was triggered!");
log.Info(req.OrganizationId);
log.Info(req.ActionId);
var notification = req.Notifications.First();
foreach (var p in notification.Keys)
{
log.Info($"{p} = {notification[p]}");
}
}
This process will be a lot smoother when the receiver is officially supported, but even with the added steps, this still beats having to parse the SOAP messages yourself :)
I hope this helps!

Resources