Skip to content

Commit d5c1577

Browse files
committed
Azure AD Authentication for Service Bus API
1 parent 7bd8980 commit d5c1577

File tree

4 files changed

+127
-17
lines changed

4 files changed

+127
-17
lines changed

dotnet/src/dotnetcore/Providers/Messaging/GXAzureServiceBus/AzureServiceBus.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@
44
using System.Reflection;
55
using System.Runtime.Serialization;
66
using System.Threading.Tasks;
7+
using Azure.Identity;
78
using Azure.Messaging.ServiceBus;
89
using GeneXus.Messaging.Common;
910
using GeneXus.Services;
1011
using GeneXus.Utils;
12+
using log4net;
1113

1214
namespace GeneXus.Messaging.GXAzureServiceBus
1315
{
1416
public class AzureServiceBus : MessageBrokerBase, IMessageBroker
1517
{
1618
private const int MAX_MESSAGES_DEFAULT = 10;
1719
private const short LOCK_DURATION = 5;
18-
public static String Name = "AZURESB";
20+
public static string Name = "AZURESB";
21+
static readonly ILog logger = LogManager.GetLogger(typeof(AzureServiceBus));
1922

2023
private ConcurrentDictionary<string, Tuple<DateTime, ServiceBusReceivedMessage>> m_messages = new ConcurrentDictionary<string, Tuple<DateTime, ServiceBusReceivedMessage>>();
2124
ServiceBusClient _serviceBusClient { get; set; }
2225
private string _queueOrTopicName { get; set; }
2326
private string _connectionString { get; set; }
27+
private string _fullyqualifiedNamespace { get; set; }
2428
private string _subscriptionName { get; set; }
2529
private ServiceBusSender _sender { get; set; }
2630
private ServiceBusReceiver _receiver { get; set; }
@@ -41,6 +45,7 @@ private void Initialize(GXService providerService)
4145
_queueOrTopicName = serviceSettings.GetEncryptedPropertyValue(PropertyConstants.QUEUE_NAME);
4246
_connectionString = serviceSettings.GetEncryptedPropertyValue(PropertyConstants.QUEUE_CONNECTION_STRING);
4347
_subscriptionName = serviceSettings.GetEncryptedPropertyValue(PropertyConstants.TOPIC_SUBSCRIPTION);
48+
_fullyqualifiedNamespace = serviceSettings.GetEncryptedPropertyValue(PropertyConstants.FULLYQUALIFIEDNAMESPACE);
4449

4550
string sessionEnabled = serviceSettings.GetEncryptedOptPropertyValue(PropertyConstants.SESSION_ENABLED);
4651

@@ -58,7 +63,18 @@ private void Initialize(GXService providerService)
5863
//TO DO Consider connection options here
5964
//https://docs.microsoft.com/en-us/javascript/api/@azure/service-bus/servicebusclientoptions?view=azure-node-latest#@azure-service-bus-servicebusclientoptions-websocketoptions
6065

61-
_serviceBusClient = new ServiceBusClient(_connectionString);
66+
//First try authenticating using Azure Active Directory
67+
if (!string.IsNullOrEmpty(_fullyqualifiedNamespace))
68+
{
69+
_serviceBusClient = new ServiceBusClient(_fullyqualifiedNamespace, new DefaultAzureCredential());
70+
GXLogging.Debug(logger, "Authenticate to Azure Service Bus using Active Directory authentication.");
71+
}
72+
else
73+
{
74+
_serviceBusClient = new ServiceBusClient(_connectionString);
75+
GXLogging.Debug(logger, "Authenticate to Azure Service Bus using SAS authentication.");
76+
}
77+
6278
if (_serviceBusClient != null)
6379
{
6480
_sender = _serviceBusClient.CreateSender(_queueOrTopicName, serviceBusSenderOptions);

dotnet/src/dotnetcore/Providers/Messaging/GXAzureServiceBus/GXAzureServiceBus.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Azure.Identity" Version="1.9.0" />
1011
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.15.0" />
1112
</ItemGroup>
1213

dotnet/src/dotnetcore/Providers/Messaging/GXAzureServiceBus/ServiceBusMessageBrokerProvider.cs

Lines changed: 106 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,115 @@
55

66
namespace GeneXus.Messaging.GXAzureServiceBus
77
{
8+
/// <summary>
9+
/// Implementation of AzureServiceBus.MessageBrokerProvider external object.
10+
/// </summary>
811
public class ServiceBusMessageBrokerProvider
912
{
10-
public MessageQueue Connect(string queueName, string connectionString, out GXBaseCollection<SdtMessages_Message> errorMessages, out bool success)
13+
14+
#region Azure Active Directory Authentication
15+
16+
public MessageQueue Authenticate(string queueName, string fullyQualifiedNamespace, bool sessionEnabled, GxUserType receiverOptions, string senderIdentifier, out GXBaseCollection<SdtMessages_Message> errorMessages, out bool success)
1117
{
1218
MessageBrokerProvider messageBrokerProvider = new MessageBrokerProvider();
13-
GXProperties properties = new GXProperties();
14-
properties.Add(PropertyConstants.MESSAGEBROKER_AZURESB_QUEUENAME, queueName);
15-
properties.Add(PropertyConstants.MESSAGEBROKER_AZURESB_CONNECTIONSTRING, connectionString);
19+
ReceiverOptions options = TransformGXUserTypeToReceiverOptions(receiverOptions);
20+
21+
GXProperties properties = new GXProperties
22+
{
23+
{ PropertyConstants.MESSAGEBROKER_AZURESB_QUEUENAME, queueName },
24+
{ PropertyConstants.MESSAGEBROKER_AZURESB_FULLYQUALIFIEDNAMESPACE, fullyQualifiedNamespace },
25+
{ PropertyConstants.SESSION_ENABLED, sessionEnabled.ToString() },
26+
{ PropertyConstants.RECEIVE_MODE, options.ReceiveMode.ToString() },
27+
{ PropertyConstants.PREFETCH_COUNT, options.PrefetchCount.ToString() },
28+
{ PropertyConstants.RECEIVER_IDENTIFIER, options.Identifier },
29+
{ PropertyConstants.RECEIVER_SESSIONID, options.SessionId },
30+
{ PropertyConstants.SENDER_IDENTIFIER, senderIdentifier }
31+
};
1632

1733
MessageQueue messageQueue = messageBrokerProvider.Connect(PropertyConstants.AZURESERVICEBUS, properties, out GXBaseCollection<SdtMessages_Message> errorMessagesConnect, out bool successConnect);
1834
errorMessages = errorMessagesConnect;
1935
success = successConnect;
2036
return messageQueue;
2137
}
2238

23-
public MessageQueue Connect(string topicName, string subcriptionName, string connectionString, out GXBaseCollection<SdtMessages_Message> errorMessages, out bool success)
39+
public MessageQueue Authenticate(string topicName, string subcriptionName, string fullyQualifiedNamespace, bool sessionEnabled, GxUserType receiverOptions, string senderIdentifier, out GXBaseCollection<SdtMessages_Message> errorMessages, out bool success)
2440
{
2541
MessageBrokerProvider messageBrokerProvider = new MessageBrokerProvider();
2642
GXProperties properties = new GXProperties();
43+
ReceiverOptions options = TransformGXUserTypeToReceiverOptions(receiverOptions);
44+
2745
properties.Add(PropertyConstants.MESSAGEBROKER_AZURESB_QUEUENAME, topicName);
2846
properties.Add(PropertyConstants.MESSAGEBROKER_AZURESB_SUBSCRIPTION_NAME, subcriptionName);
29-
properties.Add(PropertyConstants.MESSAGEBROKER_AZURESB_CONNECTIONSTRING, connectionString);
47+
properties.Add(PropertyConstants.MESSAGEBROKER_AZURESB_FULLYQUALIFIEDNAMESPACE, fullyQualifiedNamespace);
48+
properties.Add(PropertyConstants.SESSION_ENABLED, sessionEnabled.ToString());
49+
properties.Add(PropertyConstants.RECEIVE_MODE, options.ReceiveMode.ToString());
50+
properties.Add(PropertyConstants.PREFETCH_COUNT, options.PrefetchCount.ToString());
51+
properties.Add(PropertyConstants.RECEIVER_IDENTIFIER, options.Identifier);
52+
properties.Add(PropertyConstants.RECEIVER_SESSIONID, options.SessionId);
53+
properties.Add(PropertyConstants.SENDER_IDENTIFIER, senderIdentifier);
54+
55+
MessageQueue messageQueue = messageBrokerProvider.Connect(PropertyConstants.AZURESERVICEBUS, properties, out GXBaseCollection<SdtMessages_Message> errorMessagesConnect, out bool successConnect);
56+
errorMessages = errorMessagesConnect;
57+
success = successConnect;
58+
return messageQueue;
59+
}
60+
61+
public MessageQueue authenticate(string queueName, string fullyQualifiedNamespace, out GXBaseCollection<SdtMessages_Message> errorMessages, out bool success)
62+
{
63+
MessageBrokerProvider messageBrokerProvider = new MessageBrokerProvider();
64+
GXProperties properties = new GXProperties
65+
{
66+
{ PropertyConstants.MESSAGEBROKER_AZURESB_QUEUENAME, queueName },
67+
{ PropertyConstants.MESSAGEBROKER_AZURESB_FULLYQUALIFIEDNAMESPACE, fullyQualifiedNamespace }
68+
};
69+
70+
MessageQueue messageQueue = messageBrokerProvider.Connect(PropertyConstants.AZURESERVICEBUS, properties, out GXBaseCollection<SdtMessages_Message> errorMessagesConnect, out bool successConnect);
71+
errorMessages = errorMessagesConnect;
72+
success = successConnect;
73+
return messageQueue;
74+
}
75+
public MessageQueue authenticate(string topicName, string subcriptionName, string fullyQualifiedNamespace, out GXBaseCollection<SdtMessages_Message> errorMessages, out bool success)
76+
{
77+
MessageBrokerProvider messageBrokerProvider = new MessageBrokerProvider();
78+
GXProperties properties = new GXProperties
79+
{
80+
{ PropertyConstants.MESSAGEBROKER_AZURESB_QUEUENAME, topicName },
81+
{ PropertyConstants.MESSAGEBROKER_AZURESB_SUBSCRIPTION_NAME, subcriptionName },
82+
{ PropertyConstants.MESSAGEBROKER_AZURESB_FULLYQUALIFIEDNAMESPACE, fullyQualifiedNamespace }
83+
};
84+
85+
MessageQueue messageQueue = messageBrokerProvider.Connect(PropertyConstants.AZURESERVICEBUS, properties, out GXBaseCollection<SdtMessages_Message> errorMessagesConnect, out bool successConnect);
86+
errorMessages = errorMessagesConnect;
87+
success = successConnect;
88+
return messageQueue;
89+
}
90+
#endregion
91+
92+
#region Connect using SAS (Shared Access Signatures)
93+
public MessageQueue Connect(string queueName, string connectionString, out GXBaseCollection<SdtMessages_Message> errorMessages, out bool success)
94+
{
95+
MessageBrokerProvider messageBrokerProvider = new MessageBrokerProvider();
96+
GXProperties properties = new GXProperties
97+
{
98+
{ PropertyConstants.MESSAGEBROKER_AZURESB_QUEUENAME, queueName },
99+
{ PropertyConstants.MESSAGEBROKER_AZURESB_CONNECTIONSTRING, connectionString }
100+
};
101+
102+
MessageQueue messageQueue = messageBrokerProvider.Connect(PropertyConstants.AZURESERVICEBUS, properties, out GXBaseCollection<SdtMessages_Message> errorMessagesConnect, out bool successConnect);
103+
errorMessages = errorMessagesConnect;
104+
success = successConnect;
105+
return messageQueue;
106+
}
107+
108+
public MessageQueue Connect(string topicName, string subcriptionName, string connectionString, out GXBaseCollection<SdtMessages_Message> errorMessages, out bool success)
109+
{
110+
MessageBrokerProvider messageBrokerProvider = new MessageBrokerProvider();
111+
GXProperties properties = new GXProperties
112+
{
113+
{ PropertyConstants.MESSAGEBROKER_AZURESB_QUEUENAME, topicName },
114+
{ PropertyConstants.MESSAGEBROKER_AZURESB_SUBSCRIPTION_NAME, subcriptionName },
115+
{ PropertyConstants.MESSAGEBROKER_AZURESB_CONNECTIONSTRING, connectionString }
116+
};
30117

31118
MessageQueue messageQueue = messageBrokerProvider.Connect(PropertyConstants.AZURESERVICEBUS, properties, out GXBaseCollection<SdtMessages_Message> errorMessagesConnect, out bool successConnect);
32119
errorMessages = errorMessagesConnect;
@@ -39,15 +126,17 @@ public MessageQueue Connect(string queueName, string connectionString, bool sess
39126
MessageBrokerProvider messageBrokerProvider = new MessageBrokerProvider();
40127
ReceiverOptions options = TransformGXUserTypeToReceiverOptions(receiverOptions);
41128

42-
GXProperties properties = new GXProperties();
43-
properties.Add(PropertyConstants.MESSAGEBROKER_AZURESB_QUEUENAME, queueName);
44-
properties.Add(PropertyConstants.MESSAGEBROKER_AZURESB_CONNECTIONSTRING, connectionString);
45-
properties.Add(PropertyConstants.SESSION_ENABLED, sessionEnabled.ToString());
46-
properties.Add(PropertyConstants.RECEIVE_MODE, options.ReceiveMode.ToString());
47-
properties.Add(PropertyConstants.PREFETCH_COUNT, options.PrefetchCount.ToString());
48-
properties.Add(PropertyConstants.RECEIVER_IDENTIFIER, options.Identifier);
49-
properties.Add(PropertyConstants.RECEIVER_SESSIONID, options.SessionId);
50-
properties.Add(PropertyConstants.SENDER_IDENTIFIER, senderIdentifier);
129+
GXProperties properties = new GXProperties
130+
{
131+
{ PropertyConstants.MESSAGEBROKER_AZURESB_QUEUENAME, queueName },
132+
{ PropertyConstants.MESSAGEBROKER_AZURESB_CONNECTIONSTRING, connectionString },
133+
{ PropertyConstants.SESSION_ENABLED, sessionEnabled.ToString() },
134+
{ PropertyConstants.RECEIVE_MODE, options.ReceiveMode.ToString() },
135+
{ PropertyConstants.PREFETCH_COUNT, options.PrefetchCount.ToString() },
136+
{ PropertyConstants.RECEIVER_IDENTIFIER, options.Identifier },
137+
{ PropertyConstants.RECEIVER_SESSIONID, options.SessionId },
138+
{ PropertyConstants.SENDER_IDENTIFIER, senderIdentifier }
139+
};
51140

52141
MessageQueue messageQueue = messageBrokerProvider.Connect(PropertyConstants.AZURESERVICEBUS, properties, out GXBaseCollection<SdtMessages_Message> errorMessagesConnect, out bool successConnect);
53142
errorMessages = errorMessagesConnect;
@@ -77,6 +166,8 @@ public MessageQueue Connect(string topicName, string subcriptionName, string con
77166
return messageQueue;
78167
}
79168

169+
#endregion
170+
80171
#region Transformation methods
81172
private ReceiverOptions TransformGXUserTypeToReceiverOptions(GxUserType options)
82173
{

dotnet/src/dotnetcore/Providers/Messaging/GXMessageBroker/PropertyConstants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ public static class PropertyConstants
1616
public const string MESSAGEBROKER_AZURESB_TOPICNAME = "MESSAGEBROKER_AZURESB_TOPICNAME";
1717
public const string MESSAGEBROKER_AZURESB_SUBSCRIPTION_NAME = "MESSAGEBROKER_AZURESB_SUBSCRIPTION";
1818
public const string MESSAGEBROKER_AZURESB_CONNECTIONSTRING = "MESSAGEBROKER_AZURESB_QUEUECONNECTION";
19+
public const string MESSAGEBROKER_AZURESB_FULLYQUALIFIEDNAMESPACE = "MESSAGEBROKER_AZURESB_FULLYQUALIFIEDNAMESPACE";
1920
public const string QUEUE_NAME = "QUEUENAME";
2021
public const string QUEUE_CONNECTION_STRING = "QUEUECONNECTION";
22+
public const string FULLYQUALIFIEDNAMESPACE = "FULLYQUALIFIEDNAMESPACE";
2123
public const string TOPIC_SUBSCRIPTION = "SUBSCRIPTION";
2224
public const string MESSAGE_BROKER = "MESSAGEBROKER";
2325
public const string SESSION_ENABLED = "SESSION_ENABLED";

0 commit comments

Comments
 (0)