-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
CelExecutor.java
400 lines (365 loc) · 13.4 KB
/
CelExecutor.java
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
* Copyright 2022 Confluent Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.confluent.kafka.schemaregistry.rules.cel;
import static io.confluent.kafka.schemaregistry.rules.cel.avro.AvroTypeDescription.NULL_AVRO_SCHEMA;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.api.expr.v1alpha1.Decl;
import com.google.api.expr.v1alpha1.Type;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.ByteString;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Duration;
import com.google.protobuf.DynamicMessage;
import com.google.protobuf.Message;
import com.google.protobuf.Timestamp;
import com.hubspot.jackson.datatype.protobuf.ProtobufModule;
import io.confluent.kafka.schemaregistry.client.rest.entities.RuleKind;
import io.confluent.kafka.schemaregistry.rules.RuleContext;
import io.confluent.kafka.schemaregistry.rules.RuleException;
import io.confluent.kafka.schemaregistry.rules.RuleExecutor;
import io.confluent.kafka.schemaregistry.rules.cel.avro.AvroRegistry;
import io.confluent.kafka.schemaregistry.rules.cel.builtin.BuiltinLibrary;
import java.io.IOException;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericContainer;
import org.projectnessie.cel.checker.Decls;
import org.projectnessie.cel.common.types.pb.Checked;
import org.projectnessie.cel.tools.Script;
import org.projectnessie.cel.tools.ScriptException;
import org.projectnessie.cel.tools.ScriptHost;
import org.projectnessie.cel.tools.ScriptHost.ScriptBuilder;
import org.projectnessie.cel.types.jackson.JacksonRegistry;
public class CelExecutor implements RuleExecutor {
public static final String TYPE = "CEL";
public static final String CEL_IGNORE_GUARD_SEPARATOR = "cel.ignore.guard.separator";
private static final ObjectMapper mapper = new ObjectMapper();
static {
// Register ProtobufModule to convert CEL objects (such as NullValue) to JSON
mapper.registerModule(new ProtobufModule());
}
private static final int DEFAULT_CACHE_SIZE = 100;
private final LoadingCache<RuleWithArgs, Script> cache;
public CelExecutor() {
cache = CacheBuilder.newBuilder()
.maximumSize(DEFAULT_CACHE_SIZE)
.build(new CacheLoader<RuleWithArgs, Script>() {
@Override
public Script load(RuleWithArgs ruleWithArgs) throws Exception {
// Build the script factory
ScriptHost.Builder scriptHostBuilder = ScriptHost.newBuilder();
switch (ruleWithArgs.getType()) {
case AVRO:
scriptHostBuilder = scriptHostBuilder.registry(AvroRegistry.newRegistry());
break;
case JSON:
scriptHostBuilder = scriptHostBuilder.registry(JacksonRegistry.newRegistry());
break;
case PROTOBUF:
break;
default:
throw new IllegalArgumentException("Unsupported type " + ruleWithArgs.getType());
}
ScriptHost scriptHost = scriptHostBuilder.build();
ScriptBuilder scriptBuilder = scriptHost
.buildScript(ruleWithArgs.getRule())
.withDeclarations(ruleWithArgs.getDecls());
switch (ruleWithArgs.getType()) {
case AVRO:
// Register our Avro type
scriptBuilder = scriptBuilder.withTypes(ruleWithArgs.getAvroSchema());
break;
case JSON:
// Register our Jackson object message type
scriptBuilder = scriptBuilder.withTypes(ruleWithArgs.getJsonClass());
break;
case PROTOBUF:
// Use buildPartial to ignore missing required fields
scriptBuilder = scriptBuilder.withTypes(
DynamicMessage.newBuilder(ruleWithArgs.getProtobufDesc()).buildPartial());
break;
default:
throw new IllegalArgumentException("Unsupported type " + ruleWithArgs.getType());
}
scriptBuilder = scriptBuilder.withLibraries(new BuiltinLibrary());
return scriptBuilder.build();
}
});
}
@Override
public String type() {
return TYPE;
}
@Override
public Object transform(RuleContext ctx, Object message) throws RuleException {
Object input;
if (message instanceof JsonNode) {
input = mapper.convertValue(message, new TypeReference<Map<String, Object>>(){});
} else {
input = message;
}
Object result = execute(ctx, input, ImmutableMap.of("message", input));
if (result instanceof Map) {
// Convert maps to the target object type
try {
JsonNode jsonNode = mapper.valueToTree(result);
result = ctx.target().fromJson(jsonNode);
} catch (IOException e) {
throw new RuleException(e);
}
}
return result;
}
protected Object execute(
RuleContext ctx, Object obj, Map<String, Object> args)
throws RuleException {
String expr = ctx.rule().getExpr();
String ignoreGuardStr = ctx.getParameter(CEL_IGNORE_GUARD_SEPARATOR);
boolean ignoreGuard = Boolean.parseBoolean(ignoreGuardStr);
if (!ignoreGuard) {
// An optional guard (followed by semicolon) can precede the expr
int index = expr.indexOf(';');
if (index >= 0) {
String guard = expr.substring(0, index);
if (!guard.trim().isEmpty()) {
Object guardResult = execute(guard, obj, args);
if (!Boolean.TRUE.equals(guardResult)) {
// Skip the expr
return ctx.rule().getKind() == RuleKind.CONDITION ? Boolean.TRUE : obj;
}
}
expr = expr.substring(index + 1);
}
}
return execute(expr, obj, args);
}
private Object execute(String rule, Object obj, Map<String, Object> args)
throws RuleException {
try {
Object msg = args.get("message");
if (msg == null) {
msg = obj;
}
ScriptType type = ScriptType.JSON;
if (msg instanceof GenericContainer) {
type = ScriptType.AVRO;
} else if (msg instanceof Message) {
type = ScriptType.PROTOBUF;
} else if (msg instanceof List<?>) {
// list not supported
return obj;
}
List<Decl> decls = toDecls(args);
RuleWithArgs ruleWithArgs = null;
switch (type) {
case AVRO:
ruleWithArgs = new RuleWithArgs(rule, type, decls, ((GenericContainer) msg).getSchema());
break;
case JSON:
ruleWithArgs = new RuleWithArgs(rule, type, decls, msg.getClass());
break;
case PROTOBUF:
ruleWithArgs = new RuleWithArgs(rule, type, decls,
((Message) msg).getDescriptorForType());
break;
default:
throw new IllegalArgumentException("Unsupported type " + type);
}
Script script = cache.get(ruleWithArgs);
return script.execute(Object.class, args);
} catch (ScriptException e) {
throw new RuleException("Could not execute CEL script", e);
} catch (ExecutionException e) {
if (e.getCause() instanceof RuleException) {
throw (RuleException) e.getCause();
} else {
throw new RuleException("Could not get expression", e.getCause());
}
}
}
private static List<Decl> toDecls(Map<String, Object> args) {
return args.entrySet().stream()
.map(e -> Decls.newVar(e.getKey(), findType(e.getValue())))
.collect(Collectors.toList());
}
private static Type findType(Object arg) {
if (arg == null) {
return Checked.checkedNull;
} else if (arg instanceof GenericContainer) {
return findTypeForAvroType(((GenericContainer) arg).getSchema());
} else if (arg instanceof Message) {
return Decls.newObjectType(((Message) arg).getDescriptorForType().getFullName());
} else {
return findTypeForClass(arg.getClass());
}
}
private static Type findTypeForAvroType(Schema schema) {
Schema.Type type = schema.getType();
switch (type) {
case BOOLEAN:
return Checked.checkedBool;
case INT:
case LONG:
return Checked.checkedInt;
case BYTES:
case FIXED:
return Checked.checkedBytes;
case FLOAT:
case DOUBLE:
return Checked.checkedDouble;
case STRING:
return Checked.checkedString;
// TODO duration, timestamp
case ARRAY:
return Checked.checkedListDyn;
case MAP:
return Checked.checkedMapStringDyn;
case ENUM:
return Decls.newObjectType(schema.getFullName());
case NULL:
return Checked.checkedNull;
case RECORD:
return Decls.newObjectType(schema.getFullName());
case UNION:
if (schema.getTypes().size() == 2 && schema.getTypes().contains(NULL_AVRO_SCHEMA)) {
for (Schema memberSchema : schema.getTypes()) {
if (!memberSchema.equals(NULL_AVRO_SCHEMA)) {
return findTypeForAvroType(memberSchema);
}
}
}
throw new IllegalArgumentException("Unsupported union type");
default:
throw new IllegalArgumentException("Unsupported type " + type);
}
}
private static Type findTypeForClass(Class<?> type) {
Class<?> rawClass = type;
if (rawClass == boolean.class || rawClass == Boolean.class) {
return Checked.checkedBool;
} else if (rawClass == long.class
|| rawClass == Long.class
|| rawClass == int.class
|| rawClass == Integer.class
|| rawClass == short.class
|| rawClass == Short.class
|| rawClass == byte.class
|| rawClass == Byte.class) {
return Checked.checkedInt;
} else if (rawClass == byte[].class || rawClass == ByteString.class) {
return Checked.checkedBytes;
} else if (rawClass == double.class
|| rawClass == Double.class
|| rawClass == float.class
|| rawClass == Float.class) {
return Checked.checkedDouble;
} else if (rawClass == String.class) {
return Checked.checkedString;
} else if (rawClass == Duration.class || rawClass == java.time.Duration.class) {
return Checked.checkedDuration;
} else if (rawClass == Timestamp.class
|| Instant.class.isAssignableFrom(rawClass)
|| ZonedDateTime.class.isAssignableFrom(rawClass)) {
return Checked.checkedTimestamp;
} else if (Map.class.isAssignableFrom(rawClass)) {
return Checked.checkedMapStringDyn;
} else if (List.class.isAssignableFrom(rawClass)) {
return Checked.checkedListDyn;
} else {
return Decls.newObjectType(rawClass.getName());
}
}
enum ScriptType {
AVRO,
JSON,
PROTOBUF
}
static class RuleWithArgs {
private final String rule;
private final ScriptType type;
private final List<Decl> decls;
private Schema avroSchema;
private Class<?> jsonClass;
private Descriptor protobufDesc;
public RuleWithArgs(String rule, ScriptType type, List<Decl> decls, Schema avroSchema) {
this.rule = rule;
this.type = type;
this.decls = decls;
this.avroSchema = avroSchema;
}
public RuleWithArgs(String rule, ScriptType type, List<Decl> decls, Class<?> jsonClass) {
this.rule = rule;
this.type = type;
this.decls = decls;
this.jsonClass = jsonClass;
}
public RuleWithArgs(String rule, ScriptType type, List<Decl> decls, Descriptor protobufDesc) {
this.rule = rule;
this.type = type;
this.decls = decls;
this.protobufDesc = protobufDesc;
}
public String getRule() {
return rule;
}
public ScriptType getType() {
return type;
}
public List<Decl> getDecls() {
return decls;
}
public Schema getAvroSchema() {
return avroSchema;
}
public Class<?> getJsonClass() {
return jsonClass;
}
public Descriptor getProtobufDesc() {
return protobufDesc;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RuleWithArgs that = (RuleWithArgs) o;
return Objects.equals(rule, that.rule)
&& type == that.type
&& Objects.equals(decls, that.decls)
&& Objects.equals(avroSchema, that.avroSchema)
&& Objects.equals(jsonClass, that.jsonClass)
&& Objects.equals(protobufDesc, that.protobufDesc);
}
@Override
public int hashCode() {
return Objects.hash(rule, type, decls, avroSchema, jsonClass, protobufDesc);
}
}
}