Skip to content

Commit 0307922

Browse files
authored
[Storage] DataLakeFileClient RequestConditions validation (Azure#22150)
1 parent a67bc76 commit 0307922

File tree

39 files changed

+590
-0
lines changed

39 files changed

+590
-0
lines changed

sdk/storage/Azure.Storage.Files.DataLake/src/Azure.Storage.Files.DataLake.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<ProjectReference Include="..\..\Azure.Storage.Blobs\src\Azure.Storage.Blobs.csproj" />
2828
</ItemGroup>
2929
<ItemGroup>
30+
<Compile Include="$(AzureCoreSharedSources)AppContextSwitchHelper.cs" LinkBase="SharedCore" />
3031
<Compile Include="$(AzureCoreSharedSources)AuthorizationChallengeParser.cs" LinkBase="Shared\Core" />
3132
<Compile Include="$(AzureCoreSharedSources)AzureResourceProviderNamespaceAttribute.cs" LinkBase="SharedCore" />
3233
<Compile Include="$(AzureCoreSharedSources)AzureSasCredentialSynchronousPolicy.cs" LinkBase="SharedCore" />

sdk/storage/Azure.Storage.Files.DataLake/src/DataLakeExtensions.cs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,5 +831,133 @@ internal static BlobStaticWebsite ToBlobStaticWebsite(this DataLakeStaticWebsite
831831
DefaultIndexDocumentPath = dataLakeStaticWebsite.DefaultIndexDocumentPath
832832
};
833833
}
834+
835+
#region ValidateConditionsNotPresent
836+
internal static void ValidateConditionsNotPresent(
837+
this RequestConditions requestConditions,
838+
DataLakeRequestConditionProperty invalidConditions,
839+
string operationName,
840+
string parameterName)
841+
{
842+
if (AppContextSwitchHelper.GetConfigValue(
843+
Constants.DisableRequestConditionsValidationSwitchName,
844+
Constants.DisableRequestConditionsValidationEnvVar))
845+
{
846+
return;
847+
}
848+
849+
if (requestConditions == null)
850+
{
851+
return;
852+
}
853+
854+
List<string> invalidList = null;
855+
requestConditions.ValidateConditionsNotPresent(
856+
invalidConditions,
857+
ref invalidList);
858+
859+
if (invalidList?.Count > 0)
860+
{
861+
string unsupportedString = string.Join(", ", invalidList);
862+
throw new ArgumentException(
863+
$"{operationName} does not support the {unsupportedString} condition(s).",
864+
parameterName);
865+
}
866+
}
867+
868+
internal static void ValidateConditionsNotPresent(
869+
this DataLakeRequestConditions requestConditions,
870+
DataLakeRequestConditionProperty invalidConditions,
871+
string operationName,
872+
string parameterName)
873+
{
874+
if (AppContextSwitchHelper.GetConfigValue(
875+
Constants.DisableRequestConditionsValidationSwitchName,
876+
Constants.DisableRequestConditionsValidationEnvVar))
877+
{
878+
return;
879+
}
880+
881+
if (requestConditions == null)
882+
{
883+
return;
884+
}
885+
886+
List<string> invalidList = null;
887+
requestConditions.ValidateConditionsNotPresent(
888+
invalidConditions,
889+
ref invalidList);
890+
891+
if (invalidList?.Count > 0)
892+
{
893+
string unsupportedString = string.Join(", ", invalidList);
894+
throw new ArgumentException(
895+
$"{operationName} does not support the {unsupportedString} condition(s).",
896+
parameterName);
897+
}
898+
}
899+
900+
internal static void ValidateConditionsNotPresent(
901+
this RequestConditions requestConditions,
902+
DataLakeRequestConditionProperty invalidConditions,
903+
ref List<string> invalidList)
904+
{
905+
if (requestConditions == null)
906+
{
907+
return;
908+
}
909+
910+
if ((invalidConditions & DataLakeRequestConditionProperty.IfModifiedSince) == DataLakeRequestConditionProperty.IfModifiedSince
911+
&& requestConditions.IfModifiedSince != null)
912+
{
913+
invalidList ??= new List<string>();
914+
invalidList.Add(nameof(BlobRequestConditions.IfModifiedSince));
915+
}
916+
917+
if ((invalidConditions & DataLakeRequestConditionProperty.IfUnmodifiedSince) == DataLakeRequestConditionProperty.IfUnmodifiedSince
918+
&& requestConditions.IfUnmodifiedSince != null)
919+
{
920+
invalidList ??= new List<string>();
921+
invalidList.Add(nameof(BlobRequestConditions.IfUnmodifiedSince));
922+
}
923+
924+
if ((invalidConditions & DataLakeRequestConditionProperty.IfMatch) == DataLakeRequestConditionProperty.IfMatch
925+
&& requestConditions.IfMatch != null)
926+
{
927+
invalidList ??= new List<string>();
928+
invalidList.Add(nameof(BlobRequestConditions.IfMatch));
929+
}
930+
931+
if ((invalidConditions & DataLakeRequestConditionProperty.IfNoneMatch) == DataLakeRequestConditionProperty.IfNoneMatch
932+
&& requestConditions.IfNoneMatch != null)
933+
{
934+
invalidList ??= new List<string>();
935+
invalidList.Add(nameof(BlobRequestConditions.IfNoneMatch));
936+
}
937+
}
938+
939+
internal static void ValidateConditionsNotPresent(
940+
this DataLakeRequestConditions requestConditions,
941+
DataLakeRequestConditionProperty invalidConditions,
942+
ref List<string> invalidList)
943+
{
944+
if (requestConditions == null)
945+
{
946+
return;
947+
}
948+
949+
// Validate RequestConditions
950+
((RequestConditions)requestConditions).ValidateConditionsNotPresent(
951+
invalidConditions, ref invalidList);
952+
953+
// Validate BlobRequestConditions specific conditions.
954+
if ((invalidConditions & DataLakeRequestConditionProperty.LeaseId) == DataLakeRequestConditionProperty.LeaseId
955+
&& requestConditions.LeaseId != null)
956+
{
957+
invalidList ??= new List<string>();
958+
invalidList.Add(nameof(BlobRequestConditions.LeaseId));
959+
}
960+
}
961+
#endregion
834962
}
835963
}

