-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add Grant SQL Macros #5369
Merged
Merged
Add Grant SQL Macros #5369
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
6e88fe7
init push or ct-660 work
McKnight-42 6d4b938
changes to default versions of get_show_grant_sql and get_grant_sql
McKnight-42 ea35fd1
completing init default versions of all macros being called for look …
McKnight-42 7e2b707
minor update to should_revoke
McKnight-42 898aa8f
post pairing push up (does have log statements to make sure we remove)
McKnight-42 76050da
minor spacing changes
McKnight-42 7a3e7c6
Merge branch 'main' of github.com:dbt-labs/dbt into ct-660-grant-sql
McKnight-42 5fb07c1
minor changes, and removal of logs so people can have clean grab of code
McKnight-42 4cf705f
minor changes to how get_revoke_sql works
McKnight-42 2fff84b
init attempt at applying apply_grants to all materialzations
McKnight-42 6fccef6
name change from recipents -> grantee
McKnight-42 528dff6
minor changes
McKnight-42 95f7ae4
Merge branch 'main' of github.com:dbt-labs/dbt into ct-660-grant-sql
McKnight-42 9079c4a
working on making a context to handle the diff gathering between gran…
McKnight-42 6bdb4b5
removing logs from most materializations to better track diff of gran…
McKnight-42 597ab05
starting to build out postgres get_show_grant_sql getting empty query…
McKnight-42 138f443
6/27 eod update looking into diff_grants variable not getting passed …
McKnight-42 fc7e24b
changes to loop cases
McKnight-42 6f53b3d
changes after pairing meeting
McKnight-42 b3e37cb
Merge branch 'main' of github.com:dbt-labs/dbt into ct-660-grant-sql
McKnight-42 99b1445
adding apply_grants to create_or_replace_view.sql
McKnight-42 7946a3b
models are building but testing out small issues around revoke statem…
McKnight-42 0597398
postgrest must fixes from jeremy's feedback
McKnight-42 1d263b7
postgres minor change to standarize_grants_dict
McKnight-42 c2e9aeb
updating after pairing with dough and jeremey incorporating the new v…
McKnight-42 0843f66
Merge branch 'main' of github.com:dbt-labs/dbt into ct-660-grant-sql
McKnight-42 077e4ff
adding ref of diff_of_two_dicts to base keys ref
McKnight-42 ab6be85
change of method type for standardize_grants_dict
McKnight-42 da557d9
minor update trying to fix unit test
McKnight-42 f2c957f
changes based on morning feedback
McKnight-42 eb935d5
change log message in default_apply_grants macro
McKnight-42 cefcd4f
CT-808 grant adapter tests (#5447)
gshank bdc0c71
rename grant[privilege] -> grant_config[privilege]
McKnight-42 67f5beb
Merge branch 'main' of github.com:dbt-labs/dbt into ct-660-grant-sql
McKnight-42 72bdae9
postgres macro rename to copy_grants
McKnight-42 794f4d1
CT-808 more grant adapter tests (#5452)
gshank 068c59b
update to main, create changelog, whitespace fixes
McKnight-42 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,8 @@ | ||
kind: Features | ||
body: Allow users to define grants as a reasonable default in the dbt_project.yml | ||
or within each model sql or yml file combined. | ||
time: 2022-07-11T11:15:14.695386-05:00 | ||
custom: | ||
Author: McKnight-42 | ||
Issue: "5263" | ||
PR: "5369" |
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,7 @@ | ||
kind: Under the Hood | ||
body: Add tests for SQL grants | ||
time: 2022-07-06T21:50:01.498562-04:00 | ||
custom: | ||
Author: gshank | ||
Issue: "5437" | ||
PR: "5447" |
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
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
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
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
167 changes: 167 additions & 0 deletions
167
core/dbt/include/global_project/macros/adapters/apply_grants.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,167 @@ | ||
{# ------- BOOLEAN MACROS --------- #} | ||
|
||
{# | ||
-- COPY GRANTS | ||
-- When a relational object (view or table) is replaced in this database, | ||
-- do previous grants carry over to the new object? This may depend on: | ||
-- whether we use alter-rename-swap versus CREATE OR REPLACE | ||
-- user-supplied configuration (e.g. copy_grants on Snowflake) | ||
-- By default, play it safe, assume TRUE: that grants ARE copied over. | ||
-- This means dbt will first "show" current grants and then calculate diffs. | ||
-- It may require an additional query than is strictly necessary, | ||
-- but better safe than sorry. | ||
#} | ||
|
||
{% macro copy_grants() %} | ||
{{ return(adapter.dispatch('copy_grants', 'dbt')()) }} | ||
{% endmacro %} | ||
|
||
{% macro default__copy_grants() %} | ||
{{ return(True) }} | ||
{% endmacro %} | ||
|
||
|
||
{# | ||
-- SUPPORT MULTIPLE GRANTEES PER DCL STATEMENT | ||
-- Does this database support 'grant {privilege} to {grantee_1}, {grantee_2}, ...' | ||
-- Or must these be separate statements: | ||
-- `grant {privilege} to {grantee_1}`; | ||
-- `grant {privilege} to {grantee_2}`; | ||
-- By default, pick the former, because it's what we prefer when available. | ||
#} | ||
|
||
{% macro support_multiple_grantees_per_dcl_statement() %} | ||
{{ return(adapter.dispatch('support_multiple_grantees_per_dcl_statement', 'dbt')()) }} | ||
{% endmacro %} | ||
|
||
{%- macro default__support_multiple_grantees_per_dcl_statement() -%} | ||
{{ return(True) }} | ||
{%- endmacro -%} | ||
|
||
|
||
{% macro should_revoke(existing_relation, full_refresh_mode=True) %} | ||
|
||
{% if not existing_relation %} | ||
{#-- The table doesn't already exist, so no grants to copy over --#} | ||
{{ return(False) }} | ||
{% elif full_refresh_mode %} | ||
{#-- The object is being REPLACED -- whether grants are copied over depends on the value of user config --#} | ||
{{ return(copy_grants()) }} | ||
{% else %} | ||
{#-- The table is being merged/upserted/inserted -- grants will be carried over --#} | ||
{{ return(True) }} | ||
{% endif %} | ||
|
||
{% endmacro %} | ||
|
||
{# ------- DCL STATEMENT TEMPLATES --------- #} | ||
|
||
{% macro get_show_grant_sql(relation) %} | ||
{{ return(adapter.dispatch("get_show_grant_sql", "dbt")(relation)) }} | ||
{% endmacro %} | ||
|
||
{% macro default__get_show_grant_sql(relation) %} | ||
show grants on {{ relation }} | ||
{% endmacro %} | ||
|
||
|
||
{% macro get_grant_sql(relation, privilege, grantees) %} | ||
{{ return(adapter.dispatch('get_grant_sql', 'dbt')(relation, privilege, grantees)) }} | ||
{% endmacro %} | ||
|
||
{%- macro default__get_grant_sql(relation, privilege, grantees) -%} | ||
grant {{ privilege }} on {{ relation }} to {{ grantees | join(', ') }} | ||
{%- endmacro -%} | ||
|
||
|
||
{% macro get_revoke_sql(relation, privilege, grantees) %} | ||
{{ return(adapter.dispatch('get_revoke_sql', 'dbt')(relation, privilege, grantees)) }} | ||
{% endmacro %} | ||
|
||
{%- macro default__get_revoke_sql(relation, privilege, grantees) -%} | ||
revoke {{ privilege }} on {{ relation }} from {{ grantees | join(', ') }} | ||
{%- endmacro -%} | ||
|
||
|
||
{# ------- RUNTIME APPLICATION --------- #} | ||
|
||
{% macro get_dcl_statement_list(relation, grant_config, get_dcl_macro) %} | ||
{{ return(adapter.dispatch('get_dcl_statement_list', 'dbt')(relation, grant_config, get_dcl_macro)) }} | ||
{% endmacro %} | ||
|
||
{%- macro default__get_dcl_statement_list(relation, grant_config, get_dcl_macro) -%} | ||
{# | ||
-- Unpack grant_config into specific privileges and the set of users who need them granted/revoked. | ||
-- Depending on whether this database supports multiple grantees per statement, pass in the list of | ||
-- all grantees per privilege, or (if not) template one statement per privilege-grantee pair. | ||
-- `get_dcl_macro` will be either `get_grant_sql` or `get_revoke_sql` | ||
#} | ||
{%- set dcl_statements = [] -%} | ||
{%- for privilege, grantees in grant_config.items() %} | ||
{%- if support_multiple_grantees_per_dcl_statement() and grantees -%} | ||
{%- set dcl = get_dcl_macro(relation, privilege, grantees) -%} | ||
{%- do dcl_statements.append(dcl) -%} | ||
{%- else -%} | ||
{%- for grantee in grantees -%} | ||
{% set dcl = get_dcl_macro(relation, privilege, [grantee]) %} | ||
{%- do dcl_statements.append(dcl) -%} | ||
{% endfor -%} | ||
{%- endif -%} | ||
{%- endfor -%} | ||
{{ return(dcl_statements) }} | ||
{%- endmacro %} | ||
|
||
|
||
{% macro call_dcl_statements(dcl_statement_list) %} | ||
{{ return(adapter.dispatch("call_dcl_statements", "dbt")(dcl_statement_list)) }} | ||
{% endmacro %} | ||
|
||
{% macro default__call_dcl_statements(dcl_statement_list) %} | ||
{# | ||
-- By default, supply all grant + revoke statements in a single semicolon-separated block, | ||
-- so that they're all processed together. | ||
|
||
-- Some databases do not support this. Those adapters will need to override this macro | ||
-- to run each statement individually. | ||
#} | ||
{% call statement('grants') %} | ||
{% for dcl_statement in dcl_statement_list %} | ||
{{ dcl_statement }}; | ||
{% endfor %} | ||
{% endcall %} | ||
{% endmacro %} | ||
|
||
|
||
{% macro apply_grants(relation, grant_config, should_revoke) %} | ||
{{ return(adapter.dispatch("apply_grants", "dbt")(relation, grant_config, should_revoke)) }} | ||
{% endmacro %} | ||
|
||
{% macro default__apply_grants(relation, grant_config, should_revoke=True) %} | ||
{#-- If grant_config is {} or None, this is a no-op --#} | ||
{% if grant_config %} | ||
{% if should_revoke %} | ||
{#-- We think previous grants may have carried over --#} | ||
{#-- Show current grants and calculate diffs --#} | ||
{% set current_grants_table = run_query(get_show_grant_sql(relation)) %} | ||
{% set current_grants_dict = adapter.standardize_grants_dict(current_grants_table) %} | ||
{% set needs_granting = diff_of_two_dicts(grant_config, current_grants_dict) %} | ||
{% set needs_revoking = diff_of_two_dicts(current_grants_dict, grant_config) %} | ||
{% if not (needs_granting or needs_revoking) %} | ||
{{ log('On ' ~ relation ~': All grants are in place, no revocation or granting needed.')}} | ||
{% endif %} | ||
{% else %} | ||
{#-- We don't think there's any chance of previous grants having carried over. --#} | ||
{#-- Jump straight to granting what the user has configured. --#} | ||
{% set needs_revoking = {} %} | ||
{% set needs_granting = grant_config %} | ||
{% endif %} | ||
{% if needs_granting or needs_revoking %} | ||
{% set revoke_statement_list = get_dcl_statement_list(relation, needs_revoking, get_revoke_sql) %} | ||
{% set grant_statement_list = get_dcl_statement_list(relation, needs_granting, get_grant_sql) %} | ||
{% set dcl_statement_list = revoke_statement_list + grant_statement_list %} | ||
{% if dcl_statement_list %} | ||
{{ call_dcl_statements(dcl_statement_list) }} | ||
{% endif %} | ||
{% endif %} | ||
{% endif %} | ||
{% endmacro %} |
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
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
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
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
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.
Not something needed to do before merging, but we added the same change pretty much for all materialization methods. Should we consider refactoring the code so we can only do it in one place?