-
Notifications
You must be signed in to change notification settings - Fork 0
Add macros to grant roles access to schemas and tables #30
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
477d6be
Check for permissions before running grant queries
brendagutman 6347465
Modify schema names and macro
brendagutman efff79c
Minor modifications
brendagutman 66b52ce
Revert generate_schema_name
brendagutman 9630c2a
Add the adr and cross-project macro
brendagutman 556fe0f
Modify to grant dev access to dev schemas only
brendagutman cad74b7
Merge main
brendagutman 3c65c84
Remove unrelated changes
brendagutman e39cf7c
Change filetype of the adr to md
brendagutman 3a91f80
Merge branch 'main' into feature/bg/dev_grants
brendagutman 7b17adc
Merge main
brendagutman 67b3955
Modify to use on all to grant permission
brendagutman 8e1d552
Fix the _macros yml
brendagutman 8b3a59d
Ensure the create permissions are set properly
brendagutman 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 hidden or 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,28 @@ | ||
| version: 2 | ||
|
|
||
| analyses: | ||
| - name: check_schema_role_privileges | ||
| description: | | ||
| Lists schema-level privileges for roles across non-system schemas. | ||
| Useful for validating whether grant macros are correctly applying | ||
| CREATE and USAGE permissions. | ||
|
|
||
| Optional vars: | ||
| - grants_schema_like (default: %brainpower%) | ||
| - grants_role_like (default: %include%) | ||
|
|
||
| Example dbt show: | ||
| dbt show --select path:analyses/grants/check_schema_role_privileges.sql | ||
|
|
||
| Example dbt show with vars: | ||
| dbt show --select path:analyses/grants/check_schema_role_privileges.sql --vars '{"grants_schema_like":"dev_inc_%","grants_role_like":"include%"}' | ||
|
|
||
| columns: | ||
| - name: schema_name | ||
| description: Name of the schema being inspected. | ||
| - name: role_name | ||
| description: Name of the role being inspected. | ||
| - name: can_create | ||
| description: YES when the role has CREATE on the schema; otherwise NO. | ||
| - name: can_usage | ||
| description: YES when the role has USAGE on the schema; otherwise NO. |
30 changes: 30 additions & 0 deletions
30
dbt_project/analyses/grants/check_schema_role_privileges.sql
This file contains hidden or 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,30 @@ | ||
| -- PostgreSQL: List roles and their privileges on each schema | ||
| -- Optional vars: | ||
| -- grants_schema_like (default: %brainpower%) | ||
| -- grants_role_like (default: %include%) | ||
|
|
||
| {% set grants_schema_like = var('grants_schema_like', '%brainpower%') %} | ||
| {% set grants_role_like = var('grants_role_like', '%include%') %} | ||
|
|
||
| SELECT | ||
| n.nspname AS schema_name, | ||
| r.rolname AS role_name, | ||
| CASE | ||
| WHEN has_schema_privilege(r.rolname, n.nspname, 'CREATE') THEN 'YES' | ||
| ELSE 'NO' | ||
| END AS can_create, | ||
| CASE | ||
| WHEN has_schema_privilege(r.rolname, n.nspname, 'USAGE') THEN 'YES' | ||
| ELSE 'NO' | ||
| END AS can_usage | ||
| FROM | ||
| pg_namespace n | ||
| CROSS JOIN | ||
| pg_roles r | ||
| WHERE | ||
| n.nspname NOT LIKE 'pg_%' | ||
| AND n.nspname <> 'information_schema' | ||
| AND n.nspname LIKE '{{ grants_schema_like }}' | ||
| AND r.rolname LIKE '{{ grants_role_like }}' | ||
| ORDER BY | ||
| schema_name, role_name |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,77 @@ | ||
| {% macro grant_devs_access(tag, users_role) %} | ||
| {% if execute %} | ||
| {# | ||
| Use explicit table privileges instead of GRANT ALL to follow least-privilege. | ||
| In Postgres, ALL TABLE privileges also include TRIGGER, REFERENCES, and TRUNCATE, | ||
| which are broader than required for this workflow. | ||
| #} | ||
| {% set table_privileges = 'select, insert, update, delete' %} | ||
| {% set sequence_privileges = 'usage, select' %} | ||
|
|
||
| {% set command_name = flags.WHICH if flags is defined and flags.WHICH is defined else none %} | ||
| {% if command_name not in ['run', 'build'] %} | ||
| {% do log('grant_devs_access: skipping for command ' ~ (command_name or 'unknown') ~ '; grants are only applied for run/build', info=True) %} | ||
| {{ return('') }} | ||
| {% endif %} | ||
|
|
||
| {% if not tag %} | ||
| {% do exceptions.raise_compiler_error('grant_devs_access: tag is required') %} | ||
| {% endif %} | ||
|
|
||
| {% if not users_role %} | ||
| {% do exceptions.raise_compiler_error('grant_devs_access: users_role is required') %} | ||
| {% endif %} | ||
|
|
||
| {% set grantee_name = users_role %} | ||
| {% set grantee = adapter.quote(grantee_name) %} | ||
| {% set ns = namespace(run_schemas=[]) %} | ||
|
|
||
| {% if results is defined %} | ||
| {% for res in results %} | ||
| {% set node_tags = res.node.tags if res.node.tags is defined else [] %} | ||
| {% if res.node.resource_type == 'model' and tag in node_tags and 'dev_' in (res.node.schema | lower) %} | ||
| {% do ns.run_schemas.append(res.node.schema) %} | ||
| {% endif %} | ||
| {% endfor %} | ||
| {% endif %} | ||
|
|
||
| {% set run_schemas = ns.run_schemas | unique | list %} | ||
|
|
||
| {% if run_schemas | length == 0 %} | ||
| {% do log('grant_devs_access: no models with the ' ~ tag ~ ' tag and schemas containing dev_ found in run results; skipping grants', info=True) %} | ||
| {% endif %} | ||
|
|
||
| {% for schema_name in run_schemas %} | ||
| {% set quoted_schema = adapter.quote(schema_name) %} | ||
| {% set schema_granted_sql %} | ||
| select | ||
| has_schema_privilege('{{ grantee_name }}', '{{ schema_name }}', 'USAGE') as has_usage, | ||
| has_schema_privilege('{{ grantee_name }}', '{{ schema_name }}', 'CREATE') as has_create | ||
| {% endset %} | ||
| {% set schema_granted_result = run_query(schema_granted_sql) %} | ||
| {% set schema_usage_granted = false %} | ||
| {% set schema_create_granted = false %} | ||
|
|
||
| {% if schema_granted_result is not none and schema_granted_result.rows | length > 0 %} | ||
| {% set schema_usage_granted = schema_granted_result.rows[0][0] %} | ||
| {% set schema_create_granted = schema_granted_result.rows[0][1] %} | ||
| {% endif %} | ||
|
|
||
| {% if not schema_usage_granted or not schema_create_granted %} | ||
| {% do log('Applying ' ~ grantee_name ~ ' grants on schema ' ~ schema_name, info=True) %} | ||
|
|
||
| {% do run_query("grant usage on schema " ~ quoted_schema ~ " to " ~ grantee) %} | ||
| {% do run_query("grant create on schema " ~ quoted_schema ~ " to " ~ grantee) %} | ||
| {% do run_query("grant " ~ table_privileges ~ " on all tables in schema " ~ quoted_schema ~ " to " ~ grantee) %} | ||
| {% do run_query("grant " ~ sequence_privileges ~ " on all sequences in schema " ~ quoted_schema ~ " to " ~ grantee) %} | ||
|
|
||
| {% do run_query("alter default privileges in schema " ~ quoted_schema ~ " grant " ~ table_privileges ~ " on tables to " ~ grantee) %} | ||
| {% do run_query("alter default privileges in schema " ~ quoted_schema ~ " grant " ~ sequence_privileges ~ " on sequences to " ~ grantee) %} | ||
| {% else %} | ||
| {% do log('Skipping grants for schema ' ~ schema_name ~ ' (already granted)', info=True) %} | ||
| {% endif %} | ||
| {% endfor %} | ||
| {% endif %} | ||
|
|
||
| {{ return('') }} | ||
| {% endmacro %} |
This file contains hidden or 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,3 @@ | ||
| {% macro grant_inc_devs_access() %} | ||
| {{ grant_devs_access(tag='include', users_role='include_users') }} | ||
| {% endmacro %} |
This file contains hidden or 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,3 @@ | ||
| {% macro grant_kf_devs_access() %} | ||
| {{ grant_devs_access(tag='kids_first', users_role='kf_users') }} | ||
| {% endmacro %} |
This file contains hidden or 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 @@ | ||
| {% macro grant_schema_role_access(target_schema, users_role) %} | ||
| {% if execute %} | ||
| {# | ||
| Use explicit table privileges instead of GRANT ALL to follow least-privilege. | ||
| In Postgres, ALL TABLE privileges also include TRIGGER, REFERENCES, and TRUNCATE, | ||
| which are broader than required for this workflow. | ||
| #} | ||
| {% set table_privileges = 'select, insert, update, delete' %} | ||
| {% set sequence_privileges = 'usage, select' %} | ||
|
|
||
| {% if not target_schema %} | ||
| {% do exceptions.raise_compiler_error('grant_schema_role_access: target_schema is required') %} | ||
| {% endif %} | ||
|
|
||
| {% if not users_role %} | ||
| {% do exceptions.raise_compiler_error('grant_schema_role_access: users_role is required') %} | ||
| {% endif %} | ||
|
|
||
| {% set schema_name = target_schema %} | ||
| {% set grantee_name = users_role %} | ||
| {% set quoted_schema = adapter.quote(schema_name) %} | ||
| {% set grantee = adapter.quote(grantee_name) %} | ||
|
|
||
| {% set schema_granted_sql %} | ||
| select | ||
| has_schema_privilege('{{ grantee_name }}', '{{ schema_name }}', 'USAGE') as has_usage, | ||
| has_schema_privilege('{{ grantee_name }}', '{{ schema_name }}', 'CREATE') as has_create | ||
| {% endset %} | ||
| {% set schema_granted_result = run_query(schema_granted_sql) %} | ||
| {% set schema_usage_granted = false %} | ||
| {% set schema_create_granted = false %} | ||
|
|
||
| {% if schema_granted_result is not none and schema_granted_result.rows | length > 0 %} | ||
| {% set schema_usage_granted = schema_granted_result.rows[0][0] %} | ||
| {% set schema_create_granted = schema_granted_result.rows[0][1] %} | ||
| {% endif %} | ||
|
|
||
| {% if not schema_usage_granted or not schema_create_granted %} | ||
| {% do log('Applying ' ~ grantee_name ~ ' grants on schema ' ~ schema_name, info=True) %} | ||
|
|
||
| {% do run_query("grant usage on schema " ~ quoted_schema ~ " to " ~ grantee) %} | ||
| {% do run_query("grant create on schema " ~ quoted_schema ~ " to " ~ grantee) %} | ||
| {% do run_query("grant " ~ table_privileges ~ " on all tables in schema " ~ quoted_schema ~ " to " ~ grantee) %} | ||
| {% do run_query("grant " ~ sequence_privileges ~ " on all sequences in schema " ~ quoted_schema ~ " to " ~ grantee) %} | ||
|
|
||
| {% do run_query("alter default privileges in schema " ~ quoted_schema ~ " grant " ~ table_privileges ~ " on tables to " ~ grantee) %} | ||
| {% do run_query("alter default privileges in schema " ~ quoted_schema ~ " grant " ~ sequence_privileges ~ " on sequences to " ~ grantee) %} | ||
| {% else %} | ||
| {% do log('Skipping grants for schema ' ~ schema_name ~ ' (USAGE and CREATE already granted for role ' ~ grantee_name ~ ')', info=True) %} | ||
| {% endif %} | ||
| {% endif %} | ||
|
|
||
| {{ return('') }} | ||
| {% endmacro %} |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,21 +1,21 @@ | ||
| {% macro combined_stb_relations(table_name, studies_var='combined_studies') %} | ||
| {% set studies = var(studies_var, []) %} | ||
| {%- macro combined_stb_relations(table_name, studies_var='combined_studies') -%} | ||
| {%- set studies = var(studies_var, []) -%} | ||
|
|
||
| {% if studies | length == 0 %} | ||
| {%- if studies | length == 0 -%} | ||
| {{ exceptions.raise_compiler_error("Var '" ~ studies_var ~ "' must contain at least one study prefix.") }} | ||
| {% endif %} | ||
| {%- endif -%} | ||
|
|
||
| {% set relations = [] %} | ||
| {% for study in studies %} | ||
| {% do relations.append(ref(study ~ '_stb_' ~ table_name)) %} | ||
| {% endfor %} | ||
| {%- set relations = [] -%} | ||
| {%- for study in studies -%} | ||
| {%- do relations.append(ref(study ~ '_stb_' ~ table_name)) -%} | ||
| {%- endfor -%} | ||
|
|
||
| {{ return(relations) }} | ||
| {% endmacro %} | ||
| {{- return(relations) -}} | ||
| {%- endmacro -%} | ||
|
|
||
| {% macro combined_union_from_current_model(studies_var='combined_studies') %} | ||
| {% set table_name = model.name | replace('combined_', '') %} | ||
| {% set relations = combined_stb_relations(table_name=table_name, studies_var=studies_var) %} | ||
| {%- macro combined_union_from_current_model(studies_var='combined_studies') -%} | ||
| {%- set table_name = model.name | replace('combined_', '') -%} | ||
| {%- set relations = combined_stb_relations(table_name=table_name, studies_var=studies_var) -%} | ||
|
|
||
| {{ dbt_utils.union_relations(relations=relations) }} | ||
| {% endmacro %} | ||
| {{- dbt_utils.union_relations(relations=relations) -}} | ||
| {%- endmacro -%} |
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.
Uh oh!
There was an error while loading. Please reload this page.