Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added request conditions validation to BlobBaseClient.SetImmutability… #2

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions sdk/storage/Azure.Storage.Blobs/src/BlobBaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5000,6 +5000,13 @@ private async Task<Response<BlobImmutabilityPolicy>> SetImmutabilityPolicyIntern
throw new ArgumentException($"{nameof(immutabilityPolicy.PolicyMode)} must be {BlobImmutabilityPolicyMode.Locked} or {BlobImmutabilityPolicyMode.Unlocked}");
}

conditions.ValidateConditionsNotPresent(
BlobRequestConditionProperty.IfMatch
| BlobRequestConditionProperty.IfNoneMatch
| BlobRequestConditionProperty.IfModifiedSince
| BlobRequestConditionProperty.LeaseId
| BlobRequestConditionProperty.TagConditions);

try
{
scope.Start();
Expand Down
46 changes: 46 additions & 0 deletions sdk/storage/Azure.Storage.Blobs/src/BlobExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1373,5 +1373,51 @@ internal static BlobLegalHoldResult ToBlobLegalHoldInfo(this ResponseWithHeaders
};
}
#endregion

#region ValidateConditionsNotPresent
internal static void ValidateConditionsNotPresent(this BlobRequestConditions requestConditions, BlobRequestConditionProperty invalidConditions)
{
if (requestConditions == null)
{
return;
}

if ((invalidConditions & BlobRequestConditionProperty.LeaseId) == BlobRequestConditionProperty.LeaseId
&& requestConditions.LeaseId != null)
{
throw new ArgumentException($"{nameof(BlobRequestConditions.LeaseId)} is not applicable to this API.");
}

if ((invalidConditions & BlobRequestConditionProperty.TagConditions) == BlobRequestConditionProperty.TagConditions
&& requestConditions.TagConditions != null)
{
throw new ArgumentException($"{nameof(BlobRequestConditions.TagConditions)} is not applicable to this API.");
}

if ((invalidConditions & BlobRequestConditionProperty.IfModifiedSince) == BlobRequestConditionProperty.IfModifiedSince
&& requestConditions.IfModifiedSince != null)
{
throw new ArgumentException($"{nameof(BlobRequestConditions.IfModifiedSince)} is not applicable to this API.");
}

if ((invalidConditions & BlobRequestConditionProperty.IfUnmodifiedSince) == BlobRequestConditionProperty.IfUnmodifiedSince
&& requestConditions.IfUnmodifiedSince != null)
{
throw new ArgumentException($"{nameof(BlobRequestConditions.IfUnmodifiedSince)} is not applicable to this API.");
}

if ((invalidConditions & BlobRequestConditionProperty.IfMatch) == BlobRequestConditionProperty.IfMatch
&& requestConditions.IfMatch != null)
{
throw new ArgumentException($"{nameof(BlobRequestConditions.IfMatch)} is not applicable to this API.");
}

if ((invalidConditions & BlobRequestConditionProperty.IfNoneMatch) == BlobRequestConditionProperty.IfNoneMatch
&& requestConditions.IfNoneMatch != null)
{
throw new ArgumentException($"{nameof(BlobRequestConditions.IfNoneMatch)} is not applicable to this API.");
}
}
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Azure.Storage.Blobs.Models

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Models.Internal

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of our other internal models are actually in the Models.Internal namespace

{
internal enum BlobRequestConditionProperty
{
LeaseId = 1,
TagConditions = 2,
IfModifiedSince = 4,
IfUnmodifiedSince = 8,
IfMatch = 16,
IfNoneMatch = 32
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,53 @@ public async Task SetImmutibilityPolicyAsync_IfModifiedSince()
Assert.AreEqual(immutabilityPolicy.PolicyMode, response.Value.PolicyMode);
}

[Test]
[ServiceVersion(Min = BlobClientOptions.ServiceVersion.V2020_06_12)]
[TestCase(nameof(BlobRequestConditions.IfMatch))]
[TestCase(nameof(BlobRequestConditions.IfModifiedSince))]
[TestCase(nameof(BlobRequestConditions.IfNoneMatch))]
[TestCase(nameof(BlobRequestConditions.LeaseId))]
[TestCase(nameof(BlobRequestConditions.TagConditions))]
public async Task SetImmutibilityPolicyAsync_InvalidRequestConditions(string invalidCondition)
{
// Arrange
BlobBaseClient blobBaseClient = new BlobBaseClient(new Uri("https://www.doesntmatter.com"), GetOptions());

BlobImmutabilityPolicy immutabilityPolicy = new BlobImmutabilityPolicy
{
ExpiresOn = Recording.UtcNow.AddMinutes(5),
PolicyMode = BlobImmutabilityPolicyMode.Unlocked
};

BlobRequestConditions conditions = new BlobRequestConditions();

switch (invalidCondition)
{
case nameof(BlobRequestConditions.IfMatch):
conditions.IfMatch = new ETag();
break;
case nameof(BlobRequestConditions.IfModifiedSince):
conditions.IfModifiedSince = Recording.UtcNow.AddMinutes(5);
break;
case nameof(BlobRequestConditions.IfNoneMatch):
conditions.IfNoneMatch = new ETag();
break;
case nameof(BlobRequestConditions.LeaseId):
conditions.LeaseId = string.Empty;
break;
case nameof(BlobRequestConditions.TagConditions):
conditions.TagConditions = string.Empty;
break;
}

// Act
await TestHelper.AssertExpectedExceptionAsync<ArgumentException>(
blobBaseClient.SetImmutabilityPolicyAsync(
immutabilityPolicy,
conditions),
e => Assert.AreEqual($"{invalidCondition} is not applicable to this API.", e.Message));
}

[Test]
[ServiceVersion(Min = BlobClientOptions.ServiceVersion.V2020_06_12)]
[TestCase(AccountSasPermissions.All)]
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.