How to Configure Haystack MongoDB Least Privilege

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up haystack mongodb least privilege by creating database roles limited to specific collections. You then assign separate read and write users to your search and indexing pipelines. Many teams start by giving their Haystack app one shared database user with broad rights. That shortcut speeds up early tests, but it creates huge risks in production. If a prompt injection hits your pipeline, broad rights expose every collection in the database. An attacker or a bad script could alter records

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 least privilege by creating database roles limited to specific collections. You then assign separate read and write users to your search and indexing pipelines. Many teams start by giving their Haystack app one shared database user with broad rights. That shortcut speeds up early tests, but it creates huge risks in production. If a prompt injection hits your pipeline, broad rights expose every collection in the database. An attacker or a bad script could alter records or drop whole collections.

How haystack mongodb least privilege works in practice

MongoDB uses role-based authorization to control what a user or app can do. A role grants privileges to run sets of actions on defined database resources. Each role applies to the database where you define it. You can scope a role down to a single collection for tight control. MongoDB Atlas database users and self-hosted users have different built-in role options. Yet both setups build their roles from the same core privilege actions.

A Haystack app usually runs two distinct workflows against its document store. The indexing pipeline writes embeddings and text documents into your target collection. The query pipeline only needs to search and read documents during retrieval runs. Giving both pipelines the same database user violates the core principle of least privilege. Instead, you should split these duties across two distinct database users.

For the query pipeline, the built-in read role is the right starting point. The read role grants read access to all non-system collections in its database. It also covers the system JavaScript collection in that same database. It works through actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. It does not permit insert, update, remove, or dropCollection actions at all. If your database holds other collections, build a custom role for just the Haystack collection.

For the indexing pipeline, the built-in readWrite role provides the needed write actions. It includes every action in the read role plus the ability to change data. Those write actions include insert, update, remove, createCollection, createIndex, dropIndex, and dropCollection. Even here, a custom role is safer for day-to-day document indexing runs. Your daily ingestion job needs to insert and update records, not drop collections or indexes. Removing drop actions from the ingestion user prevents accidental data loss during pipeline runs.

Why native roles leave a runtime gap

Native database roles check who can log in and which collections they can touch. That setup blocks a retrieval pipeline from deleting tables or reading other databases. Still, database roles do not inspect individual queries or documents in flight. Once the Haystack retrieval user logs in, it can read every field in that collection.

Least Privilege Principle + MongoDB Authentication & Authorization: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Real-world document collections often hold sensitive customer facts alongside standard text chunks. A single document might include emails, phone numbers, card numbers, or national IDs. Native collection roles return the whole document when the retrieval pipeline runs a find action. They do not rewrite matched fields in results before the data reaches the language model. In the same way, a shared indexing user makes auditing much harder for security teams. Database logs show that the app user ran a write command, not which engineer triggered it. Teams need runtime controls that inspect queries and mask sensitive fields in results.

Add command guardrails and field masking beside MongoDB

Collection roles and IP rules control who connects to MongoDB. Yet an active client or agent session can still run a pipeline that writes data or reads raw personal fields. In practice, hoop.dev runs beside your database as a sidecar for your MongoDB. It checks every wire command and result document without changing your drivers or credentials.

Using hoop.dev MongoDB controls, guardrails refuse banned commands before they run. Data masking rewrites matched fields in returned documents in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and generates evidence for GDPR, HIPAA, and PCI DSS programs. View the project on GitHub.

Frequently asked questions

What role should a Haystack retrieval pipeline use in MongoDB?

Give the retrieval pipeline a custom role that grants only the find action on its target collection. If the app needs whole-database read rights, use the built-in read role instead. Neither option allows insert, update, remove, or dropCollection actions.

Should Haystack indexing and query pipelines share one database user?

No, you should always create separate database users for indexing and querying. The query user needs only read actions, while the indexing user needs insert and update actions. Splitting them keeps a bug in your query path from changing stored data.

Can MongoDB collection roles hide sensitive fields from Haystack prompts?

No, collection roles control access at the collection level, not inside document fields. When a role allows the find action, the database returns all fields in matching documents. To protect emails or card numbers, you need data masking that rewrites matched fields in results.

Short role checks help teams work fast. Simple rules make each review clear. Good logs keep every team safe. Scoped keys stop bad changes early.

The query runs. The emails stay hidden.