-
Notifications
You must be signed in to change notification settings - Fork 2
/
rule2rego.ts
460 lines (436 loc) · 14.2 KB
/
rule2rego.ts
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// AWS Event Rule to OPA Rego Compiler
import Parser from "web-tree-sitter";
import { ok } from "assert";
import { readFile, stat, readdir } from "fs/promises";
import { resolve, relative } from "path";
enum NodeType {
rule_or_matching = "rule_or_matching",
rule_prefix_matching = "rule_prefix_matching",
rule_suffix_matching = "rule_suffix_matching",
rule_equals_ignore_case_matching = "rule_equals_ignore_case_matching",
rule_wildcard_matching = "rule_wildcard_matching",
rule_anything_but_matching = "rule_anything_but_matching",
rule_numeric_matching = "rule_numeric_matching",
rule_ip_address_matching = "rule_ip_address_matching",
rule_exists_matching = "rule_exists_matching",
rule_exactly_matching = "rule_exactly_matching",
rule_value_matching = "rule_value_matching",
}
enum NestedNodeType {
rule_nested_prefix_matching = "rule_nested_prefix_matching",
rule_nested_equals_ignore_case_matching = "rule_nested_equals_ignore_case_matching",
}
enum PrimitiveNodeType {
number = "number",
string = "string",
object = "object",
array = "array",
pair = "pair",
}
enum Helper {
CoelesceArray = "f(x) := x if { is_array(x) }\nf(x) := [x] if { not is_array(x) }",
HasKey = "has_key(x, k) { _ = x[k] }",
}
interface Rule {
name: string;
negate?: boolean;
helpers?: Helper[];
}
function getAllRuleNodeTypes(): string[] {
return Object.values(NodeType);
}
function unquote(str: string): string {
return str[0] === '"' ? str.slice(1, -1) : str;
}
interface RuleData {
readonly name: string;
readonly inputPath: string;
readonly orderedPaths: string[];
}
function pathsToInput(paths: string[]) {
return paths.reduce((cur: string, next: string) => {
return `f(${cur}["${next}"])[_]`;
}, "input");
}
function getJsonPathFromQueryCaptureNode(node: Parser.SyntaxNode): RuleData {
const paths: string[] = [];
const names: string[] = [];
let iterator: Parser.SyntaxNode | null = node;
while (iterator) {
if (iterator.type === PrimitiveNodeType.pair) {
const name = iterator.children[0].namedChildren[0].text;
const type = iterator.children[0].namedChildren[0].type;
if (
name === '"$or"' ||
type.includes("value") ||
!type.includes("constant")
) {
names.push(unquote(name));
if (name !== '"$or"') {
paths.push(unquote(name));
}
}
}
iterator = iterator.parent;
}
const orderedPaths = paths.reverse();
const orderedNames = names.reverse();
ok(orderedNames.length > 0);
const ruleName = orderedNames
.map((n) => n.replace(/[^\w\d]+/g, ""))
.join("_");
const inputPath = pathsToInput(orderedPaths);
return { name: `allow_${ruleName || "or"}`, inputPath, orderedPaths };
}
const createParser = async (): Promise<Parser> => {
await Parser.init();
const parser = new Parser();
const language = await Parser.Language.load(
resolve(__dirname, "tree-sitter-eventrule.wasm"),
);
parser.setLanguage(language);
return parser;
};
function emitRuleOr(context: Context): Rule {
let out = "";
const { name } = getJsonPathFromQueryCaptureNode(context.node);
const childObjects = context.node.namedChildren.filter(
(c) => c.type === PrimitiveNodeType.object,
);
for (const childObject of childObjects) {
const topLevelChildRules = childObject.namedChildren
.filter(
(c) =>
c.type === PrimitiveNodeType.pair &&
getAllRuleNodeTypes().includes(c.namedChildren[0].type),
)
.map((c) => c.namedChildren[0]);
const nestedChildRules = childObject.namedChildren
.filter(
(c) =>
c.type === PrimitiveNodeType.pair &&
c.namedChildren[0].type === "string" &&
c.namedChildren?.[1]?.children?.[1]?.children?.[1]?.type ===
PrimitiveNodeType.pair &&
getAllRuleNodeTypes().includes(
c.namedChildren?.[1]?.children?.[1]?.children?.[1]
?.namedChildren?.[0]?.type,
),
)
.map((c) => c.namedChildren[1].children[1].children[1].namedChildren[0]);
const childRules = [...topLevelChildRules, ...nestedChildRules];
if (childRules.length > 0) {
out += `${name} {\n`;
for (const childRule of childRules) {
out += `\t${emitRule({ ...context, node: childRule }).name}\n`;
}
out += "\n}\n";
}
}
context.rules.push(out);
return { name };
}
function emitRulePrefix(context: Context): Rule {
const { name: firstPart, inputPath } = getJsonPathFromQueryCaptureNode(
context.node,
);
const secondPart =
context.node.parent?.type === NodeType.rule_anything_but_matching
? "_prefix"
: "";
const name = `${firstPart}${secondPart}`;
const ruleTest = context.node.namedChildren[1];
if (
ruleTest.type === NestedNodeType.rule_nested_equals_ignore_case_matching
) {
return {
name: emitRule({ ...context, node: ruleTest }).name,
};
}
const ruleTestText = unquote(ruleTest.text);
context.rules.push(
`${name} {\n\tstartswith(${inputPath}, "${ruleTestText}")\n}`,
);
return { name };
}
function emitRuleSuffix(context: Context): Rule {
const { name: firstPart, inputPath } = getJsonPathFromQueryCaptureNode(
context.node,
);
const secondPart =
context.node.parent?.type === NodeType.rule_anything_but_matching
? "_suffix"
: "";
const name = `${firstPart}${secondPart}`;
const ruleTest = context.node.namedChildren[1];
if (
ruleTest.type === NestedNodeType.rule_nested_equals_ignore_case_matching
) {
return {
name: emitRule({ ...context, node: ruleTest }).name,
};
}
const ruleTestText = unquote(ruleTest.text);
context.rules.push(
`${name} {\n\tendswith(${inputPath}, "${ruleTestText}")\n}`,
);
return { name };
}
function emitRuleEqualsIgnoreCase(context: Context): Rule {
const { name: firstPart, inputPath } = getJsonPathFromQueryCaptureNode(
context.node,
);
const isPrefix = context.node.parent?.type === NodeType.rule_prefix_matching;
const isSuffix = context.node.parent?.type === NodeType.rule_suffix_matching;
const secondPart = isPrefix
? "_prefix_ignore_case"
: isSuffix
? "_suffix_ignore_case"
: "";
const name = `${firstPart}${secondPart}`;
const ruleTest = unquote(context.node.namedChildren[1].text);
if (isPrefix) {
context.rules.push(
`${name} {\n\tstartswith(lower(${inputPath}), "${ruleTest.toLowerCase()}")\n}`,
);
return { name };
}
if (isSuffix) {
context.rules.push(
`${name} {\n\endswith(lower(${inputPath}), "${ruleTest.toLowerCase()}")\n}`,
);
return { name };
}
context.rules.push(
`${name} {\n\tlower(${inputPath}) == "${ruleTest.toLowerCase()}"\n}`,
);
return { name };
}
function emitRuleWildcard(context: Context): Rule {
const { name, inputPath } = getJsonPathFromQueryCaptureNode(context.node);
const ruleTest = unquote(context.node.namedChildren[1].text);
context.rules.push(
`${name} {\n\tglob.match("${ruleTest}", [], ${inputPath})\n}`,
);
return { name };
}
function emitRuleAnythingBut(context: Context): Rule {
let out = "";
const { name, inputPath } = getJsonPathFromQueryCaptureNode(context.node);
const ruleTest = context.node.namedChildren[1];
if (ruleTest.type === NestedNodeType.rule_nested_prefix_matching) {
return {
name: emitRule({ ...context, node: ruleTest }).name,
negate: true,
};
}
if (ruleTest.type === PrimitiveNodeType.array) {
const val = ruleTest.text.slice(1, -1);
out += `${name} {\n\tcount([match | v := ${inputPath}; s := [ ${val} ][_]; s == v; match := v]) == 0\n}`;
}
if (ruleTest.type === PrimitiveNodeType.string) {
const val = unquote(ruleTest.text);
out += `${name} {\n\t${inputPath} != "${val}"\n}`;
}
if (ruleTest.type === PrimitiveNodeType.number) {
const val = ruleTest.text;
out += `${name} {\n\t${inputPath} != ${val}\n}`;
}
context.rules.push(out);
return { name };
}
function emitRuleNumeric(context: Context): Rule {
const { name, inputPath } = getJsonPathFromQueryCaptureNode(context.node);
const firstSign = unquote(context.node.namedChildren[1].text);
const firstNum = unquote(context.node.namedChildren[2].text);
const rules = ["", ""];
rules[0] += `${name} {\n\t${inputPath} ${firstSign} ${firstNum}\n`;
const secondSign = unquote(context.node.namedChildren[3]?.text || "");
const secondNum = unquote(context.node.namedChildren[4]?.text || "");
if (secondSign && secondNum) {
rules[0] += `\t${inputPath} ${secondSign} ${secondNum}\n`;
}
rules[0] += "}";
context.rules.push(...rules);
return { name };
}
function emitRuleIpAddress(context: Context): Rule {
const { name, inputPath } = getJsonPathFromQueryCaptureNode(context.node);
const ruleTest = unquote(context.node.namedChildren[1].text);
context.rules.push(
`${name} {\n\tnet.cidr_contains("${ruleTest}", ${inputPath})\n}`,
);
return { name };
}
function emitRuleExactly(context: Context): Rule {
const { name, inputPath } = getJsonPathFromQueryCaptureNode(context.node);
const ruleTest = unquote(context.node.namedChildren[1].text);
context.rules.push(`${name} {\n\t${inputPath} == "${ruleTest}"\n}`);
return { name };
}
function emitRuleExists(context: Context): Rule {
const { name, orderedPaths } = getJsonPathFromQueryCaptureNode(context.node);
const helpers = [Helper.HasKey];
const negate = context.node.namedChildren[1].text !== "true";
const key = orderedPaths.pop();
const input = pathsToInput(orderedPaths);
const len = input.length;
const some = `${input.slice(0, len - 2)}i${input.slice(len - 1, len)}`;
context.rules.push(`${name} {\n\tsome i\n\thas_key(${some}, "${key}")\n}`);
return { name, negate, helpers };
}
function emitRuleValue(context: Context): Rule {
const { name, inputPath } = getJsonPathFromQueryCaptureNode(context.node);
const ruleTest = context.node.namedChildren[1].text.slice(1, -1);
context.rules.push(
`${name} {\n\tcount([match | v := ${inputPath}; s := [ ${ruleTest} ][_]; s == v; match := v]) > 0\n}`,
);
return { name };
}
function emitRule(context: Context): Rule {
if (context.ruleCache.has(context.node.id)) {
return context.ruleRoots.get(context.ruleCache.get(context.node.id));
}
let iterator: Parser.SyntaxNode | null = context.node.parent;
let root = true;
while (iterator) {
if (getAllRuleNodeTypes().includes(iterator.type)) {
root = false;
break;
}
iterator = iterator.parent;
}
const rule = (() => {
switch (context.node.type) {
case NodeType.rule_or_matching:
return emitRuleOr(context);
case NestedNodeType.rule_nested_prefix_matching:
case NodeType.rule_prefix_matching:
return emitRulePrefix(context);
case NodeType.rule_suffix_matching:
return emitRuleSuffix(context);
case NestedNodeType.rule_nested_equals_ignore_case_matching:
case NodeType.rule_equals_ignore_case_matching:
return emitRuleEqualsIgnoreCase(context);
case NodeType.rule_wildcard_matching:
return emitRuleWildcard(context);
case NodeType.rule_anything_but_matching:
return emitRuleAnythingBut(context);
case NodeType.rule_numeric_matching:
return emitRuleNumeric(context);
case NodeType.rule_ip_address_matching:
return emitRuleIpAddress(context);
case NodeType.rule_exactly_matching:
return emitRuleExactly(context);
case NodeType.rule_exists_matching:
return emitRuleExists(context);
case NodeType.rule_value_matching:
return emitRuleValue(context);
default:
ok(false, `unhandled node type: ${context.node.type}`);
}
})();
if (root) {
context.ruleRoots.set(rule.name, rule);
}
context.ruleCache.set(context.node.id, rule.name);
return rule;
}
interface Context {
readonly rules: string[];
readonly ruleCache: Map<number, string>;
readonly ruleRoots: Map<string, Rule>;
readonly node: Parser.SyntaxNode;
}
export async function walkInput(input: string): Promise<string[]> {
const abs = resolve(input);
const dirent = await stat(abs);
if (!dirent.isDirectory()) {
return [abs];
}
let inputs: string[] = [];
const dirents = await readdir(abs, { withFileTypes: true });
for (const stat of dirents) {
const absPath = resolve(input, stat.name);
if (stat.isDirectory()) {
inputs = inputs.concat(await walkInput(resolve(absPath)));
continue;
}
if (!stat.name.endsWith(".json")) {
continue;
}
inputs.push(absPath);
}
return inputs;
}
export async function compile(input = process.argv[2]) {
const parser = await createParser();
const sourceRoot = resolve(input);
const sources = await walkInput(input);
const bodies: string[] = [];
const regoRulenames: string[] = [];
const kebabRegExp = new RegExp(/[^a-zA-Z0-9]|-{1,}/, "g");
for (const sourcePath of sources) {
const ruleCache = new Map<number, string>();
const ruleRoots = new Map<string, Rule>();
const source = await readFile(sourcePath, "utf8");
const tree = parser.parse(source);
const token = "cap";
const ruleQueries = getAllRuleNodeTypes()
.map((q) => `(${q}) @${token}`)
.map((q) => parser.getLanguage().query(q))
.map((q) => q.captures(tree.rootNode))
.flatMap((q) => q);
const rules: string[] = [];
for (const ruleQuery of ruleQueries) {
ok(ruleQuery.name === token);
const node = ruleQuery.node;
emitRule({ node, rules, ruleCache, ruleRoots });
}
let header = "";
const futures = "import future.keywords.if";
if (sources.length === 1) {
header = `package rule2rego\n${futures}\ndefault allow := false`;
} else {
const pkgPath = relative(sourceRoot, sourcePath).slice(0, -5);
const ruleName = pkgPath.replace(kebabRegExp, "");
regoRulenames.push(ruleName);
header = `package rule2rego.${ruleName}\n${futures}\ndefault allow := false`;
}
const helpers = [Helper.CoelesceArray];
ruleRoots.forEach(
(rule) =>
rule.helpers && helpers.splice(helpers.length, 0, ...rule.helpers),
);
const init = [...new Set(Array.from(ruleCache.values()))]
.map((i) => `default ${i} := false`)
.join("\n");
const policy = `\n${rules.join("\n")}`;
const footer = `allow {\n\t${Array.from(ruleRoots.values())
.map((rule) => `${rule.negate ?? false ? "not " : ""}${rule.name}`)
.join("\n\t")}\n}`;
const body = `${header}\n${helpers.join(
"\n",
)}\n${init}${policy}\n${footer}`.replace(/\n\n/g, "\n");
if (input === process.argv[2]) {
console.log(body);
console.log("");
}
bodies.push(body);
}
if (sources.length > 1) {
// add the main
const body = `package rule2rego\ndefault allow := false\nallow {\n data.rule2rego.${regoRulenames.join(
".allow\n}\nallow {\n data.rule2rego.",
)}.allow\n}`;
bodies.splice(0, 0, body);
if (input === process.argv[2]) {
console.log(body);
console.log("");
}
}
return bodies;
}
if (process.argv[1].endsWith("rule2rego.ts")) {
compile(process.argv[2]);
}