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

[Storage] DataLakeFileClient RequestConditions validation #22150

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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<ProjectReference Include="..\..\Azure.Storage.Blobs\src\Azure.Storage.Blobs.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="$(AzureCoreSharedSources)AppContextSwitchHelper.cs" LinkBase="SharedCore" />
Copy link
Contributor

Choose a reason for hiding this comment

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

SharedCore or Shared\Core ? some links below are not consistent.

Copy link
Member Author

Choose a reason for hiding this comment

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

All the other links besides AuthorizationChallengeParser are SharedCore

<Compile Include="$(AzureCoreSharedSources)AuthorizationChallengeParser.cs" LinkBase="Shared\Core" />
<Compile Include="$(AzureCoreSharedSources)AzureResourceProviderNamespaceAttribute.cs" LinkBase="SharedCore" />
<Compile Include="$(AzureCoreSharedSources)AzureSasCredentialSynchronousPolicy.cs" LinkBase="SharedCore" />
Expand Down
128 changes: 128 additions & 0 deletions sdk/storage/Azure.Storage.Files.DataLake/src/DataLakeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -831,5 +831,133 @@ internal static BlobStaticWebsite ToBlobStaticWebsite(this DataLakeStaticWebsite
DefaultIndexDocumentPath = dataLakeStaticWebsite.DefaultIndexDocumentPath
};
}

#region ValidateConditionsNotPresent
internal static void ValidateConditionsNotPresent(
this RequestConditions requestConditions,
DataLakeRequestConditionProperty invalidConditions,
string operationName,
string parameterName)
{
if (AppContextSwitchHelper.GetConfigValue(
Constants.DisableRequestConditionsValidationSwitchName,
Constants.DisableRequestConditionsValidationEnvVar))
{
return;
}

if (requestConditions == null)
{
return;
}

List<string> invalidList = null;
requestConditions.ValidateConditionsNotPresent(
invalidConditions,
ref invalidList);

if (invalidList?.Count > 0)
{
string unsupportedString = string.Join(", ", invalidList);
throw new ArgumentException(
$"{operationName} does not support the {unsupportedString} condition(s).",
parameterName);
}
}

internal static void ValidateConditionsNotPresent(
this DataLakeRequestConditions requestConditions,
DataLakeRequestConditionProperty invalidConditions,
string operationName,
string parameterName)
{
if (AppContextSwitchHelper.GetConfigValue(
Constants.DisableRequestConditionsValidationSwitchName,
Constants.DisableRequestConditionsValidationEnvVar))
{
return;
}

if (requestConditions == null)
{
return;
}

List<string> invalidList = null;
requestConditions.ValidateConditionsNotPresent(
invalidConditions,
ref invalidList);

if (invalidList?.Count > 0)
{
string unsupportedString = string.Join(", ", invalidList);
throw new ArgumentException(
$"{operationName} does not support the {unsupportedString} condition(s).",
parameterName);
}
}

internal static void ValidateConditionsNotPresent(
this RequestConditions requestConditions,
DataLakeRequestConditionProperty invalidConditions,
ref List<string> invalidList)
{
if (requestConditions == null)
{
return;
}

if ((invalidConditions & DataLakeRequestConditionProperty.IfModifiedSince) == DataLakeRequestConditionProperty.IfModifiedSince
&& requestConditions.IfModifiedSince != null)
{
invalidList ??= new List<string>();
invalidList.Add(nameof(BlobRequestConditions.IfModifiedSince));
}

if ((invalidConditions & DataLakeRequestConditionProperty.IfUnmodifiedSince) == DataLakeRequestConditionProperty.IfUnmodifiedSince
&& requestConditions.IfUnmodifiedSince != null)
{
invalidList ??= new List<string>();
invalidList.Add(nameof(BlobRequestConditions.IfUnmodifiedSince));
}

if ((invalidConditions & DataLakeRequestConditionProperty.IfMatch) == DataLakeRequestConditionProperty.IfMatch
&& requestConditions.IfMatch != null)
{
invalidList ??= new List<string>();
invalidList.Add(nameof(BlobRequestConditions.IfMatch));
}

if ((invalidConditions & DataLakeRequestConditionProperty.IfNoneMatch) == DataLakeRequestConditionProperty.IfNoneMatch
&& requestConditions.IfNoneMatch != null)
{
invalidList ??= new List<string>();
invalidList.Add(nameof(BlobRequestConditions.IfNoneMatch));
}
}

internal static void ValidateConditionsNotPresent(
this DataLakeRequestConditions requestConditions,
DataLakeRequestConditionProperty invalidConditions,
ref List<string> invalidList)
{
if (requestConditions == null)
{
return;
}

// Validate RequestConditions
((RequestConditions)requestConditions).ValidateConditionsNotPresent(
invalidConditions, ref invalidList);

// Validate BlobRequestConditions specific conditions.
if ((invalidConditions & DataLakeRequestConditionProperty.LeaseId) == DataLakeRequestConditionProperty.LeaseId
&& requestConditions.LeaseId != null)
{
invalidList ??= new List<string>();
invalidList.Add(nameof(BlobRequestConditions.LeaseId));
}
}
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,14 @@ public virtual Response Delete(
{
DiagnosticScope scope = ClientConfiguration.ClientDiagnostics.CreateScope($"{nameof(DataLakeFileSystemClient)}.{nameof(Delete)}");

conditions.ValidateConditionsNotPresent(
invalidConditions:
DataLakeRequestConditionProperty.TagConditions
| DataLakeRequestConditionProperty.IfMatch
| DataLakeRequestConditionProperty.IfNoneMatch,
operationName: nameof(DataLakeFileClient.Delete),
parameterName: nameof(conditions));

try
{
scope.Start();
Expand Down Expand Up @@ -914,6 +922,14 @@ public virtual async Task<Response> DeleteAsync(
{
DiagnosticScope scope = ClientConfiguration.ClientDiagnostics.CreateScope($"{nameof(DataLakeFileSystemClient)}.{nameof(Delete)}");

conditions.ValidateConditionsNotPresent(
invalidConditions:
DataLakeRequestConditionProperty.TagConditions
| DataLakeRequestConditionProperty.IfMatch
| DataLakeRequestConditionProperty.IfNoneMatch,
operationName: nameof(DataLakeFileClient.Delete),
parameterName: nameof(conditions));

try
{
scope.Start();
Expand Down Expand Up @@ -966,6 +982,14 @@ public virtual Response<bool> DeleteIfExists(
{
DiagnosticScope scope = ClientConfiguration.ClientDiagnostics.CreateScope($"{nameof(DataLakeFileSystemClient)}.{nameof(DeleteIfExists)}");

conditions.ValidateConditionsNotPresent(
invalidConditions:
DataLakeRequestConditionProperty.TagConditions
| DataLakeRequestConditionProperty.IfMatch
| DataLakeRequestConditionProperty.IfNoneMatch,
operationName: nameof(DataLakeFileClient.Delete),
parameterName: nameof(conditions));

try
{
scope.Start();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;

namespace Azure.Storage.Files.DataLake.Models
{
[Flags]
internal enum DataLakeRequestConditionProperty
{
None = 0,
LeaseId = 1,
TagConditions = 2,
IfModifiedSince = 4,
IfUnmodifiedSince = 8,
IfMatch = 16,
IfNoneMatch = 32,
}
}
Loading