Skip to content

Commit 352dc44

Browse files
ssteinerStephan Steiner
andauthored
implemented ClientId authentication for all remaining targets (#173)
Co-authored-by: Stephan Steiner <[email protected]>
1 parent 8e31370 commit 352dc44

File tree

20 files changed

+137
-25
lines changed

20 files changed

+137
-25
lines changed

src/NLog.Extensions.AzureDataTables/DataTablesTarget.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ public override int GetHashCode()
134134
/// </summary>
135135
public Layout AccessKey { get; set; }
136136

137+
/// <summary>
138+
/// clientId for <see cref="Azure.Identity.ClientSecretCredential"/> authentication. Requires <see cref="ServiceUri"/>, <see cref="TenantIdentity"/> and <see cref="ClientAuthSecret"/>.
139+
/// </summary>
140+
public Layout ClientAuthId { get; set; }
141+
142+
/// <summary>
143+
/// clientSecret for <see cref="Azure.Identity.ClientSecretCredential"/> authentication. Requires <see cref="ServiceUri"/>, <see cref="TenantIdentity"/> and <see cref="ClientAuthId"/>.
144+
/// </summary>
145+
public Layout ClientAuthSecret { get; set; }
146+
137147
/// <summary>
138148
/// Gets or sets the name of the Azure table where log entries will be stored.
139149
/// </summary>
@@ -196,6 +206,8 @@ protected override void InitializeTarget()
196206
string sharedAccessSignature = string.Empty;
197207
string accountName = string.Empty;
198208
string accessKey = string.Empty;
209+
string clientAuthId = string.Empty;
210+
string clientAuthSecret = string.Empty;
199211

200212
var defaultLogEvent = LogEventInfo.CreateNullEvent();
201213

@@ -211,9 +223,11 @@ protected override void InitializeTarget()
211223
sharedAccessSignature = SharedAccessSignature?.Render(defaultLogEvent);
212224
accountName = AccountName?.Render(defaultLogEvent);
213225
accessKey = AccessKey?.Render(defaultLogEvent);
226+
clientAuthId = ClientAuthId?.Render(defaultLogEvent);
227+
clientAuthSecret = ClientAuthSecret?.Render(defaultLogEvent);
214228
}
215229

216-
_cloudTableService.Connect(connectionString, serviceUri, tenantIdentity, managedIdentityResourceId, managedIdentityClientId, sharedAccessSignature, accountName, accessKey);
230+
_cloudTableService.Connect(connectionString, serviceUri, tenantIdentity, managedIdentityResourceId, managedIdentityClientId, sharedAccessSignature, accountName, accessKey, clientAuthId, clientAuthSecret);
217231
InternalLogger.Debug("AzureDataTablesTarget(Name={0}): Initialized", Name);
218232
}
219233
catch (Exception ex)
@@ -438,7 +452,7 @@ class CloudTableService : ICloudTableService
438452
private TableServiceClient _client;
439453
private TableClient _table;
440454

