Skip to content

Commit 703675d

Browse files
committed
Merge branch 'ssteiner-add_ClientCredentialAuthentication'
2 parents e5b7cab + 012a8d6 commit 703675d

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

src/NLog.Extensions.AzureBlobStorage/BlobStorageTarget.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ public sealed class BlobStorageTarget : AsyncTaskTarget
6868
/// </summary>
6969
public Layout AccessKey { get; set; }
7070

71+
/// <summary>
72+
/// Alternative to ConnectionString. Instantiates the <see cref="BlobServiceClient"/> using a <see cref="Azure.Identity.ClientSecretCredential"/> with this value as ClientId for authentication. Requires <see cref="TenantIdentity"/> and <see cref="ClientSecret"/> to be set.
73+
/// </summary>
74+
public Layout ClientId { get; set; }
75+
76+
/// <summary>
77+
/// Secret when using when using <see cref="ClientId"/>. Instantiates the <see cref="BlobServiceClient"/> using a <see cref="Azure.Identity.ClientSecretCredential"/> for authentication. Requires <see cref="TenantIdentity"/> and <see cref="ClientId"/> to be set.
78+
/// </summary>
79+
public Layout ClientSecret { get; set; }
80+
7181
[RequiredParameter]
7282
public Layout Container { get; set; }
7383

@@ -120,6 +130,8 @@ protected override void InitializeTarget()
120130
string sharedAccessSignature = string.Empty;
121131
string storageAccountName = string.Empty;
122132
string storageAccountAccessKey = string.Empty;
133+
string clientId = string.Empty;
134+
string clientSecret = string.Empty;
123135

124136
Dictionary<string, string> blobMetadata = null;
125137
Dictionary<string, string> blobTags = null;
@@ -138,6 +150,8 @@ protected override void InitializeTarget()
138150
sharedAccessSignature = SharedAccessSignature?.Render(defaultLogEvent);
139151
storageAccountName = AccountName?.Render(defaultLogEvent);
140152
storageAccountAccessKey = AccessKey?.Render(defaultLogEvent);
153+
clientId = ClientId?.Render(defaultLogEvent);
154+
clientSecret = ClientSecret?.Render(defaultLogEvent);
141155
}
142156

143157
if (BlobMetadata?.Count > 0)
@@ -169,7 +183,7 @@ protected override void InitializeTarget()
169183
}
170184
}
171185

172-
_cloudBlobService.Connect(connectionString, serviceUri, tenantIdentity, resourceIdentifier, clientIdentity, sharedAccessSignature, storageAccountName, storageAccountAccessKey, blobMetadata, blobTags);
186+
_cloudBlobService.Connect(connectionString, serviceUri, tenantIdentity, resourceIdentifier, clientIdentity, sharedAccessSignature, storageAccountName, storageAccountAccessKey, clientId, clientSecret, blobMetadata, blobTags);
173187
InternalLogger.Debug("AzureBlobStorageTarget(Name={0}): Initialized", Name);
174188
}
175189
catch (Exception ex)
@@ -376,7 +390,7 @@ class CloudBlobService : ICloudBlobService
376390
private AppendBlobClient _appendBlob;
377391
private BlobContainerClient _container;
378392

