Skip to content

Commit

Permalink
[Storage] [DataMovement] Added back default constructors for Resource…
Browse files Browse the repository at this point in the history
…Providers for Anonymous Access (Azure#48438)

* Added back default constructors for StorageResourceProviders to support Anonymous acces

* Removed comment about static call for FromClient

* Rerecord test to avoid 500 recordings in net-pullrequest

* Trying to rerecord different tests

* bumped version to 12.1.0
  • Loading branch information
amnguye authored Feb 26, 2025
1 parent d7721df commit 9161f2f
Show file tree
Hide file tree
Showing 18 changed files with 227 additions and 9 deletions.
5 changes: 4 additions & 1 deletion sdk/storage/Azure.Storage.DataMovement.Blobs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Release History

## 12.0.1 (Unreleased)
## 12.1.0 (Unreleased)

### Features Added
- Added support for anonymous access by adding a default constructor for `BlobsStorageResourceProvider`.

### Bugs Fixed
- Fixed an issue that would prevent transfers of large files (>200 GiB) for certain destination resource types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public BlobContainerClientTransferOptions() { }
}
public partial class BlobsStorageResourceProvider : Azure.Storage.DataMovement.StorageResourceProvider
{
public BlobsStorageResourceProvider() { }
public BlobsStorageResourceProvider(Azure.AzureSasCredential credential) { }
public BlobsStorageResourceProvider(Azure.Core.TokenCredential credential) { }
public BlobsStorageResourceProvider(Azure.Storage.StorageSharedKeyCredential credential) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public BlobContainerClientTransferOptions() { }
}
public partial class BlobsStorageResourceProvider : Azure.Storage.DataMovement.StorageResourceProvider
{
public BlobsStorageResourceProvider() { }
public BlobsStorageResourceProvider(Azure.AzureSasCredential credential) { }
public BlobsStorageResourceProvider(Azure.Core.TokenCredential credential) { }
public BlobsStorageResourceProvider(Azure.Storage.StorageSharedKeyCredential credential) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public BlobContainerClientTransferOptions() { }
}
public partial class BlobsStorageResourceProvider : Azure.Storage.DataMovement.StorageResourceProvider
{
public BlobsStorageResourceProvider() { }
public BlobsStorageResourceProvider(Azure.AzureSasCredential credential) { }
public BlobsStorageResourceProvider(Azure.Core.TokenCredential credential) { }
public BlobsStorageResourceProvider(Azure.Storage.StorageSharedKeyCredential credential) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>
<PropertyGroup>
<AssemblyTitle>Microsoft Azure.Storage.DataMovement.Blobs client library</AssemblyTitle>
<Version>12.0.1</Version>
<Version>12.1.0</Version>
<!--The ApiCompatVersion is managed automatically and should not generally be modified manually.-->
<ApiCompatVersion>12.0.0</ApiCompatVersion>
<DefineConstants>BlobDataMovementSDK;$(DefineConstants)</DefineConstants>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ private enum CredentialType
private readonly Func<Uri, CancellationToken, ValueTask<StorageSharedKeyCredential>> _getStorageSharedKeyCredential;
private readonly Func<Uri, CancellationToken, ValueTask<AzureSasCredential>> _getAzureSasCredential;

/// <summary>
/// <para>
/// Constructs this provider to use no credentials when making a new Blob Storage
/// <see cref="StorageResource"/>.
/// </para>
/// <para>
/// This instance will NOT use any credential when constructing the underlying
/// Azure.Storage.Blobs client, e.g. <see cref="BlockBlobClient(Uri, BlobClientOptions)"/>.
/// This is for the purpose of either anonymous access when constructing the client.
/// </para>
/// </summary>
public BlobsStorageResourceProvider()
{
_credentialType = CredentialType.None;
}

/// <summary>
/// <para>
/// Constructs this provider to use the given credential when making a new Blob Storage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class BlobsStorageResourceProviderTests
{
public enum CredType
{
None,
SharedKey,
Token,
Sas
Expand Down Expand Up @@ -53,6 +54,11 @@ private void AssertCredPresent(BlobClientConfiguration clientConfig, CredType cr
Assert.IsNull(clientConfig.TokenCredential);
Assert.IsNotNull(clientConfig.SasCredential);
break;
case CredType.None:
Assert.IsNull(clientConfig.SharedKeyCredential);
Assert.IsNull(clientConfig.TokenCredential);
Assert.IsNull(clientConfig.SasCredential);
break;
default:
throw new ArgumentException("No assertion support for cred type " + credType.ToString());
}
Expand Down Expand Up @@ -102,7 +108,7 @@ private void AssertBlobStorageResourceType(
[Combinatorial]
public async Task FromContainer(
[Values(true, false)] bool withPrefix,
[Values(CredType.SharedKey, CredType.Token, CredType.Sas)] CredType credType)
[Values(CredType.SharedKey, CredType.Token, CredType.Sas, CredType.None)] CredType credType)
{
const string containerName = "mycontainer";
const string prefix = "my/prefix";
Expand All @@ -114,6 +120,7 @@ public async Task FromContainer(
CredType.SharedKey => new(mockCreds.SharedKey.Object),
CredType.Token => new(mockCreds.Token.Object),
CredType.Sas => new(mockCreds.Sas.Object),
CredType.None => new(),
_ => throw new ArgumentException("Bad cred type"),
};
BlobStorageResourceContainer resource = await provider.FromContainerAsync(uri) as BlobStorageResourceContainer;
Expand All @@ -128,7 +135,7 @@ public async Task FromContainer(
[Combinatorial]
public async Task FromBlob(
[Values(BlobType.Unspecified, BlobType.Block, BlobType.Page, BlobType.Append)] BlobType blobType,
[Values(CredType.SharedKey, CredType.Token, CredType.Sas)] CredType credType)
[Values(CredType.SharedKey, CredType.Token, CredType.Sas, CredType.None)] CredType credType)
{
const string containerName = "mycontainer";
const string blobName = "my/blob.txt";
Expand All @@ -140,6 +147,7 @@ public async Task FromBlob(
CredType.SharedKey => new(mockCreds.SharedKey.Object),
CredType.Token => new(mockCreds.Token.Object),
CredType.Sas => new(mockCreds.Sas.Object),
CredType.None => new(),
_ => throw new ArgumentException("Bad cred type"),
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Release History

## 12.0.1 (Unreleased)
## 12.1.0 (Unreleased)

### Features added
- Added support for anonymous access by adding a default constructor for `ShareFilesStorageResourceProvider`.

### Bugs Fixed
- Fixed an issue that would prevent transfers of large files (>200 GiB) for certain destination resource types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public ShareDirectoryClientTransferOptions() { }
}
public partial class ShareFilesStorageResourceProvider : Azure.Storage.DataMovement.StorageResourceProvider
{
public ShareFilesStorageResourceProvider() { }
public ShareFilesStorageResourceProvider(Azure.AzureSasCredential credential) { }
public ShareFilesStorageResourceProvider(Azure.Core.TokenCredential credential) { }
public ShareFilesStorageResourceProvider(Azure.Storage.StorageSharedKeyCredential credential) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public ShareDirectoryClientTransferOptions() { }
}
public partial class ShareFilesStorageResourceProvider : Azure.Storage.DataMovement.StorageResourceProvider
{
public ShareFilesStorageResourceProvider() { }
public ShareFilesStorageResourceProvider(Azure.AzureSasCredential credential) { }
public ShareFilesStorageResourceProvider(Azure.Core.TokenCredential credential) { }
public ShareFilesStorageResourceProvider(Azure.Storage.StorageSharedKeyCredential credential) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public ShareDirectoryClientTransferOptions() { }
}
public partial class ShareFilesStorageResourceProvider : Azure.Storage.DataMovement.StorageResourceProvider
{
public ShareFilesStorageResourceProvider() { }
public ShareFilesStorageResourceProvider(Azure.AzureSasCredential credential) { }
public ShareFilesStorageResourceProvider(Azure.Core.TokenCredential credential) { }
public ShareFilesStorageResourceProvider(Azure.Storage.StorageSharedKeyCredential credential) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "net",
"TagPrefix": "net/storage/Azure.Storage.DataMovement.Files.Shares",
"Tag": "net/storage/Azure.Storage.DataMovement.Files.Shares_88bb7c0e4a"
"Tag": "net/storage/Azure.Storage.DataMovement.Files.Shares_ed3312eb08"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>
<PropertyGroup>
<AssemblyTitle>Microsoft Azure.Storage.DataMovement.Files.Shares client library</AssemblyTitle>
<Version>12.0.1</Version>
<Version>12.1.0</Version>
<!--The ApiCompatVersion is managed automatically and should not generally be modified manually.-->
<ApiCompatVersion>12.0.0</ApiCompatVersion>
<DefineConstants>ShareDataMovementSDK;$(DefineConstants)</DefineConstants>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ private enum CredentialType
private readonly Func<Uri, CancellationToken, ValueTask<AzureSasCredential>> _getAzureSasCredential;

#region ctors
/// <summary>
/// <para>
/// Constructs this provider to use no credentials when making a new Blob Storage
/// <see cref="StorageResource"/>.
/// </para>
/// <para>
/// This instance will NOT use any credential when constructing the underlying
/// Azure.Storage.Blobs client, e.g. <see cref="ShareFileClient(Uri, ShareClientOptions)"/>.
/// This is for the purpose of either anonymous access when constructing the client.
/// </para>
/// </summary>
public ShareFilesStorageResourceProvider()
{
_credentialType = CredentialType.None;
}

/// <summary>
/// <para>
/// Constructs this provider to use the given credential when making a new File Share
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

extern alias DMShare;
extern alias BaseShares;

using System;
using Azure.Core;
using BaseShares::Azure.Storage.Files.Shares;
using Azure.Storage.Tests;
using DMShare::Azure.Storage.DataMovement.Files.Shares;
using Moq;
using NUnit.Framework;
using System.Threading.Tasks;
using System.Threading;

namespace Azure.Storage.DataMovement.Files.Shares.Tests
{
public class ShareFilesStorageResourceProviderTests
{
public enum CredType
{
None,
SharedKey,
Token,
Sas
}

private void AssertCredPresent(ShareClientConfiguration clientConfig, CredType credType)
{
Assert.IsNotNull(clientConfig);
switch (credType)
{
case CredType.SharedKey:
Assert.IsNotNull(clientConfig.SharedKeyCredential);
Assert.IsNull(clientConfig.TokenCredential);
Assert.IsNull(clientConfig.SasCredential);
break;
case CredType.Token:
Assert.IsNull(clientConfig.SharedKeyCredential);
Assert.IsNotNull(clientConfig.TokenCredential);
Assert.IsNull(clientConfig.SasCredential);
break;
case CredType.Sas:
Assert.IsNull(clientConfig.SharedKeyCredential);
Assert.IsNull(clientConfig.TokenCredential);
Assert.IsNotNull(clientConfig.SasCredential);
break;
case CredType.None:
Assert.IsNull(clientConfig.SharedKeyCredential);
Assert.IsNull(clientConfig.TokenCredential);
Assert.IsNull(clientConfig.SasCredential);
break;
default:
throw new ArgumentException("No assertion support for cred type " + credType.ToString());
}
}

private (Mock<StorageSharedKeyCredential> SharedKey, Mock<TokenCredential> Token, Mock<AzureSasCredential> Sas) GetMockCreds()
{
return (
new Mock<StorageSharedKeyCredential>("myaccount", new Random().NextBase64(64)),
new Mock<TokenCredential>(),
new Mock<AzureSasCredential>("mysignature"));
}

[Test]
[Combinatorial]
public async Task FromDirectoryAsync(
[Values(true, false)] bool withPrefix,
[Values(CredType.SharedKey, CredType.Token, CredType.Sas, CredType.None)] CredType credType)
{
const string containerName = "mycontainer";
const string prefix = "my/prefix";
Uri uri = new Uri($"https://myaccount.blob.core.windows.net/{containerName}" + (withPrefix ? $"/{prefix}" : ""));
(Mock<StorageSharedKeyCredential> SharedKey, Mock<TokenCredential> Token, Mock<AzureSasCredential> Sas) mockCreds = GetMockCreds();

ShareFilesStorageResourceProvider provider = credType switch
{
CredType.SharedKey => new(mockCreds.SharedKey.Object),
CredType.Token => new(mockCreds.Token.Object),
CredType.Sas => new(mockCreds.Sas.Object),
CredType.None => new(),
_ => throw new ArgumentException("Bad cred type"),
};
ShareDirectoryStorageResourceContainer resource = await provider.FromDirectoryAsync(uri) as ShareDirectoryStorageResourceContainer;

Assert.IsNotNull(resource);
Assert.IsNotNull(resource.ShareDirectoryClient);
Assert.AreEqual(uri, resource.Uri);
Assert.AreEqual(uri, resource.ShareDirectoryClient.Uri);
AssertCredPresent(resource.ShareDirectoryClient.ClientConfiguration, credType);
}

[Test]
[Combinatorial]
public async Task FromFileAsync(
[Values(CredType.SharedKey, CredType.Token, CredType.Sas, CredType.None)] CredType credType)
{
const string shareName = "mycontainer";
const string directoryName = "directoryName";
const string fileName = "file.txt";
Uri uri = new Uri($"https://myaccount.blob.core.windows.net/{shareName}/{directoryName}/{fileName}");
(Mock<StorageSharedKeyCredential> SharedKey, Mock<TokenCredential> Token, Mock<AzureSasCredential> Sas) mockCreds = GetMockCreds();

ShareFilesStorageResourceProvider provider = credType switch
{
CredType.SharedKey => new(mockCreds.SharedKey.Object),
CredType.Token => new(mockCreds.Token.Object),
CredType.Sas => new(mockCreds.Sas.Object),
CredType.None => new(),
_ => throw new ArgumentException("Bad cred type"),
};
StorageResource resource = await provider.FromFileAsync(uri);

Assert.IsNotNull(resource);
ShareFileStorageResource fileResource = resource as ShareFileStorageResource;
Assert.IsNotNull(fileResource.ShareFileClient);
Assert.AreEqual(uri, resource.Uri);
Assert.AreEqual(uri, fileResource.ShareFileClient.Uri);
AssertCredPresent(fileResource.ShareFileClient.ClientConfiguration, credType);
}

[Test]
public async Task CredentialCallback(
[Values(CredType.SharedKey, CredType.Sas)] CredType credType)
{
const string shareName = "mycontainer";
const string directoryName = "directoryName";
const string fileName = "file.txt";
Uri uri = new Uri($"https://myaccount.blob.core.windows.net/{shareName}/{directoryName}/{fileName}");
(Mock<StorageSharedKeyCredential> SharedKey, Mock<TokenCredential> Token, Mock<AzureSasCredential> Sas) mockCreds = GetMockCreds();

ValueTask<StorageSharedKeyCredential> GetSharedKeyCredential(Uri uri, CancellationToken cancellationToken)
{
return new ValueTask<StorageSharedKeyCredential>(mockCreds.SharedKey.Object);
}
ValueTask<AzureSasCredential> GetSasCredential(Uri _, CancellationToken cancellationToken)
{
return new ValueTask<AzureSasCredential>(mockCreds.Sas.Object);
}

ShareFilesStorageResourceProvider provider = credType switch
{
CredType.SharedKey => new(GetSharedKeyCredential),
CredType.Sas => new(GetSasCredential),
_ => throw new ArgumentException("Bad cred type"),
};
StorageResource resource = await provider.FromFileAsync(uri);

Assert.IsNotNull(resource);
Assert.AreEqual(uri, resource.Uri);
ShareFileStorageResource fileResource = resource as ShareFileStorageResource;
Assert.IsNotNull(fileResource.ShareFileClient);
Assert.AreEqual(uri, resource.Uri);
Assert.AreEqual(uri, fileResource.ShareFileClient.Uri);
AssertCredPresent(fileResource.ShareFileClient.ClientConfiguration, credType);
}
}
}
2 changes: 1 addition & 1 deletion sdk/storage/Azure.Storage.DataMovement/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Release History

## 12.0.1 (Unreleased)
## 12.1.0 (Unreleased)

### Bugs Fixed
- Fixed an issue that would prevent transfers of large files (>200 GiB) for certain destination resource types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>
<PropertyGroup>
<AssemblyTitle>Microsoft Azure.Storage.DataMovement client library</AssemblyTitle>
<Version>12.0.1</Version>
<Version>12.1.0</Version>
<!--The ApiCompatVersion is managed automatically and should not generally be modified manually.-->
<ApiCompatVersion>12.0.0</ApiCompatVersion>
<DefineConstants>DataMovementSDK;$(DefineConstants)</DefineConstants>
Expand Down
6 changes: 6 additions & 0 deletions sdk/storage/Azure.Storage.Files.Shares/src/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
"012ea67d2479751a0b8c087a4185541b851bd8b16f8d91b840e51b1cb0ba6fe647997e57429265" +
"e85ef62d565db50a69ae1647d54d7bd855e4db3d8a91510e5bcbd0edfbbecaa20a7bd9ae74593d" +
"aa7b11b4")]
[assembly: InternalsVisibleTo("Azure.Storage.DataMovement.Files.Shares.Tests, PublicKey=" +
"0024000004800000940000000602000000240000525341310004000001000100d15ddcb2968829" +
"5338af4b7686603fe614abd555e09efba8fb88ee09e1f7b1ccaeed2e8f823fa9eef3fdd60217fc" +
"012ea67d2479751a0b8c087a4185541b851bd8b16f8d91b840e51b1cb0ba6fe647997e57429265" +
"e85ef62d565db50a69ae1647d54d7bd855e4db3d8a91510e5bcbd0edfbbecaa20a7bd9ae74593d" +
"aa7b11b4")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
[assembly: Azure.Core.AzureResourceProviderNamespace("Microsoft.Storage")]

Expand Down

0 comments on commit 9161f2f

Please sign in to comment.