How to Configure Haystack MongoDB Permissions

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up haystack mongodb permissions by assigning database roles to the account your pipeline uses. These roles limit which collections your AI app can read or write. Many teams start by giving their Haystack app a broad admin role during testing. That quick shortcut often stays in place when the app goes live. Overly broad roles let a retrieval pipeline change records or drop whole collections by mistake. A small prompt bug or bad script can then wipe out production data. How haystack mong

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 haystack mongodb permissions by assigning database roles to the account your pipeline uses. These roles limit which collections your AI app can read or write. Many teams start by giving their Haystack app a broad admin role during testing. That quick shortcut often stays in place when the app goes live. Overly broad roles let a retrieval pipeline change records or drop whole collections by mistake. A small prompt bug or bad script can then wipe out production data.

How haystack mongodb permissions work at the database level

MongoDB controls access through role-based rules. Each role gives a user the right to run specific actions on set resources. A role applies to the database where you define it. You can narrow these permissions down to a single collection. Note that MongoDB Atlas users have different built-in roles than self-hosted database users. Both options still use the exact same set of basic privilege actions underneath.

Your Haystack components need specific rights based on their job in the pipeline. A retriever component only needs to search and pull documents for the language model. For this task, the built-in read role is the right choice. The read role lets the app read all non-system collections in that database. It allows actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. It does not allow insert, update, remove, or dropCollection actions.

An indexing pipeline needs more power because it saves new embeddings and documents. The built-in readWrite role includes everything in the read role plus data change rights. Its allowed actions include insert, update, remove, createCollection, createIndex, dropIndex, and dropCollection. Splitting your Haystack setup into two database users keeps your data much safer. Give the indexing job a custom role or the readWrite role on one collection. Give the query pipeline the read role so it cannot alter your stored documents.

What native database roles solve and what they leave open

Native roles are great for stopping unauthorized writes or deletes at the login level. When a Haystack retriever uses the read role, it cannot drop a collection. It also cannot insert fake records or delete customer documents during a query run. Custom roles let you lock a pipeline to one specific document store collection. This stops the AI app from wandering into other tables in the same database.

Still, database roles only check who logs in and which collection they touch. Once a role grants the find action, the user can read every field inside. A collection-level role cannot inspect the individual queries your Haystack pipeline sends in flight. It cannot block a broad query that pulls thousands of extra records at once. It also cannot hide sensitive fields inside the documents that match a search.

MongoDB Authentication & Authorization + AI Agent Permissions: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Real documents often mix useful text with private user details in the same record. When Haystack pulls a document for context, it gets the whole payload back. This payload might hold emails, phone numbers, card numbers, or national IDs. Native roles do not rewrite matched fields in results before they reach the model. Teams also lack a clear log tying each database query to a specific user.

Separating indexing rights from query rights in production

Good security starts with separating how your AI app writes and reads data. Build one service account just for the background script that loads new documents. Even then, the standard readWrite role might be too wide for daily use. That role includes the dropCollection and dropIndex actions by default. A bad deployment script could erase your vector index and break search for everyone. Instead, build a custom role that only grants insert, update, and find on one collection.

Your live query app should run under a separate account with strict read limits. This account only runs when a user asks a question through your Haystack app. Because it only has read rights, a prompt trick cannot force a database write. Still, you must watch what data leaves the collection during those read calls. Controlling what happens inside the active session is the final step for safe AI data access.

Check MongoDB commands and documents on the wire

MongoDB roles and SCRAM login decide who opens a session. They do not check single commands or redact sensitive fields inside returned documents. With hoop.dev, you run an open-source proxy, a sidecar for your MongoDB. It reads the MongoDB wire protocol and checks commands and bulk writes as JSON while passing login through untouched.

Before a command runs, hoop.dev guardrails check its effect and refuse banned drops or writes with MongoDB error code 13. On the way back, hoop.dev data masking rewrites sensitive fields in returned documents that match your rules. Every command and verdict is logged. See the open-source code on GitHub.

Frequently asked questions

Which built-in role should a Haystack retriever use?

Assign the built-in read role to the database user for your Haystack retriever. This role grants find and listCollections actions while blocking insert, update, remove, and dropCollection.

Why is the standard readWrite role risky for Haystack indexing pipelines?

The readWrite role allows insert and update actions, but it also allows dropCollection and dropIndex. A custom role that removes drop actions keeps your stored embeddings safe from mistakes.

Can MongoDB roles stop Haystack from pulling sensitive fields in a collection?

No, native roles grant access down to the collection level, not the field level. Once an app has the find action, it can read emails or card numbers stored there.

The query runs. The emails stay hidden.