feat: SQL API req.meta expose target dbname
Is your feature request related to a problem? Please describe. We're currently implementing a multi-tenancy authorization scenario and would like to match the target database for a connection through SQL API to data contained in the JWT of the user. We're able to extract the claims from the token in our cube.js but the target dbname isn't available in the context.
Describe the solution you'd like In addition to the existing metadata we propose to add also the dbname. This field is already stored in the session, but currently not exposed to req.meta
Describe alternatives you've considered With the available metadata we were thinking about re-using the changeUser or applicationName. Nevertheless we think it would be beneficial to expose the intended target database on a separate optional field.
Additional context I'll link a PR with a proposal for an implementation.
This is nice, exposing the connection dbname to req.meta matches a missing piece for a use case I have for JWT-matched multitenancy over the SQL API. I've hit the same wall (dbname in the session but not in the request context)
Btw have you heard back from the maintainers?
I wanted to flag an adjacent use case (I think your change is a building block for), in case it's useful for shaping the API.
We'd like the SQL API to present different parts of the semantic layer as distinct schemas, and eventually distinct databases, so BI tools get a navigable namespace instead of one flat public list.
Today get_postgres_provider resolves cubes only under a hardcoded public, and the connection database is read and then discarded. Your PR seem to unblock the first half of that (the database becoming visible in context); scoping visible cubes by that database would be a clean next step on top of it.
Two things I'd appreciate your opinion on, since you're closest to this code:
- Are you exposing the raw dbname string only, or is there room for the database to also influence what's visible/served (i.e. a routing hook, not just a claim to match against)?
- Would you be open to me building on this once it lands? I'm planning to write up a RFC for multi-schema/-database mapping in the SQL API and would rather align it with your work than duplicate it.
@marciodfg I never heard back from the maintainers (mentioned them in the PR, and announced in Slack) and we're running on our forked version with the fix currently. Feel free to integrate my proposal with your requirement. I created this as small as possible to get it merged easily. Your plan sounds a little bit more ambitious
@SaschaJohn Could you share more about the use case — what are you trying to achieve?
If the goal is to distinguish tenants connecting to the SQL API, the connection username is the intended mechanism: check_sql_auth(ctx, user, password) receives the user and returns that tenant's securityContext, so you authenticate each tenant with its own SQL user and map it to the right context there — no separate dbname field needed. If your setup needs something different, let us know what.
@igorlukanin thanks for reaching out. Our usecase is having one user accessing multiple databases. In our scenario a product gets a clientid/secret and tries to access a particular dbname (customerid - it contains data of a certain customer (tenant)). We're fetching a JWT from our token provider. The token contains the list of all customerids the product (clientid) is allowed to access. We now need to validate the current target dbname is part of the content of the token. Therefore we need to compare the content of the JWT organizations claim with the req.meta.dbname and return the datasource only, if the current db target is part of that list. Otherwise we return an auth error. The dbname was already part of your session data in Cubejs but not exposed through req.meta. My PR fixes this and is running since two months for us in production.