Repository navigation
Expand file tree
/
Copy pathProtocolValidation.cs
More file actions
164 lines (152 loc) · 9.12 KB
/
Copy pathProtocolValidation.cs
File metadata and controls
164 lines (152 loc) · 9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
namespace KeyLoad.Core;
/// <summary>Executes ProtocolValidation operations against the caller's database context.</summary>
public static class ProtocolValidation
{
internal const int ProtocolValidationEmptyElementCount = 0;
internal const int PrincipalFormatMaximumGrants = 256;
internal const int PrincipalFormatMaximumFieldGrants = 256;
internal const int PrincipalFormatMaximumProjects = 256;
internal const string ProtocolValidationPrincipalScopeExceedsItsBudgetOrContainsAnInvalidGrantDetail = "The principal scope exceeds its budget or contains an invalid grant.";
internal const string ProtocolValidationMutationResourceAndCanonicalOwnerDifferDetail = "The mutation resource and canonical owner differ.";
internal const string ProtocolValidationNestedMutationEntryOrEnumValueIsInvalidDetail = "A nested mutation entry or enum value is invalid.";
internal const string ProtocolValidationMutationIsUnsupportedDetail = "The mutation is unsupported.";
internal static void ValidatePrincipalStructure(PrincipalRecord principal)
{
if (principal.Grants.IsDefault || principal.FieldGrants.IsDefault || principal.Projects.IsDefault || principal.Teams.IsDefault || principal.Grants.Length > PrincipalFormatMaximumGrants || principal.FieldGrants.Length > PrincipalFormatMaximumFieldGrants || principal.Teams.Length > PrincipalFormatMaximumProjects || principal.Projects.Length > PrincipalFormatMaximumProjects - principal.Teams.Length || principal.Grants.Any(grant => grant is null || (grant.Capabilities & ~Capability.All) != ProtocolValidationEmptyElementCount))
{
throw Errors.Fail(ErrorCode.Validation, ProtocolValidationPrincipalScopeExceedsItsBudgetOrContainsAnInvalidGrantDetail);
}
foreach (var grant in principal.Grants)
{
JsonData.Identifier(grant.Database);
JsonData.Identifier(grant.Resource);
}
foreach (var field in principal.FieldGrants)
{
JsonData.Identifier(field);
}
foreach (var project in principal.Projects)
{
JsonData.Identifier(project);
}
foreach (var team in principal.Teams)
{
JsonData.Identifier(team);
}
if (principal.OwnerId is { } owner)
{
JsonData.Identifier(owner);
}
}
internal static void ValidateMutationStructure(Mutation mutation)
{
if (mutation is null)
{
throw Errors.Fail(ErrorCode.Validation, MutationAuthorization.MissingMutationEntryMessage);
}
ValidateExtendedMutationStructure(mutation);
if (mutation.Resource != CanonicalMutationOwner(mutation))
{
throw Errors.Fail(ErrorCode.Validation, ProtocolValidationMutationResourceAndCanonicalOwnerDifferDetail);
}
var invalid = mutation switch
{
UpsertAnalyticsEvents analytics => analytics.Events.IsDefault || analytics.Events.Any(item => item is null) || analytics.ExpectedRevisions is null,
DeleteAnalyticsEvents analytics => analytics.ExpectedRevisions is null,
PatchDocument patch => patch.Patches.IsDefault || patch.Patches.Any(item => item is null || item.Kind is not (PatchKind.Set or PatchKind.Remove)),
AppendEvents append => append.Events.IsDefault || append.Events.Any(item => item is null) || append.ExpectedRevision.State is not (ExpectedStreamState.Any or ExpectedStreamState.Exact or ExpectedStreamState.NoStream),
PublishTopic topic => topic.Events.IsDefault || topic.Events.Any(item => item is null),
AppendSamples samples => samples.Samples.IsDefault || samples.Samples.Any(item => item is null),
PutVector vector => vector.Values.IsDefault,
ApplyVectorProjection projection => projection.Target.Values.IsDefault,
_ => false
};
if (invalid)
{
throw Errors.Fail(ErrorCode.Validation, ProtocolValidationNestedMutationEntryOrEnumValueIsInvalidDetail);
}
}
internal static string CanonicalMutationOwner(Mutation mutation) => mutation switch
{
UpsertAnalyticsEvents analytics => analytics.Store,
DeleteAnalyticsEvents analytics => analytics.Store,
InsertTypedRow insert => insert.Collection,
PutDocument put => put.Collection,
PatchDocument patch => patch.Collection,
DeleteDocument delete => delete.Collection,
AppendEvents append => append.StreamSet,
PublishTopic topic => topic.Topic,
PurgeTopic topic => topic.Topic,
EnqueueMessage message => message.Queue,
AdvanceQueueDeadline message => message.Queue,
RedriveQueueMessage message => message.Queue,
CancelQueueMessage message => message.Queue,
ParkPendingQueueMessage message => message.Queue,
UpsertEdge edge => edge.Graph,
DeleteEdge edge => edge.Graph,
ApplyCrossPartitionReverseEdge edge => edge.Graph,
CompleteCrossPartitionReverseEdge edge => edge.Graph,
AppendSamples samples => samples.SeriesSet,
ExpireSamples samples => samples.SeriesSet,
RefreshSampleRollup rollup => rollup.SeriesSet,
OpenSampleChunkWindow window => window.SeriesSet,
SealSampleChunkWindow window => window.SeriesSet,
MergeSampleChunkWindow window => window.SeriesSet,
DropSampleChunkWindow window => window.SeriesSet,
DropSampleRollup rollup => rollup.SeriesSet,
StoreAggregateSnapshot snapshot => snapshot.StreamSet,
PutVector vector => vector.Collection,
QueueToGraph projection => projection.Graph,
GraphToQueueMutation projection => projection.Queue,
CreateQueueTransfer transfer => transfer.SourceQueue.Queue,
AcceptQueueTransfer transfer => transfer.DestinationQueue.Queue,
CompleteQueueTransfer transfer => transfer.SourceQueue.Queue,
AdvanceQueueTransferAttempt transfer => transfer.SourceQueue.Queue,
AdvanceQueueTransferRepair transfer => transfer.SourceQueue.Queue,
ApplyVectorProjection projection => projection.Target.Collection,
ConfigureRecurringSchedule schedule => schedule.Definition.Lane.Queue,
EmitRecurringOccurrences schedule => schedule.Lane.Queue,
CancelRecurringSchedule schedule => schedule.Lane.Queue,
CompareExchangeSaga saga => saga.Lane.Queue,
ExpireSaga saga => saga.Lane.Queue,
_ => throw Errors.Fail(ErrorCode.UnsupportedCapability, ProtocolValidationMutationIsUnsupportedDetail)
};
internal static void ValidateExtendedMutationStructure(Mutation mutation)
{
if (mutation is AdvanceQueueDeadline deadline)
{
MessagingValidationQueueDeadlines.ValidateQueueDeadlineShape(deadline);
return;
}
if (mutation is RedriveQueueMessage or CancelQueueMessage or ParkPendingQueueMessage)
{
QueueLifecycleConditions.ValidateQueueLifecycleShape(mutation);
return;
}
var invalid = mutation switch
{
CreateQueueTransfer transfer => HasInvalidTransferShape(transfer),
AcceptQueueTransfer transfer => transfer.DestinationQueue?.Partition is null,
CompleteQueueTransfer transfer => transfer.SourceQueue?.Partition is null,
AdvanceQueueTransferAttempt transfer => transfer.SourceQueue?.Partition is null,
AdvanceQueueTransferRepair transfer => transfer.SourceQueue?.Partition is null,
ApplyVectorProjection projection => HasInvalidProjectionShape(projection),
ApplyCrossPartitionReverseEdge edge => HasInvalidReverseEdgeShape(edge.SourcePartition, edge.Destination),
CompleteCrossPartitionReverseEdge edge => HasInvalidReverseEdgeShape(edge.SourcePartition, edge.Destination),
ConfigureRecurringSchedule schedule => schedule.Definition?.Lane?.Partition is null,
EmitRecurringOccurrences schedule => schedule.Lane?.Partition is null,
CancelRecurringSchedule schedule => schedule.Lane?.Partition is null,
CompareExchangeSaga saga => HasInvalidSagaShape(saga),
ExpireSaga saga => saga.Lane?.Partition is null,
_ => false
};
if (invalid)
{
throw Errors.Fail(ErrorCode.Validation, MutationAuthorization.MissingMutationEntryMessage);
}
}
internal static bool HasInvalidTransferShape(CreateQueueTransfer transfer) => transfer.SourceQueue?.Partition is null || transfer.Destination?.Partition is null || transfer.Message is null;
internal static bool HasInvalidProjectionShape(ApplyVectorProjection projection) => projection.SourceStream is null || projection.SourceDocument is null || projection.SourceStream.Partition is null || projection.SourceDocument.Partition is null || projection.Target is null || projection.Target.Space is null;
internal static bool HasInvalidReverseEdgeShape(PartitionRef? source, EntityRef? destination) => source is null || destination?.Partition is null;
internal static bool HasInvalidSagaShape(CompareExchangeSaga saga) => saga.Lane?.Partition is null || saga.Timeout is { } timeout && timeout.Queue?.Partition is null;
}