Introducing AI agent: Get information about your infrastructure faster. Learn more >

From schedule to event-driven: Introducing events in policy language

Posted by Victor Moene
November 27, 2026

CFEngine’s default model, pulling and evaluating on a mostly fixed schedule, is great at continuously converging your systems to the desired state. But convergence happens on a schedule. If something drifts between two agent runs, it stays wrong until the next run comes along. Sometimes you want to react right away.

That’s what cf-reactor is for. In CFEngine 3.29 and beyond, cf-reactor listens for events on the host, such as a file being modified, and acts on them immediately instead of waiting for the next run.

In this post, we’ll walk through an example of how cf-reactor can complement an existing policy.

The starting policy

We’ll start with a simple policy that makes sure root’s SSH private key is never readable by others:

bundle agent restrict_ssh_key_permissions
{
  files:
    "/root/.ssh/id_ed25519" perms => m("600");
}

To run it, add it to your policy set and append its bundle to the bundlesequence. On every run, cf-agent checks the permissions of the key and sets them back to 0600 if they have changed. But if someone makes the key readable by others immediately after a run, it stays that way until the next one.

Reacting to an event

The new events promise type addresses this. It has two main attributes, when and then, and it works just like it sounds: when this happens, then run this. Expanding on our use case above, let’s protect the SSH key file by ensuring the permissions are fixed quickly if they change:

bundle reactor enforce_ssh_key_perms
{
  events:
    "protect SSH key"
      when => permissions_changed("/root/.ssh/id_ed25519"),
      then => restrict_ssh_key_permissions;
}

The when attribute takes a body when that describes the event to listen for. The then attribute takes the bundle, or list of bundles, to run when the event fires. These can be common, agent, or reactor bundles.

In the example above, permissions_changed needs to be defined somewhere in your policy set, but once defined, it can be reused for many events. Its definition is straight forward:

body when permissions_changed(file)
{
  file_permissions_changed => "$(file)";
}

Notice that we didn’t have to change the bundle restrict_ssh_key_permissions at all. The same bundle that cf-agent runs on schedule now also runs the moment the permissions of the key file change.

Event types currently cover files, users, and packages. See the documentation for the full list. Each body when can only describe one event type. For example, you can’t combine file_permissions_changed and file_owner_changed in the same body. To react to both, write two separate bodies, each used by its own events promise.

Logging the changes

In many cases, it can be useful to do something more after we’ve fixed the issue. This can, for example, be to explicitly log the action. By default, cf-reactor and cf-agent log changes with the info log level, if you’re not running with -I or not watching the output, you might not notice this change happened. We can make a reactor bundle when implementing something only for cf-reactor:

bundle reactor log_ssh_key_permissions_change
{
  reports:
    "Permissions of /root/.ssh/id_ed25519 changed on $(sys.fqhost) \
and were restored";
}

This bundle has no events promise, which brings us to an important distinction. There are two kinds of reactor bundles:

  1. Bundles with events promises: Used to set up events. These bundles may only contain events promises, as well as classes and variables to set them up. Other promises which make changes to the system are not allowed as it would be ambiguous when these changes should be made.
  2. Bundles without events promises: Callable by events, used to make changes to the system, similar to agent bundles. These can use all the promise types except events.

When cf-reactor reads the policy, it evaluates the reactor bundles that contain events promises and sets up event listeners. The ones without events promises, like log_ssh_key_permissions_change, are not evaluated on their own - they only run when an events promise calls them through then.

To wire it up, we add it to our promise’s then bundle sequence:

   "protect SSH key"
     when => permissions_changed("/root/.ssh/id_ed25519"),
     then => restrict_ssh_key_permissions;
     then => {
       restrict_ssh_key_permissions, log_ssh_key_permissions_change
     };

Now every change to the permissions of the key is both reverted and logged.

Wrapping up

Event-driven policy with cf-reactor adds a new tool to the CFEngine toolbox: policy that reacts to events the moment they happen. You can reuse existing agent and common bundles as event handlers, or write new reactor bundles only for cf-reactor.

The full policy for this example is available here. We hope it inspires you to write some event-driven policy of your own!

Share your thoughts

Now you know the basics of cf-reactor. If you have any cool use cases for this policy component or have any questions about CFEngine in general, let us know at GitHub Discussions. We are eager to hear from you.