@sentriflow/core 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/LICENSE +190 -0
  2. package/README.md +86 -0
  3. package/package.json +60 -0
  4. package/src/constants.ts +77 -0
  5. package/src/engine/RuleExecutor.ts +256 -0
  6. package/src/engine/Runner.ts +312 -0
  7. package/src/engine/SandboxedExecutor.ts +208 -0
  8. package/src/errors.ts +88 -0
  9. package/src/helpers/arista/helpers.ts +1220 -0
  10. package/src/helpers/arista/index.ts +12 -0
  11. package/src/helpers/aruba/helpers.ts +637 -0
  12. package/src/helpers/aruba/index.ts +13 -0
  13. package/src/helpers/cisco/helpers.ts +534 -0
  14. package/src/helpers/cisco/index.ts +11 -0
  15. package/src/helpers/common/helpers.ts +265 -0
  16. package/src/helpers/common/index.ts +5 -0
  17. package/src/helpers/common/validation.ts +280 -0
  18. package/src/helpers/cumulus/helpers.ts +676 -0
  19. package/src/helpers/cumulus/index.ts +12 -0
  20. package/src/helpers/extreme/helpers.ts +422 -0
  21. package/src/helpers/extreme/index.ts +12 -0
  22. package/src/helpers/fortinet/helpers.ts +892 -0
  23. package/src/helpers/fortinet/index.ts +12 -0
  24. package/src/helpers/huawei/helpers.ts +790 -0
  25. package/src/helpers/huawei/index.ts +11 -0
  26. package/src/helpers/index.ts +53 -0
  27. package/src/helpers/juniper/helpers.ts +756 -0
  28. package/src/helpers/juniper/index.ts +12 -0
  29. package/src/helpers/mikrotik/helpers.ts +722 -0
  30. package/src/helpers/mikrotik/index.ts +12 -0
  31. package/src/helpers/nokia/helpers.ts +856 -0
  32. package/src/helpers/nokia/index.ts +11 -0
  33. package/src/helpers/paloalto/helpers.ts +939 -0
  34. package/src/helpers/paloalto/index.ts +12 -0
  35. package/src/helpers/vyos/helpers.ts +429 -0
  36. package/src/helpers/vyos/index.ts +12 -0
  37. package/src/index.ts +30 -0
  38. package/src/json-rules/ExpressionEvaluator.ts +292 -0
  39. package/src/json-rules/HelperRegistry.ts +177 -0
  40. package/src/json-rules/JsonRuleCompiler.ts +339 -0
  41. package/src/json-rules/JsonRuleValidator.ts +371 -0
  42. package/src/json-rules/index.ts +97 -0
  43. package/src/json-rules/schema.json +350 -0
  44. package/src/json-rules/types.ts +303 -0
  45. package/src/pack-loader/PackLoader.ts +332 -0
  46. package/src/pack-loader/index.ts +17 -0
  47. package/src/pack-loader/types.ts +135 -0
  48. package/src/parser/IncrementalParser.ts +527 -0
  49. package/src/parser/Sanitizer.ts +104 -0
  50. package/src/parser/SchemaAwareParser.ts +504 -0
  51. package/src/parser/VendorSchema.ts +72 -0
  52. package/src/parser/vendors/arista-eos.ts +206 -0
  53. package/src/parser/vendors/aruba-aoscx.ts +123 -0
  54. package/src/parser/vendors/aruba-aosswitch.ts +113 -0
  55. package/src/parser/vendors/aruba-wlc.ts +173 -0
  56. package/src/parser/vendors/cisco-ios.ts +110 -0
  57. package/src/parser/vendors/cisco-nxos.ts +107 -0
  58. package/src/parser/vendors/cumulus-linux.ts +161 -0
  59. package/src/parser/vendors/extreme-exos.ts +154 -0
  60. package/src/parser/vendors/extreme-voss.ts +167 -0
  61. package/src/parser/vendors/fortinet-fortigate.ts +217 -0
  62. package/src/parser/vendors/huawei-vrp.ts +192 -0
  63. package/src/parser/vendors/index.ts +1521 -0
  64. package/src/parser/vendors/juniper-junos.ts +230 -0
  65. package/src/parser/vendors/mikrotik-routeros.ts +274 -0
  66. package/src/parser/vendors/nokia-sros.ts +251 -0
  67. package/src/parser/vendors/paloalto-panos.ts +264 -0
  68. package/src/parser/vendors/vyos-vyos.ts +454 -0
  69. package/src/types/ConfigNode.ts +72 -0
  70. package/src/types/DeclarativeRule.ts +158 -0
  71. package/src/types/IRule.ts +270 -0
