-
-
Notifications
You must be signed in to change notification settings - Fork 323
/
AzureBlobDataProvider.cs
346 lines (312 loc) · 14.2 KB
/
AzureBlobDataProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using Audit.AzureTableStorage.ConfigurationApi;
using Audit.Core;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Audit.AzureTableStorage.Providers
{
public class AzureBlobDataProvider : AuditDataProvider
{
/// <summary>
/// Value indicating whether to use Azure Active Directory for the connections to the storage service.
/// </summary>
public bool UseActiveDirectory { get; set; }
/// <summary>
/// The resource URL when using Azure AD. Default is "https://storage.azure.com/".
/// </summary>
public string ResourceUrl { get; set; } = "https://storage.azure.com/";
/// <summary>
/// The Tenant ID (Directory ID) to use for Azure AD
/// </summary>
public Func<AuditEvent, string> TenantIdBuilder { get; set; }
/// <summary>
/// The storage Account Name to use for Azure AD
/// </summary>
public Func<AuditEvent, string> AccountNameBuilder { get; set; }
/// <summary>
/// The DNS endpoint suffix to use when connecting to azure storage services. Default is "core.windows.net".
/// </summary>
public string EndpointSuffix { get; set; } = "core.windows.net";
/// <summary>
/// Value indicating whether to use HTTPS for Azure AD
/// </summary>
public bool UseHttps { get; set; } = true;
private static readonly IDictionary<string, CloudBlobContainer> ContainerCache = new ConcurrentDictionary<string, CloudBlobContainer>();
/// <summary>
/// Gets or sets a function that returns a unique name for the blob (can contain folders).
/// </summary>
public Func<AuditEvent, string> BlobNameBuilder { get; set; }
/// <summary>
/// Gets or sets a function that returns the standard blob tier to set on the blob after uploading (or null to use the default).
/// </summary>
public Func<AuditEvent, StandardBlobTier?> AccessTierBuilder { get; set; }
/// <summary>
/// Gets or sets a function that returns the metadata key/values to store on the blob.
/// </summary>
public Func<AuditEvent, IDictionary<string, string>> MetadataBuilder { get; set; }
/// <summary>
/// Gets or sets a function that returns a container name for the event.
/// </summary>
public Func<AuditEvent, string> ContainerNameBuilder { get; set; }
/// <summary>
/// Gets or sets the container name to use.
/// </summary>
public string ContainerName { set { ContainerNameBuilder = _ => value; } }
/// <summary>
/// Gets or sets the Azure Storage connection string
/// </summary>
public Func<AuditEvent, string> ConnectionStringBuilder { get; set; }
/// <summary>
/// Sets the Azure Storage connection string
/// </summary>
public string ConnectionString { set { ConnectionStringBuilder = _ => value; } }
public AzureBlobDataProvider()
{
}
public AzureBlobDataProvider(Action<IAzureBlobProviderConfigurator> config)
{
var azConfig = new AzureBlobProviderConfigurator();
if (config != null)
{
config.Invoke(azConfig);
UseActiveDirectory = azConfig._eventConfig._useActiveDirectory;
BlobNameBuilder = azConfig._eventConfig._blobNameBuilder;
ContainerNameBuilder = azConfig._eventConfig._containerNameBuilder;
ConnectionStringBuilder = azConfig._eventConfig._connectionStringBuilder;
if (UseActiveDirectory)
{
ResourceUrl = azConfig._eventConfig._activeDirectoryConfiguration._resourceUrl;
TenantIdBuilder = azConfig._eventConfig._activeDirectoryConfiguration._tenantIdBuilder;
}
}
}
#region Overrides
public override object InsertEvent(AuditEvent auditEvent)
{
var name = GetBlobName(auditEvent);
Upload(name, auditEvent);
return name;
}
public override T GetEvent<T>(object eventId)
{
var name = eventId.ToString();
var container = EnsureContainer(null);
var blob = container.GetBlockBlobReference(name);
var json = blob.DownloadTextAsync().Result;
return AuditEvent.FromJson<T>(json);
}
public override async Task<object> InsertEventAsync(AuditEvent auditEvent, CancellationToken cancellationToken = default)
{
var name = GetBlobName(auditEvent);
await UploadAsync(name, auditEvent, cancellationToken);
return name;
}
public override void ReplaceEvent(object eventId, AuditEvent auditEvent)
{
var name = eventId as string;
Upload(name, auditEvent);
}
public override async Task ReplaceEventAsync(object eventId, AuditEvent auditEvent, CancellationToken cancellationToken = default)
{
var name = eventId as string;
await UploadAsync(name, auditEvent, cancellationToken);
}
public override async Task<T> GetEventAsync<T>(object eventId, CancellationToken cancellationToken = default)
{
var name = eventId.ToString();
var container = await EnsureContainerAsync(null, cancellationToken);
var blob = container.GetBlockBlobReference(name);
var json = await blob.DownloadTextAsync(null, null, null, null, cancellationToken);
return AuditEvent.FromJson<T>(json);
}
#endregion
#region Private Methods
private void Upload(string name, AuditEvent auditEvent)
{
var container = EnsureContainer(auditEvent);
var blob = container.GetBlockBlobReference(name);
var json = Configuration.JsonAdapter.Serialize(auditEvent);
if (MetadataBuilder != null)
{
var metadata = MetadataBuilder.Invoke(auditEvent);
if (metadata != null)
{
foreach(var md in metadata)
{
blob.Metadata[md.Key] = md.Value;
}
}
}
blob.UploadTextAsync(json).GetAwaiter().GetResult();
if (AccessTierBuilder != null)
{
var accessTier = AccessTierBuilder.Invoke(auditEvent);
if (accessTier.HasValue)
{
blob.SetStandardBlobTierAsync(accessTier.Value).GetAwaiter().GetResult();
}
}
}
private async Task UploadAsync(string name, AuditEvent auditEvent, CancellationToken cancellationToken)
{
var container = await EnsureContainerAsync(auditEvent, cancellationToken);
var blob = container.GetBlockBlobReference(name);
var json = Configuration.JsonAdapter.Serialize(auditEvent);
if (MetadataBuilder != null)
{
var metadata = MetadataBuilder.Invoke(auditEvent);
if (metadata != null)
{
foreach (var md in metadata)
{
blob.Metadata[md.Key] = md.Value;
}
}
}
await blob.UploadTextAsync(json, null, null, null, null, cancellationToken);
if (AccessTierBuilder != null)
{
var accessTier = AccessTierBuilder.Invoke(auditEvent);
if (accessTier.HasValue)
{
await blob.SetStandardBlobTierAsync(accessTier.Value, null, null, null, cancellationToken);
}
}
}
protected string GetBlobName(AuditEvent auditEvent)
{
if (BlobNameBuilder != null)
{
return BlobNameBuilder.Invoke(auditEvent);
}
return string.Format("{0}.json", Guid.NewGuid());
}
protected string GetContainerName(AuditEvent auditEvent)
{
if (ContainerNameBuilder != null)
{
return ContainerNameBuilder.Invoke(auditEvent);
}
return "event";
}
protected string GetConnectionString(AuditEvent auditEvent)
{
return ConnectionStringBuilder?.Invoke(auditEvent);
}
protected string GetTenantId(AuditEvent auditEvent)
{
return TenantIdBuilder?.Invoke(auditEvent);
}
protected string GetAccountName(AuditEvent auditEvent)
{
return AccountNameBuilder?.Invoke(auditEvent);
}
protected virtual async Task<string> GetAccessTokenAsync(AuditEvent auditEvent, CancellationToken cancellationToken)
{
var authCnnString = GetConnectionString(auditEvent);
var tenantId = GetTenantId(auditEvent);
var provider = new AzureServiceTokenProvider(authCnnString);
var token = await provider.GetAccessTokenAsync(ResourceUrl, tenantId);
return token;
}
internal CloudBlobContainer EnsureContainer(AuditEvent auditEvent)
{
var containerName = GetContainerName(auditEvent);
if (UseActiveDirectory)
{
var accountName = GetAccountName(auditEvent);
var token = GetAccessTokenAsync(auditEvent, default).GetAwaiter().GetResult();
return EnsureContainerActiveDirectory(accountName, token, containerName);
}
else
{
var cnnString = GetConnectionString(auditEvent);
return EnsureContainer(cnnString, containerName);
}
}
internal async Task<CloudBlobContainer> EnsureContainerAsync(AuditEvent auditEvent, CancellationToken cancellationToken)
{
var containerName = GetContainerName(auditEvent);
if (UseActiveDirectory)
{
var accountName = GetAccountName(auditEvent);
var token = await GetAccessTokenAsync(auditEvent, cancellationToken);
return await EnsureContainerActiveDirectoryAsync(accountName, token, containerName, cancellationToken);
}
else
{
var cnnString = GetConnectionString(auditEvent);
return await EnsureContainerAsync(cnnString, containerName, cancellationToken);
}
}
internal CloudBlobContainer EnsureContainer(string cnnString, string containerName)
{
CloudBlobContainer result;
var cacheKey = cnnString + "|" + containerName;
if (ContainerCache.TryGetValue(cacheKey, out result))
{
return result;
}
var storageAccount = CloudStorageAccount.Parse(cnnString);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
container.CreateIfNotExistsAsync().GetAwaiter().GetResult();
ContainerCache[cacheKey] = container;
return container;
}
internal async Task<CloudBlobContainer> EnsureContainerAsync(string cnnString, string containerName, CancellationToken cancellationToken)
{
var cacheKey = cnnString + "|" + containerName;
if (ContainerCache.TryGetValue(cacheKey, out var result))
{
return result;
}
var storageAccount = CloudStorageAccount.Parse(cnnString);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
await container.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Off, null, null, cancellationToken);
ContainerCache[cacheKey] = container;
return container;
}
internal CloudBlobContainer EnsureContainerActiveDirectory(string accountName, string token, string containerName)
{
CloudBlobContainer result;
var cacheKey = accountName + "." + EndpointSuffix + "|" + containerName + "|" + token;
if (ContainerCache.TryGetValue(cacheKey, out result))
{
return result;
}
var tokenCredential = new TokenCredential(token);
var storageCredentials = new StorageCredentials(tokenCredential);
var storageAccount = new CloudStorageAccount(storageCredentials, accountName, EndpointSuffix, UseHttps);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
container.CreateIfNotExistsAsync().GetAwaiter().GetResult();
ContainerCache[cacheKey] = container;
return container;
}
internal async Task<CloudBlobContainer> EnsureContainerActiveDirectoryAsync(string accountName, string token, string containerName, CancellationToken cancellationToken)
{
var cacheKey = accountName + "." + EndpointSuffix + "|" + containerName + "|" + token;
if (ContainerCache.TryGetValue(cacheKey, out var result))
{
return result;
}
var tokenCredential = new TokenCredential(token);
var storageCredentials = new StorageCredentials(tokenCredential);
var storageAccount = new CloudStorageAccount(storageCredentials, accountName, EndpointSuffix, UseHttps);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
await container.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Off, null, null, cancellationToken);
ContainerCache[cacheKey] = container;
return container;
}
#endregion
}
}