379-
public void Connect(string connectionString, string serviceUri, string tenantIdentity, string resourceIdentifier, string clientIdentity, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey, IDictionary<string, string> blobMetadata, IDictionary<string, string> blobTags)
393+
public void Connect(string connectionString, string serviceUri, string tenantIdentity, string resourceIdentifier, string clientIdentity, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey, string clientId, string clientSecret, IDictionary<string, string> blobMetadata, IDictionary<string, string> blobTags)
380394
{
381395
_blobMetadata = blobMetadata?.Count > 0 ? blobMetadata : null;
382396
_blobTags = blobTags?.Count > 0 ? blobTags : null;
@@ -393,6 +407,11 @@ public void Connect(string connectionString, string serviceUri, string tenantIde
393407
{
394408
_client = new BlobServiceClient(new Uri(serviceUri), new Azure.Storage.StorageSharedKeyCredential(storageAccountName, storageAccountAccessKey));
395409
}
410+
else if (!string.IsNullOrEmpty(clientId) && !string.IsNullOrEmpty(clientSecret) && !string.IsNullOrEmpty(tenantIdentity))
411+
{
412+
var tokenCredentials = new Azure.Identity.ClientSecretCredential(tenantIdentity, clientId, clientSecret);
413+
_client = new BlobServiceClient(new Uri(serviceUri), tokenCredentials);
414+
}
396415
else
397416
{
398417
var tokenCredentials = AzureCredentialHelpers.CreateTokenCredentials(clientIdentity, tenantIdentity, resourceIdentifier);

src/NLog.Extensions.AzureBlobStorage/ICloudBlobService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace NLog.Extensions.AzureStorage
99
{
1010
interface ICloudBlobService
1111
{
12-
void Connect(string connectionString, string serviceUri, string tenantIdentity, string resourceIdentifier, string clientIdentity, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey, IDictionary<string, string> blobMetadata, IDictionary<string, string> blobTags);
12+
void Connect(string connectionString, string serviceUri, string tenantIdentity, string resourceIdentifier, string clientIdentity, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey, string clientId, string clientSecret, IDictionary<string, string> blobMetadata, IDictionary<string, string> blobTags);
1313
Task AppendFromByteArrayAsync(string containerName, string blobName, string contentType, byte[] buffer, CancellationToken cancellationToken);
1414
}
1515
}

src/NLog.Extensions.AzureBlobStorage/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,18 @@ _clientIdentity_ - Alternative to ConnectionString. Used together with ServiceUr
4545

4646
_resourceIdentity_ - Alternative to ConnectionString. Used together with ServiceUri. Input for DefaultAzureCredential as ManagedIdentityResourceId.
4747

48-
_tenantIdentity_ - Alternative to ConnectionString. Used together with ServiceUri. Input for DefaultAzureCredential.
48+
_tenantIdentity_ - Alternative to ConnectionString. Used together with ServiceUri. Input for DefaultAzureCredential / ClientSecretCredential.
4949

5050
_sharedAccessSignature_ - Alternative to ConnectionString. Used together with ServiceUri. Input for AzureSasCredential
5151

5252
_accountName_ - Alternative to ConnectionString. Used together with ServiceUri. Input for StorageSharedKeyCredential-AccountName
5353

5454
_accessKey_ - Alternative to ConnectionString. Used together with ServiceUri. Input for StorageSharedKeyCredential-AccessKey
5555

56+
_clientId_ - Alternative to ConnectionString. Instantiates the `BlobServiceClient` using a `ClientSecretCredential` for authentication. Requires `TenantIdentity` and `ClientSecret`.
57+
58+
_clientSecret_ - Secret when using ClientId. Instantiates the `BlobServiceClient` using a `ClientSecretCredential` for authentication. Requires `TenantIdentity` and `ClientId`.
59+
5660
### Batching Policy
5761

5862
_batchSize_ - Number of EventData items to send in a single batch (Default=100)

test/NLog.Extensions.AzureBlobStorage.Tests/CloudBlobServiceMock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class CloudBlobServiceMock : ICloudBlobService
1717

1818
public IDictionary<string, string> BlobTags { get; private set; }
1919

20-
public void Connect(string connectionString, string serviceUri, string tenantIdentity, string resourceIdentifier, string clientIdentity, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey, IDictionary<string, string> blobMetadata, IDictionary<string, string> blobTags)
20+
public void Connect(string connectionString, string serviceUri, string tenantIdentity, string resourceIdentifier, string clientIdentity, string sharedAccessSignature, string storageAccountName, string storageAccountAccessKey, string clientId, string clientSecret, IDictionary<string, string> blobMetadata, IDictionary<string, string> blobTags)
2121
{
2222
ConnectionString = connectionString;
2323
BlobMetadata = blobMetadata;

0 commit comments

Comments
 (0)