|
| 1 | +--- |
| 2 | +title: Tips and Tricks |
| 3 | +permalink: /faqs/tips-and-tricks |
| 4 | +category: FAQs |
| 5 | +--- |
| 6 | + |
| 7 | +## How can I read from two different database schemas in my database when I'm only able to select one while connecting? |
| 8 | + |
| 9 | +Use your first schema when setting up your database connection in Cube Cloud. |
| 10 | + |
| 11 | +To use your second database schema, update the `CUBE_DB_NAME` environment |
| 12 | +variable in **Settings > Configuration**. Change `CUBE_DB_NAME` to the name of |
| 13 | +your second schema. |
| 14 | + |
| 15 | +This will trigger a new build. Once it's completed click on the Schema tab on |
| 16 | +the left hand side navigation, and then in the upper-right corner, click the |
| 17 | +three-dot menu -> Generate Schema. You should be able to see the name of the |
| 18 | +second schema from your database and generate new models. |
| 19 | + |
| 20 | +## Can I track my customers' query usage? |
| 21 | + |
| 22 | +You can track query usage by user (or other dimension) by setting up [Log |
| 23 | +Export][ref-cloud-o11y-logs] and parsing the necessary information. |
| 24 | + |
| 25 | +## Can I bypass Row-Level Security when using the SQL API? |
| 26 | + |
| 27 | +There may be times when you want the permissions through Cube's REST API to be |
| 28 | +different from the permissions of the SQL API. |
| 29 | + |
| 30 | +For example, perhaps your customers use the REST API to access their own data. |
| 31 | +You might use row-level security to prevent them from seeing any data associated |
| 32 | +with other customers. |
| 33 | + |
| 34 | +For your internal analytics, you could provide access to your Data Analysts via |
| 35 | +the SQL API. Since this is for your internal use, you will need access to all |
| 36 | +the data rather than a single customer's. To give yourself higher permissions |
| 37 | +through the SQL API, you could create an exception for the usual Row-Level |
| 38 | +Security checks. |
| 39 | + |
| 40 | +In the following schema, we have created some example Row-Level Security rules |
| 41 | +and an exception for querying data via the SQL API. |
| 42 | + |
| 43 | +### Defining basic RLS |
| 44 | + |
| 45 | +First, in the `cube.js` configuration file, we'll define the |
| 46 | +[`queryRewrite()`][ref-conf-ref-queryrewrite] property to push a filter to each |
| 47 | +query depending on the `tenantId` within the [Security Context][ref-sec-ctx]. |
| 48 | + |
| 49 | +```javascript |
| 50 | +module.exports = { |
| 51 | + queryRewrite: (query, { securityContext }) => { |
| 52 | + if (!securityContext.tenantId) { |
| 53 | + throw new Error('No id found in Security Context!'); |
| 54 | + } else { |
| 55 | + query.filters.push({ |
| 56 | + member: 'Orders.tenantId', |
| 57 | + operator: 'equals', |
| 58 | + values: [securityContext.tenantId], |
| 59 | + }); |
| 60 | + |
| 61 | + return query; |
| 62 | + } |
| 63 | + }, |
| 64 | +}; |
| 65 | +``` |
| 66 | + |
| 67 | +With this logic, each tenant can see their data and nothing else. |
| 68 | + |
| 69 | +### Bypassing RLS for queries created with the SQL API |
| 70 | + |
| 71 | +When we want to bypass the RLS we defined above, we need to create a sort of |
| 72 | +"superuser" only accessible when authenticating via the SQL API. We need to |
| 73 | +define two new things for this to work: |
| 74 | + |
| 75 | +1. Leverage the [`checkSqlAuth()`][ref-conf-ref-checksqlauth] configuration |
| 76 | + option to inject a new property into the Security Context that defines a |
| 77 | + superuser. In this case, we'll call it `isSuperUser`. |
| 78 | + |
| 79 | +2. Handle the new `isSuperUser` property in our previously defined |
| 80 | + `queryRewrite` to bypass the filter push. |
| 81 | + |
| 82 | +```javascript |
| 83 | +module.exports = { |
| 84 | + // Create a "superuser" security context for the SQL API |
| 85 | + checkSqlAuth: async (req, username) => { |
| 86 | + if (username === process.env.CUBEJS_SQL_USER) { |
| 87 | + return { |
| 88 | + password: process.env.CUBEJS_SQL_PASSWORD, |
| 89 | + securityContext: { isSuperUser: true }, |
| 90 | + }; |
| 91 | + } |
| 92 | + }, |
| 93 | + queryRewrite: (query, { securityContext }) => { |
| 94 | + // Bypass row-level-security when connected from the SQL API |
| 95 | + if (securityContext.isSuperUser) { |
| 96 | + return query; |
| 97 | + } else if (!securityContext.tenantId) { |
| 98 | + throw new Error('No id found in Security Context!'); |
| 99 | + } else { |
| 100 | + query.filters.push({ |
| 101 | + member: 'Orders.tenantId', |
| 102 | + operator: 'equals', |
| 103 | + values: [securityContext.tenantId], |
| 104 | + }); |
| 105 | + |
| 106 | + return query; |
| 107 | + } |
| 108 | + }, |
| 109 | +}; |
| 110 | +``` |
| 111 | + |
| 112 | +With this exception in place we should be able to query all the customers' data |
| 113 | +via the SQL API without being hindered by the row-level security checks. |
| 114 | + |
| 115 | +[ref-cloud-o11y-logs]: /cloud/workspace/logs |
| 116 | +[ref-conf-ref-checksqlauth]: /config#options-reference-check-sql-auth |
| 117 | +[ref-conf-ref-queryrewrite]: /config#options-reference-query-rewrite |
| 118 | +[ref-sec-ctx]: /security/context |
0 commit comments