Skip to content

Commit

Permalink
Add DefaultAzureIdentity support (#155)
Browse files Browse the repository at this point in the history
* Add DefaultAzureIdentity support for blob snapshot

* Add documentation.
  • Loading branch information
Arkatufus authored Apr 15, 2021
1 parent 9c79c55 commit e0f14d0
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ akka.persistence.snapshot-store.azure-blob-store.connection-string = "Your Azure
akka.persistence.snapshot-store.azure-blob-store.container-name = "Your container name"
```

### Configuring snapshots Blob Storage using DefaultAzureCredential

You can use `Azure.Identity` `DefaultAzureCredential` to configure the resource by using `AzureBlobSnapshotSetup`. When using `DefaultAzureCredential`, the HOCON 'connection-string' setting is ignored.

Example:
```
var blobStorageSetup = AzureBlobSnapshotSetup.Create(
new Uri("https://{account_name}.blob.core.windows.net"), // This is the blob service URI
new DefaultAzureCredential() // You can pass a DefaultAzureCredentialOption here.
// https://docs.microsoft.com/en-us/dotnet/api/azure.identity.defaultazurecredential?view=azure-dotnet
);
var bootstrap = BootstrapSetup.Create().And(blobStorageSetup);
var system = ActorSystem.Create("actorSystem", bootstrap);
```

## Using the plugin in local development environment

You can use this plugin with Azure Storage Emulator in a local development environment by setting the development flag in the configuration file:
Expand Down
1 change: 1 addition & 0 deletions src/Akka.Persistence.Azure/Akka.Persistence.Azure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<ItemGroup>
<PackageReference Include="Akka.Persistence.Query" Version="$(AkkaVersion)" />
<PackageReference Include="Azure.Identity" Version="1.3.0" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.8.1" />
<PackageReference Include="Microsoft.Azure.Cosmos.Table" Version="1.0.8" />
<PackageReference Include="System.Linq.Async" Version="5.0.0" />
Expand Down
49 changes: 49 additions & 0 deletions src/Akka.Persistence.Azure/Snapshot/AzureBlobSnapshotSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Text;
using Akka.Actor.Setup;
using Azure.Identity;
using Azure.Storage.Blobs;

namespace Akka.Persistence.Azure.Snapshot
{
public class AzureBlobSnapshotSetup : Setup
{
/// <summary>
/// Create a new <see cref="AzureBlobSnapshotSetup"/>
/// </summary>
/// <param name="serviceUri">
/// A <see cref="Uri"/> referencing the blob service.
/// This is likely to be similar to "https://{account_name}.blob.core.windows.net".
/// </param>
/// <param name="defaultAzureCredential">
/// The <see cref="DefaultAzureCredential"/> used to sign requests.
/// </param>
/// <param name="blobClientOptions">
/// Optional client options that define the transport pipeline policies for authentication,
/// retries, etc., that are applied to every request.
/// </param>
/// <returns>A new <see cref="AzureBlobSnapshotSetup"/> instance</returns>
public static AzureBlobSnapshotSetup Create(
Uri serviceUri,
DefaultAzureCredential defaultAzureCredential,
BlobClientOptions blobClientOptions = default)
=> new AzureBlobSnapshotSetup(serviceUri, defaultAzureCredential, blobClientOptions);

private AzureBlobSnapshotSetup(
Uri serviceUri,
DefaultAzureCredential azureCredential,
BlobClientOptions blobClientOptions)
{
ServiceUri = serviceUri;
DefaultAzureCredential = azureCredential;
BlobClientOptions = blobClientOptions;
}

public Uri ServiceUri { get; }

public DefaultAzureCredential DefaultAzureCredential { get; }

public BlobClientOptions BlobClientOptions { get; }
}
}
22 changes: 19 additions & 3 deletions src/Akka.Persistence.Azure/Snapshot/AzureBlobSnapshotStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,25 @@ public AzureBlobSnapshotStore(Config config = null)
? AzurePersistence.Get(Context.System).BlobSettings
: AzureBlobSnapshotStoreSettings.Create(config);

_serviceClient = _settings.Development ?
new BlobServiceClient("UseDevelopmentStorage=true") :
new BlobServiceClient(_settings.ConnectionString);
if (_settings.Development)
{
_serviceClient = new BlobServiceClient("UseDevelopmentStorage=true");
}
else
{
var credentialSetup = Context.System.Settings.Setup.Get<AzureBlobSnapshotSetup>();
if (credentialSetup.HasValue)
{
_serviceClient = new BlobServiceClient(
credentialSetup.Value.ServiceUri,
credentialSetup.Value.DefaultAzureCredential,
credentialSetup.Value.BlobClientOptions);
}
else
{
_serviceClient = new BlobServiceClient(_settings.ConnectionString);
}
}

_containerClient = new Lazy<BlobContainerClient>(() => InitCloudStorage(5).Result);
}
Expand Down

0 comments on commit e0f14d0

Please sign in to comment.