PL EN
WB-FA04EAC8TLP:CLEARWazuh 4

Wazuh rules from a repository: deployment through the API and cluster sync

Deploy rules, decoders and CDB lists from a git repository to a Wazuh 4.14 manager or cluster: PUT /rules/files, logtest, validation, restart, RBAC, rollback.

Rules, decoders and CDB lists kept in a git repository can be deployed to a Wazuh 4.14 manager only through the server API, without logging in to a node. This post describes the endpoints for uploading files, testing through logtest, configuration validation, restart, cluster synchronization and RBAC permissions, and at the end proposes a pipeline built from them.

The values in the examples are synthetic: addresses from the 192.0.2.0/24 range, the host example.com, rule ID 100010.

Where user files live

Wazuh separates the ruleset shipped with the product from user files. The /var/ossec/ruleset/rules directory is overwritten on upgrade, so custom rules go to /var/ossec/etc/rules/. The documentation recommends rule IDs from 100000 to 120000 and new files in the etc/rules directory for larger changes, and local_rules.xml for small fixes. How the directories are loaded is described in the post on ruleset loading.

File typeUser directoryEndpointrelative_dirname parameter
rules/var/ossec/etc/rules//rules/files/{filename}yes
decoders/var/ossec/etc/decoders//decoders/files/{filename}yes
CDB lists/var/ossec/etc/lists//lists/files/{filename}no

API responses show the path relative to /var/ossec: etc/rules/new_rule.xml, etc/decoders/new_decoder.xml, etc/lists/audit-keys. A list file must also be registered in the <ruleset> block of ossec.conf as <list>etc/lists/example-blocklist</list>. Uploading the file through the API does not add it. The list format and the lookup attributes are described in the post on CDB lists.

Authentication

Every endpoint except login requires a JWT token. The documentation shows how to get it with the raw=true parameter, which returns the token as plain text:

TOKEN=$(curl -u <WAZUH_API_USER>:<WAZUH_API_PASSWORD> -k -X POST "https://localhost:55000/security/user/authenticate?raw=true")

The -k flag skips server certificate verification, and the documentation gives it for the default self-signed certificates. I use it the same way in the later examples. The API specification says the token expires after auth_token_exp_timeout seconds (900 by default), and the value is changed through PUT /security/config. A pipeline should therefore log in at the start of every run and renew the token after a restart.

RBAC permissions

The specification assigns a list of actions (x-rbac-actions) to every endpoint. The table collects the ones the pipeline needs.

EndpointActions per the specification
PUT /rules/files/{filename}rules:update, rules:delete
PUT /decoders/files/{filename}decoders:update, decoders:delete
PUT /lists/files/{filename}lists:update, lists:delete
GET on a file (reading the content)rules:read, decoders:read, lists:read
DELETE on a filerules:delete, decoders:delete, lists:delete
PUT /logtest, DELETE /logtest/sessions/{token}logtest:run
GET /manager/configuration/validationmanager:read
GET /cluster/configuration/validation, GET /cluster/ruleset/synchronizationcluster:read
PUT /manager/restartmanager:read, manager:restart
PUT /cluster/restartcluster:read, cluster:restart

For PUT on rule, decoder and list files the specification lists two actions each: update and delete. The RBAC reference page lists PUT under both actions for decoders and lists, but for rules only under rules:update. The pipeline policy therefore includes both actions. The resource for PUT is *:*, and in a policy it is written as *:*:*. DELETE uses the resources rule:file, decoder:file and list:file:

{
  "actions": ["rules:update", "rules:delete"],
  "resources": ["*:*:*"],
  "effect": "allow"
}

For PUT /cluster/restart the resource is node:id, so the permission can be limited to selected nodes, for example node:id:worker1.

Uploading, reading and deleting files

PUT /rules/files/{filename} uploads or replaces a user rules file. The request body is the raw file content with the Content-Type: application/octet-stream header. Any other type ends with a 406 response and the message The body type is not the one specified in the content-type. A 413 response means the maximum body size was exceeded. For decoders and lists only the path changes.