@@ -0,0 +1,97 @@
1
+ // packages/core/src/json-rules/index.ts
2
+
3
+ /**
4
+ * JSON Rules Module
5
+ *
6
+ * Provides support for JSON-based rule definitions that can be
7
+ * authored without TypeScript knowledge while maintaining full
8
+ * access to helper functions.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * import { compileJsonRules, validateJsonRuleFile } from '@sentriflow/core';
13
+ *
14
+ * // Validate a JSON rule file
15
+ * const validation = validateJsonRuleFile(jsonData);
16
+ * if (!validation.valid) {
17
+ * console.error(validation.errors);
18
+ * }
19
+ *
20
+ * // Compile JSON rules to IRule objects
21
+ * const rules = compileJsonRules(jsonData.rules);
22
+ * ```
23
+ */
24
+
25
+ // Types
26
+ export type {
27
+ JsonArgValue,
28
+ JsonCheck,
29
+ JsonRule,
30
+ JsonRuleFile,
31
+ } from './types';
32
+
33
+ export {
34
+ isJsonArgValue,
35
+ isJsonCheck,
36
+ isJsonRule,
37
+ isJsonRuleFile,
38
+ } from './types';
39
+
40
+ // Helper Registry
41
+ export type {
42
+ HelperFunction,
43
+ VendorHelpers,
44
+ HelperRegistry,
45
+ } from './HelperRegistry';
46
+
47
+ export {
48
+ createHelperRegistry,
49
+ resolveHelper,
50
+ getAvailableHelpers,
51
+ hasHelper,
52
+ getHelperRegistry,
53
+ clearHelperRegistryCache,
54
+ VENDOR_NAMESPACES,
55
+ } from './HelperRegistry';
56
+
57
+ export type { VendorNamespace } from './HelperRegistry';
58
+
59
+ // Expression Evaluator
60
+ export {
61
+ ExpressionEvaluator,
62
+ createExpressionEvaluator,
63
+ getExpressionEvaluator,
64
+ clearExpressionEvaluator,
65
+ isValidExpression,
66
+ } from './ExpressionEvaluator';
67
+
68
+ // JSON Rule Compiler
69
+ export type {
70
+ JsonRuleCompilerOptions,
71
+ } from './JsonRuleCompiler';
72
+
73
+ export {
74
+ JsonRuleCompiler,
75
+ createJsonRuleCompiler,
76
+ getJsonRuleCompiler,
77
+ compileJsonRule,
78
+ compileJsonRules,
79
+ clearJsonRuleCompiler,
80
+ } from './JsonRuleCompiler';
81
+
82
+ // JSON Rule Validator
83
+ export type {
84
+ ValidationError,
85
+ ValidationResult,
86
+ ValidationOptions,
87
+ } from './JsonRuleValidator';
88
+
89
+ export {
90
+ validateJsonRuleFile,
91
+ validateJsonRule,
92
+ formatValidationResult,
93
+ } from './JsonRuleValidator';
94
+
95
+ // JSON Schema (as a module for runtime access)
96
+ import schema from './schema.json';
97
+ export { schema as jsonRuleSchema };
@@ -0,0 +1,350 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "$id": "https://sentriflow.io/schemas/json-rules/v1.0.json",
4
+ "title": "SentriFlow JSON Rules",
5
+ "description": "Schema for SentriFlow JSON rule files",
6
+ "type": "object",
7
+ "required": ["version", "rules"],
8
+ "additionalProperties": false,
9
+ "properties": {
10
+ "version": {
11
+ "type": "string",
12
+ "const": "1.0",
13
+ "description": "Schema version"
14
+ },
15
+ "meta": {
16
+ "type": "object",
17
+ "description": "Optional metadata about this rule file",
18
+ "additionalProperties": false,
19
+ "properties": {
20
+ "name": {
21
+ "type": "string",
22
+ "description": "Name of this rule collection"
23
+ },
24
+ "description": {
25
+ "type": "string",
26
+ "description": "Description of the rule collection"
27
+ },
28
+ "author": {
29
+ "type": "string",
30
+ "description": "Author or organization"
31
+ },
32
+ "license": {
33
+ "type": "string",
34
+ "description": "License for the rules"
35
+ }
36
+ }
37
+ },
38
+ "rules": {
39
+ "type": "array",
40
+ "description": "Array of JSON rules",
41
+ "items": {
42
+ "$ref": "#/definitions/JsonRule"
43
+ }
44
+ }
45
+ },
46
+ "definitions": {
47
+ "JsonRule": {
48
+ "type": "object",
49
+ "required": ["id", "metadata", "check"],
50
+ "additionalProperties": false,
51
+ "properties": {
52
+ "id": {
53
+ "type": "string",
54
+ "pattern": "^[A-Z][A-Z0-9_-]{2,49}$",
55
+ "description": "Unique rule identifier"
56
+ },
57
+ "selector": {
58
+ "type": "string",
59
+ "description": "Optional selector for node filtering"
60
+ },
61
+ "vendor": {
62
+ "oneOf": [
63
+ { "$ref": "#/definitions/RuleVendor" },
64
+ {
65
+ "type": "array",
66
+ "items": { "$ref": "#/definitions/RuleVendor" },
67
+ "minItems": 1
68
+ }
69
+ ],
70
+ "description": "Optional vendor(s) this rule applies to"
71
+ },
72
+ "metadata": {
73
+ "$ref": "#/definitions/RuleMetadata"
74
+ },
75
+ "check": {
76
+ "$ref": "#/definitions/JsonCheck"
77
+ },
78
+ "failureMessage": {
79
+ "type": "string",
80
+ "description": "Custom message template for failures"
81
+ },
82
+ "successMessage": {
83
+ "type": "string",
84
+ "description": "Custom message template for passes"
85
+ }
86
+ }
87
+ },
88
+ "RuleVendor": {
89
+ "type": "string",
90
+ "enum": [
91
+ "common",
92
+ "cisco-ios",
93
+ "cisco-nxos",
94
+ "juniper-junos",
95
+ "aruba-aoscx",
96
+ "aruba-aosswitch",
97
+ "aruba-wlc",
98
+ "paloalto-panos",
99
+ "arista-eos",
100
+ "vyos",
101
+ "fortinet-fortigate",
102
+ "extreme-exos",
103
+ "extreme-voss",
104
+ "huawei-vrp",
105
+ "mikrotik-routeros",
106
+ "nokia-sros",
107
+ "cumulus-linux"
108
+ ]
109
+ },
110
+ "RuleMetadata": {
111
+ "type": "object",
112
+ "required": ["level", "obu", "owner"],
113
+ "additionalProperties": false,
114
+ "properties": {
115
+ "level": {
116
+ "type": "string",
117
+ "enum": ["error", "warning", "info"],
118
+ "description": "Severity level of the rule"
119
+ },
120
+ "obu": {
121
+ "type": "string",
122
+ "description": "Organizational Business Unit responsible for this rule"
123
+ },
124
+ "owner": {
125
+ "type": "string",
126
+ "description": "Owner of the rule logic"
127
+ },
128
+ "description": {
129
+ "type": "string",
130
+ "description": "Brief description of what the rule checks"
131
+ },
132
+ "remediation": {
133
+ "type": "string",
134
+ "description": "Suggested steps to fix the violation"
135
+ },
136
+ "security": {
137
+ "$ref": "#/definitions/SecurityMetadata"
138
+ }
139
+ }
140
+ },
141
+ "SecurityMetadata": {
142
+ "type": "object",
143
+ "additionalProperties": false,
144
+ "properties": {
145
+ "cwe": {
146
+ "type": "array",
147
+ "items": { "type": "string" },
148
+ "description": "CWE identifiers"
149
+ },
150
+ "cvssScore": {
151
+ "type": "number",
152
+ "minimum": 0,
153
+ "maximum": 10,
154
+ "description": "CVSS v3.1 base score"
155
+ },
156
+ "cvssVector": {
157
+ "type": "string",
158
+ "description": "CVSS v3.1 vector string"
159
+ },
160
+ "tags": {
161
+ "type": "array",
162
+ "items": { "type": "string" },
163
+ "description": "Security-related tags"
164
+ }
165
+ }
166
+ },
167
+ "JsonCheck": {
168
+ "oneOf": [
169
+ { "$ref": "#/definitions/MatchCheck" },
170
+ { "$ref": "#/definitions/NotMatchCheck" },
171
+ { "$ref": "#/definitions/ContainsCheck" },
172
+ { "$ref": "#/definitions/NotContainsCheck" },
173
+ { "$ref": "#/definitions/ChildExistsCheck" },
174
+ { "$ref": "#/definitions/ChildNotExistsCheck" },
175
+ { "$ref": "#/definitions/ChildMatchesCheck" },
176
+ { "$ref": "#/definitions/ChildContainsCheck" },
177
+ { "$ref": "#/definitions/HelperCheck" },
178
+ { "$ref": "#/definitions/ExprCheck" },
179
+ { "$ref": "#/definitions/AndCheck" },
180
+ { "$ref": "#/definitions/OrCheck" },
181
+ { "$ref": "#/definitions/NotCheck" }
182
+ ]
183
+ },
184
+ "MatchCheck": {
185
+ "type": "object",
186
+ "required": ["type", "pattern"],
187
+ "additionalProperties": false,
188
+ "properties": {
189
+ "type": { "const": "match" },
190
+ "pattern": { "type": "string" },
191
+ "flags": { "type": "string" }
192
+ }
193
+ },
194
+ "NotMatchCheck": {
195
+ "type": "object",
196
+ "required": ["type", "pattern"],
197
+ "additionalProperties": false,
198
+ "properties": {
199
+ "type": { "const": "not_match" },
200
+ "pattern": { "type": "string" },
201
+ "flags": { "type": "string" }
202
+ }
203
+ },
204
+ "ContainsCheck": {
205
+ "type": "object",
206
+ "required": ["type", "text"],
207
+ "additionalProperties": false,
208
+ "properties": {
209
+ "type": { "const": "contains" },
210
+ "text": { "type": "string" }
211
+ }
212
+ },
213
+ "NotContainsCheck": {
214
+ "type": "object",
215
+ "required": ["type", "text"],
216
+ "additionalProperties": false,
217
+ "properties": {
218
+ "type": { "const": "not_contains" },
219
+ "text": { "type": "string" }
220
+ }
221
+ },
222
+ "ChildExistsCheck": {
223
+ "type": "object",
224
+ "required": ["type", "selector"],
225
+ "additionalProperties": false,
226
+ "properties": {
227
+ "type": { "const": "child_exists" },
228
+ "selector": { "type": "string" }
229
+ }
230
+ },
231
+ "ChildNotExistsCheck": {
232
+ "type": "object",
233
+ "required": ["type", "selector"],
234
+ "additionalProperties": false,
235
+ "properties": {
236
+ "type": { "const": "child_not_exists" },
237
+ "selector": { "type": "string" }
238
+ }
239
+ },
240
+ "ChildMatchesCheck": {
241
+ "type": "object",
242
+ "required": ["type", "selector", "pattern"],
243
+ "additionalProperties": false,
244
+ "properties": {
245
+ "type": { "const": "child_matches" },
246
+ "selector": { "type": "string" },
247
+ "pattern": { "type": "string" },
248
+ "flags": { "type": "string" }
249
+ }
250
+ },
251
+ "ChildContainsCheck": {
252
+ "type": "object",
253
+ "required": ["type", "selector", "text"],
254
+ "additionalProperties": false,
255
+ "properties": {
256
+ "type": { "const": "child_contains" },
257
+ "selector": { "type": "string" },
258
+ "text": { "type": "string" }
259
+ }
260
+ },
261
+ "HelperCheck": {
262
+ "type": "object",
263
+ "required": ["type", "helper"],
264
+ "additionalProperties": false,
265
+ "properties": {
266
+ "type": { "const": "helper" },
267
+ "helper": {
268
+ "type": "string",
269
+ "description": "Helper name, optionally namespaced (e.g., 'cisco.isTrunkPort')"
270
+ },
271
+ "args": {
272
+ "type": "array",
273
+ "items": { "$ref": "#/definitions/JsonArgValue" },
274
+ "description": "Arguments to pass to the helper"
275
+ },
276
+ "negate": {
277
+ "type": "boolean",
278
+ "description": "If true, negate the result"
279
+ }
280
+ }
281
+ },
282
+ "ExprCheck": {
283
+ "type": "object",
284
+ "required": ["type", "expr"],
285
+ "additionalProperties": false,
286
+ "properties": {
287
+ "type": { "const": "expr" },
288
+ "expr": {
289
+ "type": "string",
290
+ "description": "JavaScript expression to evaluate (sandboxed)"
291
+ }
292
+ }
293
+ },
294
+ "AndCheck": {
295
+ "type": "object",
296
+ "required": ["type", "conditions"],
297
+ "additionalProperties": false,
298
+ "properties": {
299
+ "type": { "const": "and" },
300
+ "conditions": {
301
+ "type": "array",
302
+ "items": { "$ref": "#/definitions/JsonCheck" },
303
+ "minItems": 1
304
+ }
305
+ }
306
+ },
307
+ "OrCheck": {
308
+ "type": "object",
309
+ "required": ["type", "conditions"],
310
+ "additionalProperties": false,
311
+ "properties": {
312
+ "type": { "const": "or" },
313
+ "conditions": {
314
+ "type": "array",
315
+ "items": { "$ref": "#/definitions/JsonCheck" },
316
+ "minItems": 1
317
+ }
318
+ }
319
+ },
320
+ "NotCheck": {
321
+ "type": "object",
322
+ "required": ["type", "condition"],
323
+ "additionalProperties": false,
324
+ "properties": {
325
+ "type": { "const": "not" },
326
+ "condition": { "$ref": "#/definitions/JsonCheck" }
327
+ }
328
+ },
329
+ "JsonArgValue": {
330
+ "oneOf": [
331
+ { "type": "string" },
332
+ { "type": "number" },
333
+ { "type": "boolean" },
334
+ { "type": "null" },
335
+ { "$ref": "#/definitions/RefArg" }
336
+ ]
337
+ },
338
+ "RefArg": {
339
+ "type": "object",
340
+ "required": ["$ref"],
341
+ "additionalProperties": false,
342
+ "properties": {
343
+ "$ref": {
344
+ "type": "string",
345
+ "enum": ["node", "node.id", "node.type", "node.children", "node.params", "node.rawText"]
346
+ }
347
+ }
348
+ }
349
+ }
350
+ }