Skip to content

Commit f2d06e5

Browse files
fix and test (Azure#48579)
1 parent 25b5b37 commit f2d06e5

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

sdk/storage/Azure.Storage.Blobs/src/BlobContainerClient.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,10 @@ internal BlobContainerClient(
407407
{
408408
_uri = containerUri;
409409
_clientConfiguration = clientConfiguration;
410-
_authenticationPolicy = StorageClientOptions.GetAuthenticationPolicy(_clientConfiguration.SharedKeyCredential);
410+
_authenticationPolicy = StorageClientOptions.GetAuthenticationPolicy(
411+
(object)_clientConfiguration.SharedKeyCredential ??
412+
(object)_clientConfiguration.TokenCredential ??
413+
(object)_clientConfiguration.SasCredential);
411414
_clientSideEncryption = clientSideEncryption?.Clone();
412415
_containerRestClient = BuildContainerRestClient(containerUri);
413416

sdk/storage/Azure.Storage.Blobs/tests/ContainerClientTests.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@
1111
using System.Threading;
1212
using System.Threading.Tasks;
1313
using Azure.Core;
14+
using Azure.Core.Pipeline;
1415
using Azure.Core.TestFramework;
1516
using Azure.Identity;
1617
using Azure.Storage.Blobs.Models;
1718
using Azure.Storage.Blobs.Specialized;
1819
using Azure.Storage.Blobs.Tests;
20+
using Azure.Storage.Common;
1921
using Azure.Storage.Sas;
2022
using Azure.Storage.Shared;
2123
using Azure.Storage.Test;
2224
using Azure.Storage.Test.Shared;
25+
using Azure.Storage.Tests;
2326
using Moq;
2427
using Moq.Protected;
2528
using NUnit.Framework;
@@ -441,6 +444,67 @@ public void ctor_BlobContainerClient_clientSideEncryptionOptions()
441444
Assert.NotNull(client.ClientSideEncryption);
442445
}
443446

447+
[Test]
448+
public void Ctor_FromConfig(
449+
[Values(
450+
StorageAuthType.None,
451+
StorageAuthType.StorageSharedKey,
452+
StorageAuthType.Token,
453+
StorageAuthType.Sas)] StorageAuthType authType)
454+
{
455+
StorageSharedKeyCredential sharedKeyCred =
456+
authType == StorageAuthType.StorageSharedKey ? new("", "") : null;
457+
TokenCredential tokenCred =
458+
authType == StorageAuthType.Token ? new DefaultAzureCredential() : null;
459+
AzureSasCredential sasCred =
460+
authType == StorageAuthType.Sas ? new("?foo=bar") : null;
461+
462+
BlobClientOptions options = new();
463+
BlobContainerClient container = new(
464+
new Uri("https://example.blob.core.windows.net"),
465+
new BlobClientConfiguration(
466+
options.Build(authType switch
467+
{
468+
StorageAuthType.StorageSharedKey => sharedKeyCred,
469+
StorageAuthType.Token => tokenCred,
470+
StorageAuthType.Sas => sasCred,
471+
_ => null,
472+
}),
473+
sharedKeyCred,
474+
tokenCred,
475+
sasCred,
476+
new ClientDiagnostics(options),
477+
_serviceVersion,
478+
customerProvidedKey: default,
479+
transferValidation: null,
480+
encryptionScope: null,
481+
trimBlobNameSlashes: default),
482+
null);
483+
484+
Assert.That(container.ClientConfiguration.SharedKeyCredential,
485+
authType == StorageAuthType.StorageSharedKey ? Is.EqualTo(sharedKeyCred) : Is.Null);
486+
Assert.That(container.ClientConfiguration.TokenCredential,
487+
authType == StorageAuthType.Token ? Is.EqualTo(tokenCred) : Is.Null);
488+
Assert.That(container.ClientConfiguration.SasCredential,
489+
authType == StorageAuthType.Sas ? Is.EqualTo(sasCred) : Is.Null);
490+
491+
switch (authType)
492+
{
493+
case StorageAuthType.None:
494+
Assert.That(container.AuthenticationPolicy, Is.Null);
495+
break;
496+
case StorageAuthType.StorageSharedKey:
497+
Assert.That(container.AuthenticationPolicy, Is.TypeOf<StorageSharedKeyPipelinePolicy>());
498+
break;
499+
case StorageAuthType.Token:
500+
Assert.That(container.AuthenticationPolicy, Is.TypeOf<StorageBearerTokenChallengeAuthorizationPolicy>());
501+
break;
502+
case StorageAuthType.Sas:
503+
Assert.That(container.AuthenticationPolicy, Is.TypeOf<AzureSasCredentialSynchronousPolicy>());
504+
break;
505+
}
506+
}
507+
444508
[RecordedTest]
445509
public async Task CreateAsync_WithSharedKey()
446510
{

sdk/storage/Azure.Storage.Common/src/Shared/StorageClientOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public static HttpPipelinePolicy GetAuthenticationPolicy(object credentials = nu
9292
return sharedKey.AsPolicy();
9393
case TokenCredential token:
9494
return token.AsPolicy(scope, options);
95+
case AzureSasCredential sas:
96+
return new AzureSasCredentialSynchronousPolicy(sas);
9597
default:
9698
throw Errors.InvalidCredentials(credentials.GetType().FullName);
9799
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
namespace Azure.Storage.Tests;
5+
6+
public enum StorageAuthType
7+
{
8+
None,
9+
StorageSharedKey,
10+
Token,
11+
Sas,
12+
}

0 commit comments

Comments
 (0)