sdk/storage/Azure.Storage.Files.DataLake/src/DataLakeFileSystemClient.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,14 @@ public virtual Response Delete(
865865
{
866866
DiagnosticScope scope = ClientConfiguration.ClientDiagnostics.CreateScope($"{nameof(DataLakeFileSystemClient)}.{nameof(Delete)}");
867867

868+
conditions.ValidateConditionsNotPresent(
869+
invalidConditions:
870+
DataLakeRequestConditionProperty.TagConditions
871+
| DataLakeRequestConditionProperty.IfMatch
872+
| DataLakeRequestConditionProperty.IfNoneMatch,
873+
operationName: nameof(DataLakeFileClient.Delete),
874+
parameterName: nameof(conditions));
875+
868876
try
869877
{
870878
scope.Start();
@@ -914,6 +922,14 @@ public virtual async Task<Response> DeleteAsync(
914922
{
915923
DiagnosticScope scope = ClientConfiguration.ClientDiagnostics.CreateScope($"{nameof(DataLakeFileSystemClient)}.{nameof(Delete)}");
916924

925+
conditions.ValidateConditionsNotPresent(
926+
invalidConditions:
927+
DataLakeRequestConditionProperty.TagConditions
928+
| DataLakeRequestConditionProperty.IfMatch
929+
| DataLakeRequestConditionProperty.IfNoneMatch,
930+
operationName: nameof(DataLakeFileClient.Delete),
931+
parameterName: nameof(conditions));
932+
917933
try
918934
{
919935
scope.Start();
@@ -966,6 +982,14 @@ public virtual Response<bool> DeleteIfExists(
966982
{
967983
DiagnosticScope scope = ClientConfiguration.ClientDiagnostics.CreateScope($"{nameof(DataLakeFileSystemClient)}.{nameof(DeleteIfExists)}");
968984

985+
conditions.ValidateConditionsNotPresent(
986+
invalidConditions:
987+
DataLakeRequestConditionProperty.TagConditions
988+
| DataLakeRequestConditionProperty.IfMatch
989+
| DataLakeRequestConditionProperty.IfNoneMatch,
990+
operationName: nameof(DataLakeFileClient.Delete),
991+
parameterName: nameof(conditions));
992+
969993
try
970994
{
971995
scope.Start();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
6+
namespace Azure.Storage.Files.DataLake.Models
7+
{
8+
[Flags]
9+
internal enum DataLakeRequestConditionProperty
10+
{
11+
None = 0,
12+
LeaseId = 1,
13+
TagConditions = 2,
14+
IfModifiedSince = 4,
15+
IfUnmodifiedSince = 8,
16+
IfMatch = 16,
17+
IfNoneMatch = 32,
18+
}
19+
}

0 commit comments

Comments
 (0)