ParameterApplies toMeaning
overwriterules, decoders, listsfalse by default: writing to an existing file ends with an error. With true the file is replaced
relative_dirnamerules, decodersRelative directory of the file, the specification describes it as Filter by relative directory name
rawGET on a fileResponse as plain text instead of JSON
wait_for_completeallDisables the timeout response, false by default

I chose the curl syntax for uploading files based on the specification. The getting-started page has no upload example. --data-binary sends the file unchanged:

API="https://localhost:55000"

# Upload (or replace) a rules file
curl -k -X PUT "$API/rules/files/example_rules.xml?overwrite=true" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/octet-stream" \
  --data-binary @rules/example_rules.xml

# Read it back as plain text
curl -k -X GET "$API/rules/files/example_rules.xml?raw=true" \
  -H "Authorization: Bearer $TOKEN"

# Delete it
curl -k -X DELETE "$API/rules/files/example_rules.xml" \
  -H "Authorization: Bearer $TOKEN"

A successful response has "error": 0 and the file path in affected_items. The list of rule files from GET /rules/files contains filename, relative_dirname and status (enabled or disabled), and GET /rules?rule_ids=100010 returns the rule itself together with the file where it is defined. The first endpoint filters by filename and relative_dirname, so it works for checking that the uploaded file is visible in etc/rules.

The example file rules/example_rules.xml is based on a rule from the documentation (level 0 there, level 3 here):

<group name="example_rules,">
  <rule id="100010" level="3">
    <program_name>example</program_name>
    <description>Example: user logged in</description>
  </rule>
</group>

Testing through the logtest API

Logtest loads rules and decoders from the manager’s disk. The documentation says that saving the files is enough for a test, and a restart is needed only to generate alerts. In a cluster, logtest events are processed by the master node. The tool options and the output phases are described in the post on wazuh-logtest.

The request requires the fields event, log_format and location, and token is optional. Without a token (or with an invalid one) a new session starts, which reloads rules and decoders. As a result, after an upload every request without a token sees the current ruleset. The file tests/example-login.json with the request:

{
  "event": "Dec 25 20:45:02 agent-01 example[12345]: User 'admin' logged from '192.0.2.10'",
  "log_format": "syslog",
  "location": "master->/var/log/syslog"
}
curl -k -X PUT "$API/logtest?pretty=true" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  --data-binary @tests/example-login.json

In the response data.output.rule.id points to the matched rule, data.alert says whether the event will generate an alert, and data.token identifies the session. DELETE /logtest/sessions/{token} closes the session, which frees the event history and the loaded rules. An unused session also closes itself after session_timeout (15 minutes by default), and the limit of open sessions, max_sessions, is 64 by default. With more than 64 sessions, the longest inactive one is closed.

Validation, restart and cluster synchronization

Configuration validation returns a status per node:

curl -k -X GET "$API/cluster/configuration/validation?pretty=true" \
  -H "Authorization: Bearer $TOKEN"

A successful response has entries with status: OK in affected_items for every node. For a single manager use GET /manager/configuration/validation. A restart is done by PUT /manager/restart or PUT /cluster/restart. The second one takes nodes_list, which is node identifiers separated by commas, and without the parameter it restarts all of them.

# Restart a single worker, then check validation again after it is back
curl -k -X PUT "$API/cluster/restart?nodes_list=worker1" \
  -H "Authorization: Bearer $TOKEN"

The cluster synchronizes files from the master to the workers in the integrity thread. Among the synchronized files the documentation lists agent keys, user rules, decoders, custom SCA policies, CDB lists and group files. The local integrity thread on the master periodically computes MD5 sums and modification times of files, and the workers connect to the master themselves. The cluster log is /var/ossec/logs/cluster.log. Synchronization goes only from the master to the workers, so the pipeline uploads files to the master. Before a restart the files should reach the workers, and GET /cluster/ruleset/synchronization shows this directly: it returns synced: true or false for every node and covers only the user ruleset. Cluster state and node health are described in the post on cluster operations.

The specification also contains PUT /manager/analysisd/reload and PUT /cluster/analysisd/reload, but the documentation does not say whether the reload loads new rule files. The pipeline relies on the restart described in the rules documentation.

This part is a proposal, not a vendor procedure. It assumes a cluster with a master and a separate test manager.

Repository layout:

example-wazuh-ruleset/
  rules/example_rules.xml
  decoders/example_decoders.xml
  lists/example-blocklist
  tests/example-login.json
  deploy.sh

Logtest checks rules only after the files are uploaded to the server, so a test before production needs a test manager. The order of steps:

  1. Upload the files to the test manager (overwrite=true) and run logtest without a token with an assertion on rule.id.
  2. On production: download the current files with GET ...?raw=true as a rollback copy.
  3. Upload the files to the production master.
  4. Logtest with an assertion on rule.id.
  5. Wait until GET /cluster/ruleset/synchronization returns synced: true for all nodes.
  6. GET /cluster/configuration/validation, status: OK required on every node.
  7. PUT /cluster/restart, then a new token and another logtest.

The deploy.sh fragment covers steps 3 to 7, without the copy download from step 2:

#!/bin/bash
set -euo pipefail

API="https://wazuh-master.example.com:55000"
# Credentials live in a curl config file (user = "name:password"), not in argv
CURL=(curl -sS -k -K "$HOME/.config/example-wazuh/api.curlrc")

TOKEN=$("${CURL[@]}" -X POST "$API/security/user/authenticate?raw=true")
AUTH=(-H "Authorization: Bearer $TOKEN")

put_file() { # $1 = rules|decoders|lists, $2 = local file
  "${CURL[@]}" "${AUTH[@]}" -X PUT \
    "$API/$1/files/$(basename "$2")?overwrite=true" \
    -H "Content-Type: application/octet-stream" \
    --data-binary "@$2" | jq -e '.error == 0' >/dev/null
}

put_file decoders decoders/example_decoders.xml
put_file rules rules/example_rules.xml
put_file lists lists/example-blocklist

# Logtest without a token starts a new session that reloads rules and decoders
RES=$("${CURL[@]}" "${AUTH[@]}" -X PUT "$API/logtest" \
  -H "Content-Type: application/json" --data-binary @tests/example-login.json)
echo "$RES" | jq -e '(.data.output.rule.id | tostring) == "100010"' >/dev/null
"${CURL[@]}" "${AUTH[@]}" -X DELETE \
  "$API/logtest/sessions/$(echo "$RES" | jq -r '.data.token')" >/dev/null

# Wait until every node reports the custom ruleset as synchronized
until "${CURL[@]}" "${AUTH[@]}" "$API/cluster/ruleset/synchronization" \
  | jq -e '[.data.affected_items[].synced] | all' >/dev/null; do sleep 5; done

"${CURL[@]}" "${AUTH[@]}" "$API/cluster/configuration/validation" \
  | jq -e '[.data.affected_items[].status] | all(. == "OK")' >/dev/null

"${CURL[@]}" "${AUTH[@]}" -X PUT "$API/cluster/restart" | jq -e '.error == 0' >/dev/null

In jq I use the tostring assertion because the documentation shows rule.id as text ("5710"), while the example in the specification shows it as a number. The script ends at the restart because the documentation does not specify how long the API takes to come back. After the restart, get a new token, repeat logtest and check GET /rules?rule_ids=100010. I upload decoders before the rules that use them. The decoder syntax is described in the post on custom decoders.

Rollback

On an error at any step, it is enough to upload the previous version of the file with overwrite=true and then repeat the steps from logtest to restart. The previous version comes from the copy from step 2 or from git show <commit>:rules/example_rules.xml. A file added for the first time has no previous version, so it is rolled back with DELETE /rules/files/example_rules.xml (rules:delete).

What the documentation does not specify

  • Whether the validation endpoints check the syntax of uploaded rule, decoder and list files. The description only mentions configuration correctness.
  • How the API responds to an XML file with a syntax error, beyond the general 400 response, and whether logtest rejects a broken rules file.
  • Whether a file uploaded to a worker’s API is forwarded to the master. The examples in the documentation upload files to the master.
  • The synchronization interval from the master to the workers.
  • The allowed values of relative_dirname for PUT, beyond the one-sentence description in the specification.
  • Whether all actions in x-rbac-actions are required when there are several, and why the RBAC reference page does not list PUT /rules/files/{filename} under rules:delete.
  • The maximum body size after which the API returns 413.

Sources

↑↓ select Enter open Esc close