forked from eclipse-biscuit/biscuit-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchemaVersion.java
More file actions
182 lines (167 loc) · 5.74 KB
/
SchemaVersion.java
File metadata and controls
182 lines (167 loc) · 5.74 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright (c) 2019 Geoffroy Couprie <contact@geoffroycouprie.com> and Contributors to the Eclipse Foundation.
* SPDX-License-Identifier: Apache-2.0
*/
package org.eclipse.biscuit.datalog;
import java.util.List;
import java.util.Optional;
import org.eclipse.biscuit.crypto.PublicKey;
import org.eclipse.biscuit.datalog.expressions.Expression;
import org.eclipse.biscuit.datalog.expressions.Op;
import org.eclipse.biscuit.error.Error;
import org.eclipse.biscuit.error.Result;
import org.eclipse.biscuit.token.format.SerializedBiscuit;
public final class SchemaVersion {
private final boolean containsScopes;
private final boolean containsV31;
private final boolean containsCheckAll;
private final boolean containsV33;
private final boolean containsExternalKey;
public SchemaVersion(
List<Fact> facts,
List<Rule> rules,
List<Check> checks,
List<Scope> scopes,
Optional<PublicKey> externalKey) {
containsScopes =
!scopes.isEmpty()
|| rules.stream().anyMatch(r -> !r.scopes().isEmpty())
|| checks.stream()
.anyMatch(c -> c.queries().stream().anyMatch(q -> !q.scopes().isEmpty()));
containsCheckAll = checks.stream().anyMatch(c -> c.kind() == Check.Kind.ALL);
containsV31 =
rules.stream().anyMatch(r -> containsV31Op(r.expressions()))
|| checks.stream()
.anyMatch(c -> c.queries().stream().anyMatch(q -> containsV31Op(q.expressions())));
containsV33 =
checks.stream().anyMatch(c -> c.kind() == Check.Kind.REJECT)
|| rules.stream()
.anyMatch(
r ->
containsV33Predicate(r.head())
|| r.body().stream().anyMatch(SchemaVersion::containsV33Predicate)
|| containsV33Op(r.expressions()))
|| checks.stream()
.anyMatch(
c ->
c.queries().stream()
.anyMatch(
q ->
q.body().stream().anyMatch(SchemaVersion::containsV33Predicate)
|| containsV33Op(q.expressions())))
|| facts.stream().anyMatch(f -> containsV33Predicate(f.predicate()));
containsExternalKey = externalKey.isPresent();
}
public int version() {
if (containsV33) {
return SerializedBiscuit.DATALOG_3_3;
}
if (containsExternalKey) {
return SerializedBiscuit.DATALOG_3_2;
}
if (containsScopes || containsV31 || containsCheckAll) {
return SerializedBiscuit.DATALOG_3_1;
}
return SerializedBiscuit.MIN_SCHEMA_VERSION;
}
public Result<Void, Error.FormatError> checkCompatibility(int version) {
if (version < SerializedBiscuit.DATALOG_3_1) {
if (containsScopes) {
return Result.err(
new Error.FormatError.DeserializationError(
"scopes are only supported in datalog v3.1+"));
}
if (containsV31) {
return Result.err(
new Error.FormatError.DeserializationError(
"bitwise operators and != are only supported in datalog v3.1+"));
}
if (containsCheckAll) {
return Result.err(
new Error.FormatError.DeserializationError(
"check all is only supported in datalog v3.1+"));
}
}
if (version < SerializedBiscuit.DATALOG_3_2 && containsExternalKey) {
return Result.err(
new Error.FormatError.DeserializationError(
"third-party blocks are only supported in datalog v3.2+"));
}
if (version < SerializedBiscuit.DATALOG_3_3 && containsV33) {
return Result.err(
new Error.FormatError.DeserializationError(
"maps, arrays, null, closures are only supported in datalog v3.3+"));
}
return Result.ok(null);
}
private static boolean containsV31Op(List<Expression> expressions) {
for (Expression e : expressions) {
for (Op op : e.getOps()) {
if (op instanceof Op.Binary) {
Op.Binary b = (Op.Binary) op;
switch (b.getOp()) {
case BitwiseAnd:
case BitwiseOr:
case BitwiseXor:
case NotEqual:
return true;
default:
}
}
}
}
return false;
}
private static boolean containsV33Op(List<Expression> expressions) {
for (Expression e : expressions) {
for (Op op : e.getOps()) {
if (op instanceof Op.Unary) {
Op.Unary b = (Op.Unary) op;
switch (b.getOp()) {
case TypeOf:
return true;
default:
}
} else if (op instanceof Op.Binary) {
Op.Binary b = (Op.Binary) op;
switch (b.getOp()) {
case HeterogeneousEqual:
case HeterogeneousNotEqual:
case LazyAnd:
case LazyOr:
case Get:
case Any:
case All:
case TryOr:
return true;
default:
}
} else if (op instanceof Op.Closure) {
return true;
} else if (op instanceof Term.Null) {
return true;
} else if (op instanceof Term.Array) {
return true;
} else if (op instanceof Term.Map) {
return true;
}
}
}
return false;
}
private static boolean containsV33Predicate(Predicate predicate) {
return predicate.terms().stream().anyMatch(SchemaVersion::containsV33Term);
}
private static boolean containsV33Term(Term term) {
if (term instanceof Term.Null) {
return true;
}
if (term instanceof Term.Array) {
return true;
}
if (term instanceof Term.Map) {
return true;
}
return false;
}
}