Practical technical guides / Agent-era architecture: CQRS and event sourcing (single-writer event topics enforced by broker permissions)

Only the Owning Service May Write to an Event Topic: Broker Permissions and a Provenance Check Stop an Agent Publishing Facts

An agent with write access to an event topic published six refunds and overpaid 95.49. Broker write permissions and a provenance check refused all six.

By Sandeep Belgavi · Published 2026-11-19

Only the owning service writes the event topic
Original explanatory diagram; synthetic values where shown.

An agent with write access to an event topic published six refunds and overpaid 95.49. Broker write permissions and a provenance check refused all six.

The agent announced a refund nobody approved

Geetha runs the help desk at Belgavi's Book Store. Refunds are handled by a refund service that owns each order's refund balance. Downstream, a ledger service listens on a topic called refund.events and pays out every RefundIssued event it receives, because in an event-driven system an event is a fact: something that already happened, which consumers may act on without asking anyone.

A help-desk agent was added to triage cases. To keep the integration simple, someone gave it a publish_event tool and broker credentials that could write to refund.events. On a quiet Tuesday it handled six cases, decided each of them deserved a refund and published six RefundIssued events. The ledger paid all six, 131.49 in total. Five of those orders should not have been paid that much: one had already been refunded, one was delivered 41 days ago against a 30-day window, one asked for more than the customer paid, one repeated an earlier proposal after the agent re-planned, and one referred to an order number misread from an email. The overpayment came to 95.49.

Nothing malfunctioned. The ledger did what an event consumer should do. The defect was a permission: a principal that does not own refunds was allowed to write facts about them.

Facts need a single writer

The design rule is familiar. An agent sends commands to the service that owns a decision, and that service checks its invariants and publishes the resulting event. The companion guide on agents as command senders in CQRS systems covers that side: read models as context, the aggregate as the gate. This guide is about the other half, which is usually left as a convention: making sure nothing except the owner can publish the event at all.

Consumers trust an event topic because they assume a single writer. If that assumption is not enforced by the broker, every new integration, test script or agent with broad credentials is a potential author of facts.

The same six proposals without write access

The companion example replays the six proposals in three configurations.

ConfigurationRefunds paidMoney paidOrders overpaid
Agent may write to refund.events6131.495, by 95.49
Same, with per-topic writer permissions0 (all 6 publishes refused)0.000
Agent sends commands; refund service publishes124.000

With the writer permission in place, the agent's publish fails immediately with an authorisation error. That is the correct failure: loud, at the source, before any consumer acts. In the third configuration the refund service accepted one proposal, rejected an already-refunded order, an amount above what was paid and an unknown order number, sent the 41-day-old delivery to a person, and recognised the repeated proposal by its command id. Every published event carries a causation_id pointing at the command that caused it.

Give the event topic one writer

Kafka is a common broker for this, and its authorisation model maps directly onto the rule. With an authorizer configured (in KRaft clusters, org.apache.kafka.metadata.authorizer.StandardAuthorizer), an ACL says that a principal is allowed or denied an operation on a resource pattern, and the documentation states that principals without an ACL allowing an operation are denied by default. Writing to a topic is the Write operation.

bin/kafka-acls.sh --bootstrap-server localhost:9092 --add \
  --allow-principal User:refund-service --operation Write --operation Describe \
  --topic refund.events

bin/kafka-acls.sh --bootstrap-server localhost:9092 --add \
  --allow-principal User:help-desk-agent --operation Write --operation Describe \
  --topic refund.commands

Three settings in the same documentation quietly undo this.

  • allow.everyone.if.no.acl.found=true in the broker configuration allows everyone access to any resource that has no ACLs. A new event topic created before its ACLs exist is then writable by any authenticated principal.
  • Wildcard and prefixed resource patterns. An ACL with --topic '*', or a prefixed pattern such as refund, written to let one service produce to its own topics, also grants write on refund.events.
  • The --producer convenience option generates Write, Describe and Create on the topic. That is reasonable for the owner and too broad for anything else.

Super users bypass ACLs entirely, so agent workloads must never run with a super-user principal, even in staging.

Test the permission, not the convention

A rule that lives only in a design document disappears in the next integration. Put it in a test that runs against a staging broker with production ACLs, using the agent's real credentials:

import pytest

def test_agent_cannot_write_refund_events(agent_producer, authorization_error):
    # agent_producer connects with the agent's own credentials;
    # authorization_error is the topic-authorisation exception of your client library.
    with pytest.raises(authorization_error):
        agent_producer.send_and_wait("refund.events", b'{"type": "RefundIssued"}')

def test_agent_can_write_commands(agent_producer):
    agent_producer.send_and_wait("refund.commands", b'{"command_id": "probe-1"}')

The fixtures hide the client library; the point is that the test expects a refusal for every event topic whose consumers move money, stock or permissions. The companion's offline equivalent asserts that all six agent publishes are refused when writer permissions are on.

A consumer that checks provenance

Broker permissions can be misconfigured, so the ledger should also refuse events it cannot trace. In the example it drops any RefundIssued that was not written by the refund service or has no causation_id:

def check_event_shape(event):
    problems = []
    if event.get("_writer") != "refund-service":
        problems.append("written by " + str(event.get("_writer")))
    if not event.get("causation_id"):
        problems.append("no causation_id")
    return problems

The writer identity must come from something the publisher cannot fill in: the broker's authenticated principal recorded by an interceptor, or a signature over the event envelope. A source field in the payload proves nothing. When the check fails, the consumer should alert, not only drop, because a refused event means either a permission is wrong or someone is probing.

What the agent still owns

None of this makes the agent less useful. It still reads the case, gathers evidence, chooses the amount and writes the explanation, and the reply to its command, such as a rejection with a reason, becomes its next observation. It may also publish facts about its own work, such as TriageCompleted on a topic its service owns, because those describe decisions it did make. What it must not hold is write access to facts another service decides.

Removing write access an agent already has

If agents already publish to event topics, removing the permission in one step breaks whatever depended on it. A staged change keeps the ledger safe from the first day.

  1. Inventory first. For each event topic, list the principals with Write and the code paths that use them. Agent workloads are usually easy to spot because their credentials were created recently and for one integration.
  2. Turn on the consumer-side provenance check in report-only mode. Log every event that would be refused, with its writer, for a week. That log is the real list of unofficial writers.
  3. Give each unofficial writer a command topic and a handler in the owning service, then switch the writer to commands.
  4. Remove the Write ACLs, switch the provenance check from report-only to refuse, and add the staging test above so the permission cannot come back unnoticed.

Failure modes to test

  • A new event topic created by automation before its ACLs. Check whether the broker allows access when no ACL exists.
  • A prefixed or wildcard producer ACL added for an unrelated service. List every principal with Write on each event topic after every ACL change.
  • An agent principal reused by a human operator or a batch job. One principal per workload keeps the audit trail meaningful.
  • A consumer that trusts a payload field for provenance. Send an event with a forged source and expect it to be refused.
  • The same command id with a different amount. Decide whether that is a conflict to reject; the example returns the recorded outcome.

Verification

Run the companion: python example.py prints the three configurations and python -m unittest -v test_example.py asserts 131.49 paid and 95.49 overpaid with direct publishing, six refusals with writer permissions, and one 24.00 refund with commands. In your own system, list every principal with write access to each event topic and compare the list with the topic's documented owner. Any difference is either a missing ACL or an undocumented writer, and both deserve a fix.

Primary references and authorship

Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/agent_commands_not_events. Views are my own and do not represent any employer, past or present.