{"id":18204376,"url":"https://github.com/3p3r/tree-sitter-eventrule","last_synced_at":"2025-04-02T13:31:53.032Z","repository":{"id":65201701,"uuid":"587029675","full_name":"3p3r/tree-sitter-eventrule","owner":"3p3r","description":"Grammar for AWS Event Rules: https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns-content-based-filtering.html","archived":false,"fork":false,"pushed_at":"2024-04-10T16:31:54.000Z","size":893,"stargazers_count":9,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-17T01:51:16.280Z","etag":null,"topics":["aws-eventbridge","parser","tree-sitter"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/3p3r.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-09T19:50:59.000Z","updated_at":"2024-04-02T02:34:49.000Z","dependencies_parsed_at":"2024-04-10T17:56:24.011Z","dependency_job_id":null,"html_url":"https://github.com/3p3r/tree-sitter-eventrule","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3p3r%2Ftree-sitter-eventrule","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3p3r%2Ftree-sitter-eventrule/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3p3r%2Ftree-sitter-eventrule/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3p3r%2Ftree-sitter-eventrule/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/3p3r","download_url":"https://codeload.github.com/3p3r/tree-sitter-eventrule/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246823817,"owners_count":20839779,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws-eventbridge","parser","tree-sitter"],"created_at":"2024-11-03T11:03:58.159Z","updated_at":"2025-04-02T13:31:51.688Z","avatar_url":"https://github.com/3p3r.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tree-sitter-eventrule\n\nGrammar for AWS Event Rules:\n\u003chttps://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns-content-based-filtering.html\u003e\n\nEvent Rules are JSON documents that are used to filter other JSON documents.  \nOn AWS EventBridge, they are used to filter AWS CloudWatch Events.\n\nComparing to other policy formats, Event Rules are limited in terms of features,\nbut are easy to understand for non-technical folks and are an extension to JSON.\n\n![Syntax Highlighting](highlight.png)\n\n## `rule2rego` utility\n\nThis utility is shipped with the npm package and is a small compiler that makes\nOPA REGO policies from AWS Event Rule patterns. It requires NodeJS version which\nis capable of running WASM binaries (recent version). WASM is used to parse the\nJSON input and generate the REGO policy.\n\n```bash\n$ npm install -g tree-sitter-eventrule\n$ rule2rego --help\nCompiles AWS Event Rule pattern JSON to OPA REGO policy.\nUsage: rule2rego [\u003crule\u003e.json] [folder]\n```\n\nFor example, for the following input Rule Event:\n\n```json\n// Effect of \"source\" \u0026\u0026 (\"metricName\" || \"namespace\")\n{\n  \"source\": [\"aws.cloudwatch\"],\n  \"$or\": [\n    { \"metricName\": [\"CPUUtilization\", \"ReadLatency\"] },\n    { \"namespace\": [\"AWS/EC2\", \"AWS/ES\"] }\n  ]\n}\n```\n\nYou will get the following REGO policy output:\n\n```rego\npackage rule2rego\ndefault allow := false\ndefault allow_or_metricName := false\ndefault allow_or_namespace := false\ndefault allow_or := false\ndefault allow_source := false\nallow_or_metricName {\n\t({ \"CPUUtilization\", \"ReadLatency\" } \u0026 { input[\"metricName\"] }) == { input[\"metricName\"] }\n}\nallow_or_namespace {\n\t({ \"AWS/EC2\", \"AWS/ES\" } \u0026 { input[\"namespace\"] }) == { input[\"namespace\"] }\n}\nallow_or {\n\tallow_or_metricName\n}\nallow_or {\n\tallow_or_namespace\n}\nallow_source {\n\t({ \"aws.cloudwatch\" } \u0026 { input[\"source\"] }) == { input[\"source\"] }\n}\nallow {\n\tallow_or\n\tallow_source\n}\n```\n\n\u003e If you compile a directory you will get each policy output separated by a single blank line. The final policy is a combination policy that checks if input matches _any_ of the generated policies.\n\nCompile the rules with OPA (example compiles to WASM)\n\n```sh\nnpx rule2rego rule.json \u003e policy.rego\nopa build -t wasm -e rule2rego -o bundle.tar.gz policy.rego\ntar -xvf bundle.tar.gz /policy.wasm\n```\n\nNow you can use the WASM compiled OPA in any application that supports WASM.\n\n\u003e You can compile the policies to any format opa supports and use it however you want. This applications main concern is to convert cloudwatch event rules to rego.\n\nA quick way to write each rule.json rule in a folder to separate policies is:\n\n```sh\nmkdir -p out\nnpx rule2rego folder | awk -v RS= '{print \u003e (\"out/\" NR \".rego\")}'\n# write each converted rule to out/#.rego\n```\n\n## `rule2rego` in javascript application\n\nIn addition to being a CLI utility you can use rule2rego directly in your JS applications.\n\n```javascript\nconst fs = require(\"fs\");\nconst path = require(\"path\");\nconst {compile} = require(\"tree-sitter-eventrule/dist/main\");\n\n(async () =\u003e {\n  const rules = await compile(\"rule.json\");\n  for (const rule of rules) {\n    console.log(rule);\n  }\n})()\n\n```\n\n## Contributing\n\n[See the contributing guide](CONTRIBUTING.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F3p3r%2Ftree-sitter-eventrule","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F3p3r%2Ftree-sitter-eventrule","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F3p3r%2Ftree-sitter-eventrule/lists"}