Skip to content

Commit

Permalink
add blob storage authentication with managed identity (#6636)
Browse files Browse the repository at this point in the history
Co-authored-by: JhontSouth <jhonatan.sandoval@southworks.com>
  • Loading branch information
ceciliaavila and JhontSouth authored May 23, 2023
1 parent 5549586 commit 48084a6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
40 changes: 40 additions & 0 deletions libraries/Microsoft.Bot.Builder.Azure.Blobs/BlobsStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading.Tasks;
using System.Web;
using Azure;
using Azure.Core;
using Azure.Storage;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
Expand Down Expand Up @@ -90,6 +91,45 @@ public BlobsStorage(string dataConnectionString, string containerName, StorageTr
_containerClient = new BlobContainerClient(dataConnectionString, containerName);
}

/// <summary>
/// Initializes a new instance of the <see cref="BlobsStorage"/> class.
/// </summary>
/// <param name="blobContainerUri">Azure blob storage container Uri.</param>
/// <param name="tokenCredential">The token credential to authenticate to the Azure storage.</param>
/// <param name="storageTransferOptions">Used for providing options for parallel transfers <see cref="StorageTransferOptions"/>.</param>
/// <param name="options">Client options that define the transport pipeline policies for authentication, retries, etc., that are applied to every request.</param>
/// <param name="jsonSerializer">If passing in a custom JsonSerializer, we recommend the following settings:
/// <para>jsonSerializer.TypeNameHandling = TypeNameHandling.None.</para>
/// <para>jsonSerializer.NullValueHandling = NullValueHandling.Include.</para>
/// <para>jsonSerializer.ContractResolver = new DefaultContractResolver().</para>
/// <para>jsonSerializer.SerializationBinder = new AllowedTypesSerializationBinder().</para>
/// </param>
public BlobsStorage(Uri blobContainerUri, TokenCredential tokenCredential, StorageTransferOptions storageTransferOptions, BlobClientOptions options = default, JsonSerializer jsonSerializer = null)
{
if (blobContainerUri == null)
{
throw new ArgumentNullException(nameof(blobContainerUri));
}

if (tokenCredential == null)
{
throw new ArgumentNullException(nameof(tokenCredential));
}

_storageTransferOptions = storageTransferOptions;

_jsonSerializer = jsonSerializer ?? JsonSerializer.Create(new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All, // lgtm [cs/unsafe-type-name-handling]
MaxDepth = null,
});

// Triggers a check for the existence of the container
_checkForContainerExistence = 1;

_containerClient = new BlobContainerClient(blobContainerUri, tokenCredential, options);
}

/// <summary>
/// Initializes a new instance of the <see cref="BlobsStorage"/> class.
/// </summary>
Expand Down
23 changes: 23 additions & 0 deletions tests/Microsoft.Bot.Builder.Azure.Tests/BlobsStorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text;
using System.Threading;
using Azure;
using Azure.Core;
using Azure.Storage;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
Expand Down Expand Up @@ -44,6 +45,28 @@ public void ConstructorValidation()
Assert.Throws<ArgumentNullException>(() => new BlobsStorage(ConnectionString, string.Empty));
}

[Fact]
public void ConstructorWithTokenCredentialValidation()
{
var mockTokenCredential = new Moq.Mock<TokenCredential>();
var storageTransferOptions = new StorageTransferOptions();
var uri = new Uri("https://uritest.com");

// Should work.
_ = new BlobsStorage(
uri,
mockTokenCredential.Object,
storageTransferOptions,
new BlobClientOptions(),
JsonSerializer.Create(new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }));

// No blobContainerUri. Should throw.
Assert.Throws<ArgumentNullException>(() => new BlobsStorage(null, mockTokenCredential.Object, storageTransferOptions));

// No tokenCredential. Should throw.
Assert.Throws<ArgumentNullException>(() => new BlobsStorage(uri, null, storageTransferOptions));
}

[Fact]
public async void WriteAsyncValidation()
{
Expand Down

0 comments on commit 48084a6

Please sign in to comment.