When someone in finance asks an AI coding tool for a dashboard on live data, the first thing the tool asks for is a connection string. This is how to answer without giving them one.
The usual pattern, and what it costs
Most internal apps get their data the same way. The builder asks a data engineer for credentials, pastes them into a .env file or the code, and the app connects as that login from then on.
That leaves four problems:
- The builder holds a password. It works from their laptop, in any tool, for anything the login can reach.
- Logins get shared. One analytics login often serves many apps, so one leak exposes all of them and no record says which app ran a query.
- Nobody knows who asked. The database sees the app's login, not the person looking at the dashboard.
- Nothing expires. When the builder leaves, the credential stays in the app, and often in their own files too.
The OWASP Secrets Management Cheat Sheet puts the starting point plainly: many organizations have secrets "hardcoded within the source code in plaintext". AI tools do not change that. They make more apps, faster, so the pattern repeats more often.
The pattern to aim for
Five rules. Each one is something a database already supports.
- One login per app, never per person and never shared.
- Read-only, and scoped to the schema the app needs.
- Limits on how long a query runs and what it costs.
- The viewer's identity on each query, so the record says who asked, not only which app.
- The builder never sees the secret. It is created by the data owner and handed to the running app, not to a person.
In Postgres
Create a login for the app, give it read access to one schema, and set limits on the role. The statements below follow the PostgreSQL documentation for CREATE ROLE, GRANT, ALTER DEFAULT PRIVILEGES and ALTER ROLE.
-- One login for one app, with a cap on connections.
CREATE ROLE app_revenue_dashboard LOGIN CONNECTION LIMIT 5;
-- Read access to the reporting schema only.
GRANT USAGE ON SCHEMA reporting TO app_revenue_dashboard;
GRANT SELECT ON ALL TABLES IN SCHEMA reporting TO app_revenue_dashboard;
-- Tables created later in the schema by the role that owns them.
ALTER DEFAULT PRIVILEGES FOR ROLE reporting_owner IN SCHEMA reporting
GRANT SELECT ON TABLES TO app_revenue_dashboard;
-- Defaults applied each time the app logs in.
ALTER ROLE app_revenue_dashboard SET statement_timeout = '30s';
ALTER ROLE app_revenue_dashboard SET default_transaction_read_only = on;
Things to know:
-
The grants are the control.
statement_timeoutanddefault_transaction_read_onlyare session defaults that the session itself can change, so treat them as guards against accidents. What stops writes is that the role was only ever grantedSELECT. -
GRANT ... ALL TABLEScovers tables that exist now. Tables created later needALTER DEFAULT PRIVILEGES, which applies only to objects created by the role you name. -
Avoid
pg_read_all_datafor apps. It reads every table in every schema. -
Row-level security can narrow it further. With row security on a table, a role sees only the rows a policy allows. Superusers, table owners and roles with
BYPASSRLSskip the policies, so the app's login must be none of those. -
CONNECTION LIMITis approximate, per the docs, and is never enforced for superusers.
In Snowflake
The same shape applies: a user and role per app, SELECT on one schema, and a warehouse of its own.
-
Authenticate with a key pair, not a password. Snowflake's key-pair authentication needs at least a 2048-bit RSA key, and
ALTER USER ... ROTATE KEY PAIRreplaces a key while the old one keeps working for 24 hours by default. - Cap the cost with a resource monitor on the app's warehouse. A resource monitor sets a credit quota per interval and can notify, suspend after running queries finish, or suspend immediately. It works for warehouses only, and only account administrators can create one.
-
Cap query time.
STATEMENT_TIMEOUT_IN_SECONDSdefaults to two days; set it far lower for the app's warehouse or user. -
Tag each query with who asked. The
QUERY_TAGsession parameter takes a string of up to 2,000 characters and appears in query history. Set it to the signed-in viewer before each query.
Keeping the secret away from people
The grants above limit what a leaked credential can do. The rest is keeping it from leaking.
- The data owner creates the login, not the builder, and stores it in a secrets manager.
- The platform hands it to the running app as an environment variable or a mounted file. It is never written into the code or the repository.
- Rotate it. OWASP's advice is to rotate regularly "so that any stolen credentials will only work for a short time". AWS Secrets Manager can update the secret and the database password together.
- Scan for it. GitHub secret scanning checks a repository's whole history. If it finds a credential, GitHub's advice is to rotate it immediately; deleting it from history is not enough.
Tell the AI tool the rule too. "Read the connection string from the DATABASE_URL environment variable and never write a password into a file" is an instruction Claude, Claude Code, Codex and Cursor all follow.
A checklist for the data owner
Before an app gets a connection:
- [ ] A login of its own, named after the app
- [ ]
SELECTon the schemas it needs, nothing else - [ ] A query timeout and a cost limit
- [ ] Not a superuser, not a table owner, no
BYPASSRLS - [ ] Stored in a secrets manager, delivered only to the running app
- [ ] A named owner, and a date to review it
- [ ] Queries carry the viewer's identity
First published on intrium.dev. intrium is governed hosting for the apps your team builds themselves. The steps above work with or without it.
Top comments (0)