From 9756ae1e7b2d7bbd984b15fdd01f100aa1eef498 Mon Sep 17 00:00:00 2001 From: Sean Feldman Date: Mon, 18 May 2020 00:16:16 -0600 Subject: [PATCH 01/12] Initial take on Storage v12 Signed-off-by: Sean Feldman --- .../AzureStorageAttachment.cs | 50 ++++++++++--------- .../ServiceBus.AttachmentPlugin.csproj | 2 +- .../TokenGenerator.cs | 5 +- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachment.cs b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachment.cs index 61c69c4..545cbf5 100644 --- a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachment.cs +++ b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachment.cs @@ -2,11 +2,15 @@ { using System; using System.Collections.Generic; + using System.IO; using System.Threading.Tasks; + using Azure; + using Azure.Core; + using Azure.Storage; + using Azure.Storage.Blobs; + using Azure.Storage.Blobs.Specialized; using Microsoft.Azure.ServiceBus; using Microsoft.Azure.ServiceBus.Core; - using Microsoft.Azure.Storage; - using Microsoft.Azure.Storage.Blob; class AzureStorageAttachment : ServiceBusPlugin { @@ -41,7 +45,7 @@ public override async Task BeforeMessageSend(Message message) } var containerUri = new Uri($"{configuration.BlobEndpoint}{configuration.ContainerName}"); - var container = new CloudBlobContainer(containerUri, configuration.StorageCredentials); + var container = new BlobContainerClient(containerUri, configuration.StorageCredentials); try { @@ -51,19 +55,21 @@ public override async Task BeforeMessageSend(Message message) await container.CreateIfNotExistsAsync().ConfigureAwait(false); } } - catch (StorageException) + catch (RequestFailedException) { // swallow in case a container SAS is used } var blobName = configuration.BlobNameResolver(message); var blobUri = new Uri($"{containerUri}/{blobName}"); - var blob = new CloudBlockBlob(blobUri, configuration.StorageCredentials); + var blob = new BlockBlobClient(blobUri, configuration.StorageCredentials); - SetValidMessageId(blob, message.MessageId); - SetValidUntil(blob, message.TimeToLive); + var metadata = new Dictionary(); + SetValidMessageId(metadata, message.MessageId); + SetValidUntil(metadata, message.TimeToLive); - await blob.UploadFromByteArrayAsync(message.Body, 0, message.Body.Length).ConfigureAwait(false); + using var memory = new MemoryStream(message.Body); + await blob.UploadAsync(memory, metadata:metadata).ConfigureAwait(false); message.Body = configuration.BodyReplacer(message); message.UserProperties[configuration.MessagePropertyToIdentifyAttachmentBlob] = blob.Name; @@ -83,15 +89,15 @@ public override async Task BeforeMessageSend(Message message) bool AttachmentBlobAssociated(IDictionary messageUserProperties) => messageUserProperties.TryGetValue(configuration.MessagePropertyToIdentifyAttachmentBlob, out var _); - static void SetValidMessageId(ICloudBlob blob, string messageId) + static void SetValidMessageId(Dictionary metadata, string messageId) { if (!string.IsNullOrWhiteSpace(messageId)) { - blob.Metadata[MessageId] = messageId; + metadata[MessageId] = messageId; } } - static void SetValidUntil(ICloudBlob blob, TimeSpan timeToBeReceived) + static void SetValidUntil(Dictionary metadata, TimeSpan timeToBeReceived) { if (timeToBeReceived == TimeSpan.MaxValue) { @@ -99,7 +105,7 @@ static void SetValidUntil(ICloudBlob blob, TimeSpan timeToBeReceived) } var validUntil = DateTimeFunc().Add(timeToBeReceived); - blob.Metadata[ValidUntilUtc] = validUntil.ToString(DateFormat); + metadata[ValidUntilUtc] = validUntil.ToString(DateFormat); } public override async Task AfterMessageReceive(Message message) @@ -115,34 +121,32 @@ public override async Task AfterMessageReceive(Message message) try { - await blob.FetchAttributesAsync().ConfigureAwait(false); + using var memory = new MemoryStream(); + await blob.DownloadToAsync(memory).ConfigureAwait(false); + message.Body = memory.ToArray(); + return message; } - catch (StorageException exception) + catch (RequestFailedException exception) { - throw new Exception($"Blob with name '{blob.Name}' under container '{blob.Container.Name}' cannot be found." + throw new Exception($"Blob with name '{blob.Name}' under container '{blob.BlobContainerName}' cannot be found." + $" Check {nameof(AzureStorageAttachmentConfiguration)}.{nameof(AzureStorageAttachmentConfiguration.ContainerName)} or" + $" {nameof(AzureStorageAttachmentConfiguration)}.{nameof(AzureStorageAttachmentConfiguration.MessagePropertyToIdentifyAttachmentBlob)} for correct values.", exception); } - var fileByteLength = blob.Properties.Length; - var bytes = new byte[fileByteLength]; - await blob.DownloadToByteArrayAsync(bytes, 0).ConfigureAwait(false); - message.Body = bytes; - return message; } - CloudBlockBlob BuildBlob(IDictionary userProperties, object blobNameObject) + BlockBlobClient BuildBlob(IDictionary userProperties, object blobNameObject) { if (configuration.MessagePropertyForBlobSasUri != null) { if (userProperties.TryGetValue(configuration.MessagePropertyForBlobSasUri, out var propertyForBlobSasUri)) { - return new CloudBlockBlob(new Uri((string)propertyForBlobSasUri)); + return new BlockBlobClient(new Uri((string)propertyForBlobSasUri)); } } var blobName = (string) blobNameObject; var blobUri = new Uri($"{configuration.BlobEndpoint}{configuration.ContainerName}/{blobName}"); - return new CloudBlockBlob(blobUri, configuration.StorageCredentials); + return new BlockBlobClient(blobUri, configuration.StorageCredentials); } } } diff --git a/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj b/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj index e7ce3e3..7546141 100644 --- a/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj +++ b/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj @@ -31,7 +31,7 @@ - + diff --git a/src/ServiceBus.AttachmentPlugin/TokenGenerator.cs b/src/ServiceBus.AttachmentPlugin/TokenGenerator.cs index 843454b..0fab2b4 100644 --- a/src/ServiceBus.AttachmentPlugin/TokenGenerator.cs +++ b/src/ServiceBus.AttachmentPlugin/TokenGenerator.cs @@ -1,12 +1,15 @@ namespace ServiceBus.AttachmentPlugin { using System; + using Azure.Storage.Blobs.Specialized; using Microsoft.Azure.Storage.Blob; static class TokenGenerator { - internal static string GetBlobSasUri(CloudBlockBlob blob, TimeSpan timeSpan) + internal static string GetBlobSasUri(BlockBlobClient blob, TimeSpan timeSpan) { + // TODO: review https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blob-user-delegation-sas-create-dotnet + //Set the expiry time and permissions for the blob. //In this case the start time is specified as a few minutes in the past, to mitigate clock skew. //The shared access signature will be valid immediately. From 3e2193a7ad4510bda34afe91cce2e40480285199 Mon Sep 17 00:00:00 2001 From: Sean Feldman Date: Wed, 20 May 2020 00:21:43 -0600 Subject: [PATCH 02/12] Update ReceiveOnlyAzureStorageAttachment to use Storage v12 SDK Signed-off-by: Sean Feldman --- .../ReceiveOnlyAzureStorageAttachment.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/ServiceBus.AttachmentPlugin/ReceiveOnlyAzureStorageAttachment.cs b/src/ServiceBus.AttachmentPlugin/ReceiveOnlyAzureStorageAttachment.cs index e7c1e59..a790d33 100644 --- a/src/ServiceBus.AttachmentPlugin/ReceiveOnlyAzureStorageAttachment.cs +++ b/src/ServiceBus.AttachmentPlugin/ReceiveOnlyAzureStorageAttachment.cs @@ -1,11 +1,12 @@ namespace ServiceBus.AttachmentPlugin { using System; + using System.IO; using System.Threading.Tasks; + using Azure; + using Azure.Storage.Blobs.Specialized; using Microsoft.Azure.ServiceBus; using Microsoft.Azure.ServiceBus.Core; - using Microsoft.Azure.Storage; - using Microsoft.Azure.Storage.Blob; class ReceiveOnlyAzureStorageAttachment : ServiceBusPlugin { @@ -28,19 +29,18 @@ public override async Task AfterMessageReceive(Message message) return message; } - var blob = new CloudBlockBlob(new Uri(userProperties[messagePropertyToIdentifySasUri].ToString())); + var blob = new BlockBlobClient(new Uri(userProperties[messagePropertyToIdentifySasUri].ToString())); try { - await blob.FetchAttributesAsync().ConfigureAwait(false); + await blob.GetPropertiesAsync().ConfigureAwait(false); } - catch (StorageException exception) + catch (RequestFailedException exception) { - throw new Exception($"Blob with name '{blob.Name}' under container '{blob.Container.Name}' cannot be found.", exception); + throw new Exception($"Blob with name '{blob.Name}' under container '{blob.BlobContainerName}' cannot be found.", exception); } - var fileByteLength = blob.Properties.Length; - var bytes = new byte[fileByteLength]; - await blob.DownloadToByteArrayAsync(bytes, 0).ConfigureAwait(false); - message.Body = bytes; + using var memory = new MemoryStream(); + await blob.DownloadToAsync(memory).ConfigureAwait(false); + message.Body = memory.ToArray(); return message; } } From 51035f3e4f6129d85d02f984e3792470087160ed Mon Sep 17 00:00:00 2001 From: Sean Feldman Date: Mon, 18 May 2020 00:16:16 -0600 Subject: [PATCH 03/12] Initial take on Storage v12 Signed-off-by: Sean Feldman --- .../AzureStorageAttachment.cs | 50 ++++++++++--------- .../ServiceBus.AttachmentPlugin.csproj | 2 +- .../TokenGenerator.cs | 5 +- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachment.cs b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachment.cs index 61c69c4..545cbf5 100644 --- a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachment.cs +++ b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachment.cs @@ -2,11 +2,15 @@ { using System; using System.Collections.Generic; + using System.IO; using System.Threading.Tasks; + using Azure; + using Azure.Core; + using Azure.Storage; + using Azure.Storage.Blobs; + using Azure.Storage.Blobs.Specialized; using Microsoft.Azure.ServiceBus; using Microsoft.Azure.ServiceBus.Core; - using Microsoft.Azure.Storage; - using Microsoft.Azure.Storage.Blob; class AzureStorageAttachment : ServiceBusPlugin { @@ -41,7 +45,7 @@ public override async Task BeforeMessageSend(Message message) } var containerUri = new Uri($"{configuration.BlobEndpoint}{configuration.ContainerName}"); - var container = new CloudBlobContainer(containerUri, configuration.StorageCredentials); + var container = new BlobContainerClient(containerUri, configuration.StorageCredentials); try { @@ -51,19 +55,21 @@ public override async Task BeforeMessageSend(Message message) await container.CreateIfNotExistsAsync().ConfigureAwait(false); } } - catch (StorageException) + catch (RequestFailedException) { // swallow in case a container SAS is used } var blobName = configuration.BlobNameResolver(message); var blobUri = new Uri($"{containerUri}/{blobName}"); - var blob = new CloudBlockBlob(blobUri, configuration.StorageCredentials); + var blob = new BlockBlobClient(blobUri, configuration.StorageCredentials); - SetValidMessageId(blob, message.MessageId); - SetValidUntil(blob, message.TimeToLive); + var metadata = new Dictionary(); + SetValidMessageId(metadata, message.MessageId); + SetValidUntil(metadata, message.TimeToLive); - await blob.UploadFromByteArrayAsync(message.Body, 0, message.Body.Length).ConfigureAwait(false); + using var memory = new MemoryStream(message.Body); + await blob.UploadAsync(memory, metadata:metadata).ConfigureAwait(false); message.Body = configuration.BodyReplacer(message); message.UserProperties[configuration.MessagePropertyToIdentifyAttachmentBlob] = blob.Name; @@ -83,15 +89,15 @@ public override async Task BeforeMessageSend(Message message) bool AttachmentBlobAssociated(IDictionary messageUserProperties) => messageUserProperties.TryGetValue(configuration.MessagePropertyToIdentifyAttachmentBlob, out var _); - static void SetValidMessageId(ICloudBlob blob, string messageId) + static void SetValidMessageId(Dictionary metadata, string messageId) { if (!string.IsNullOrWhiteSpace(messageId)) { - blob.Metadata[MessageId] = messageId; + metadata[MessageId] = messageId; } } - static void SetValidUntil(ICloudBlob blob, TimeSpan timeToBeReceived) + static void SetValidUntil(Dictionary metadata, TimeSpan timeToBeReceived) { if (timeToBeReceived == TimeSpan.MaxValue) { @@ -99,7 +105,7 @@ static void SetValidUntil(ICloudBlob blob, TimeSpan timeToBeReceived) } var validUntil = DateTimeFunc().Add(timeToBeReceived); - blob.Metadata[ValidUntilUtc] = validUntil.ToString(DateFormat); + metadata[ValidUntilUtc] = validUntil.ToString(DateFormat); } public override async Task AfterMessageReceive(Message message) @@ -115,34 +121,32 @@ public override async Task AfterMessageReceive(Message message) try { - await blob.FetchAttributesAsync().ConfigureAwait(false); + using var memory = new MemoryStream(); + await blob.DownloadToAsync(memory).ConfigureAwait(false); + message.Body = memory.ToArray(); + return message; } - catch (StorageException exception) + catch (RequestFailedException exception) { - throw new Exception($"Blob with name '{blob.Name}' under container '{blob.Container.Name}' cannot be found." + throw new Exception($"Blob with name '{blob.Name}' under container '{blob.BlobContainerName}' cannot be found." + $" Check {nameof(AzureStorageAttachmentConfiguration)}.{nameof(AzureStorageAttachmentConfiguration.ContainerName)} or" + $" {nameof(AzureStorageAttachmentConfiguration)}.{nameof(AzureStorageAttachmentConfiguration.MessagePropertyToIdentifyAttachmentBlob)} for correct values.", exception); } - var fileByteLength = blob.Properties.Length; - var bytes = new byte[fileByteLength]; - await blob.DownloadToByteArrayAsync(bytes, 0).ConfigureAwait(false); - message.Body = bytes; - return message; } - CloudBlockBlob BuildBlob(IDictionary userProperties, object blobNameObject) + BlockBlobClient BuildBlob(IDictionary userProperties, object blobNameObject) { if (configuration.MessagePropertyForBlobSasUri != null) { if (userProperties.TryGetValue(configuration.MessagePropertyForBlobSasUri, out var propertyForBlobSasUri)) { - return new CloudBlockBlob(new Uri((string)propertyForBlobSasUri)); + return new BlockBlobClient(new Uri((string)propertyForBlobSasUri)); } } var blobName = (string) blobNameObject; var blobUri = new Uri($"{configuration.BlobEndpoint}{configuration.ContainerName}/{blobName}"); - return new CloudBlockBlob(blobUri, configuration.StorageCredentials); + return new BlockBlobClient(blobUri, configuration.StorageCredentials); } } } diff --git a/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj b/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj index e7ce3e3..7546141 100644 --- a/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj +++ b/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj @@ -31,7 +31,7 @@ - + diff --git a/src/ServiceBus.AttachmentPlugin/TokenGenerator.cs b/src/ServiceBus.AttachmentPlugin/TokenGenerator.cs index 843454b..0fab2b4 100644 --- a/src/ServiceBus.AttachmentPlugin/TokenGenerator.cs +++ b/src/ServiceBus.AttachmentPlugin/TokenGenerator.cs @@ -1,12 +1,15 @@ namespace ServiceBus.AttachmentPlugin { using System; + using Azure.Storage.Blobs.Specialized; using Microsoft.Azure.Storage.Blob; static class TokenGenerator { - internal static string GetBlobSasUri(CloudBlockBlob blob, TimeSpan timeSpan) + internal static string GetBlobSasUri(BlockBlobClient blob, TimeSpan timeSpan) { + // TODO: review https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blob-user-delegation-sas-create-dotnet + //Set the expiry time and permissions for the blob. //In this case the start time is specified as a few minutes in the past, to mitigate clock skew. //The shared access signature will be valid immediately. From eb7a62799a8396fd1ba1291193faf6fa03a0cefd Mon Sep 17 00:00:00 2001 From: Sean Feldman Date: Wed, 20 May 2020 00:21:43 -0600 Subject: [PATCH 04/12] Update ReceiveOnlyAzureStorageAttachment to use Storage v12 SDK Signed-off-by: Sean Feldman --- .../ReceiveOnlyAzureStorageAttachment.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/ServiceBus.AttachmentPlugin/ReceiveOnlyAzureStorageAttachment.cs b/src/ServiceBus.AttachmentPlugin/ReceiveOnlyAzureStorageAttachment.cs index e7c1e59..a790d33 100644 --- a/src/ServiceBus.AttachmentPlugin/ReceiveOnlyAzureStorageAttachment.cs +++ b/src/ServiceBus.AttachmentPlugin/ReceiveOnlyAzureStorageAttachment.cs @@ -1,11 +1,12 @@ namespace ServiceBus.AttachmentPlugin { using System; + using System.IO; using System.Threading.Tasks; + using Azure; + using Azure.Storage.Blobs.Specialized; using Microsoft.Azure.ServiceBus; using Microsoft.Azure.ServiceBus.Core; - using Microsoft.Azure.Storage; - using Microsoft.Azure.Storage.Blob; class ReceiveOnlyAzureStorageAttachment : ServiceBusPlugin { @@ -28,19 +29,18 @@ public override async Task AfterMessageReceive(Message message) return message; } - var blob = new CloudBlockBlob(new Uri(userProperties[messagePropertyToIdentifySasUri].ToString())); + var blob = new BlockBlobClient(new Uri(userProperties[messagePropertyToIdentifySasUri].ToString())); try { - await blob.FetchAttributesAsync().ConfigureAwait(false); + await blob.GetPropertiesAsync().ConfigureAwait(false); } - catch (StorageException exception) + catch (RequestFailedException exception) { - throw new Exception($"Blob with name '{blob.Name}' under container '{blob.Container.Name}' cannot be found.", exception); + throw new Exception($"Blob with name '{blob.Name}' under container '{blob.BlobContainerName}' cannot be found.", exception); } - var fileByteLength = blob.Properties.Length; - var bytes = new byte[fileByteLength]; - await blob.DownloadToByteArrayAsync(bytes, 0).ConfigureAwait(false); - message.Body = bytes; + using var memory = new MemoryStream(); + await blob.DownloadToAsync(memory).ConfigureAwait(false); + message.Body = memory.ToArray(); return message; } } From 35a7efd6bffc30d070454ecabd1d9559cb63338b Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Tue, 2 Jun 2020 22:29:43 -0600 Subject: [PATCH 05/12] Bump MarkdownSnippets from 18.0.0 to 19.0.0 in /src (#206) Bumps [MarkdownSnippets](https://github.com/SimonCropp/MarkdownSnippets) from 18.0.0 to 19.0.0. - [Release notes](https://github.com/SimonCropp/MarkdownSnippets/releases) - [Commits](https://github.com/SimonCropp/MarkdownSnippets/compare/18.0.0...19.0.0) Signed-off-by: dependabot-preview[bot] Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> --- .../ServiceBus.AttachmentPlugin.Tests.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ServiceBus.AttachmentPlugin.Tests/ServiceBus.AttachmentPlugin.Tests.csproj b/src/ServiceBus.AttachmentPlugin.Tests/ServiceBus.AttachmentPlugin.Tests.csproj index 8d8b559..3d20c95 100644 --- a/src/ServiceBus.AttachmentPlugin.Tests/ServiceBus.AttachmentPlugin.Tests.csproj +++ b/src/ServiceBus.AttachmentPlugin.Tests/ServiceBus.AttachmentPlugin.Tests.csproj @@ -9,7 +9,7 @@ - + From 4ce463f2706e653a4282fd4f0a4257624f89a2e6 Mon Sep 17 00:00:00 2001 From: Sean Feldman Date: Tue, 2 Jun 2020 22:41:11 -0600 Subject: [PATCH 06/12] update azure.storage.blobs nuget Signed-off-by: Sean Feldman --- .../ServiceBus.AttachmentPlugin.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj b/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj index 7546141..126c42c 100644 --- a/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj +++ b/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj @@ -31,7 +31,7 @@ - + From 00a9d613303e6cd324e578af5d3270230f15e41d Mon Sep 17 00:00:00 2001 From: Sean Feldman Date: Tue, 9 Jun 2020 23:02:45 -0600 Subject: [PATCH 07/12] Add Azure.Identity for authentication with SDK v12 Signed-off-by: Sean Feldman --- .../ServiceBus.AttachmentPlugin.csproj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj b/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj index 126c42c..830ed3d 100644 --- a/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj +++ b/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj @@ -31,7 +31,8 @@ - + + From 76296251fbb4b48ad19f4b218d4d578531e8a850 Mon Sep 17 00:00:00 2001 From: Sean Feldman Date: Fri, 19 Jun 2020 00:29:44 -0600 Subject: [PATCH 08/12] Minimal Configuration object Signed-off-by: Sean Feldman --- .../AzureStorageAttachment.cs | 6 +- .../AzureStorageAttachmentConfiguration.cs | 79 +++++++++++-------- 2 files changed, 47 insertions(+), 38 deletions(-) diff --git a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachment.cs b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachment.cs index 545cbf5..cefed22 100644 --- a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachment.cs +++ b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachment.cs @@ -45,7 +45,7 @@ public override async Task BeforeMessageSend(Message message) } var containerUri = new Uri($"{configuration.BlobEndpoint}{configuration.ContainerName}"); - var container = new BlobContainerClient(containerUri, configuration.StorageCredentials); + var container = new BlobContainerClient(containerUri, configuration.StorageSharedKeyCredentials); try { @@ -62,7 +62,7 @@ public override async Task BeforeMessageSend(Message message) var blobName = configuration.BlobNameResolver(message); var blobUri = new Uri($"{containerUri}/{blobName}"); - var blob = new BlockBlobClient(blobUri, configuration.StorageCredentials); + var blob = new BlockBlobClient(blobUri, configuration.StorageSharedKeyCredentials); var metadata = new Dictionary(); SetValidMessageId(metadata, message.MessageId); @@ -146,7 +146,7 @@ BlockBlobClient BuildBlob(IDictionary userProperties, object blo var blobName = (string) blobNameObject; var blobUri = new Uri($"{configuration.BlobEndpoint}{configuration.ContainerName}/{blobName}"); - return new BlockBlobClient(blobUri, configuration.StorageCredentials); + return new BlockBlobClient(blobUri, configuration.StorageSharedKeyCredentials); } } } diff --git a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfiguration.cs b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfiguration.cs index 8f176f4..ccfc11f 100644 --- a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfiguration.cs +++ b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfiguration.cs @@ -1,8 +1,7 @@ namespace Microsoft.Azure.ServiceBus { using System; - using Storage; - using Storage.Auth; + using global::Azure.Storage; /// Runtime configuration for Azure Storage Attachment plugin. public class AzureStorageAttachmentConfiguration @@ -17,30 +16,39 @@ public AzureStorageAttachmentConfiguration( string containerName = AzureStorageAttachmentConfigurationConstants.DefaultContainerName, string messagePropertyToIdentifyAttachmentBlob = AzureStorageAttachmentConfigurationConstants.DefaultMessagePropertyToIdentifyAttachmentBlob, Func? messageMaxSizeReachedCriteria = default) - : this(new PlainTextConnectionStringProvider(connectionString), containerName, messagePropertyToIdentifyAttachmentBlob, messageMaxSizeReachedCriteria) { + Guard.AgainstNull(nameof(connectionString), connectionString); + Guard.AgainstEmpty(nameof(containerName), containerName); + Guard.AgainstEmpty(nameof(messagePropertyToIdentifyAttachmentBlob), messagePropertyToIdentifyAttachmentBlob); + + ContainerName = containerName; + MessagePropertyToIdentifyAttachmentBlob = messagePropertyToIdentifyAttachmentBlob; + MessageMaxSizeReachedCriteria = GetMessageMaxSizeReachedCriteria(messageMaxSizeReachedCriteria); + + StorageSharedKeyCredentials = null; + BlobEndpoint = null; } /// Constructor to create new configuration object. /// Container name is not required as it's included in the SharedAccessSignature. - /// + /// /// Blob endpoint in the format of "https://account.blob.core.windows.net/". For the emulator the value is "http://127.0.0.1:10000/devstoreaccount1". /// /// /// Default is always use attachments public AzureStorageAttachmentConfiguration( - StorageCredentials storageCredentials, + StorageSharedKeyCredential storageSharedKeyCredentials, string blobEndpoint, string containerName = AzureStorageAttachmentConfigurationConstants.DefaultContainerName, string messagePropertyToIdentifyAttachmentBlob = AzureStorageAttachmentConfigurationConstants.DefaultMessagePropertyToIdentifyAttachmentBlob, Func? messageMaxSizeReachedCriteria = default) { - Guard.AgainstNull(nameof(storageCredentials), storageCredentials); + Guard.AgainstNull(nameof(storageSharedKeyCredentials), storageSharedKeyCredentials); Guard.AgainstEmpty(nameof(blobEndpoint), blobEndpoint); Guard.AgainstEmpty(nameof(containerName), containerName); Guard.AgainstEmpty(nameof(messagePropertyToIdentifyAttachmentBlob), messagePropertyToIdentifyAttachmentBlob); - StorageCredentials = storageCredentials; + StorageSharedKeyCredentials = storageSharedKeyCredentials; BlobEndpoint = EnsureBlobEndpointEndsWithSlash(blobEndpoint); ContainerName = containerName; MessagePropertyToIdentifyAttachmentBlob = messagePropertyToIdentifyAttachmentBlob; @@ -58,31 +66,31 @@ static Uri EnsureBlobEndpointEndsWithSlash(string blobEndpoint) return new Uri(blobEndpoint + "/"); } - /// Constructor to create new configuration object. - /// Provider to retrieve connection string such as - /// Storage container name - /// Message user property to use for blob URI - /// Default is always use attachments - public AzureStorageAttachmentConfiguration( - IProvideStorageConnectionString connectionStringProvider, - string containerName = AzureStorageAttachmentConfigurationConstants.DefaultContainerName, - string messagePropertyToIdentifyAttachmentBlob = AzureStorageAttachmentConfigurationConstants.DefaultMessagePropertyToIdentifyAttachmentBlob, - Func? messageMaxSizeReachedCriteria = default) - { - Guard.AgainstNull(nameof(connectionStringProvider), connectionStringProvider); - Guard.AgainstEmpty(nameof(containerName), containerName); - Guard.AgainstEmpty(nameof(messagePropertyToIdentifyAttachmentBlob), messagePropertyToIdentifyAttachmentBlob); - - var connectionString = connectionStringProvider.GetConnectionString().GetAwaiter().GetResult(); - var account = CloudStorageAccount.Parse(connectionString); - - ConnectionStringProvider = connectionStringProvider; - StorageCredentials = account.Credentials; - BlobEndpoint = EnsureBlobEndpointEndsWithSlash(account.BlobEndpoint.ToString()); - ContainerName = containerName; - MessagePropertyToIdentifyAttachmentBlob = messagePropertyToIdentifyAttachmentBlob; - MessageMaxSizeReachedCriteria = GetMessageMaxSizeReachedCriteria(messageMaxSizeReachedCriteria); - } + // /// Constructor to create new configuration object. + // /// Provider to retrieve connection string such as + // /// Storage container name + // /// Message user property to use for blob URI + // /// Default is always use attachments + // public AzureStorageAttachmentConfiguration( + // IProvideStorageConnectionString connectionStringProvider, + // string containerName = AzureStorageAttachmentConfigurationConstants.DefaultContainerName, + // string messagePropertyToIdentifyAttachmentBlob = AzureStorageAttachmentConfigurationConstants.DefaultMessagePropertyToIdentifyAttachmentBlob, + // Func? messageMaxSizeReachedCriteria = default) + // { + // Guard.AgainstNull(nameof(connectionStringProvider), connectionStringProvider); + // Guard.AgainstEmpty(nameof(containerName), containerName); + // Guard.AgainstEmpty(nameof(messagePropertyToIdentifyAttachmentBlob), messagePropertyToIdentifyAttachmentBlob); + // + // var connectionString = connectionStringProvider.GetConnectionString().GetAwaiter().GetResult(); + // var account = CloudStorageAccount.Parse(connectionString); + // + // ConnectionStringProvider = connectionStringProvider; + // StorageSharedKeyCredentials = account.Credentials; + // BlobEndpoint = EnsureBlobEndpointEndsWithSlash(account.BlobEndpoint.ToString()); + // ContainerName = containerName; + // MessagePropertyToIdentifyAttachmentBlob = messagePropertyToIdentifyAttachmentBlob; + // MessageMaxSizeReachedCriteria = GetMessageMaxSizeReachedCriteria(messageMaxSizeReachedCriteria); + // } Func GetMessageMaxSizeReachedCriteria(Func? messageMaxSizeReachedCriteria) { @@ -115,11 +123,12 @@ Func GetMessageMaxSizeReachedCriteria(Func? messag internal Func MessageMaxSizeReachedCriteria { get; } - internal StorageCredentials StorageCredentials { get; } + internal StorageSharedKeyCredential? StorageSharedKeyCredentials { get; } + + internal Uri? BlobEndpoint { get; } - internal bool UsingSas => StorageCredentials.IsSAS; + // internal bool UsingSas => StorageSharedKeyCredentials.IsSAS; - internal Uri BlobEndpoint { get; } internal Func BlobNameResolver { get; set; } = message => Guid.NewGuid().ToString(); From 703449dade40b772c348f0badcd829fe2a0478fb Mon Sep 17 00:00:00 2001 From: Sean Feldman Date: Fri, 19 Jun 2020 00:32:59 -0600 Subject: [PATCH 09/12] Throw on what's not implemented Signed-off-by: Sean Feldman --- .../AzureStorageAttachment.cs | 2 - .../AzureStorageAttachmentConfiguration.cs | 51 ++++++++++--------- ...torageAttachmentConfigurationExtensions.cs | 31 +++++------ .../TokenGenerator.cs | 32 ++++++------ 4 files changed, 58 insertions(+), 58 deletions(-) diff --git a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachment.cs b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachment.cs index cefed22..e6d70ea 100644 --- a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachment.cs +++ b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachment.cs @@ -5,8 +5,6 @@ using System.IO; using System.Threading.Tasks; using Azure; - using Azure.Core; - using Azure.Storage; using Azure.Storage.Blobs; using Azure.Storage.Blobs.Specialized; using Microsoft.Azure.ServiceBus; diff --git a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfiguration.cs b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfiguration.cs index ccfc11f..a8e2491 100644 --- a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfiguration.cs +++ b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfiguration.cs @@ -66,31 +66,32 @@ static Uri EnsureBlobEndpointEndsWithSlash(string blobEndpoint) return new Uri(blobEndpoint + "/"); } - // /// Constructor to create new configuration object. - // /// Provider to retrieve connection string such as - // /// Storage container name - // /// Message user property to use for blob URI - // /// Default is always use attachments - // public AzureStorageAttachmentConfiguration( - // IProvideStorageConnectionString connectionStringProvider, - // string containerName = AzureStorageAttachmentConfigurationConstants.DefaultContainerName, - // string messagePropertyToIdentifyAttachmentBlob = AzureStorageAttachmentConfigurationConstants.DefaultMessagePropertyToIdentifyAttachmentBlob, - // Func? messageMaxSizeReachedCriteria = default) - // { - // Guard.AgainstNull(nameof(connectionStringProvider), connectionStringProvider); - // Guard.AgainstEmpty(nameof(containerName), containerName); - // Guard.AgainstEmpty(nameof(messagePropertyToIdentifyAttachmentBlob), messagePropertyToIdentifyAttachmentBlob); - // - // var connectionString = connectionStringProvider.GetConnectionString().GetAwaiter().GetResult(); - // var account = CloudStorageAccount.Parse(connectionString); - // - // ConnectionStringProvider = connectionStringProvider; - // StorageSharedKeyCredentials = account.Credentials; - // BlobEndpoint = EnsureBlobEndpointEndsWithSlash(account.BlobEndpoint.ToString()); - // ContainerName = containerName; - // MessagePropertyToIdentifyAttachmentBlob = messagePropertyToIdentifyAttachmentBlob; - // MessageMaxSizeReachedCriteria = GetMessageMaxSizeReachedCriteria(messageMaxSizeReachedCriteria); - // } + /// Constructor to create new configuration object. + /// Provider to retrieve connection string such as + /// Storage container name + /// Message user property to use for blob URI + /// Default is always use attachments + public AzureStorageAttachmentConfiguration( + IProvideStorageConnectionString connectionStringProvider, + string containerName = AzureStorageAttachmentConfigurationConstants.DefaultContainerName, + string messagePropertyToIdentifyAttachmentBlob = AzureStorageAttachmentConfigurationConstants.DefaultMessagePropertyToIdentifyAttachmentBlob, + Func? messageMaxSizeReachedCriteria = default) + { + throw new NotImplementedException(); + // Guard.AgainstNull(nameof(connectionStringProvider), connectionStringProvider); + // Guard.AgainstEmpty(nameof(containerName), containerName); + // Guard.AgainstEmpty(nameof(messagePropertyToIdentifyAttachmentBlob), messagePropertyToIdentifyAttachmentBlob); + // + // var connectionString = connectionStringProvider.GetConnectionString().GetAwaiter().GetResult(); + // var account = CloudStorageAccount.Parse(connectionString); + // + // ConnectionStringProvider = connectionStringProvider; + // StorageSharedKeyCredentials = account.Credentials; + // BlobEndpoint = EnsureBlobEndpointEndsWithSlash(account.BlobEndpoint.ToString()); + // ContainerName = containerName; + // MessagePropertyToIdentifyAttachmentBlob = messagePropertyToIdentifyAttachmentBlob; + // MessageMaxSizeReachedCriteria = GetMessageMaxSizeReachedCriteria(messageMaxSizeReachedCriteria); + } Func GetMessageMaxSizeReachedCriteria(Func? messageMaxSizeReachedCriteria) { diff --git a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfigurationExtensions.cs b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfigurationExtensions.cs index 7719969..9cb598e 100644 --- a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfigurationExtensions.cs +++ b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfigurationExtensions.cs @@ -21,21 +21,22 @@ public static AzureStorageAttachmentConfiguration WithBlobSasUri( string messagePropertyToIdentifySasUri = AzureStorageAttachmentConfigurationConstants.DefaultMessagePropertyToIdentitySasUri, TimeSpan? sasTokenValidationTime = null) { - if (azureStorageAttachmentConfiguration.UsingSas) - { - throw new Exception("Invalid configuration: .WithBlobSasUri() requires account shared key and cannot be used with service/container Shared Access Signature."); - } - - if (sasTokenValidationTime == null) - { - sasTokenValidationTime = DefaultSasTokenValidationTime; - } - Guard.AgainstNegativeOrZeroTimeSpan(nameof(sasTokenValidationTime), sasTokenValidationTime); - - azureStorageAttachmentConfiguration.MessagePropertyForBlobSasUri = messagePropertyToIdentifySasUri; - azureStorageAttachmentConfiguration.BlobSasTokenValidationTime = sasTokenValidationTime.Value; - - return azureStorageAttachmentConfiguration; + throw new NotImplementedException(); + // if (azureStorageAttachmentConfiguration.UsingSas) + // { + // throw new Exception("Invalid configuration: .WithBlobSasUri() requires account shared key and cannot be used with service/container Shared Access Signature."); + // } + // + // if (sasTokenValidationTime == null) + // { + // sasTokenValidationTime = DefaultSasTokenValidationTime; + // } + // Guard.AgainstNegativeOrZeroTimeSpan(nameof(sasTokenValidationTime), sasTokenValidationTime); + // + // azureStorageAttachmentConfiguration.MessagePropertyForBlobSasUri = messagePropertyToIdentifySasUri; + // azureStorageAttachmentConfiguration.BlobSasTokenValidationTime = sasTokenValidationTime.Value; + // + // return azureStorageAttachmentConfiguration; } /// diff --git a/src/ServiceBus.AttachmentPlugin/TokenGenerator.cs b/src/ServiceBus.AttachmentPlugin/TokenGenerator.cs index 0fab2b4..cc6b136 100644 --- a/src/ServiceBus.AttachmentPlugin/TokenGenerator.cs +++ b/src/ServiceBus.AttachmentPlugin/TokenGenerator.cs @@ -2,7 +2,6 @@ { using System; using Azure.Storage.Blobs.Specialized; - using Microsoft.Azure.Storage.Blob; static class TokenGenerator { @@ -10,21 +9,22 @@ internal static string GetBlobSasUri(BlockBlobClient blob, TimeSpan timeSpan) { // TODO: review https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blob-user-delegation-sas-create-dotnet - //Set the expiry time and permissions for the blob. - //In this case the start time is specified as a few minutes in the past, to mitigate clock skew. - //The shared access signature will be valid immediately. - var now = DateTime.UtcNow; - var sasConstraints = new SharedAccessBlobPolicy - { - SharedAccessStartTime = now.AddMinutes(-5), - SharedAccessExpiryTime = now.Add(timeSpan), - Permissions = SharedAccessBlobPermissions.Delete | SharedAccessBlobPermissions.Read - }; - //Generate the shared access signature on the blob, setting the constraints directly on the signature. - var sasBlobToken = blob.GetSharedAccessSignature(sasConstraints); - - //Return the URI string for the container, including the SAS token. - return blob.Uri + sasBlobToken; + throw new NotImplementedException(); + // //Set the expiry time and permissions for the blob. + // //In this case the start time is specified as a few minutes in the past, to mitigate clock skew. + // //The shared access signature will be valid immediately. + // var now = DateTime.UtcNow; + // var sasConstraints = new SharedAccessBlobPolicy + // { + // SharedAccessStartTime = now.AddMinutes(-5), + // SharedAccessExpiryTime = now.Add(timeSpan), + // Permissions = SharedAccessBlobPermissions.Delete | SharedAccessBlobPermissions.Read + // }; + // //Generate the shared access signature on the blob, setting the constraints directly on the signature. + // var sasBlobToken = blob.GetSharedAccessSignature(sasConstraints); + // + // //Return the URI string for the container, including the SAS token. + // return blob.Uri + sasBlobToken; } } } \ No newline at end of file From b8d7073fda303ed23334e8f53deae07b257fb192 Mon Sep 17 00:00:00 2001 From: Sean Feldman Date: Mon, 22 Jun 2020 00:50:09 -0600 Subject: [PATCH 10/12] Compiling, not working 100% Signed-off-by: Sean Feldman --- README.md | 27 +++---- ...zureStorageAttachmentConfigurationTests.cs | 3 +- .../AzureStorageEmulatorFixture.cs | 77 ++++++++++--------- .../Snippets.cs | 6 +- .../When_receiving_message.cs | 6 +- .../When_registering_plugin.cs | 3 +- ...sending_message_using_connection_string.cs | 35 ++++----- ...hen_sending_message_using_container_sas.cs | 49 ++++++------ ...n_sending_message_with_embedded_sas_uri.cs | 2 +- .../When_using_message_extensions.cs | 12 +-- .../When_using_receive_only_plugin.cs | 2 +- .../AzureStorageAttachmentConfiguration.cs | 7 +- 12 files changed, 114 insertions(+), 115 deletions(-) diff --git a/README.md b/README.md index 7871a4c..5251d38 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ var sender = new MessageSender(connectionString, queueName); var config = new AzureStorageAttachmentConfiguration(storageConnectionString); sender.RegisterAzureStorageAttachmentPlugin(config); ``` -snippet source | anchor +snippet source | anchor Sending @@ -72,7 +72,7 @@ var serialized = JsonConvert.SerializeObject(payload); var payloadAsBytes = Encoding.UTF8.GetBytes(serialized); var message = new Message(payloadAsBytes); ``` -snippet source | anchor +snippet source | anchor Receiving @@ -85,7 +85,7 @@ receiver.RegisterAzureStorageAttachmentPlugin(config); var message = await receiver.ReceiveAsync().ConfigureAwait(false); // message will contain the original payload ``` -snippet source | anchor +snippet source | anchor ### Sending a message without exposing the storage account to receivers @@ -102,7 +102,7 @@ var config = new AzureStorageAttachmentConfiguration(storageConnectionString) messagePropertyToIdentifySasUri: "mySasUriProperty"); sender.RegisterAzureStorageAttachmentPlugin(config); ``` -snippet source | anchor +snippet source | anchor Sending @@ -118,7 +118,7 @@ var serialized = JsonConvert.SerializeObject(payload); var payloadAsBytes = Encoding.UTF8.GetBytes(serialized); var message = new Message(payloadAsBytes); ``` -snippet source | anchor +snippet source | anchor Receiving only mode (w/o Storage account credentials) @@ -131,7 +131,7 @@ Receiving only mode (w/o Storage account credentials) messageReceiver.RegisterAzureStorageAttachmentPluginForReceivingOnly("mySasUriProperty"); var message = await messageReceiver.ReceiveAsync().ConfigureAwait(false); ``` -snippet source | anchor +snippet source | anchor ### Configure blob container name @@ -167,7 +167,7 @@ var config = new AzureStorageAttachmentConfiguration(storageConnectionString) }); sender.RegisterAzureStorageAttachmentPlugin(config); ``` -snippet source | anchor +snippet source | anchor @@ -185,7 +185,7 @@ new AzureStorageAttachmentConfiguration(storageConnectionString) messagePropertyToIdentifySasUri: "mySasUriProperty", sasTokenValidationTime: TimeSpan.FromHours(12)); ``` -snippet source | anchor +snippet source | anchor ### Configure criteria for message max size identification @@ -199,7 +199,7 @@ Default is to convert any body to attachment. new AzureStorageAttachmentConfiguration(storageConnectionString, messageMaxSizeReachedCriteria: message => message.Body.Length > 200 * 1024); ``` -snippet source | anchor +snippet source | anchor ### Configuring connection string provider @@ -213,7 +213,7 @@ The plugin comes with a `PlainTextConnectionStringProvider` and can be used in t var provider = new PlainTextConnectionStringProvider(connectionString); var config = new AzureStorageAttachmentConfiguration(provider); ``` -snippet source | anchor +snippet source | anchor ### Configuring plugin using StorageCredentials (Service or Container SAS) @@ -221,10 +221,11 @@ var config = new AzureStorageAttachmentConfiguration(provider); ```cs -var credentials = new StorageCredentials( /*Shared key OR Service SAS OR Container SAS*/); -var config = new AzureStorageAttachmentConfiguration(credentials, blobEndpoint); +// TODO: change +// var credentials = new StorageCredentials( /*Shared key OR Service SAS OR Container SAS*/); +// var config = new AzureStorageAttachmentConfiguration(credentials, blobEndpoint); ``` -snippet source | anchor +snippet source | anchor See [`StorageCredentials`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.storage.auth.storagecredentials) for more details. diff --git a/src/ServiceBus.AttachmentPlugin.Tests/AzureStorageAttachmentConfigurationTests.cs b/src/ServiceBus.AttachmentPlugin.Tests/AzureStorageAttachmentConfigurationTests.cs index 28fe908..876351d 100644 --- a/src/ServiceBus.AttachmentPlugin.Tests/AzureStorageAttachmentConfigurationTests.cs +++ b/src/ServiceBus.AttachmentPlugin.Tests/AzureStorageAttachmentConfigurationTests.cs @@ -3,7 +3,6 @@ using System; using System.Threading.Tasks; using Microsoft.Azure.ServiceBus; - using Microsoft.Azure.Storage.Auth; using Xunit; public class AzureStorageAttachmentConfigurationTests @@ -31,6 +30,6 @@ public void Should_not_accept_negative_token_validation_time() => [Fact] public void Should_throw_when_embedded_SAS_option_is_used_with_container_SAS() => Assert.Throws(() => new AzureStorageAttachmentConfiguration( - new StorageCredentials("?sv=2018-03-28&sr=c&sig=5XxlRKoP4yEmibM2HhJlQuV7MG3rYgQXD89mLpNp%2F24%3D"), "http://127.0.0.1:10000/devstoreaccount1", "devstoreaccount1").WithBlobSasUri()); + "http://127.0.0.1:10000/devstoreaccount1?sv=2018-03-28&sr=c&sig=5XxlRKoP4yEmibM2HhJlQuV7MG3rYgQXD89mLpNp%2F24%3D", "devstoreaccount1").WithBlobSasUri()); } } \ No newline at end of file diff --git a/src/ServiceBus.AttachmentPlugin.Tests/AzureStorageEmulatorFixture.cs b/src/ServiceBus.AttachmentPlugin.Tests/AzureStorageEmulatorFixture.cs index 8409c61..30c705c 100644 --- a/src/ServiceBus.AttachmentPlugin.Tests/AzureStorageEmulatorFixture.cs +++ b/src/ServiceBus.AttachmentPlugin.Tests/AzureStorageEmulatorFixture.cs @@ -1,73 +1,77 @@ namespace ServiceBus.AttachmentPlugin.Tests { using System; + using System.Linq; using System.Threading.Tasks; - using Microsoft.Azure.ServiceBus; - using Microsoft.Azure.Storage; - using Microsoft.Azure.Storage.Blob; + using Azure.Storage; + using Azure.Storage.Blobs; + using Azure.Storage.Blobs.Models; + using Azure.Storage.Sas; public class AzureStorageEmulatorFixture { - static string ConnectionString = "UseDevelopmentStorage=true"; - public static IProvideStorageConnectionString ConnectionStringProvider = new PlainTextConnectionStringProvider(ConnectionString); - CloudStorageAccount TestingStorageAccount = CloudStorageAccount.Parse(ConnectionString); + // development account + public const string TestingStorageAccountConnectionString = "UseDevelopmentStorage=true;"; - public string GetBlobEndpoint() + public static string GetBlobEndpoint() { - return TestingStorageAccount.BlobEndpoint.ToString(); + return "http://127.0.0.1:10000/devstoreaccount1"; } - public async Task GetContainerSas(string containerName) + public static async Task GetContainerSas(string containerName) { // get container - var blobClient = TestingStorageAccount.CreateCloudBlobClient(); - var container = blobClient.GetContainerReference(containerName); - if (!await container.ExistsAsync()) + var containerClient = new BlobContainerClient(TestingStorageAccountConnectionString, containerName); + if (!await containerClient.ExistsAsync()) { - await container.CreateIfNotExistsAsync(); + await containerClient.CreateIfNotExistsAsync(); } - await container.FetchAttributesAsync(); - var permissionsFound = await container.GetPermissionsAsync(); + await containerClient.GetPropertiesAsync(); + BlobContainerAccessPolicy permissionsFound = await containerClient.GetAccessPolicyAsync(); // create access policy and store it var accessPolicyId = "test-policy"; - if (!permissionsFound.SharedAccessPolicies.ContainsKey(accessPolicyId)) + var blobSignedIdentifier = permissionsFound.SignedIdentifiers.FirstOrDefault(x=> x.Id == accessPolicyId); + if (blobSignedIdentifier is null) { - var permissions = new BlobContainerPermissions + var permissions = new BlobSignedIdentifier { - PublicAccess = BlobContainerPublicAccessType.Off, - SharedAccessPolicies = + Id = accessPolicyId, + AccessPolicy = new BlobAccessPolicy { - { - accessPolicyId, - new SharedAccessBlobPolicy - { - Permissions = SharedAccessBlobPermissions.Add | SharedAccessBlobPermissions.Create | SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write, - SharedAccessStartTime = DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)), - SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddDays(1) - } - } + Permissions = "acrw" } }; - await container.SetPermissionsAsync(permissions); + await containerClient.SetAccessPolicyAsync(PublicAccessType.None, new []{permissions}); } else { - permissionsFound.SharedAccessPolicies[accessPolicyId].SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddDays(1); - await container.SetPermissionsAsync(permissionsFound); + blobSignedIdentifier.AccessPolicy.ExpiresOn = DateTimeOffset.UtcNow.AddDays(1); + await containerClient.SetAccessPolicyAsync(PublicAccessType.None, new []{blobSignedIdentifier}); } - + // create SAS with policy - return container.GetSharedAccessSignature(null, accessPolicyId); + //return containerClient.sas.GetSharedAccessSignature(null, accessPolicyId); + + var blobSasBuilder = new BlobSasBuilder + { + Identifier = accessPolicyId, + BlobContainerName = containerName, + // StartsOn = DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1)), + // ExpiresOn = DateTimeOffset.UtcNow.AddDays(1) + }; + //blobSasBuilder.SetPermissions(BlobSasPermissions.Add | BlobSasPermissions.Create | BlobSasPermissions.Read | BlobSasPermissions.Write); + var blobSasQueryParameters = blobSasBuilder.ToSasQueryParameters(new StorageSharedKeyCredential("devstoreaccount1", "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==")); + var fullUri = $"{GetBlobEndpoint()}/{blobSasQueryParameters}"; + return fullUri; } public async Task CreateContainer(string containerName) { - var containerUri = new Uri($"{TestingStorageAccount.BlobEndpoint}/{containerName}"); - var container = new CloudBlobContainer(containerUri, TestingStorageAccount.Credentials); + var container = new BlobContainerClient(TestingStorageAccountConnectionString, containerName); if (!await container.ExistsAsync()) { await container.CreateIfNotExistsAsync(); @@ -75,8 +79,7 @@ public async Task CreateContainer(string containerName) } public async Task DeleteContainer(string containerName) { - var containerUri = new Uri($"{TestingStorageAccount.BlobEndpoint}/{containerName}"); - var container = new CloudBlobContainer(containerUri, TestingStorageAccount.Credentials); + var container = new BlobContainerClient(TestingStorageAccountConnectionString, containerName); if (await container.ExistsAsync()) { await container.DeleteIfExistsAsync(); diff --git a/src/ServiceBus.AttachmentPlugin.Tests/Snippets.cs b/src/ServiceBus.AttachmentPlugin.Tests/Snippets.cs index 8254533..c9bd09a 100644 --- a/src/ServiceBus.AttachmentPlugin.Tests/Snippets.cs +++ b/src/ServiceBus.AttachmentPlugin.Tests/Snippets.cs @@ -4,7 +4,6 @@ using System.Threading.Tasks; using Microsoft.Azure.ServiceBus; using Microsoft.Azure.ServiceBus.Core; -using Microsoft.Azure.Storage.Auth; using Newtonsoft.Json; class Snippets @@ -161,8 +160,9 @@ void Configuring_plugin_using_StorageCredentials(string connectionString, string { #region Configuring_plugin_using_StorageCredentials - var credentials = new StorageCredentials( /*Shared key OR Service SAS OR Container SAS*/); - var config = new AzureStorageAttachmentConfiguration(credentials, blobEndpoint); + // TODO: change + // var credentials = new StorageCredentials( /*Shared key OR Service SAS OR Container SAS*/); + // var config = new AzureStorageAttachmentConfiguration(credentials, blobEndpoint); #endregion } diff --git a/src/ServiceBus.AttachmentPlugin.Tests/When_receiving_message.cs b/src/ServiceBus.AttachmentPlugin.Tests/When_receiving_message.cs index 8d76e27..74d6a04 100644 --- a/src/ServiceBus.AttachmentPlugin.Tests/When_receiving_message.cs +++ b/src/ServiceBus.AttachmentPlugin.Tests/When_receiving_message.cs @@ -19,15 +19,15 @@ public async Task Should_throw_exception_with_blob_path_for_blob_that_cant_be_fo }; var sendingPlugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration( - connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments")); + connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"))); await sendingPlugin.BeforeMessageSend(message); var receivingPlugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration( - connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments-wrong-containers")); + connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments-wrong-containers"))); var exception = await Assert.ThrowsAsync(() => receivingPlugin.AfterMessageReceive(message)); Assert.Contains("attachments-wrong-containers", actualString: exception.Message); Assert.Contains(message.UserProperties["$attachment.blob"].ToString(), actualString: exception.Message); } } -} +} \ No newline at end of file diff --git a/src/ServiceBus.AttachmentPlugin.Tests/When_registering_plugin.cs b/src/ServiceBus.AttachmentPlugin.Tests/When_registering_plugin.cs index 70beb70..310d552 100644 --- a/src/ServiceBus.AttachmentPlugin.Tests/When_registering_plugin.cs +++ b/src/ServiceBus.AttachmentPlugin.Tests/When_registering_plugin.cs @@ -6,7 +6,6 @@ using System.Threading.Tasks; using Microsoft.Azure.ServiceBus; using Microsoft.Azure.ServiceBus.Core; - using Microsoft.Azure.Storage.Auth; using Xunit; public class When_registering_plugin : IClassFixture @@ -31,7 +30,7 @@ public void Should_get_back_AzureStorageAttachment_for_container_sas_based_confi { IClientEntity client = new FakeClientEntity("fake", string.Empty, RetryPolicy.Default); - var azureStorageAttachmentConfiguration = new AzureStorageAttachmentConfiguration(new StorageCredentials("?sv=2018-03-28&sr=c&sig=5XxlRKoP4yEmibM2HhJlQuV7MG3rYgQXD89mLpNp%2F24%3D"), "http://127.0.0.1:10000/devstoreaccount1"); + var azureStorageAttachmentConfiguration = new AzureStorageAttachmentConfiguration("http://127.0.0.1:10000/devstoreaccount1?sv=2018-03-28&sr=c&sig=5XxlRKoP4yEmibM2HhJlQuV7MG3rYgQXD89mLpNp%2F24%3D"); var registeredPlugin = AzureStorageAttachmentExtensions.RegisterAzureStorageAttachmentPlugin(client, azureStorageAttachmentConfiguration); diff --git a/src/ServiceBus.AttachmentPlugin.Tests/When_sending_message_using_connection_string.cs b/src/ServiceBus.AttachmentPlugin.Tests/When_sending_message_using_connection_string.cs index db40e07..3b21673 100644 --- a/src/ServiceBus.AttachmentPlugin.Tests/When_sending_message_using_connection_string.cs +++ b/src/ServiceBus.AttachmentPlugin.Tests/When_sending_message_using_connection_string.cs @@ -3,10 +3,9 @@ using System; using System.Text; using System.Threading.Tasks; + using Azure.Storage.Blobs; + using Azure.Storage.Blobs.Models; using Microsoft.Azure.ServiceBus; - using Microsoft.Azure.Storage; - using Microsoft.Azure.Storage.Auth; - using Microsoft.Azure.Storage.Blob; using Xunit; public class When_sending_message_using_connection_string : IClassFixture @@ -28,7 +27,7 @@ public async Task Should_nullify_body_when_body_should_be_sent_as_attachment() MessageId = Guid.NewGuid().ToString(), }; var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration( - connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName:"attachments", messagePropertyToIdentifyAttachmentBlob:"attachment-id")); + connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob:"attachment-id")); var result = await plugin.BeforeMessageSend(message); Assert.Null(result.Body); @@ -46,7 +45,7 @@ public async Task Should_leave_body_as_is_for_message_not_exceeding_max_size() TimeToLive = TimeSpan.FromHours(1) }; var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration( - connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName:"attachments", messagePropertyToIdentifyAttachmentBlob:"attachment-id", + connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob:"attachment-id", messageMaxSizeReachedCriteria:msg => msg.Body.Length > 100)); var result = await plugin.BeforeMessageSend(message); @@ -65,19 +64,17 @@ public async Task Should_set_valid_until_datetime_on_blob_same_as_message_TTL() TimeToLive = TimeSpan.FromHours(1) }; var dateTimeNowUtc = new DateTime(2017, 1, 2); - var configuration = new AzureStorageAttachmentConfiguration(connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName:"attachments", + var configuration = new AzureStorageAttachmentConfiguration(connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob:"attachment-id"); AzureStorageAttachment.DateTimeFunc = () => dateTimeNowUtc; var plugin = new AzureStorageAttachment(configuration); await plugin.BeforeMessageSend(message); - var account = CloudStorageAccount.Parse(await configuration.ConnectionStringProvider!.GetConnectionString()); - var client = account.CreateCloudBlobClient(); - var container = client.GetContainerReference(configuration.ContainerName); + var container = configuration.ContainerName; var blobName = (string)message.UserProperties[configuration.MessagePropertyToIdentifyAttachmentBlob]; - var blob = container.GetBlockBlobReference(blobName); - await blob.FetchAttributesAsync(); - var validUntil = blob.Metadata[AzureStorageAttachment.ValidUntilUtc]; + var blob = new BlobClient(connectionString: AzureStorageEmulatorFixture.TestingStorageAccountConnectionString, blobContainerName: container, blobName: blobName); + BlobProperties properties = await blob.GetPropertiesAsync(); + var validUntil = properties.Metadata[AzureStorageAttachment.ValidUntilUtc]; Assert.Equal(dateTimeNowUtc.Add(message.TimeToLive).ToString(AzureStorageAttachment.DateFormat), validUntil); } @@ -88,7 +85,7 @@ public async Task Should_receive_it_using_connection_string() var bytes = Encoding.UTF8.GetBytes(payload); var message = new Message(bytes); var configuration = new AzureStorageAttachmentConfiguration( - connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id"); + connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); var plugin = new AzureStorageAttachment(configuration); await plugin.BeforeMessageSend(message); @@ -107,7 +104,7 @@ public async Task Should_not_reupload_blob_if_one_is_already_assigned() var bytes = Encoding.UTF8.GetBytes(payload); var message = new Message(bytes); var configuration = new AzureStorageAttachmentConfiguration( - connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id"); + connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); var plugin = new AzureStorageAttachment(configuration); @@ -130,7 +127,7 @@ public async Task Should_not_set_sas_uri_by_default() MessageId = Guid.NewGuid().ToString(), }; var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration( - connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id")); + connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id")); var result = await plugin.BeforeMessageSend(message); Assert.Null(result.Body); @@ -145,15 +142,15 @@ public async Task Should_be_able_to_receive_using_container_sas() var bytes = Encoding.UTF8.GetBytes(payload); var message = new Message(bytes); var configuration = new AzureStorageAttachmentConfiguration( - connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id"); + connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); var plugin = new AzureStorageAttachment(configuration); await plugin.BeforeMessageSend(message); Assert.Null(message.Body); - var credentials = new StorageCredentials(await fixture.GetContainerSas("attachments")); - var receiveConfiguration = new AzureStorageAttachmentConfiguration(credentials, fixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); + var credentials = await AzureStorageEmulatorFixture.GetContainerSas("attachments"); + var receiveConfiguration = new AzureStorageAttachmentConfiguration(credentials, AzureStorageEmulatorFixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); var receivePlugin = new AzureStorageAttachment(receiveConfiguration); @@ -171,7 +168,7 @@ public async Task Should_be_able_to_send_if_container_was_not_found() var bytes = Encoding.UTF8.GetBytes(payload); var message = new Message(bytes); var configuration = new AzureStorageAttachmentConfiguration( - connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments-that-didnt-exist", messagePropertyToIdentifyAttachmentBlob: "attachment-id"); + connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments-that-didnt-exist"), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); var plugin = new AzureStorageAttachment(configuration); await plugin.BeforeMessageSend(message); diff --git a/src/ServiceBus.AttachmentPlugin.Tests/When_sending_message_using_container_sas.cs b/src/ServiceBus.AttachmentPlugin.Tests/When_sending_message_using_container_sas.cs index 2ee8d6e..6a34442 100644 --- a/src/ServiceBus.AttachmentPlugin.Tests/When_sending_message_using_container_sas.cs +++ b/src/ServiceBus.AttachmentPlugin.Tests/When_sending_message_using_container_sas.cs @@ -3,19 +3,18 @@ using System; using System.Text; using System.Threading.Tasks; + using Azure.Storage.Blobs; + using Azure.Storage.Blobs.Models; using Microsoft.Azure.ServiceBus; - using Microsoft.Azure.Storage; - using Microsoft.Azure.Storage.Auth; - using Microsoft.Azure.Storage.Blob; using Xunit; public class When_sending_message_using_container_sas : IClassFixture { - readonly AzureStorageEmulatorFixture fixture; + // readonly AzureStorageEmulatorFixture fixture; public When_sending_message_using_container_sas(AzureStorageEmulatorFixture fixture) { - this.fixture = fixture; + // this.fixture = fixture; } [Fact] @@ -27,8 +26,8 @@ public async Task Should_nullify_body_when_body_should_be_sent_as_attachment() { MessageId = Guid.NewGuid().ToString(), }; - var credentials = new StorageCredentials(await fixture.GetContainerSas("attachments")); - var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration(credentials, fixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob:"attachment-id")); + var credentials = await AzureStorageEmulatorFixture.GetContainerSas("attachments"); + var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration(credentials, AzureStorageEmulatorFixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob:"attachment-id")); var result = await plugin.BeforeMessageSend(message); Assert.Null(result.Body); @@ -45,8 +44,8 @@ public async Task Should_leave_body_as_is_for_message_not_exceeding_max_size() MessageId = Guid.NewGuid().ToString(), TimeToLive = TimeSpan.FromHours(1) }; - var credentials = new StorageCredentials(await fixture.GetContainerSas("attachments")); - var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration(credentials, "http://127.0.0.1:10000/devstoreaccount1", messagePropertyToIdentifyAttachmentBlob:"attachment -id", + var credentials = await AzureStorageEmulatorFixture.GetContainerSas("attachments"); + var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration(credentials, AzureStorageEmulatorFixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob:"attachment -id", messageMaxSizeReachedCriteria:msg => msg.Body.Length > 100)); var result = await plugin.BeforeMessageSend(message); @@ -64,20 +63,18 @@ public async Task Should_set_valid_until_datetime_on_blob_same_as_message_TTL() MessageId = Guid.NewGuid().ToString(), TimeToLive = TimeSpan.FromHours(1) }; - var credentials = new StorageCredentials(await fixture.GetContainerSas("attachments")); - var configuration = new AzureStorageAttachmentConfiguration(credentials, fixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob:"attachment-id"); + var credentials = await AzureStorageEmulatorFixture.GetContainerSas("attachments"); + var configuration = new AzureStorageAttachmentConfiguration(credentials, AzureStorageEmulatorFixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob:"attachment-id"); var dateTimeNowUtc = new DateTime(2017, 1, 2); AzureStorageAttachment.DateTimeFunc = () => dateTimeNowUtc; var plugin = new AzureStorageAttachment(configuration); await plugin.BeforeMessageSend(message); - var account = CloudStorageAccount.Parse(await AzureStorageEmulatorFixture.ConnectionStringProvider.GetConnectionString()); - var client = account.CreateCloudBlobClient(); - var container = client.GetContainerReference("attachments"); + var client = new BlobContainerClient(connectionString:AzureStorageEmulatorFixture.TestingStorageAccountConnectionString, blobContainerName: "attachments"); var blobName = (string)message.UserProperties[configuration.MessagePropertyToIdentifyAttachmentBlob]; - var blob = container.GetBlockBlobReference(blobName); - await blob.FetchAttributesAsync(); - var validUntil = blob.Metadata[AzureStorageAttachment.ValidUntilUtc]; + var blob = client.GetBlobClient(blobName); + BlobProperties properties = await blob.GetPropertiesAsync(); + var validUntil = properties.Metadata[AzureStorageAttachment.ValidUntilUtc]; Assert.Equal(dateTimeNowUtc.Add(message.TimeToLive).ToString(AzureStorageAttachment.DateFormat), validUntil); } @@ -87,8 +84,8 @@ public async Task Should_receive_it_using_container_sas() var payload = "payload"; var bytes = Encoding.UTF8.GetBytes(payload); var message = new Message(bytes); - var credentials = new StorageCredentials(await fixture.GetContainerSas("attachments")); - var configuration = new AzureStorageAttachmentConfiguration(credentials, fixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); + var credentials = await AzureStorageEmulatorFixture.GetContainerSas("attachments"); + var configuration = new AzureStorageAttachmentConfiguration(credentials, AzureStorageEmulatorFixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); var plugin = new AzureStorageAttachment(configuration); await plugin.BeforeMessageSend(message); @@ -106,8 +103,8 @@ public async Task Should_not_reupload_blob_if_one_is_already_assigned() var payload = "payload"; var bytes = Encoding.UTF8.GetBytes(payload); var message = new Message(bytes); - var credentials = new StorageCredentials(await fixture.GetContainerSas("attachments")); - var configuration = new AzureStorageAttachmentConfiguration(credentials, fixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); + var credentials = await AzureStorageEmulatorFixture.GetContainerSas("attachments"); + var configuration = new AzureStorageAttachmentConfiguration(credentials, AzureStorageEmulatorFixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); var plugin = new AzureStorageAttachment(configuration); @@ -129,8 +126,8 @@ public async Task Should_not_set_embedded_sas_uri_by_default() { MessageId = Guid.NewGuid().ToString(), }; - var credentials = new StorageCredentials(await fixture.GetContainerSas("attachments")); - var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration(credentials, fixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob: "attachment-id")); + var credentials = await AzureStorageEmulatorFixture.GetContainerSas("attachments"); + var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration(credentials, AzureStorageEmulatorFixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob: "attachment-id")); var result = await plugin.BeforeMessageSend(message); Assert.Null(result.Body); @@ -144,8 +141,8 @@ public async Task Should_be_able_to_receive_using_storage_connection_string() var payload = "payload"; var bytes = Encoding.UTF8.GetBytes(payload); var message = new Message(bytes); - var credentials = new StorageCredentials(await fixture.GetContainerSas("attachments")); - var configuration = new AzureStorageAttachmentConfiguration(credentials, fixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); + var credentials = await AzureStorageEmulatorFixture.GetContainerSas("attachments"); + var configuration = new AzureStorageAttachmentConfiguration(credentials, AzureStorageEmulatorFixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); var plugin = new AzureStorageAttachment(configuration); await plugin.BeforeMessageSend(message); @@ -153,7 +150,7 @@ public async Task Should_be_able_to_receive_using_storage_connection_string() Assert.Null(message.Body); var receivePlugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration( - connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id")); + connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id")); var receivedMessage = await receivePlugin.AfterMessageReceive(message); diff --git a/src/ServiceBus.AttachmentPlugin.Tests/When_sending_message_with_embedded_sas_uri.cs b/src/ServiceBus.AttachmentPlugin.Tests/When_sending_message_with_embedded_sas_uri.cs index 924a49d..c51dd00 100644 --- a/src/ServiceBus.AttachmentPlugin.Tests/When_sending_message_with_embedded_sas_uri.cs +++ b/src/ServiceBus.AttachmentPlugin.Tests/When_sending_message_with_embedded_sas_uri.cs @@ -18,7 +18,7 @@ public async Task Should_set_sas_uri_when_specified() MessageId = Guid.NewGuid().ToString(), }; var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration( - connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id") + connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id") .WithBlobSasUri(sasTokenValidationTime: TimeSpan.FromHours(4), messagePropertyToIdentifySasUri: "mySasUriProperty")); var result = await plugin.BeforeMessageSend(message); diff --git a/src/ServiceBus.AttachmentPlugin.Tests/When_using_message_extensions.cs b/src/ServiceBus.AttachmentPlugin.Tests/When_using_message_extensions.cs index 810ad3b..965c7ef 100644 --- a/src/ServiceBus.AttachmentPlugin.Tests/When_using_message_extensions.cs +++ b/src/ServiceBus.AttachmentPlugin.Tests/When_using_message_extensions.cs @@ -23,7 +23,7 @@ public async Task Should_send_and_receive_with_configuration() var bytes = Encoding.UTF8.GetBytes(payload); var message = new Message(bytes); var configuration = new AzureStorageAttachmentConfiguration( - connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id"); + connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); await message.UploadAzureStorageAttachment(configuration); @@ -41,7 +41,7 @@ public async Task Should_send_and_receive_with_default_sas_uri_property() var bytes = Encoding.UTF8.GetBytes(payload); var message = new Message(bytes); var configuration = new AzureStorageAttachmentConfiguration( - connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id") + connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id") .WithBlobSasUri(); await message.UploadAzureStorageAttachment(configuration); @@ -61,7 +61,7 @@ public async Task Should_send_and_receive_with_custom_sas_uri_property() var message = new Message(bytes); var customSasUri = "$custom-attachment.sas.uri"; var configuration = new AzureStorageAttachmentConfiguration( - connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id") + connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id") .WithBlobSasUri(customSasUri); await message.UploadAzureStorageAttachment(configuration); @@ -80,7 +80,7 @@ public async Task Should_be_able_to_override_blob_name_and_receive_message_paylo var bytes = Encoding.UTF8.GetBytes(payload); var message = new Message(bytes) { MessageId = Guid.NewGuid().ToString("N") }; var configuration = new AzureStorageAttachmentConfiguration( - connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider); + connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments")); configuration.OverrideBlobName(msg => $"test/{msg.MessageId}"); @@ -104,7 +104,7 @@ public async Task Should_set_body_to_the_value_provided_by_body_replacer_overrid MessageId = Guid.NewGuid().ToString(), }; var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration( - connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id") + connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id") .OverrideBody(msg => Array.Empty())); var result = await plugin.BeforeMessageSend(message); @@ -122,7 +122,7 @@ public async Task Should_set_body_to_null_if_body_replacer_override_is_not_provi MessageId = Guid.NewGuid().ToString(), }; var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration( - connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, containerName: "attachments", messagePropertyToIdentifyAttachmentBlob: "attachment-id")); + connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id")); var result = await plugin.BeforeMessageSend(message); Assert.Null(result.Body); diff --git a/src/ServiceBus.AttachmentPlugin.Tests/When_using_receive_only_plugin.cs b/src/ServiceBus.AttachmentPlugin.Tests/When_using_receive_only_plugin.cs index 63c2f27..d9cd46c 100644 --- a/src/ServiceBus.AttachmentPlugin.Tests/When_using_receive_only_plugin.cs +++ b/src/ServiceBus.AttachmentPlugin.Tests/When_using_receive_only_plugin.cs @@ -29,7 +29,7 @@ public async Task Should_download_attachment_using_provided_blob_sas_uri() MessageId = Guid.NewGuid().ToString(), }; var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration( - connectionStringProvider: AzureStorageEmulatorFixture.ConnectionStringProvider, + connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), containerName: "attachments-sendonly", messagePropertyToIdentifyAttachmentBlob: "attachment-id") diff --git a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfiguration.cs b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfiguration.cs index a8e2491..caacb9d 100644 --- a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfiguration.cs +++ b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfiguration.cs @@ -25,8 +25,10 @@ public AzureStorageAttachmentConfiguration( MessagePropertyToIdentifyAttachmentBlob = messagePropertyToIdentifyAttachmentBlob; MessageMaxSizeReachedCriteria = GetMessageMaxSizeReachedCriteria(messageMaxSizeReachedCriteria); - StorageSharedKeyCredentials = null; - BlobEndpoint = null; + // TODO: figure out + StorageSharedKeyCredentials = new StorageSharedKeyCredential("devstoreaccount1", "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="); + // TODO: figure out + BlobEndpoint = EnsureBlobEndpointEndsWithSlash("http://127.0.0.1:10000/devstoreaccount1"); } /// Constructor to create new configuration object. @@ -49,6 +51,7 @@ public AzureStorageAttachmentConfiguration( Guard.AgainstEmpty(nameof(messagePropertyToIdentifyAttachmentBlob), messagePropertyToIdentifyAttachmentBlob); StorageSharedKeyCredentials = storageSharedKeyCredentials; + // TODO: figure out BlobEndpoint = EnsureBlobEndpointEndsWithSlash(blobEndpoint); ContainerName = containerName; MessagePropertyToIdentifyAttachmentBlob = messagePropertyToIdentifyAttachmentBlob; From f62ed2e4c05d7feb6ff5872a7e486120c8bf5ebb Mon Sep 17 00:00:00 2001 From: Sean Feldman Date: Thu, 25 Jun 2020 01:04:16 -0600 Subject: [PATCH 11/12] Reflecting StorageConnectionString to partse connection strings Signed-off-by: Sean Feldman --- ...zureStorageAttachmentConfigurationTests.cs | 8 ++- .../AzureStorageEmulatorFixture.cs | 2 +- .../ServiceBus.AttachmentPlugin.Tests.csproj | 2 +- .../When_receiving_message.cs | 6 ++- ...sending_message_using_connection_string.cs | 27 ++++++---- .../When_using_message_extensions.cs | 18 ++++--- .../When_using_receive_only_plugin.cs | 3 +- .../AzureStorageAttachmentConfiguration.cs | 49 ++++++++++--------- .../ReceiveOnlyAzureStorageAttachment.cs | 6 ++- .../ReflectedAzureStorageConnectionString.cs | 47 ++++++++++++++++++ .../ServiceBus.AttachmentPlugin.csproj | 2 +- 11 files changed, 117 insertions(+), 53 deletions(-) create mode 100644 src/ServiceBus.AttachmentPlugin/ReflectedAzureStorageConnectionString.cs diff --git a/src/ServiceBus.AttachmentPlugin.Tests/AzureStorageAttachmentConfigurationTests.cs b/src/ServiceBus.AttachmentPlugin.Tests/AzureStorageAttachmentConfigurationTests.cs index 876351d..d6acc87 100644 --- a/src/ServiceBus.AttachmentPlugin.Tests/AzureStorageAttachmentConfigurationTests.cs +++ b/src/ServiceBus.AttachmentPlugin.Tests/AzureStorageAttachmentConfigurationTests.cs @@ -1,7 +1,6 @@ namespace ServiceBus.AttachmentPlugin.Tests { using System; - using System.Threading.Tasks; using Microsoft.Azure.ServiceBus; using Xunit; @@ -10,11 +9,10 @@ public class AzureStorageAttachmentConfigurationTests const string ConnectionString = "AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;DefaultEndpointsProtocol=http;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1"; [Fact] - public async Task Should_apply_defaults_for_missing_arguments() + public void Should_apply_defaults_for_missing_arguments() { - var configuration = new AzureStorageAttachmentConfiguration(new PlainTextConnectionStringProvider(ConnectionString)) + var configuration = new AzureStorageAttachmentConfiguration(ConnectionString) .WithBlobSasUri(); - Assert.Equal(ConnectionString, await configuration.ConnectionStringProvider!.GetConnectionString()); Assert.NotEmpty(configuration.ContainerName); Assert.NotEmpty(configuration.MessagePropertyToIdentifyAttachmentBlob); Assert.Equal(AzureStorageAttachmentConfigurationExtensions.DefaultSasTokenValidationTime.Days, configuration.BlobSasTokenValidationTime!.Value.Days); @@ -24,7 +22,7 @@ public async Task Should_apply_defaults_for_missing_arguments() [Fact] public void Should_not_accept_negative_token_validation_time() => - Assert.Throws(() => new AzureStorageAttachmentConfiguration(new PlainTextConnectionStringProvider(ConnectionString)) + Assert.Throws(() => new AzureStorageAttachmentConfiguration(ConnectionString) .WithBlobSasUri(sasTokenValidationTime: TimeSpan.FromHours(-4))); [Fact] diff --git a/src/ServiceBus.AttachmentPlugin.Tests/AzureStorageEmulatorFixture.cs b/src/ServiceBus.AttachmentPlugin.Tests/AzureStorageEmulatorFixture.cs index 30c705c..af30740 100644 --- a/src/ServiceBus.AttachmentPlugin.Tests/AzureStorageEmulatorFixture.cs +++ b/src/ServiceBus.AttachmentPlugin.Tests/AzureStorageEmulatorFixture.cs @@ -65,7 +65,7 @@ public static async Task GetContainerSas(string containerName) }; //blobSasBuilder.SetPermissions(BlobSasPermissions.Add | BlobSasPermissions.Create | BlobSasPermissions.Read | BlobSasPermissions.Write); var blobSasQueryParameters = blobSasBuilder.ToSasQueryParameters(new StorageSharedKeyCredential("devstoreaccount1", "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==")); - var fullUri = $"{GetBlobEndpoint()}/{blobSasQueryParameters}"; + var fullUri = $"{GetBlobEndpoint()}?{blobSasQueryParameters}"; return fullUri; } diff --git a/src/ServiceBus.AttachmentPlugin.Tests/ServiceBus.AttachmentPlugin.Tests.csproj b/src/ServiceBus.AttachmentPlugin.Tests/ServiceBus.AttachmentPlugin.Tests.csproj index 986d688..49a8261 100644 --- a/src/ServiceBus.AttachmentPlugin.Tests/ServiceBus.AttachmentPlugin.Tests.csproj +++ b/src/ServiceBus.AttachmentPlugin.Tests/ServiceBus.AttachmentPlugin.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1;net461 + netcoreapp3.1 ServiceBus.AttachmentPlugin.Tests false true diff --git a/src/ServiceBus.AttachmentPlugin.Tests/When_receiving_message.cs b/src/ServiceBus.AttachmentPlugin.Tests/When_receiving_message.cs index 74d6a04..d77f306 100644 --- a/src/ServiceBus.AttachmentPlugin.Tests/When_receiving_message.cs +++ b/src/ServiceBus.AttachmentPlugin.Tests/When_receiving_message.cs @@ -19,11 +19,13 @@ public async Task Should_throw_exception_with_blob_path_for_blob_that_cant_be_fo }; var sendingPlugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration( - connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"))); + connectionString: AzureStorageEmulatorFixture.TestingStorageAccountConnectionString, + containerName: "attachments")); await sendingPlugin.BeforeMessageSend(message); var receivingPlugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration( - connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments-wrong-containers"))); + connectionString: AzureStorageEmulatorFixture.TestingStorageAccountConnectionString, + containerName: "attachments-wrong-containers")); var exception = await Assert.ThrowsAsync(() => receivingPlugin.AfterMessageReceive(message)); Assert.Contains("attachments-wrong-containers", actualString: exception.Message); diff --git a/src/ServiceBus.AttachmentPlugin.Tests/When_sending_message_using_connection_string.cs b/src/ServiceBus.AttachmentPlugin.Tests/When_sending_message_using_connection_string.cs index 3b21673..5be36cd 100644 --- a/src/ServiceBus.AttachmentPlugin.Tests/When_sending_message_using_connection_string.cs +++ b/src/ServiceBus.AttachmentPlugin.Tests/When_sending_message_using_connection_string.cs @@ -27,7 +27,8 @@ public async Task Should_nullify_body_when_body_should_be_sent_as_attachment() MessageId = Guid.NewGuid().ToString(), }; var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration( - connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob:"attachment-id")); + connectionString: AzureStorageEmulatorFixture.TestingStorageAccountConnectionString, + messagePropertyToIdentifyAttachmentBlob:"attachment-id")); var result = await plugin.BeforeMessageSend(message); Assert.Null(result.Body); @@ -45,7 +46,8 @@ public async Task Should_leave_body_as_is_for_message_not_exceeding_max_size() TimeToLive = TimeSpan.FromHours(1) }; var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration( - connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob:"attachment-id", + connectionString: AzureStorageEmulatorFixture.TestingStorageAccountConnectionString, + messagePropertyToIdentifyAttachmentBlob:"attachment-id", messageMaxSizeReachedCriteria:msg => msg.Body.Length > 100)); var result = await plugin.BeforeMessageSend(message); @@ -64,7 +66,8 @@ public async Task Should_set_valid_until_datetime_on_blob_same_as_message_TTL() TimeToLive = TimeSpan.FromHours(1) }; var dateTimeNowUtc = new DateTime(2017, 1, 2); - var configuration = new AzureStorageAttachmentConfiguration(connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), + var configuration = new AzureStorageAttachmentConfiguration( + connectionString: AzureStorageEmulatorFixture.TestingStorageAccountConnectionString, messagePropertyToIdentifyAttachmentBlob:"attachment-id"); AzureStorageAttachment.DateTimeFunc = () => dateTimeNowUtc; var plugin = new AzureStorageAttachment(configuration); @@ -85,7 +88,8 @@ public async Task Should_receive_it_using_connection_string() var bytes = Encoding.UTF8.GetBytes(payload); var message = new Message(bytes); var configuration = new AzureStorageAttachmentConfiguration( - connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); + connectionString: AzureStorageEmulatorFixture.TestingStorageAccountConnectionString, + messagePropertyToIdentifyAttachmentBlob: "attachment-id"); var plugin = new AzureStorageAttachment(configuration); await plugin.BeforeMessageSend(message); @@ -104,7 +108,8 @@ public async Task Should_not_reupload_blob_if_one_is_already_assigned() var bytes = Encoding.UTF8.GetBytes(payload); var message = new Message(bytes); var configuration = new AzureStorageAttachmentConfiguration( - connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); + connectionString: AzureStorageEmulatorFixture.TestingStorageAccountConnectionString, + messagePropertyToIdentifyAttachmentBlob: "attachment-id"); var plugin = new AzureStorageAttachment(configuration); @@ -127,7 +132,8 @@ public async Task Should_not_set_sas_uri_by_default() MessageId = Guid.NewGuid().ToString(), }; var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration( - connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id")); + connectionString: AzureStorageEmulatorFixture.TestingStorageAccountConnectionString, + messagePropertyToIdentifyAttachmentBlob: "attachment-id")); var result = await plugin.BeforeMessageSend(message); Assert.Null(result.Body); @@ -142,7 +148,8 @@ public async Task Should_be_able_to_receive_using_container_sas() var bytes = Encoding.UTF8.GetBytes(payload); var message = new Message(bytes); var configuration = new AzureStorageAttachmentConfiguration( - connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); + connectionString: AzureStorageEmulatorFixture.TestingStorageAccountConnectionString, + messagePropertyToIdentifyAttachmentBlob: "attachment-id"); var plugin = new AzureStorageAttachment(configuration); await plugin.BeforeMessageSend(message); @@ -150,7 +157,7 @@ public async Task Should_be_able_to_receive_using_container_sas() Assert.Null(message.Body); var credentials = await AzureStorageEmulatorFixture.GetContainerSas("attachments"); - var receiveConfiguration = new AzureStorageAttachmentConfiguration(credentials, AzureStorageEmulatorFixture.GetBlobEndpoint(), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); + var receiveConfiguration = new AzureStorageAttachmentConfiguration(credentials, messagePropertyToIdentifyAttachmentBlob: "attachment-id"); var receivePlugin = new AzureStorageAttachment(receiveConfiguration); @@ -168,7 +175,9 @@ public async Task Should_be_able_to_send_if_container_was_not_found() var bytes = Encoding.UTF8.GetBytes(payload); var message = new Message(bytes); var configuration = new AzureStorageAttachmentConfiguration( - connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments-that-didnt-exist"), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); + connectionString: AzureStorageEmulatorFixture.TestingStorageAccountConnectionString, + containerName: "attachments-that-didnt-exist", + messagePropertyToIdentifyAttachmentBlob: "attachment-id"); var plugin = new AzureStorageAttachment(configuration); await plugin.BeforeMessageSend(message); diff --git a/src/ServiceBus.AttachmentPlugin.Tests/When_using_message_extensions.cs b/src/ServiceBus.AttachmentPlugin.Tests/When_using_message_extensions.cs index 965c7ef..af7d373 100644 --- a/src/ServiceBus.AttachmentPlugin.Tests/When_using_message_extensions.cs +++ b/src/ServiceBus.AttachmentPlugin.Tests/When_using_message_extensions.cs @@ -23,7 +23,8 @@ public async Task Should_send_and_receive_with_configuration() var bytes = Encoding.UTF8.GetBytes(payload); var message = new Message(bytes); var configuration = new AzureStorageAttachmentConfiguration( - connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id"); + connectionString: AzureStorageEmulatorFixture.TestingStorageAccountConnectionString, + messagePropertyToIdentifyAttachmentBlob: "attachment-id"); await message.UploadAzureStorageAttachment(configuration); @@ -41,7 +42,8 @@ public async Task Should_send_and_receive_with_default_sas_uri_property() var bytes = Encoding.UTF8.GetBytes(payload); var message = new Message(bytes); var configuration = new AzureStorageAttachmentConfiguration( - connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id") + connectionString: AzureStorageEmulatorFixture.TestingStorageAccountConnectionString, + messagePropertyToIdentifyAttachmentBlob: "attachment-id") .WithBlobSasUri(); await message.UploadAzureStorageAttachment(configuration); @@ -61,7 +63,8 @@ public async Task Should_send_and_receive_with_custom_sas_uri_property() var message = new Message(bytes); var customSasUri = "$custom-attachment.sas.uri"; var configuration = new AzureStorageAttachmentConfiguration( - connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id") + connectionString: AzureStorageEmulatorFixture.TestingStorageAccountConnectionString, + messagePropertyToIdentifyAttachmentBlob: "attachment-id") .WithBlobSasUri(customSasUri); await message.UploadAzureStorageAttachment(configuration); @@ -80,7 +83,8 @@ public async Task Should_be_able_to_override_blob_name_and_receive_message_paylo var bytes = Encoding.UTF8.GetBytes(payload); var message = new Message(bytes) { MessageId = Guid.NewGuid().ToString("N") }; var configuration = new AzureStorageAttachmentConfiguration( - connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments")); + connectionString: AzureStorageEmulatorFixture.TestingStorageAccountConnectionString, + messagePropertyToIdentifyAttachmentBlob: "attachment-id"); configuration.OverrideBlobName(msg => $"test/{msg.MessageId}"); @@ -104,7 +108,8 @@ public async Task Should_set_body_to_the_value_provided_by_body_replacer_overrid MessageId = Guid.NewGuid().ToString(), }; var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration( - connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id") + connectionString: AzureStorageEmulatorFixture.TestingStorageAccountConnectionString, + messagePropertyToIdentifyAttachmentBlob: "attachment-id") .OverrideBody(msg => Array.Empty())); var result = await plugin.BeforeMessageSend(message); @@ -122,7 +127,8 @@ public async Task Should_set_body_to_null_if_body_replacer_override_is_not_provi MessageId = Guid.NewGuid().ToString(), }; var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration( - connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), messagePropertyToIdentifyAttachmentBlob: "attachment-id")); + AzureStorageEmulatorFixture.TestingStorageAccountConnectionString, + messagePropertyToIdentifyAttachmentBlob: "attachment-id")); var result = await plugin.BeforeMessageSend(message); Assert.Null(result.Body); diff --git a/src/ServiceBus.AttachmentPlugin.Tests/When_using_receive_only_plugin.cs b/src/ServiceBus.AttachmentPlugin.Tests/When_using_receive_only_plugin.cs index d9cd46c..9896738 100644 --- a/src/ServiceBus.AttachmentPlugin.Tests/When_using_receive_only_plugin.cs +++ b/src/ServiceBus.AttachmentPlugin.Tests/When_using_receive_only_plugin.cs @@ -31,8 +31,7 @@ public async Task Should_download_attachment_using_provided_blob_sas_uri() var plugin = new AzureStorageAttachment(new AzureStorageAttachmentConfiguration( connectionString: await AzureStorageEmulatorFixture.GetContainerSas("attachments"), containerName: "attachments-sendonly", - messagePropertyToIdentifyAttachmentBlob: - "attachment-id") + messagePropertyToIdentifyAttachmentBlob: "attachment-id") .WithBlobSasUri( sasTokenValidationTime: TimeSpan.FromHours(4), messagePropertyToIdentifySasUri: "mySasUriProperty")); diff --git a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfiguration.cs b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfiguration.cs index caacb9d..615f37a 100644 --- a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfiguration.cs +++ b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfiguration.cs @@ -1,6 +1,7 @@ namespace Microsoft.Azure.ServiceBus { using System; + using global::Azure.Core; using global::Azure.Storage; /// Runtime configuration for Azure Storage Attachment plugin. @@ -25,12 +26,11 @@ public AzureStorageAttachmentConfiguration( MessagePropertyToIdentifyAttachmentBlob = messagePropertyToIdentifyAttachmentBlob; MessageMaxSizeReachedCriteria = GetMessageMaxSizeReachedCriteria(messageMaxSizeReachedCriteria); - // TODO: figure out - StorageSharedKeyCredentials = new StorageSharedKeyCredential("devstoreaccount1", "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="); - // TODO: figure out - BlobEndpoint = EnsureBlobEndpointEndsWithSlash("http://127.0.0.1:10000/devstoreaccount1"); + var storageConnectionString = ReflectedAzureStorageConnectionString.Create(connectionString); + StorageSharedKeyCredentials = storageConnectionString.Credentials; + BlobEndpoint = EnsureBlobEndpointEndsWithSlash(storageConnectionString.BlobEndpoint); } - + /// Constructor to create new configuration object. /// Container name is not required as it's included in the SharedAccessSignature. /// @@ -52,17 +52,17 @@ public AzureStorageAttachmentConfiguration( StorageSharedKeyCredentials = storageSharedKeyCredentials; // TODO: figure out - BlobEndpoint = EnsureBlobEndpointEndsWithSlash(blobEndpoint); + BlobEndpoint = EnsureBlobEndpointEndsWithSlash(new Uri(blobEndpoint)); ContainerName = containerName; MessagePropertyToIdentifyAttachmentBlob = messagePropertyToIdentifyAttachmentBlob; MessageMaxSizeReachedCriteria = GetMessageMaxSizeReachedCriteria(messageMaxSizeReachedCriteria); } - static Uri EnsureBlobEndpointEndsWithSlash(string blobEndpoint) + static Uri EnsureBlobEndpointEndsWithSlash(Uri blobEndpoint) { - if (blobEndpoint.EndsWith("/", StringComparison.OrdinalIgnoreCase)) + if (blobEndpoint.Segments[^1].Equals("/", StringComparison.OrdinalIgnoreCase)) { - return new Uri(blobEndpoint); + return blobEndpoint; } // Emulator blob endpoint doesn't end with slash @@ -115,27 +115,28 @@ Func GetMessageMaxSizeReachedCriteria(Func? messag }; } - internal IProvideStorageConnectionString? ConnectionStringProvider { get; } - + //////////////////// + internal Uri? BlobEndpoint { get; } + internal string ContainerName { get; } + + internal StorageSharedKeyCredential? StorageSharedKeyCredentials { get; } + // or + internal TokenCredential? TokenCredential { get; } + + internal Func BlobNameResolver { get; set; } = message => Guid.NewGuid().ToString(); - internal string? MessagePropertyForBlobSasUri { get; set; } - - internal TimeSpan? BlobSasTokenValidationTime { get; set; } - + internal Func BodyReplacer { get; set; } = message => null; + internal string MessagePropertyToIdentifyAttachmentBlob { get; } internal Func MessageMaxSizeReachedCriteria { get; } + + //////?? + internal string? MessagePropertyForBlobSasUri { get; set; } - internal StorageSharedKeyCredential? StorageSharedKeyCredentials { get; } - - internal Uri? BlobEndpoint { get; } - - // internal bool UsingSas => StorageSharedKeyCredentials.IsSAS; - - - internal Func BlobNameResolver { get; set; } = message => Guid.NewGuid().ToString(); + internal TimeSpan? BlobSasTokenValidationTime { get; set; } - internal Func BodyReplacer { get; set; } = message => null; + internal bool UsingSas { get; } = false; } } \ No newline at end of file diff --git a/src/ServiceBus.AttachmentPlugin/ReceiveOnlyAzureStorageAttachment.cs b/src/ServiceBus.AttachmentPlugin/ReceiveOnlyAzureStorageAttachment.cs index a790d33..e2bd0ed 100644 --- a/src/ServiceBus.AttachmentPlugin/ReceiveOnlyAzureStorageAttachment.cs +++ b/src/ServiceBus.AttachmentPlugin/ReceiveOnlyAzureStorageAttachment.cs @@ -29,7 +29,8 @@ public override async Task AfterMessageReceive(Message message) return message; } - var blob = new BlockBlobClient(new Uri(userProperties[messagePropertyToIdentifySasUri].ToString())); + var blob = new BlockBlobClient(new Uri(userProperties[messagePropertyToIdentifySasUri].ToString() + ?? throw new Exception($"Value of {nameof(messagePropertyToIdentifySasUri)} `{messagePropertyToIdentifySasUri}` was null."))); try { await blob.GetPropertiesAsync().ConfigureAwait(false); @@ -38,7 +39,8 @@ public override async Task AfterMessageReceive(Message message) { throw new Exception($"Blob with name '{blob.Name}' under container '{blob.BlobContainerName}' cannot be found.", exception); } - using var memory = new MemoryStream(); + + await using var memory = new MemoryStream(); await blob.DownloadToAsync(memory).ConfigureAwait(false); message.Body = memory.ToArray(); return message; diff --git a/src/ServiceBus.AttachmentPlugin/ReflectedAzureStorageConnectionString.cs b/src/ServiceBus.AttachmentPlugin/ReflectedAzureStorageConnectionString.cs new file mode 100644 index 0000000..cd7fd8a --- /dev/null +++ b/src/ServiceBus.AttachmentPlugin/ReflectedAzureStorageConnectionString.cs @@ -0,0 +1,47 @@ +namespace Microsoft.Azure.ServiceBus +{ + using System; + using global::Azure.Storage; + + internal class ReflectedAzureStorageConnectionString + { + static Type storageConnectionStringType = Type.GetType("Azure.Storage.StorageConnectionString, Azure.Storage.Common") ?? throw new Exception("`Azure.Storage.StorageConnectionString, Azure.Storage.Common` is not found"); + object storageConnectionStringObject; + + public static ReflectedAzureStorageConnectionString Create(string connectionString) + { + var storageConnectionStringObject = storageConnectionStringType! + .GetMethod("Parse", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)! + .Invoke(null, new object[] { connectionString }); + + if (storageConnectionStringObject is null) + { + throw new Exception($"Failed to initialize {nameof(ReflectedAzureStorageConnectionString)}."); + } + + return new ReflectedAzureStorageConnectionString(storageConnectionStringObject!); + } + + public StorageSharedKeyCredential Credentials => Get("Credentials"); + public string EndpointSuffix => Get("EndpointSuffix"); + public Uri BlobEndpoint => Get("BlobEndpoint"); + public bool IsDevStoreAccount => Get("IsDevStoreAccount"); + public (Uri primary, Uri secondary) BlobStorageUri => Get<(Uri, Uri)>("BlobStorageUri"); + + ReflectedAzureStorageConnectionString(object storageConnectionStringObject) + { + this.storageConnectionStringObject = storageConnectionStringObject; + } + + T Get(string propertyName, bool isPublic = false) + { + var propertyInfo = storageConnectionStringType.GetProperty(propertyName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic); + if (propertyInfo is null) + { + throw new Exception($"{storageConnectionStringType.FullName} does not contain a property '{propertyName}'."); + } + + return (T)propertyInfo.GetValue(storageConnectionStringObject)!; + } + } +} \ No newline at end of file diff --git a/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj b/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj index 830ed3d..04b797a 100644 --- a/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj +++ b/src/ServiceBus.AttachmentPlugin/ServiceBus.AttachmentPlugin.csproj @@ -4,7 +4,7 @@ Microsoft Azure ServiceBus attachment plugin 6.2.0 Sean Feldman - netstandard2.0;net461 + netcoreapp3.1 Azure;Service Bus;ServiceBus;.NET;AMQP;IoT;Queue;Topic;Attachment;Plugin project-icon.png LICENSE.md From d679d282965a6debe4f33cd8fe51b4cd7c5e55fa Mon Sep 17 00:00:00 2001 From: Sean Feldman Date: Thu, 25 Jun 2020 01:04:16 -0600 Subject: [PATCH 12/12] Reflecting StorageConnectionString to partse connection strings Signed-off-by: Sean Feldman --- ...torageAttachmentConfigurationExtensions.cs | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfigurationExtensions.cs b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfigurationExtensions.cs index 9cb598e..4f4786e 100644 --- a/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfigurationExtensions.cs +++ b/src/ServiceBus.AttachmentPlugin/AzureStorageAttachmentConfigurationExtensions.cs @@ -21,22 +21,21 @@ public static AzureStorageAttachmentConfiguration WithBlobSasUri( string messagePropertyToIdentifySasUri = AzureStorageAttachmentConfigurationConstants.DefaultMessagePropertyToIdentitySasUri, TimeSpan? sasTokenValidationTime = null) { - throw new NotImplementedException(); - // if (azureStorageAttachmentConfiguration.UsingSas) - // { - // throw new Exception("Invalid configuration: .WithBlobSasUri() requires account shared key and cannot be used with service/container Shared Access Signature."); - // } - // - // if (sasTokenValidationTime == null) - // { - // sasTokenValidationTime = DefaultSasTokenValidationTime; - // } - // Guard.AgainstNegativeOrZeroTimeSpan(nameof(sasTokenValidationTime), sasTokenValidationTime); - // - // azureStorageAttachmentConfiguration.MessagePropertyForBlobSasUri = messagePropertyToIdentifySasUri; - // azureStorageAttachmentConfiguration.BlobSasTokenValidationTime = sasTokenValidationTime.Value; - // - // return azureStorageAttachmentConfiguration; + if (azureStorageAttachmentConfiguration.UsingSas) + { + throw new Exception("Invalid configuration: .WithBlobSasUri() requires account shared key and cannot be used with service/container Shared Access Signature."); + } + + if (sasTokenValidationTime == null) + { + sasTokenValidationTime = DefaultSasTokenValidationTime; + } + Guard.AgainstNegativeOrZeroTimeSpan(nameof(sasTokenValidationTime), sasTokenValidationTime); + + azureStorageAttachmentConfiguration.MessagePropertyForBlobSasUri = messagePropertyToIdentifySasUri; + azureStorageAttachmentConfiguration.BlobSasTokenValidationTime = sasTokenValidationTime.Value; + + return azureStorageAttachmentConfiguration; } ///