terraform-provider-snowflake icon indicating copy to clipboard operation
terraform-provider-snowflake copied to clipboard

GRANT SELECT TO ALL

Open aaronsteers opened this issue 4 years ago • 14 comments

Is your feature request related to a problem? Please describe.

I'm not sure how to replicate my existing snowflake grants into terraform without access to an option for ALL TABLES IN SCHEMA/DATABASE.

Describe the solution you'd like

Just as "future" tables/views are supported in grants, the request would be to support granting access to "all" tables/views.

Describe alternatives you've considered

I considered creating a "task" resource to manually declare the grants - however, that "task" resource appears to be missing an "undo_sql_statement" to provide an "undo"/"destroy" capability, for instance when permission is changed/revoked.

Additional context

Add any other context or screenshots about the feature request here.

aaronsteers avatar Oct 25 '20 20:10 aaronsteers

Would love to help implement this!

tnightengale avatar Oct 27 '20 00:10 tnightengale

It would be great to have this!

JimFawkes avatar Nov 11 '20 21:11 JimFawkes

Would be great to have GRANT < PRIVILEGE> ON ALL TABLES/VIEWS/SCHEMAS

a2m1 avatar Nov 18 '20 17:11 a2m1

Having this would be awesome

JavierLopezT avatar Feb 01 '21 13:02 JavierLopezT

+1 on the awesomeness of having something like this 👍

juangesino avatar May 19 '21 16:05 juangesino

+1 - I can specify that I want to grant select on all future tables, but not all existing ones!

Would like the same functionality applied to snowflake_schema_grant too (e.g., grant usage on all schemas in database blah)

gnilrets avatar Jun 29 '21 21:06 gnilrets

Hey @tnightengale, @JimFawkes, @a2m1, @JavierLopezT, @juangesino , and @gnilrets... I just created a provider that allows us to declare GRANT ALL statements in Terraform! Here is a code snippet from the terraform-provider-snowsql/examples/simple/main.tf.

resource "snowsql_exec" "dcl" {
  name = local.name

  create {
    statements = <<-EOT
    GRANT ALL PRIVILEGES ON ALL TABLES IN DATABASE ${snowflake_database.database.name} TO ROLE ${snowflake_role.role.name};
    GRANT ALL PRIVILEGES ON FUTURE TABLES IN DATABASE ${snowflake_database.database.name} TO ROLE ${snowflake_role.role.name};
    EOT
  }

  delete {
    statements = <<-EOT
    REVOKE ALL PRIVILEGES ON ALL TABLES IN DATABASE ${snowflake_database.database.name} FROM ROLE ${snowflake_role.role.name};
    REVOKE ALL PRIVILEGES ON FUTURE TABLES IN DATABASE ${snowflake_database.database.name} FROM ROLE ${snowflake_role.role.name};
    EOT
  }
}

Check out the terraform-provider-snowsql snowsql_exec resource documentation for more information.

aidanmelen avatar Aug 30 '21 00:08 aidanmelen

+1 this would be fantastic

zack-klein avatar Aug 30 '21 15:08 zack-klein

Any news about this one?

ricardobf avatar Jul 05 '22 12:07 ricardobf

@aidanmelen

Hey @tnightengale, @JimFawkes, @a2m1, @JavierLopezT, @juangesino , and @gnilrets... I just created a provider that allows us to declare GRANT ALL statements in Terraform! Here is a code snippet from the terraform-provider-snowsql/examples/simple/main.tf.

resource "snowsql_exec" "dcl" {
  name = local.name

  create {
    statements = <<-EOT
    GRANT ALL PRIVILEGES ON ALL TABLES IN DATABASE ${snowflake_database.database.name} TO ROLE ${snowflake_role.role.name};
    GRANT ALL PRIVILEGES ON FUTURE TABLES IN DATABASE ${snowflake_database.database.name} TO ROLE ${snowflake_role.role.name};
    EOT
  }

  delete {
    statements = <<-EOT
    REVOKE ALL PRIVILEGES ON ALL TABLES IN DATABASE ${snowflake_database.database.name} FROM ROLE ${snowflake_role.role.name};
    REVOKE ALL PRIVILEGES ON FUTURE TABLES IN DATABASE ${snowflake_database.database.name} FROM ROLE ${snowflake_role.role.name};
    EOT
  }

  delete_on_create = true
}

Check out the terraform-provider-snowsql snowsql_exec resource documentation for more information.

There is no possible use a list ? Like in null_resources with for_each and triggers? I want to use the snowsql with more than 130 commands.

vlucas-caylent avatar Aug 09 '22 00:08 vlucas-caylent

There is no possible use a list ?

You could create snowflake tables using a list and a for_each loop. But that doesn't seem fun to manage. Also you would have to manually update the list for newly created tables.

