Skip to content
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

[CT-506] Apply grants within materializations #5090

Closed
jtcohen6 opened this issue Apr 19, 2022 · 1 comment
Closed

[CT-506] Apply grants within materializations #5090

jtcohen6 opened this issue Apr 19, 2022 · 1 comment
Labels
enhancement New feature or request Team:Adapters Issues designated for the adapter area of the code

Comments

@jtcohen6
Copy link
Contributor

How this works today

{{ config(post_hook = 'grant select on {{ this }} to role reporter') }}

select 1 as id

During each materialization, in run_hooks(post_hooks), dbt will run the arbitrary SQL that the user has provided.

What's not great about this?

What we want

I can define grants as a resource config on each model/seed/snapshot. As with all resource configs, I can define reasonable defaults in dbt_project.yml, plus the ability to define within each model SQL file or yml file.

# dbt_project.yml
models:
  export:
    +grants:
      select: ['reporter', 'bi']
{{ config(
    grants = {'select': ['other_user']}
) }}
-- this should totally replace the 'reporter' + 'bi' default configs defined above

select ...

When my dbt model runs, all grants are automatically applied:

-- e.g. on Snowflake
create or replace table dbt_jerco.my_model
as (
  select 1 as id
);

grant select on table dbt_jerco.my_model to other_user;

We’re targeting the 95% use case here: The right people can select from your dbt models, as soon as those models are created. There may be super specific grants that users want to put together. For that, there are always hooks, as above.

Required changes

  • Add grants as a supported node config. Grants should be merged/clobbered—like meta, not tags. (Opt for less access, not more.)
  • In all materializations, add a call to an apply_grants macro, very similar to persist_docs
  • dbt-core’s global project implements a dispatched macro, {% macro get_grant_sql(relation, privilege, recipients) %}, with a sane default__get_grant_sql
  • Adapter plugins reimplement that macro as adapter__get_grant_sql if the default doesn’t work for them

Considerations

grants will support grants on the current model only. dbt grants access on model X, as soon as model X has finished running. It won’t be possible to grant permissions on model Y as soon as model X has finished running.

On some databases, grants are automatically “inherited” when a table is recreated (e.g. copy grants on Snowflake). Should we strongly advise use of those configs, where available? Should we always revoke + rerun every grant, every time? Or should we first ask which grants are in place (show grants), calculate diffs, and then decide which grants to use? Related: If users have configured column/row-level restrictive access policies, we need to ensure that those restrictions are applied first, before grants (which are permissive). Otherwise we risk a moment in which a user has more access than they should.

The words here are different on different databases. ("Role" on BigQuery means "privilege," whereas on Snowflake it means "recipient group.") How should we factor this config, to avoid bad abstractions later on?

# dbt_project.yml
models:
  export:
    +grants:
      # 'concise' version
      select: ['reporter', 'bi']

      # we really only expect people to be granting 'select' on views/tables,
      # but let's make sure we're not hurting ourselves in a future version where
      # we support grants on other object types (schemas, policies, functions, ...)

      # how to uniquely identify this combo to support merging/clobbering?
      - privileges:
          - select # on Postgres/Redshift/Snowflake
          - 'roles/viewer' # same idea but on BigQuery - should we support 'select' as an alias?
        recipients:
          - reporter
          - bi

We’ll need to update, in our documentation, the places where we strongly recommend running grants inside of hooks:

@jtcohen6 jtcohen6 added enhancement New feature or request Team:Adapters Issues designated for the adapter area of the code labels Apr 19, 2022
@github-actions github-actions bot changed the title Apply grants within materializations [CT-506] Apply grants within materializations Apr 19, 2022
@jtcohen6 jtcohen6 added this to the v1.2 milestone Apr 19, 2022
@jtcohen6 jtcohen6 removed this from the v1.2 milestone Jun 30, 2022
@jtcohen6
Copy link
Contributor Author

Closing in favor of the implementation tickets (#5189 + #5263)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Team:Adapters Issues designated for the adapter area of the code
Projects
None yet
Development

No branches or pull requests

1 participant