Rule templates API
These endpoints list, create, change and apply rule templates, and save a scope's current rules as a new template. They do from a script what the Templates page does.
What a template is
A rule template is a named, ordered set of rules you can add to a rule scope (your global rules, or one machine's) in one step. See Rule templates for the page itself.
There are two kinds. Your own templates are created by you and seen only by you. EyalSec templates are published by EyalSec for every account: you can apply them but not change them.
Applying a template copies its rules and settings into the scope. From then on they are ordinary machine rules and settings: edit, disable or delete them like any other. Changing or deleting the template later does not touch what was already applied from it.
In the calls below, machine is a machine's public_id, or an empty string "" for your global rules.
List templates
GET /api/rule_templates/ lists every template you can see: your own and the EyalSec ones, each with its rules in order. It is a read.
GET /api/rule_templates/
{
"templates": [
{
"id": 3,
"owner": "eyalsec",
"name": "OWASP basics",
"description": "",
"items": [
{ "source": "socket", "event_pattern": "exec|eval",
"mode": 2, "sanitize_scope": "any", "position": 0 }
],
"settings": { "watch": { "gates": { "socket": "on" } } }
}
],
"categories": [
{ "key": "protect", "name": "Block attacks", "blurb": "Stop the attack, not only report it. Needs Raise." }
]
}
owner is me for your own templates and eyalsec for the published ones. The ready-made templates also carry product, the product whose events their rules are written for (for example "product": "python" for es-python); other templates have no product field. They also carry category, the key of the entry in categories they are listed under on the Templates page (for example "category": "protect"). Another EyalSec template carries category when an EyalSec administrator filed it under one. categories lists every category in the order the page shows them; a template with no category field is not in one. An item carries the same four fields a rule does: source, event_pattern, mode and sanitize_scope. It has no enabled flag (an applied rule is always enabled) and no polarity (it follows the mode).
settings is what the template sets besides rules, and {} when it sets nothing else. Each section is optional:
| Section | Shape | Values |
|---|---|---|
watch |
{"gates": {"socket": "on", "report_env": "off"}} |
a taint source or switch, on or off |
web |
{"settings": {"block": "on", "rate.limit": "100"}} |
any per-machine web protection setting |
outbound |
{"default_action": "deny", "mode": "all", "on_violation": "block", "rules": [{"action": "deny", "target": "@metadata", "note": ""}]} |
as outbound connections takes them |
unique |
{"axes": {"python": ["source", "detail"]}} |
per product, the fields that make an event unique |
Create a template
POST /api/rule_templates/create/ creates one of your own templates and answers 201 with it. It is a write.
POST /api/rule_templates/create/
JSON body: name (required, unique among your templates, ignoring case), description (optional) and items (the rules). Each item is checked exactly as creating a rule checks a rule, so a template can never hold a rule that would be refused. A missing sanitize_scope means any.
settings (optional) takes the shape the list shows. Each value is checked as the Configure window checks it, and an unknown section or key is refused.
You can have at most 50 templates, with at most 200 items each.
curl -s https://eyalsec.com/api/rule_templates/create/ \
-H "X-API-Key: es2_EXAMPLEKEYdoNotUse0000000000000000000000" \
-H "Content-Type: application/json" \
-d '{"name":"My web app","items":[{"source":"socket","event_pattern":"exec|eval","mode":2}]}'
{ "id": 12, "owner": "me", "name": "My web app", "description": "",
"items": [ { "source": "socket", "event_pattern": "exec|eval", "mode": 2,
"sanitize_scope": "any", "position": 0 } ] }
Update a template
PATCH /api/rule_templates/{id}/update/ renames one of your templates and replaces its whole rule list. It is a write and answers 204.
PATCH /api/rule_templates/{id}/update/
It takes the same body as create. The items you send replace the old list, so include every rule you want to keep. settings, when sent, replaces the old settings; leave it out to keep them. A template that is not yours, which includes every EyalSec template, is a 404.
curl -s -X PATCH https://eyalsec.com/api/rule_templates/12/update/ \
-H "X-API-Key: es2_EXAMPLEKEYdoNotUse0000000000000000000000" \
-H "Content-Type: application/json" \
-d '{"name":"My web app","items":[{"source":"any","event_pattern":"eval","mode":2}]}'
Delete a template
DELETE /api/rule_templates/{id}/delete/ deletes one of your templates. It is a write and answers 204; rules already applied from it stay.
DELETE /api/rule_templates/{id}/delete/
A template that is not yours is a 404.
Apply a template
POST /api/rule_templates/{id}/apply/ adds a template's rules and settings to a scope. It works on your own templates and on EyalSec ones, and it is a write. See applying a template.
POST /api/rule_templates/{id}/apply/
JSON body: machine, a public_id, or "" for your global rules.
curl -s https://eyalsec.com/api/rule_templates/3/apply/ \
-H "X-API-Key: es2_EXAMPLEKEYdoNotUse0000000000000000000000" \
-H "Content-Type: application/json" \
-d '{"machine":""}'
{ "added": 5, "skipped_raise": 2, "skipped_duplicate": 1, "sources_on": [],
"settings_set": ["watch.socket", "outbound.rules"], "settings_skipped_raise": 0,
"settings_skipped_scope": 1, "settings_skipped_full": 0 }
Nothing is replaced: the scope keeps its rules and the template's rules are added to them, enabled. Two kinds of item are skipped rather than added, and both are counted, so a partial apply is never silent:
skipped_duplicate: the scope already has a rule with the samesource,event_pattern,modeandsanitize_scope. Applying the same template twice adds nothing the second time.skipped_raise: a Raise rule (mode 3) from an older template that is narrowed bysanitize_scope, which nothing could enforce. The rest of the template still applies.
Settings are written into the scope, each as the Configure window would write it. A setting the template leaves out is not touched, and outbound rules are added after the scope's own. The answer names what changed and counts what did not:
settings_set: each setting that changed, assection.key(outbound.ruleswhen outbound rules were added). A setting already as the template says is not listed.settings_skipped_raise: always0, since every account can block; kept for older scripts.settings_skipped_scope: settings nothing in the scope reads, such as a source the machine's product does not have, or a product your account does not hold.settings_skipped_full: outbound rules left out because the scope already holds the most it can.
A template you cannot see is a 404.
Save a scope as a template
POST /api/rule_templates/snapshot/ saves the rules currently in a scope, and the settings that scope sets itself, as a new template of your own and answers 201 with it. It is a write. See saving a scope as a template.
POST /api/rule_templates/snapshot/
JSON body: name (required), description (optional) and machine (a public_id, or "" for your global rules). Disabled rules are saved too, since a template item has no enabled flag. The same limits of 50 templates and 200 items apply.
curl -s https://eyalsec.com/api/rule_templates/snapshot/ \
-H "X-API-Key: es2_EXAMPLEKEYdoNotUse0000000000000000000000" \
-H "Content-Type: application/json" \
-d '{"name":"prod baseline","machine":""}'
{ "id": 13, "owner": "me", "name": "prod baseline", "description": "", "items": [] }