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

Features/storage/blob lease client request conditions validation #22111

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
60 changes: 30 additions & 30 deletions sdk/storage/Azure.Storage.Blobs/src/BlobLeaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,12 @@ private async Task<Response<BlobLease>> AcquireInternal(
}
else
{
if (conditions?.IfMatch != default || conditions?.IfNoneMatch != default)
{
throw BlobErrors.BlobConditionsMustBeDefault(
nameof(conditions.IfMatch),
nameof(conditions.IfNoneMatch));
}
conditions.ValidateConditionsNotPresent(
invalidConditions:
BlobRequestConditionProperty.IfMatch
| BlobRequestConditionProperty.IfNoneMatch,
operationName: nameof(BlobLeaseClient.Acquire),
parameterName: nameof(conditions));

ResponseWithHeaders<ContainerAcquireLeaseHeaders> containerClientResponse;

Expand Down Expand Up @@ -561,12 +561,12 @@ private async Task<Response<BlobLease>> RenewInternal(
}
else
{
if (conditions?.IfMatch != default || conditions?.IfNoneMatch != default)
{
throw BlobErrors.BlobConditionsMustBeDefault(
nameof(conditions.IfMatch),
nameof(conditions.IfNoneMatch));
}
conditions.ValidateConditionsNotPresent(
invalidConditions:
BlobRequestConditionProperty.IfMatch
| BlobRequestConditionProperty.IfNoneMatch,
operationName: nameof(BlobLeaseClient.Release),
parameterName: nameof(conditions));

ResponseWithHeaders<ContainerRenewLeaseHeaders> containerClientResponse;

Expand Down Expand Up @@ -784,12 +784,12 @@ public virtual async Task<Response<ReleasedObjectInfo>> ReleaseInternal(
}
else
{
if (conditions?.IfMatch != default || conditions?.IfNoneMatch != default)
{
throw BlobErrors.BlobConditionsMustBeDefault(
nameof(RequestConditions.IfMatch),
nameof(RequestConditions.IfNoneMatch));
}
conditions.ValidateConditionsNotPresent(
invalidConditions:
BlobRequestConditionProperty.IfMatch
| BlobRequestConditionProperty.IfNoneMatch,
operationName: nameof(BlobLeaseClient.Release),
parameterName: nameof(conditions));

ResponseWithHeaders<ContainerReleaseLeaseHeaders> response;

Expand Down Expand Up @@ -1009,12 +1009,12 @@ private async Task<Response<BlobLease>> ChangeInternal(
}
else
{
if (conditions?.IfMatch != default || conditions?.IfNoneMatch != default)
{
throw BlobErrors.BlobConditionsMustBeDefault(
nameof(conditions.IfMatch),
nameof(conditions.IfNoneMatch));
}
conditions.ValidateConditionsNotPresent(
invalidConditions:
BlobRequestConditionProperty.IfMatch
| BlobRequestConditionProperty.IfNoneMatch,
operationName: nameof(BlobLeaseClient.Change),
parameterName: nameof(conditions));

ResponseWithHeaders<ContainerChangeLeaseHeaders> containerClientResponse;

Expand Down Expand Up @@ -1293,12 +1293,12 @@ private async Task<Response<BlobLease>> BreakInternal(
}
else
{
if (conditions?.IfMatch != default || conditions?.IfNoneMatch != default)
{
throw BlobErrors.BlobConditionsMustBeDefault(
nameof(conditions.IfMatch),
nameof(conditions.IfNoneMatch));
}
conditions.ValidateConditionsNotPresent(
invalidConditions:
BlobRequestConditionProperty.IfMatch
| BlobRequestConditionProperty.IfNoneMatch,
operationName: nameof(BlobLeaseClient.Break),
parameterName: nameof(conditions));

ResponseWithHeaders<ContainerBreakLeaseHeaders> response;

Expand Down
173 changes: 173 additions & 0 deletions sdk/storage/Azure.Storage.Blobs/tests/ContainerClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,42 @@ public async Task AcquireLeaseAsync()
await container.DeleteIfExistsAsync(conditions: new BlobRequestConditions { LeaseId = response.Value.LeaseId });
}

[RecordedTest]
[TestCase(nameof(RequestConditions.IfMatch))]
[TestCase(nameof(RequestConditions.IfNoneMatch))]
public async Task AcquireLeaseAsync_InvalidRequestConditions(string invalidCondition)
{
// Arrange
Uri uri = new Uri("https://www.doesntmatter.com");
BlobContainerClient containerClient = new BlobContainerClient(uri, GetOptions());
string id = Recording.Random.NewGuid().ToString();
TimeSpan duration = TimeSpan.FromSeconds(15);
BlobLeaseClient leaseClient = InstrumentClient(containerClient.GetBlobLeaseClient(id));

RequestConditions conditions = new RequestConditions();

switch (invalidCondition)
{
case nameof(RequestConditions.IfMatch):
conditions.IfMatch = new ETag();
break;
case nameof(RequestConditions.IfNoneMatch):
conditions.IfNoneMatch = new ETag();
break;
}

// Act
await TestHelper.AssertExpectedExceptionAsync<ArgumentException>(
leaseClient.AcquireAsync(
duration,
conditions),
e =>
{
Assert.IsTrue(e.Message.Contains($"Acquire does not support the {invalidCondition} condition(s)."));
Assert.IsTrue(e.Message.Contains("conditions"));
});
}

[RecordedTest]
public async Task AcquireLeaseAsync_ErrorDurationTooLarge()
{
Expand Down Expand Up @@ -1577,6 +1613,40 @@ public async Task RenewLeaseAsync()
await container.DeleteIfExistsAsync(conditions: new BlobRequestConditions { LeaseId = renewResponse.Value.LeaseId });
}

[RecordedTest]
[TestCase(nameof(RequestConditions.IfMatch))]
[TestCase(nameof(RequestConditions.IfNoneMatch))]
public async Task RenewLeaseAsync_InvalidRequestConditions(string invalidCondition)
{
// Arrange
Uri uri = new Uri("https://www.doesntmatter.com");
BlobContainerClient containerClient = new BlobContainerClient(uri, GetOptions());
string id = Recording.Random.NewGuid().ToString();
BlobLeaseClient leaseClient = InstrumentClient(containerClient.GetBlobLeaseClient(id));

RequestConditions conditions = new RequestConditions();

switch (invalidCondition)
{
case nameof(RequestConditions.IfMatch):
conditions.IfMatch = new ETag();
break;
case nameof(RequestConditions.IfNoneMatch):
conditions.IfNoneMatch = new ETag();
break;
}

// Act
await TestHelper.AssertExpectedExceptionAsync<ArgumentException>(
leaseClient.RenewAsync(
conditions: conditions),
e =>
{
Assert.IsTrue(e.Message.Contains($"Release does not support the {invalidCondition} condition(s)."));
Assert.IsTrue(e.Message.Contains("conditions"));
});
}

[RecordedTest]
public async Task RenewLeaseAsync_Error()
{
Expand Down Expand Up @@ -1671,6 +1741,40 @@ public async Task ReleaseLeaseAsync()
Assert.AreEqual(LeaseState.Available, response.Value.LeaseState);
}

[RecordedTest]
[TestCase(nameof(RequestConditions.IfMatch))]
[TestCase(nameof(RequestConditions.IfNoneMatch))]
public async Task ReleaseLeaseAsync_InvalidRequestConditions(string invalidCondition)
{
// Arrange
Uri uri = new Uri("https://www.doesntmatter.com");
BlobContainerClient containerClient = new BlobContainerClient(uri, GetOptions());
string id = Recording.Random.NewGuid().ToString();
BlobLeaseClient leaseClient = InstrumentClient(containerClient.GetBlobLeaseClient(id));

RequestConditions conditions = new RequestConditions();

switch (invalidCondition)
{
case nameof(RequestConditions.IfMatch):
conditions.IfMatch = new ETag();
break;
case nameof(RequestConditions.IfNoneMatch):
conditions.IfNoneMatch = new ETag();
break;
}

// Act
await TestHelper.AssertExpectedExceptionAsync<ArgumentException>(
leaseClient.ReleaseAsync(
conditions: conditions),
e =>
{
Assert.IsTrue(e.Message.Contains($"Release does not support the {invalidCondition} condition(s)."));
Assert.IsTrue(e.Message.Contains("conditions"));
});
}

[RecordedTest]
public async Task ReleaseLeaseAsync_Error()
{
Expand Down Expand Up @@ -1762,6 +1866,40 @@ public async Task BreakLeaseAsync()
Assert.AreEqual(LeaseState.Broken, response.Value.LeaseState);
}

[RecordedTest]
[TestCase(nameof(RequestConditions.IfMatch))]
[TestCase(nameof(RequestConditions.IfNoneMatch))]
public async Task BreakLeaseAsync_InvalidRequestConditions(string invalidCondition)
{
// Arrange
Uri uri = new Uri("https://www.doesntmatter.com");
BlobContainerClient containerClient = new BlobContainerClient(uri, GetOptions());
string id = Recording.Random.NewGuid().ToString();
BlobLeaseClient leaseClient = InstrumentClient(containerClient.GetBlobLeaseClient(id));

RequestConditions conditions = new RequestConditions();

switch (invalidCondition)
{
case nameof(RequestConditions.IfMatch):
conditions.IfMatch = new ETag();
break;
case nameof(RequestConditions.IfNoneMatch):
conditions.IfNoneMatch = new ETag();
break;
}

// Act
await TestHelper.AssertExpectedExceptionAsync<ArgumentException>(
leaseClient.BreakAsync(
conditions: conditions),
e =>
{
Assert.IsTrue(e.Message.Contains($"Break does not support the {invalidCondition} condition(s)."));
Assert.IsTrue(e.Message.Contains("conditions"));
});
}

[RecordedTest]
public async Task BreakLeaseAsync_Error()
{
Expand Down Expand Up @@ -1861,6 +1999,41 @@ public async Task ChangeLeaseAsync()
await InstrumentClient(test.Container.GetBlobLeaseClient(changeResponse.Value.LeaseId)).ReleaseAsync();
}

[RecordedTest]
[TestCase(nameof(RequestConditions.IfMatch))]
[TestCase(nameof(RequestConditions.IfNoneMatch))]
public async Task ChangeLeaseAsync_InvalidRequestConditions(string invalidCondition)
{
// Arrange
Uri uri = new Uri("https://www.doesntmatter.com");
BlobContainerClient containerClient = new BlobContainerClient(uri, GetOptions());
string id = Recording.Random.NewGuid().ToString();
BlobLeaseClient leaseClient = InstrumentClient(containerClient.GetBlobLeaseClient(id));

RequestConditions conditions = new RequestConditions();

switch (invalidCondition)
{
case nameof(RequestConditions.IfMatch):
conditions.IfMatch = new ETag();
break;
case nameof(RequestConditions.IfNoneMatch):
conditions.IfNoneMatch = new ETag();
break;
}

// Act
await TestHelper.AssertExpectedExceptionAsync<ArgumentException>(
leaseClient.ChangeAsync(
id,
conditions: conditions),
e =>
{
Assert.IsTrue(e.Message.Contains($"Change does not support the {invalidCondition} condition(s)."));
Assert.IsTrue(e.Message.Contains("conditions"));
});
}

[RecordedTest]
public async Task ChangeLeaseAsync_Error()
{
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.

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.

Loading