-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Setup metabase_read_only
role
#2856
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6492a48
feat: Setup `metabase_read_only` role
DafyddLlyr 5dfa3c4
feat: Setup `metabase_read_only` role
DafyddLlyr b27deee
docs: Explain how new role will work
DafyddLlyr 4dd9b14
docs: Add context on IaC setup and `MB_DB_CONNECTION_URI\' env var
DafyddLlyr 2cd3917
chore: Remove tables not referenced in Metabase
DafyddLlyr 39d4329
chore: Grant access to analytics and analytics_logs
DafyddLlyr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# How to grant Metabase permissions | ||
|
||
## What is Metabase? | ||
[Metabase](https://www.metabase.com/) is an open source BI service which we self-host as part of PlanX. It allows teams to view and self-serve analytics dashboards related to their flows, applications, and users. | ||
|
||
Metabase is set up and running on both Staging and Production environments, but only the Production instance (with Production data) has dashboards maintained and curated for teams. | ||
|
||
## Context | ||
Metabase accesses our staging and production databases through the `metabase_read_only` role which has `SELECT` (read-only) access to a subset of tables and views. This ensures that sensitive user data cannot be inadvertently exposed via Metabase, and any new tables added to the PlanX database have to be explicitly exposed via this role. | ||
|
||
The permissions granted for the `metabase_read_only` role are applied via Hasura migrations. This ensures that we have a version-controlled history of this role, and it's access level is documented in code. This is not controlled via Pulumi (IaC) as this is not used for local development or test environments ("Pizzas"), which would necessitate a second method for these environments. | ||
|
||
## How is this role used? | ||
|
||
### Staging & Production | ||
The `metabase_read_only` role is granted to the `metabase_user` database user, which is manually set up on both staging and production with the following SQL - | ||
|
||
```sql | ||
CREATE USER metabase_user WITH PASSWORD `$PASSWORD`; | ||
GRANT metabase_read_only TO metabase_user; | ||
``` | ||
|
||
The password for Staging and Production databases can be found the OSL 1Password account. | ||
|
||
The username and password for Metabase are not controlled via IaC - they are manually entered via the Metabase "Admin" dashboard (`Admin Setting > Databases > "staging" | "production" > Username / Password fields`). | ||
|
||
Please note - this is separate to the role used to read/write Metabase internal application data (such as dashboard and queries). This role is setup in IaC [here](https://github.com/theopensystemslab/planx-new/blob/main/infrastructure/application/index.ts#L100). For more information, please see [the Metabase docs](https://www.metabase.com/docs/latest/installation-and-operation/configuring-application-database). | ||
|
||
### Locally & Pizzas | ||
If you wish to run Metabase locally using the "analytics" Docker profile (`pnpm analytics` from project root), you will need to manually run the above SQL on your local database with a password of your choice. Alternatively, you can use the root DB username/password. | ||
|
||
The Metabase service does not run on Pizzas. | ||
|
||
## Metabase permissions | ||
Metabase also operates it's own permissions model, which allows more fine-grained control over tables. This allows "Administrator" users access to all tables granted to the Postgres `metabase_read_only` role, but other users can only access summary views. | ||
|
||
## Process | ||
The process for exposing a new table / view to Metabase is as follows - | ||
|
||
### Tables | ||
Generally, we'd favour exposing views of data via Metabase. This means only certain columns can be exposed, and data can be formatted in a more user-friendly manner. | ||
|
||
If you need to expose a new table (e.g. public data) access can be granted via a Hasura migration, e.g. - | ||
|
||
```sql | ||
GRANT SELECT ON public.flows TO metabase_read_only; | ||
``` | ||
|
||
### Views | ||
When adding a new view, you will need to grant the `metabase_read_only` role `SELECT` access the view. Access should be applied via Hasura migrations, e.g. - | ||
|
||
```sql | ||
GRANT SELECT ON public.YOUR_NEW_VIEW TO metabase_read_only; | ||
``` |
15 changes: 15 additions & 0 deletions
15
hasura.planx.uk/migrations/1709565833346_run_sql_migration/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- Revoke select permissions from views used by Metabase | ||
REVOKE SELECT ON public.analytics_summary FROM metabase_read_only; | ||
REVOKE SELECT ON public.feedback_summary FROM metabase_read_only; | ||
REVOKE SELECT ON public.submission_services_summary FROM metabase_read_only; | ||
|
||
-- Revoke select permissions from tables used by Metabase | ||
REVOKE SELECT ON public.flows FROM metabase_read_only; | ||
REVOKE SELECT ON public.published_flows FROM metabase_read_only; | ||
REVOKE SELECT ON public.teams FROM metabase_read_only; | ||
|
||
-- Revoke usage on schema | ||
REVOKE USAGE ON SCHEMA public FROM metabase_read_only; | ||
|
||
-- Drop the role | ||
DROP ROLE IF EXISTS metabase_read_only; |
15 changes: 15 additions & 0 deletions
15
hasura.planx.uk/migrations/1709565833346_run_sql_migration/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- Create the role | ||
CREATE ROLE metabase_read_only; | ||
|
||
-- Grant usage on schema | ||
GRANT USAGE ON SCHEMA public TO metabase_read_only; | ||
|
||
-- Grant select permissions on public tables used by Metabase (useful for joins) | ||
GRANT SELECT ON public.flows TO metabase_read_only; | ||
GRANT SELECT ON public.published_flows TO metabase_read_only; | ||
GRANT SELECT ON public.teams TO metabase_read_only; | ||
|
||
-- Grant select permissions on views used by Metabase | ||
GRANT SELECT ON public.analytics_summary TO metabase_read_only; | ||
GRANT SELECT ON public.feedback_summary TO metabase_read_only; | ||
GRANT SELECT ON public.submission_services_summary TO metabase_read_only; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might actually be redundant - I think there might be enough information in the views? If it's simpler to remove this I'm all for it 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll contact Ollie today, or manually check Metabase, to see if this is required 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed - these aren't currently used. See https://opensystemslab.slack.com/archives/C5Q59R3HB/p1709720214019159 (OSL Slack)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update - in order to move this forward I've granted access to the tables
analytics
andanalytics_logs
. They are safe to read, and currently used by SQL questions. The aim would be that once we migrate to GUI questions, we can rely on summary tables and tidy this up.@zz-hh-aa Indicated that
lowcal_sessions
is used, which we do not want, so I've intentionally left this out to force a transition tosubmission_services_summary
.If this breaking change it too harsh, we can toggle the PG username/password used in Metabase until this is resolved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I've removed
lowcal_sessions
as a source from all the questions now anyway, and am only using summary tables +analytics
oranalytics_logs
, so don't think anything should break! All good on my end.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah that's perfect @zz-hh-aa thanks very much!