Like in null_resources with for_each and triggers?

The local_exec null_resource would work to run Snowsql commands. However, in addition to having terraform installs, operators would also have to ensure that Snowsql is install and configured correctly. You would also not have any logic for deletion, so all the snowflake objects created with the null resource would have to be manually destroyed.

I want to use the snowsql with more than 130 commands.

The snowsql provider uses the same golang lib as the snowflake provider. I not familiar with any limits on how many statements you provide for a single transaction.

Cheers! hope that helps

aidanmelen avatar Aug 09 '22 01:08 aidanmelen

There is no possible use a list ?

You could create snowflake tables using a list and a for_each loop. But that doesn't seem fun to manage. Also you would have to manually update the list for newly created tables.

Like in null_resources with for_each and triggers?

The local_exec null_resource would work to run Snowsql commands. However, in addition to having terraform installs, operators would also have to ensure that Snowsql is install and configured correctly. You would also not have any logic for deletion, so all the snowflake objects created with the null resource would have to be manually destroyed.

I want to use the snowsql with more than 130 commands.

The snowsql provider uses the same golang lib as the snowflake provider. I not familiar with any limits on how many statements you provide for a single transaction.

Cheers! hope that helps

Yea, i'm using null_resource with local-exec snowsql commands, i receive a list using for_each and formatlist for db,schema,roles. My guess is about your provider, i'm trying to create a list and for_each to run in sequence in create statements. But its seems its not possible.

My idea is something like this: [According the documentation is not possible].

I just start this discussion, because i'm trying to migrate to your provider aswell.

resource "snowsql_exec" "dcl" {
  name = local.name
  for_each = snowflake_table_grant.update_schema_future
  triggers = {
    database  = each.value.database_name
    schema    = each.value.schema_name
    roles     = join(",", each.value.roles)
    }  
  create {
    statements = <<-EOT
    GRANT SELECT, REFERENCES ON ALL TABLES IN SCHEMA ${self.triggers.database_name} TO ROLE ${self.triggers.roles};
    GRANT SELECT, REFERENCES ON ALL VIEWS IN SCHEMA ${self.triggers.database_name} TO ROLE ${self.triggers.roles};
    GRANT SELECT ON ALL MATERIALIZED VIEWS IN SCHEMA ${self.triggers.database_name} TO ROLE ${self.triggers.roles};
    GRANT SELECT, REFERENCES ON ALL TABLES IN SCHEMA ${self.triggers.database_name}.${self.triggers.schema} TO ROLE %s", self.triggers.roles;
    GRANT SELECT, REFERENCES ON ALL VIEWS IN SCHEMA ${self.triggers.database_name}.${self.triggers.schema} TO ROLE %s", self.triggers.roles;
    GRANT SELECT ON ALL MATERIALIZED VIEWS IN SCHEMA ${self.triggers.database_name}.${self.triggers.schema} TO ROLE %s", self.triggers.roles;
    EOT
  }
  delete {
    statements = <<-EOT
    GRANT UPDATE, INSERT, DELETE, TRUNCATE ON ALL TABLES IN SCHEMA ${self.triggers.database_name} TO ROLE %s" ${self.triggers.roles};
    GRANT UPDATE, INSERT, DELETE, TRUNCATE ON ALL TABLES IN DATABASE ${self.triggers.database_name}.${self.triggers.schema} TO ROLE %s ${self.triggers.roles};
    EOT
  }
}

vlucas-caylent avatar Aug 09 '22 03:08 vlucas-caylent

According the documentation is not possible

no it is definitely supported and encouraged. Could you please create an issue in the snowsql provider? I would be able to help more over there. Somebody else might have the same question as you and I would like scope this to the snowsql project if possible.

aidanmelen avatar Aug 09 '22 03:08 aidanmelen

@aidanmelen Done. Thanks bro

vlucas-caylent avatar Aug 09 '22 03:08 vlucas-caylent

Can we close this issue?

iamontheinet avatar Dec 13 '22 01:12 iamontheinet

+1, currently working around this using data sources + for_each and it's quite tedious

cybdeen avatar Jan 03 '23 04:01 cybdeen

Can we close this issue?

No, please, we want the feature, not workarounds.

kamilamarcinekpgs avatar Jan 16 '23 13:01 kamilamarcinekpgs

Hi what is the status of this issue? Snowflake documentation supports using grant ALL privileges, it would be great if terraform worked with this aswell.

tursid avatar Mar 01 '23 08:03 tursid

We are closing this issue as part of a cleanup described in announcement. If you believe that the issue is still valid in v0.89.0, please open a new ticket.

sfc-gh-asawicki avatar Apr 30 '24 17:04 sfc-gh-asawicki