441-
public void Connect(string connectionString, string serviceUri, string tenantIdentity, string managedIdentityResourceId, string managedIdentityClientId, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey)
455+
public void Connect(string connectionString, string serviceUri, string tenantIdentity, string managedIdentityResourceId, string managedIdentityClientId, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey, string clientAuthId, string clientAuthSecret)
442456
{
443457
if (string.IsNullOrWhiteSpace(serviceUri))
444458
{
@@ -452,6 +466,11 @@ public void Connect(string connectionString, string serviceUri, string tenantIde
452466
{
453467
_client = new TableServiceClient(new Uri(serviceUri), new TableSharedKeyCredential(storageAccountName, storageAccountAccessKey));
454468
}
469+
else if (!string.IsNullOrEmpty(clientAuthId) && !string.IsNullOrEmpty(clientAuthSecret) && !string.IsNullOrEmpty(tenantIdentity))
470+
{
471+
var tokenCredentials = new Azure.Identity.ClientSecretCredential(tenantIdentity, clientAuthId, clientAuthSecret);
472+
_client = new TableServiceClient(new Uri(serviceUri), tokenCredentials);
473+
}
455474
else
456475
{
457476
var tokenCredentials = AzureCredentialHelpers.CreateTokenCredentials(managedIdentityClientId, tenantIdentity, managedIdentityResourceId);

src/NLog.Extensions.AzureDataTables/ICloudTableService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace NLog.Extensions.AzureStorage
77
{
88
interface ICloudTableService
99
{
10-
void Connect(string connectionString, string serviceUri, string tenantIdentity, string managedIdentityResourceId, string managedIdentityClientId, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey);
10+
void Connect(string connectionString, string serviceUri, string tenantIdentity, string managedIdentityResourceId, string managedIdentityClientId, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey, string clientAuthId, string clientAuthSecret);
1111
Task SubmitTransactionAsync(string tableName, IEnumerable<TableTransactionAction> tableTransaction, CancellationToken cancellationToken);
1212
}
1313
}

src/NLog.Extensions.AzureDataTables/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ _accountName_ - accountName for `TableSharedKeyCredential` authentication. Requi
4444

4545
_accessKey_ - accountKey for `TableSharedKeyCredential` authentication. Requires `serviceUri` and `accountName`.
4646

47+
_clientAuthId_ - clientId for `ClientSecretCredential` authentication. Requires `serviceUri`, `tenantIdentity` and `clientAuthSecret`.
48+
49+
_clientAuthSecret_ - clientSecret for `ClientSecretCredential` authentication. Requires `serviceUri`,`tenantIdentity` and `clientAuthId`.
50+
4751
_tableName_ - Azure table name. [Layout](https://github.com/NLog/NLog/wiki/Layouts)
4852

4953
_rowKey_ - Azure Table RowKey. [Layout](https://github.com/NLog/NLog/wiki/Layouts). Default = "InverseTicks_${guid}"

src/NLog.Extensions.AzureEventGrid/EventGridTarget.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ public string DataFormat
119119
/// </summary>
120120
public Layout SharedAccessSignature { get; set; }
121121

122+
/// <summary>
123+
/// clientId for <see cref="Azure.Identity.ClientSecretCredential"/> authentication. Requires <see cref="TenantIdentity"/> and <see cref="ClientAuthSecret"/>.
124+
/// </summary>
125+
public Layout ClientAuthId { get; set; }
126+
127+
/// <summary>
128+
/// clientSecret for <see cref="Azure.Identity.ClientSecretCredential"/> authentication. Requires <see cref="TenantIdentity"/> and <see cref="ClientAuthId"/>.
129+
/// </summary>
130+
public Layout ClientAuthSecret { get; set; }
131+
122132
/// <summary>
123133
/// Gets a list of message properties aka. custom CloudEvent Extension Attributes
124134
/// </summary>
@@ -166,6 +176,8 @@ protected override void InitializeTarget()
166176
string managedIdentityClientId = string.Empty;
167177
string sharedAccessSignature = string.Empty;
168178
string accessKey = string.Empty;
179+
string clientAuthId = string.Empty;
180+
string clientAuthSecret = string.Empty;
169181

170182
var defaultLogEvent = LogEventInfo.CreateNullEvent();
171183

@@ -177,8 +189,10 @@ protected override void InitializeTarget()
177189
managedIdentityClientId = ManagedIdentityClientId?.Render(defaultLogEvent);
178190
sharedAccessSignature = SharedAccessSignature?.Render(defaultLogEvent);
179191
accessKey = AccessKey?.Render(defaultLogEvent);
192+
clientAuthId = ClientAuthId?.Render(defaultLogEvent);
193+
clientAuthSecret = ClientAuthSecret?.Render(defaultLogEvent);
180194

181-
_eventGridService.Connect(topic, tenantIdentity, managedIdentityResourceId, managedIdentityClientId, sharedAccessSignature, accessKey);
195+
_eventGridService.Connect(topic, tenantIdentity, managedIdentityResourceId, managedIdentityClientId, sharedAccessSignature, accessKey, clientAuthId, clientAuthSecret);
182196
InternalLogger.Debug("AzureEventGridTarget(Name={0}): Initialized", Name);
183197
}
184198
catch (Exception ex)
@@ -221,7 +235,7 @@ private sealed class EventGridService : IEventGridService
221235

222236
public string Topic { get; private set; }
223237

224-
public void Connect(string topic, string tenantIdentity, string managedIdentityResourceId, string managedIdentityClientId, string sharedAccessSignature, string accessKey)
238+
public void Connect(string topic, string tenantIdentity, string managedIdentityResourceId, string managedIdentityClientId, string sharedAccessSignature, string accessKey, string clientAuthId, string clientAuthSecret)
225239
{
226240
Topic = topic;
227241

@@ -233,6 +247,11 @@ public void Connect(string topic, string tenantIdentity, string managedIdentityR
233247
{
234248
_client = new EventGridPublisherClient(new Uri(topic), new AzureKeyCredential(accessKey));
235249
}
250+
else if (!string.IsNullOrEmpty(clientAuthId) && !string.IsNullOrEmpty(clientAuthSecret) && !string.IsNullOrEmpty(tenantIdentity))
251+
{
252+
var tokenCredentials = new Azure.Identity.ClientSecretCredential(tenantIdentity, clientAuthId, clientAuthSecret);
253+
_client = new EventGridPublisherClient(new Uri(topic), tokenCredentials);
254+
}
236255
else
237256
{
238257
var tokenCredentials = AzureCredentialHelpers.CreateTokenCredentials(managedIdentityClientId, tenantIdentity, managedIdentityResourceId);

src/NLog.Extensions.AzureEventGrid/IEventGridService.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using Azure.Messaging;
42
using Azure.Messaging.EventGrid;
5-
using System.Threading.Tasks;
6-
using Azure.Messaging;
73
using System.Threading;
4+
using System.Threading.Tasks;
85

96
namespace NLog.Extensions.AzureStorage
107
{
118
internal interface IEventGridService
129
{
1310
string Topic { get; }
1411

15-
void Connect(string topic, string tenantIdentity, string managedIdentityResourceId, string managedIdentityClientId, string sharedAccessSignature, string accessKey);
12+
void Connect(string topic, string tenantIdentity, string managedIdentityResourceId, string managedIdentityClientId, string sharedAccessSignature, string accessKey, string clientAuthId, string clientAuthSecret);
1613

1714
Task SendEventAsync(EventGridEvent gridEvent, CancellationToken cancellationToken);
1815

src/NLog.Extensions.AzureEventGrid/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ _sharedAccessSignature_ - Access signature for `AzureSasCredential` authenticati
5858

5959
_accessKey_ - Key for `AzureKeyCredential` authentication. Requires `serviceUri`.
6060

61+
_clientAuthId_ - clientId for `ClientSecretCredential` authentication. Requires `tenantIdentity` and `clientAuthSecret`.
62+
63+
_clientAuthSecret_ - clientSecret for `ClientSecretCredential` authentication. Requires `tenantIdentity` and `clientAuthId`.
64+
6165
### Retry Policy
6266

6367
_taskTimeoutSeconds_ - How many seconds a Task is allowed to run before it is cancelled (Default 150 secs)

src/NLog.Extensions.AzureEventHub/EventHubTarget.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,16 @@ public class EventHubTarget : AsyncTaskTarget
143143
/// </summary>
144144
public Layout AccessKey { get; set; }
145145

146+
/// <summary>
147+
/// clientId for <see cref="Azure.Identity.ClientSecretCredential"/> authentication. Requires <see cref="ServiceUri"/>, <see cref="TenantIdentity"/> and <see cref="ClientAuthSecret"/>.
148+
/// </summary>
149+
public Layout ClientAuthId { get; set; }
150+
151+
/// <summary>
152+
/// clientSecret for <see cref="Azure.Identity.ClientSecretCredential"/> authentication. Requires <see cref="ServiceUri"/>, <see cref="TenantIdentity"/> and <see cref="ClientAuthId"/>.
153+
/// </summary>
154+
public Layout ClientAuthSecret { get; set; }
155+
146156
/// <summary>
147157
/// The connection uses the AMQP protocol over web sockets. See also <see cref="EventHubsTransportType.AmqpWebSockets"/>
148158
/// </summary>
@@ -209,6 +219,8 @@ protected override void InitializeTarget()
209219
string sharedAccessSignature = string.Empty;
210220
string storageAccountName = string.Empty;
211221
string storageAccountAccessKey = string.Empty;
222+
string clientAuthId = string.Empty;
223+
string clientAuthSecret = string.Empty;
212224
string eventHubName = string.Empty;
213225
string useWebSockets = string.Empty;
214226
string webSocketProxyAddress = string.Empty;
@@ -229,6 +241,8 @@ protected override void InitializeTarget()
229241
sharedAccessSignature = SharedAccessSignature?.Render(defaultLogEvent);
230242
storageAccountName = AccountName?.Render(defaultLogEvent);
231243
storageAccountAccessKey = AccessKey?.Render(defaultLogEvent);
244+
clientAuthId = ClientAuthId?.Render(defaultLogEvent);
245+
clientAuthSecret = ClientAuthSecret?.Render(defaultLogEvent);
232246
}
233247

234248
useWebSockets = UseWebSockets?.Render(defaultLogEvent) ?? string.Empty;
@@ -239,7 +253,7 @@ protected override void InitializeTarget()
239253
customEndPointAddress = CustomEndpointAddress?.Render(defaultLogEvent) ?? string.Empty;
240254
webSocketProxyAddress = WebSocketProxyAddress?.Render(defaultLogEvent) ?? string.Empty;
241255

242-
_eventHubService.Connect(connectionString, eventHubName, serviceUri, tenantIdentity, managedIdentityResourceId, managedIdentityClientId, sharedAccessSignature, storageAccountName, storageAccountAccessKey, bool.TrueString == useWebSockets, webSocketProxyAddress, customEndPointAddress);
256+
_eventHubService.Connect(connectionString, eventHubName, serviceUri, tenantIdentity, managedIdentityResourceId, managedIdentityClientId, sharedAccessSignature, storageAccountName, storageAccountAccessKey, clientAuthId, clientAuthSecret, bool.TrueString == useWebSockets, webSocketProxyAddress, customEndPointAddress);
243257
InternalLogger.Debug("AzureEventHubTarget(Name={0}): Initialized", Name);
244258
}
245259
catch (Exception ex)
@@ -542,7 +556,7 @@ private sealed class EventHubService : IEventHubService
542556

543557
public string EventHubName { get; private set; }
544558

545-
public void Connect(string connectionString, string eventHubName, string serviceUri, string tenantIdentity, string managedIdentityResourceId, string managedIdentityClientId, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey, bool useWebSockets, string webSocketsProxyAddress, string endPointAddress)
559+
public void Connect(string connectionString, string eventHubName, string serviceUri, string tenantIdentity, string managedIdentityResourceId, string managedIdentityClientId, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey, string clientAuthId, string clientAuthSecret, bool useWebSockets, string webSocketsProxyAddress, string endPointAddress)
546560
{
547561
EventHubName = eventHubName;
548562

@@ -574,6 +588,11 @@ public void Connect(string connectionString, string eventHubName, string service
574588
{
575589
_client = new Azure.Messaging.EventHubs.Producer.EventHubProducerClient(serviceUri, eventHubName, new Azure.AzureNamedKeyCredential(storageAccountName, storageAccountAccessKey), options);
576590
}
591+
else if (!string.IsNullOrEmpty(clientAuthId) && !string.IsNullOrEmpty(clientAuthSecret) && !string.IsNullOrEmpty(tenantIdentity))
592+
{
593+
var tokenCredentials = new Azure.Identity.ClientSecretCredential(tenantIdentity, clientAuthId, clientAuthSecret);
594+
_client = new Azure.Messaging.EventHubs.Producer.EventHubProducerClient(serviceUri, eventHubName, tokenCredentials);
595+
}
577596
else
578597
{
579598
var tokenCredentials = AzureCredentialHelpers.CreateTokenCredentials(managedIdentityClientId, tenantIdentity, managedIdentityResourceId);

src/NLog.Extensions.AzureEventHub/IEventHubService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace NLog.Extensions.AzureStorage
88
internal interface IEventHubService
99
{
1010
string EventHubName { get; }
11-
void Connect(string connectionString, string eventHubName, string serviceUri, string tenantIdentity, string managedIdentityResourceId, string managedIdentityClientId, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey, bool useWebSockets, string webSocketsProxyAddress, string endPointAddress);
11+
void Connect(string connectionString, string eventHubName, string serviceUri, string tenantIdentity, string managedIdentityResourceId, string managedIdentityClientId, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey, string clientAuthId, string clientAuthSecret, bool useWebSockets, string webSocketsProxyAddress, string endPointAddress);
1212
Task CloseAsync();
1313
Task SendAsync(IEnumerable<EventData> eventDataBatch, string partitionKey, CancellationToken cancellationToken);
1414
}

src/NLog.Extensions.AzureEventHub/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ _accountName_ - accountName for `AzureNamedKeyCredential` authentication. Requir
7272

7373
_accessKey_ - accountKey for `AzureNamedKeyCredential` authentication. Requires `serviceUri` and `accountName`.
7474

75+
_clientAuthId_ - clientId for `ClientSecretCredential` authentication. Requires `serviceUri`, `tenantIdentity` and `clientAuthSecret`.
76+
77+
_clientAuthSecret_ - clientSecret for `ClientSecretCredential` authentication. Requires `serviceUri`,`tenantIdentity` and `clientAuthId`.
78+
7579
### Batching Policy
7680

7781
_maxBatchSizeBytes_ - Max size of a single batch in bytes [Integer](https://github.com/NLog/NLog/wiki/Data-types) (Default=1024*1024)

src/NLog.Extensions.AzureQueueStorage/ICloudQueueService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace NLog.Extensions.AzureStorage
77
{
88
internal interface ICloudQueueService
99
{
10-
void Connect(string connectionString, string serviceUri, string tenantIdentity, string managedIdentityResourceId, string managedIdentityClientId, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey, TimeSpan? timeToLive, IDictionary<string, string> queueMetadata);
10+
void Connect(string connectionString, string serviceUri, string tenantIdentity, string managedIdentityResourceId, string managedIdentityClientId, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey, string clientAuthId, string clientAuthSecret, TimeSpan? timeToLive, IDictionary<string, string> queueMetadata);
1111
Task AddMessageAsync(string queueName, string queueMessage, CancellationToken cancellationToken);
1212
}
1313
}

0 commit comments

Comments
 (0)