How to Configure Mastra CockroachDB Audit Log

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up a mastra cockroachdb audit log by turning on table logging in your database. Next, you route those log records to a central storage bucket or log tool. Teams often build AI agents in Mastra that query SQL databases directly. They grant the agent a single database user role to run these queries. When that role has broad permissions, the agent can read or change sensitive tables. How the mastra cockroachdb audit log works Mastra lets developers build AI agents that run workflows and

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You set up a mastra cockroachdb audit log by turning on table logging in your database. Next, you route those log records to a central storage bucket or log tool. Teams often build AI agents in Mastra that query SQL databases directly. They grant the agent a single database user role to run these queries. When that role has broad permissions, the agent can read or change sensitive tables.

How the mastra cockroachdb audit log works

Mastra lets developers build AI agents that run workflows and call external tools. Many teams give these agents a database client to read and write state. CockroachDB serves as the distributed SQL database for these agent tasks. To track what the agent does, admins turn on the native audit log feature.

In CockroachDB, admins mark specific tables or roles for auditing. When the Mastra agent runs a query against a marked table, the database records the event. The log entry captures the SQL statement, the database role, and the timestamp. It writes these entries to local log files on each database node. From there, teams forward the files to an external log sink for review.

Admins also set up role-based permissions inside the database. They create a dedicated SQL user for the Mastra app. Then they assign read or write grants on specific tables to that user. This ensures the audit log tags every agent query with that specific database role.

What native database logging solves and what it misses

Native audit logging gives security teams a clear record of past database events. If a Mastra agent alters a row by mistake, you can check the log. You can see which SQL statement ran and when the change happened. This helps teams pass security reviews and debug broken agent workflows.

Still, native database setup and logging leave an operational gap open. Database roles only decide who can log in and which tables they can touch. They do not inspect what individual statements do in flight before execution. And an audit log only tells you about a bad query after it finishes.

Shared service roles also create a tracking problem in the logs. Every Mastra agent workflow uses the same static database key to log in. Because of this, the database log shows the same SQL user for every query. You cannot tell which human user or specific agent task triggered a given statement.

Audit Log Integrity + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Finally, native logs do not stop sensitive data from leaving the database. If an agent queries a customer table, the database returns the full rows. Raw emails, phone numbers, card numbers, and national IDs flow right back to the model. Even worse, the audit log itself might store those sensitive values in plain text.

Managing credentials and data exposure for AI agents

Storing long-lived database keys inside your Mastra app setup adds risk. If someone leaks the app settings, attackers gain direct access to your data. Teams must rotate these static keys by hand across every running service. This manual rotation takes time and often breaks active agent workflows.

To reduce risk, teams should separate identity checks from the database itself. They also need a way to inspect queries before the database runs them. Rewriting matched fields in results keeps raw emails and card numbers out of model memory. Blocking dangerous delete statements in flight stops an agent from dropping production data.

Check database and API queries on the wire

Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev runs beside those services as an open-source proxy, a sidecar for your Postgres or API. It inspects requests coming in and results going back on the wire.

Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run. On responses, hoop.dev data masking rewrites matched sensitive values in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.

Frequently asked questions

Does CockroachDB log every Mastra agent query by default?

No, the database does not log all queries out of the box. Admins must turn on audit rules for specific tables or user roles first. Turning on full logging for every table can slow down your cluster.

Why do all Mastra queries show the same user in the audit log?

Most apps use one shared database user and password for all connections. CockroachDB only sees that single shared role when the agent sends a query. It cannot see the end user identity inside the Mastra app.

Can native audit logs hide sensitive data from AI agents?

No, an audit log only writes down what happened during a query. It does not change or filter the results sent back to the client. You need inline data masking to rewrite emails or card numbers in results.

The query runs. The emails stay hidden.