├── variables.tf ├── versions.tf ├── warehouses.tf ├── .github ├── CODEOWNERS ├── dependabot.yml └── workflows │ ├── security.yml │ ├── docs.yml │ ├── format.yml │ ├── release.yml │ ├── dependabot.yml │ └── validate.yml ├── roles.tf ├── .gitignore ├── .releaserc.json ├── databases.tf ├── CHANGELOG.md ├── README.md ├── locals.tf └── LICENSE /variables.tf: -------------------------------------------------------------------------------- 1 | variable "spec_file_path" { 2 | type = string 3 | description = "The path to the RBAC specification file." 4 | } 5 | -------------------------------------------------------------------------------- /versions.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_version = ">= 1.0.0" 3 | 4 | required_providers { 5 | snowflake = { 6 | source = "Snowflake-Labs/snowflake" 7 | version = ">= 0.46.0" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /warehouses.tf: -------------------------------------------------------------------------------- 1 | resource "snowflake_warehouse_grant" "warehouse_grants" { 2 | for_each = local.warehouse_grants 3 | warehouse_name = each.value.warehouse_name 4 | privilege = each.value.privilege 5 | roles = each.value.roles 6 | } 7 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # These owners will be the default owners for everything in 2 | # the repo. Unless a later match takes precedence, 3 | # @Infostrux-Solutions/open-source-maintainers will be requested 4 | # for review when someone opens a pull request. 5 | * @Infostrux-Solutions/open-source-maintainers 6 | -------------------------------------------------------------------------------- /roles.tf: -------------------------------------------------------------------------------- 1 | resource "snowflake_role_grants" "role_grants" { 2 | for_each = local.role_grants 3 | role_name = each.value.role_name 4 | roles = each.value.roles 5 | } 6 | 7 | resource "snowflake_role_grants" "user_grants" { 8 | for_each = local.user_grants 9 | role_name = each.value.role_name 10 | users = each.value.users 11 | } 12 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | --- 2 | version: 2 3 | 4 | updates: 5 | - package-ecosystem: "terraform" 6 | directory: "/" 7 | schedule: 8 | interval: "weekly" 9 | commit-message: 10 | prefix: "chore" 11 | 12 | - package-ecosystem: "github-actions" 13 | directory: "/" 14 | schedule: 15 | interval: "weekly" 16 | commit-message: 17 | prefix: "chore" 18 | -------------------------------------------------------------------------------- /.github/workflows/security.yml: -------------------------------------------------------------------------------- 1 | name: Security Checks 2 | 3 | on: 4 | pull_request 5 | 6 | permissions: 7 | contents: read 8 | pull-requests: write 9 | 10 | jobs: 11 | security: 12 | name: Security Checks 13 | runs-on: ubuntu-20.04 14 | steps: 15 | - name: Checkout 16 | uses: actions/checkout@v3 17 | - name: tfsec 18 | uses: aquasecurity/tfsec-pr-commenter-action@v1.2.0 19 | with: 20 | github_token: ${{ github.token }} 21 | -------------------------------------------------------------------------------- /.github/workflows/docs.yml: -------------------------------------------------------------------------------- 1 | name: Generate Docs 2 | 3 | on: 4 | pull_request 5 | 6 | permissions: 7 | contents: write 8 | 9 | jobs: 10 | docs: 11 | name: Generate Docs 12 | runs-on: ubuntu-22.04 13 | steps: 14 | - name: Checkout 15 | uses: actions/checkout@v3 16 | with: 17 | ref: ${{ github.event.pull_request.head.ref }} 18 | - name: Generate Docs 19 | uses: terraform-docs/gh-actions@v1.0.0 20 | with: 21 | working-dir: . 22 | output-method: inject 23 | git-push: true 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Local .terraform directories 2 | **/.terraform/* 3 | 4 | # .tfstate files 5 | *.tfstate 6 | *.tfstate.* 7 | 8 | # Crash log files 9 | crash.log 10 | 11 | # Ignore any .tfvars files that are generated automatically for each Terraform run. Most 12 | # .tfvars files are managed as part of configuration and so should be included in 13 | # version control. 14 | # 15 | terraform.tfvars 16 | 17 | # Ignore override files as they are usually used to override resources locally and so 18 | # are not checked in 19 | override.tf 20 | override.tf.json 21 | *_override.tf 22 | *_override.tf.json 23 | 24 | # Include override files you do wish to add to version control using negated pattern 25 | # 26 | # !example_override.tf 27 | 28 | # Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan 29 | # example: *tfplan* 30 | -------------------------------------------------------------------------------- /.github/workflows/format.yml: -------------------------------------------------------------------------------- 1 | name: Format Code 2 | 3 | on: 4 | pull_request 5 | 6 | permissions: 7 | contents: write 8 | 9 | jobs: 10 | format: 11 | name: Format Code 12 | runs-on: ubuntu-22.04 13 | steps: 14 | - name: Checkout 15 | uses: actions/checkout@v3 16 | with: 17 | ref: ${{ github.event.pull_request.head.ref }} 18 | token: ${{ secrets.TERRAFORM_FMT_TOKEN }} 19 | - name: Setup Terraform 20 | uses: hashicorp/setup-terraform@v2 21 | with: 22 | terraform_version: "1.0.0" 23 | - name: Format Code 24 | run: | 25 | terraform fmt -recursive . 26 | - name: Commit Format Changes 27 | run: | 28 | git config user.name 'github-actions[bot]' 29 | git config user.email '41898282+github-actions[bot]@users.noreply.github.com' 30 | git add . 31 | git diff-index --quiet HEAD || git commit -m "terraform-fmt: Auto format codebase" 32 | git push 33 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | workflow_dispatch: 5 | push: 6 | branches: 7 | - main 8 | paths: 9 | - '**/*.tf' 10 | - '**/*.md' 11 | - 'LICENSE' 12 | 13 | jobs: 14 | release: 15 | name: Release 16 | runs-on: ubuntu-22.04 17 | # Skip running release workflow on forks 18 | if: github.repository_owner == 'Infostrux-Solutions' 19 | steps: 20 | - name: Checkout 21 | uses: actions/checkout@v3 22 | with: 23 | persist-credentials: false 24 | fetch-depth: 0 25 | 26 | - name: Release 27 | uses: cycjimmy/semantic-release-action@v3.4.2 28 | with: 29 | semantic_version: 19.0.5 30 | extra_plugins: | 31 | @semantic-release/changelog@6.0.1 32 | @semantic-release/git@10.0.1 33 | conventional-changelog-conventionalcommits@5.0.0 34 | env: 35 | GITHUB_TOKEN: ${{ secrets.SEMANTIC_RELEASE_TOKEN }} 36 | -------------------------------------------------------------------------------- /.github/workflows/dependabot.yml: -------------------------------------------------------------------------------- 1 | name: Dependabot auto-merge 2 | 3 | on: pull_request 4 | 5 | permissions: 6 | contents: write 7 | pull-requests: write 8 | 9 | jobs: 10 | dependabot: 11 | runs-on: ubuntu-20.04 12 | if: ${{ github.actor == 'dependabot[bot]' }} 13 | steps: 14 | - name: Dependabot Metadata 15 | id: metadata 16 | uses: dependabot/fetch-metadata@v1.4.0 17 | with: 18 | github-token: "${{ secrets.GITHUB_TOKEN }}" 19 | - name: Enable auto-merge for Dependabot PRs 20 | if: ${{contains(steps.metadata.outputs.dependency-names, 'Snowflake-Labs/snowflake') && steps.metadata.outputs.update-type == 'version-update:semver-minor'}} 21 | run: gh pr merge --auto --squash "$PR_URL" 22 | env: 23 | PR_URL: ${{github.event.pull_request.html_url}} 24 | GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} 25 | - name: Approve a PR 26 | run: gh pr review --approve "$PR_URL" 27 | env: 28 | PR_URL: ${{github.event.pull_request.html_url}} 29 | GITHUB_TOKEN: ${{secrets.CI_TOKEN}} 30 | -------------------------------------------------------------------------------- /.releaserc.json: -------------------------------------------------------------------------------- 1 | { 2 | "branches": [ 3 | "main" 4 | ], 5 | "ci": false, 6 | "plugins": [ 7 | [ 8 | "@semantic-release/commit-analyzer", 9 | { 10 | "preset": "conventionalcommits", 11 | "releaseRules": [ 12 | {"type": "docs", "release": "patch"} 13 | ] 14 | } 15 | ], 16 | [ 17 | "@semantic-release/release-notes-generator", 18 | { 19 | "preset": "conventionalcommits", 20 | "presetConfig": { 21 | "types": [ 22 | { "type": "docs", "section": "Documentation", "hidden": false }, 23 | { "type": "feat", "section": "Features", "hidden": false }, 24 | { "type": "fix", "section": "Bug Fixes", "hidden": false } 25 | ] 26 | } 27 | } 28 | ], 29 | [ 30 | "@semantic-release/github", 31 | { 32 | "successComment": "This ${issue.pull_request ? 'PR is included' : 'issue has been resolved'} in version ${nextRelease.version} :tada:", 33 | "labels": false, 34 | "releasedLabels": false 35 | } 36 | ], 37 | [ 38 | "@semantic-release/changelog", 39 | { 40 | "changelogFile": "CHANGELOG.md", 41 | "changelogTitle": "# Changelog\n\nAll notable changes to this project will be documented in this file." 42 | } 43 | ], 44 | [ 45 | "@semantic-release/git", 46 | { 47 | "assets": [ 48 | "CHANGELOG.md" 49 | ], 50 | "message": "chore(release): version ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}" 51 | } 52 | ] 53 | ] 54 | } 55 | -------------------------------------------------------------------------------- /.github/workflows/validate.yml: -------------------------------------------------------------------------------- 1 | name: Validate PR Title 2 | 3 | on: 4 | pull_request_target: 5 | types: 6 | - opened 7 | - edited 8 | - synchronize 9 | 10 | jobs: 11 | validate: 12 | name: Validate PR Title 13 | runs-on: ubuntu-22.04 14 | steps: 15 | # Please look up the latest version from 16 | # https://github.com/amannn/action-semantic-pull-request/releases 17 | - uses: amannn/action-semantic-pull-request@v5.2.0 18 | env: 19 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 20 | with: 21 | # Configure which types are allowed. 22 | # Default: https://github.com/commitizen/conventional-commit-types 23 | types: | 24 | fix 25 | feat 26 | docs 27 | ci 28 | chore 29 | # Configure that a scope must always be provided. 30 | requireScope: false 31 | # Configure additional validation for the subject based on a regex. 32 | # This example ensures the subject starts with an uppercase character. 33 | subjectPattern: ^[A-Z].+$ 34 | # If `subjectPattern` is configured, you can use this property to override 35 | # the default error message that is shown when the pattern doesn't match. 36 | # The variables `subject` and `title` can be used within the message. 37 | subjectPatternError: | 38 | The subject "{subject}" found in the pull request title "{title}" 39 | didn't match the configured pattern. Please ensure that the subject 40 | starts with an uppercase character. 41 | # For work-in-progress PRs you can typically use draft pull requests 42 | # from Github. However, private repositories on the free plan don't have 43 | # this option and therefore this action allows you to opt-in to using the 44 | # special "[WIP]" prefix to indicate this state. This will avoid the 45 | # validation of the PR title and the pull request checks remain pending. 46 | # Note that a second check will be reported if this is enabled. 47 | wip: true 48 | # When using "Squash and merge" on a PR with only one commit, GitHub 49 | # will suggest using that commit message instead of the PR title for the 50 | # merge commit, and it's easy to commit this by mistake. Enable this option 51 | # to also validate the commit message for one commit PRs. 52 | validateSingleCommit: false 53 | -------------------------------------------------------------------------------- /databases.tf: -------------------------------------------------------------------------------- 1 | # Database Privileges 2 | 3 | resource "snowflake_database_grant" "database_read_grants" { 4 | for_each = local.database_read_grants 5 | database_name = each.value.database_name 6 | privilege = each.value.privilege 7 | roles = each.value.roles 8 | enable_multiple_grants = true 9 | } 10 | 11 | resource "snowflake_database_grant" "database_write_grants" { 12 | for_each = local.database_write_grants 13 | database_name = each.value.database_name 14 | privilege = each.value.privilege 15 | roles = each.value.roles 16 | enable_multiple_grants = true 17 | } 18 | 19 | # Schema Privileges 20 | 21 | resource "snowflake_schema_grant" "schema_read_grants" { 22 | for_each = local.schema_read_grants 23 | database_name = each.value.database_name 24 | privilege = each.value.privilege 25 | roles = each.value.roles 26 | enable_multiple_grants = true 27 | on_future = true 28 | with_grant_option = false 29 | } 30 | 31 | resource "snowflake_schema_grant" "schema_write_grants" { 32 | for_each = local.schema_write_grants 33 | database_name = each.value.database_name 34 | privilege = each.value.privilege 35 | roles = each.value.roles 36 | enable_multiple_grants = true 37 | on_future = true 38 | with_grant_option = false 39 | } 40 | 41 | # Table Privileges 42 | 43 | resource "snowflake_table_grant" "table_read_grants" { 44 | for_each = local.table_read_grants 45 | database_name = each.value.database_name 46 | privilege = each.value.privilege 47 | roles = each.value.roles 48 | enable_multiple_grants = true 49 | on_future = true 50 | with_grant_option = false 51 | } 52 | 53 | resource "snowflake_table_grant" "table_write_grants" { 54 | for_each = local.table_write_grants 55 | database_name = each.value.database_name 56 | privilege = each.value.privilege 57 | roles = each.value.roles 58 | enable_multiple_grants = true 59 | on_future = true 60 | with_grant_option = false 61 | } 62 | 63 | # View Privileges 64 | 65 | resource "snowflake_view_grant" "view_read_grants" { 66 | for_each = local.view_read_grants 67 | database_name = each.value.database_name 68 | privilege = each.value.privilege 69 | roles = each.value.roles 70 | enable_multiple_grants = true 71 | on_future = true 72 | with_grant_option = false 73 | } 74 | 75 | resource "snowflake_view_grant" "view_write_grants" { 76 | for_each = local.view_write_grants 77 | database_name = each.value.database_name 78 | privilege = each.value.privilege 79 | roles = each.value.roles 80 | enable_multiple_grants = true 81 | on_future = true 82 | with_grant_option = false 83 | } 84 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | ## [1.0.3](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/compare/v1.0.2...v1.0.3) (2023-03-02) 6 | 7 | 8 | ### Bug Fixes 9 | 10 | * Grant privileges on views ([#34](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/issues/34)) ([c853968](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/commit/c85396831528d94f2d06c5fbeecba1ce5f3698f1)) 11 | 12 | ## [1.0.2](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/compare/v1.0.1...v1.0.2) (2022-10-20) 13 | 14 | 15 | ### Documentation 16 | 17 | * Remove APPENDIX section from the LICENSE file ([#25](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/issues/25)) ([2b48ecd](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/commit/2b48ecd0a727b140f27d4039576ae92aa0770ecb)) 18 | 19 | ## [1.0.1](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/compare/v1.0.0...v1.0.1) (2022-10-14) 20 | 21 | 22 | ### Documentation 23 | 24 | * Push a patch release for docs updates ([#21](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/issues/21)) ([7866341](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/commit/7866341bd2ad875c94fc4c7dbc7d9b4b8a9af1da)) 25 | * Update README title to be consistent with other modules ([#20](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/issues/20)) ([929b9cb](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/commit/929b9cbcf7f2a1d58b8c5064edcea75d62b5b9d3)) 26 | * Update usage instructions to correct source and versions ([#18](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/issues/18)) ([07470c0](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/commit/07470c0ce18f14ededb47de82c9ff93f79f02e19)) 27 | 28 | ## [1.0.0](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/compare/v0.3.0...v1.0.0) (2022-10-05) 29 | 30 | 31 | ### ⚠ BREAKING CHANGES 32 | 33 | * Update Module to Use YAML Configuration (#4) 34 | 35 | ### Features 36 | 37 | * Add Code Formatting Pipeline ([#6](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/issues/6)) ([3c4e9d6](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/commit/3c4e9d610e6b8753a0891fe3705e75e423317a4f)) 38 | * Add Docs Generation Pipeline ([#8](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/issues/8)) ([b59de7b](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/commit/b59de7b9b8bb8907da3f5dfd05d6156134507153)) 39 | * Add PR title validation workflow ([#10](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/issues/10)) ([9ef908b](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/commit/9ef908be7e4765acedaae4c5e9f1a34855b66e16)) 40 | * Add release pipeline ([#9](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/issues/9)) ([90fb317](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/commit/90fb317c502a71e8309920d7fb76de77c1a2848c)) 41 | * Add tfsec Security Workflow ([#7](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/issues/7)) ([02af00c](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/commit/02af00cf91d135ec584d3eacd214aa83ce5049c4)) 42 | * Convert to Terraform Module ([#5](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/issues/5)) ([d74a53d](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/commit/d74a53d049d53e58156d382b1f872330098397be)) 43 | * Update Module to Use YAML Configuration ([#4](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/issues/4)) ([cfe8050](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/commit/cfe80500fddc443b6e564b7784463eaeb4bb7c7b)) 44 | 45 | 46 | ### Bug Fixes 47 | 48 | * Configure dependabot to use conventional commits ([4d5308b](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/commit/4d5308b54481a2424a95b7dc92180a5053273609)) 49 | * Pin GitHub runners to specific Ubuntu version ([#14](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/issues/14)) ([192bef3](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/commit/192bef34f4d700f576274f04700577f633c08418)) 50 | * Update workflow job names ([#11](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/issues/11)) ([bf9afda](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/commit/bf9afda9b71d8500009ff607da094b0bb434a9a5)) 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Snowflake RBAC Terraform Module 2 | 3 | Quickly deploy Snowflake RBAC resources using a simple configuration model. 4 | 5 | This module assumes the resources specified have been created in Snowflake by other means (e.g. through the Snowflake UI, using DDL commands or another Terraform project). In other words, no resources are created using this module, instead, they are associated using their respective `snowflake_*_grant` Terraform resource blocks. 6 | 7 | ## Usage 8 | 9 | ```hcl 10 | module "rbac" { 11 | source = "Infostrux-Solutions/rbac/snowflake" 12 | version = "1.0.0" 13 | 14 | providers = { 15 | snowflake = snowflake.securityadmin 16 | } 17 | 18 | depends_on = [ 19 | snowflake_database.databases, 20 | snowflake_role.roles, 21 | snowflake_user.users, 22 | snowflake_warehouse.warehouses, 23 | ] 24 | 25 | spec_file_path = "spec.yml" 26 | } 27 | ``` 28 | 29 | ### Snowflake Role 30 | 31 | Snowflake recommends using the **SECURITYADMIN** system role to grant or revoke privileges on objects in the account. Though not required, this module should be configured with a provider alias that uses **SECURITYADMIN** to deploy the resource grants. 32 | 33 | The following is an example of a working `providers.tf` file which specifies a user-configurable role (default) and the **SECURITYADMIN** aliased role: 34 | 35 | ```hcl 36 | terraform { 37 | required_version = ">= 1.0.0" 38 | 39 | required_providers { 40 | snowflake = { 41 | source = "Snowflake-Labs/snowflake" 42 | version = "0.46.0" 43 | } 44 | } 45 | } 46 | 47 | provider "snowflake" { 48 | account = var.snowflake_account 49 | username = var.snowflake_username 50 | private_key_path = var.snowflake_private_key_path 51 | private_key_passphrase = var.snowflake_private_key_passphrase 52 | role = var.snowflake_role 53 | } 54 | 55 | provider "snowflake" { 56 | alias = "securityadmin" 57 | account = var.snowflake_account 58 | username = var.snowflake_username 59 | private_key_path = var.snowflake_private_key_path 60 | private_key_passphrase = var.snowflake_private_key_passphrase 61 | role = "SECURITYADMIN" 62 | } 63 | ``` 64 | 65 | ### Configuring Resources 66 | 67 | The YAML specification file defines the relative permissions between databases, roles, users and warehouses in a Snowflake account. 68 | 69 | Database permissions are abbreviated as `read` or `write` permissions, with this module generating the proper grants. The following table shows the association between this module's permissions and Snowflake grants. 70 | 71 | | Objects | Permissions | Snowflake Grants | 72 | |------------|-------------|----------------------------------------------| 73 | | Databases | read | USAGE | 74 | | | write | MONITOR, CREATE SCHEMA | 75 | | Schemas | read | USAGE | 76 | | | write | MONITOR, CREATE TABLE, CREATE VIEW, CREATE STAGE, CREATE FILE FORMAT, CREATE SEQUENCE, CREATE FUNCTION, CREATE PIPE | 77 | | Tables | read | SELECT | 78 | | | write | INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES | 79 | | Views | read | SELECT | 80 | | | write | REFERENCES | 81 | | Warehouses | | USAGE, OPERATE, MONITOR | 82 | 83 | If no permission is specified for `schemas` and `tables`, then the permissions set for the database are assumed. The `warehouse` objects have a single permission type, so they are specified without a `read` or `write` qualifier (see below). 84 | 85 | #### spec.yml 86 | 87 | An RBAC specification file has the following structure: 88 | 89 | ```yaml 90 | databases: 91 | database_name: 92 | privileges: 93 | database: 94 | read: 95 | roles: 96 | - role_name 97 | - ... 98 | write: 99 | roles: 100 | - role_name 101 | - ... 102 | schemas: 103 | read: 104 | roles: 105 | - role_name 106 | - ... 107 | write: 108 | roles: 109 | - role_name 110 | - ... 111 | tables: 112 | read: 113 | roles: 114 | - role_name 115 | - ... 116 | write: 117 | roles: 118 | - role_name 119 | - ... 120 | ... ... ... 121 | 122 | roles: 123 | role_name: 124 | grant_to: 125 | roles: 126 | - role_name 127 | - ... 128 | users: 129 | - user_name 130 | - ... 131 | ... ... ... 132 | 133 | warehouses: 134 | warehouse_name: 135 | privileges: 136 | roles: 137 | - role_name 138 | - ... 139 | ... ... ... 140 | ``` 141 | 142 | 143 | ## Requirements 144 | 145 | | Name | Version | 146 | |------|---------| 147 | | [terraform](#requirement\_terraform) | >= 1.0.0 | 148 | | [snowflake](#requirement\_snowflake) | >= 0.46.0 | 149 | 150 | ## Providers 151 | 152 | | Name | Version | 153 | |------|---------| 154 | | [snowflake](#provider\_snowflake) | >= 0.46.0 | 155 | 156 | ## Modules 157 | 158 | No modules. 159 | 160 | ## Resources 161 | 162 | | Name | Type | 163 | |------|------| 164 | | [snowflake_database_grant.database_read_grants](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/database_grant) | resource | 165 | | [snowflake_database_grant.database_write_grants](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/database_grant) | resource | 166 | | [snowflake_role_grants.role_grants](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/role_grants) | resource | 167 | | [snowflake_role_grants.user_grants](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/role_grants) | resource | 168 | | [snowflake_schema_grant.schema_read_grants](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/schema_grant) | resource | 169 | | [snowflake_schema_grant.schema_write_grants](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/schema_grant) | resource | 170 | | [snowflake_table_grant.table_read_grants](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/table_grant) | resource | 171 | | [snowflake_table_grant.table_write_grants](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/table_grant) | resource | 172 | | [snowflake_view_grant.view_read_grants](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/view_grant) | resource | 173 | | [snowflake_view_grant.view_write_grants](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/view_grant) | resource | 174 | | [snowflake_warehouse_grant.warehouse_grants](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/warehouse_grant) | resource | 175 | 176 | ## Inputs 177 | 178 | | Name | Description | Type | Default | Required | 179 | |------|-------------|------|---------|:--------:| 180 | | [spec\_file\_path](#input\_spec\_file\_path) | The path to the RBAC specification file. | `string` | n/a | yes | 181 | 182 | ## Outputs 183 | 184 | No outputs. 185 | 186 | 187 | ## Authors 188 | Module is maintained by [Infostrux Solutions](mailto:opensource@infostrux.com) with help from [these awesome contributors](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/graphs/contributors). 189 | 190 | ## License 191 | Apache 2 Licensed. See [LICENSE](https://github.com/Infostrux-Solutions/terraform-snowflake-rbac/blob/main/LICENSE) for full details. 192 | -------------------------------------------------------------------------------- /locals.tf: -------------------------------------------------------------------------------- 1 | locals { 2 | # Constants 3 | 4 | spec = yamldecode(file(var.spec_file_path)) 5 | 6 | database_read_privileges = [ 7 | "USAGE", 8 | ] 9 | 10 | database_write_privileges = [ 11 | "USAGE", 12 | "MONITOR", 13 | "CREATE SCHEMA", 14 | ] 15 | 16 | schema_read_privileges = [ 17 | "USAGE", 18 | ] 19 | 20 | schema_write_privileges = [ 21 | "USAGE", 22 | "MONITOR", 23 | "CREATE TABLE", 24 | "CREATE VIEW", 25 | "CREATE STAGE", 26 | "CREATE FILE FORMAT", 27 | "CREATE SEQUENCE", 28 | "CREATE FUNCTION", 29 | "CREATE PIPE", 30 | ] 31 | 32 | table_read_privileges = [ 33 | "SELECT", 34 | ] 35 | 36 | table_write_privileges = [ 37 | "SELECT", 38 | "INSERT", 39 | "UPDATE", 40 | "DELETE", 41 | "TRUNCATE", 42 | "REFERENCES", 43 | ] 44 | 45 | view_read_privileges = [ 46 | "SELECT", 47 | ] 48 | 49 | view_write_privileges = [ 50 | "SELECT", 51 | "REFERENCES", 52 | ] 53 | 54 | warehouse_privileges = [ 55 | "USAGE", 56 | "OPERATE", 57 | "MONITOR", 58 | ] 59 | 60 | 61 | 62 | # Resource Maps 63 | 64 | ## Databases 65 | 66 | databases_with_privileges = { 67 | for database, spec in local.spec["databases"] : database => spec 68 | if contains(keys(spec), "privileges") 69 | } 70 | 71 | databases_with_database_privileges = { 72 | for database, spec in local.databases_with_privileges : database => spec 73 | if contains(keys(spec.privileges), "database") 74 | } 75 | 76 | databases_with_database_read_privileges = { 77 | for database, spec in local.databases_with_database_privileges : database => spec 78 | if contains(keys(spec.privileges.database), "read") 79 | } 80 | 81 | databases_with_database_write_privileges = { 82 | for database, spec in local.databases_with_database_privileges : database => spec 83 | if contains(keys(spec.privileges.database), "write") 84 | } 85 | 86 | database_read_grants = { 87 | for grant in flatten([ 88 | for privilege in local.database_read_privileges : [ 89 | for database, spec in local.databases_with_database_read_privileges : { 90 | database_name = upper(database) 91 | privilege = upper(privilege) 92 | roles = [for role in spec.privileges.database.read.roles : upper(role)] 93 | } 94 | ] 95 | ]) : lower(replace("${grant.database_name}_${grant.privilege}", " ", "_")) => grant } 96 | 97 | database_write_grants = { 98 | for grant in flatten([ 99 | for privilege in local.database_write_privileges : [ 100 | for database, spec in local.databases_with_database_write_privileges : { 101 | database_name = upper(database) 102 | privilege = upper(privilege) 103 | roles = [for role in spec.privileges.database.write.roles : upper(role)] 104 | } 105 | ] 106 | ]) : lower(replace("${grant.database_name}_${grant.privilege}", " ", "_")) => grant } 107 | 108 | ## Schemas 109 | 110 | databases_with_schema_privileges = { 111 | for database, spec in local.databases_with_privileges : database => spec 112 | if contains(keys(spec.privileges), "schemas") 113 | } 114 | 115 | databases_with_schema_read_privileges = { 116 | for database, spec in local.databases_with_schema_privileges : database => spec 117 | if contains(keys(spec.privileges.schema), "read") 118 | } 119 | 120 | databases_with_schema_write_privileges = { 121 | for database, spec in local.databases_with_schema_privileges : database => spec 122 | if contains(keys(spec.privileges.schema), "write") 123 | } 124 | 125 | schema_read_grants = { 126 | for grant in flatten([ 127 | for privilege in local.schema_read_privileges : [ 128 | for database, spec in local.databases_with_database_read_privileges : { 129 | database_name = upper(database) 130 | privilege = upper(privilege) 131 | roles = [for role in spec.privileges.database.read.roles : upper(role)] 132 | } 133 | ] 134 | ]) : lower(replace("${grant.database_name}_${grant.privilege}", " ", "_")) => grant } 135 | 136 | schema_write_grants = { 137 | for grant in flatten([ 138 | for privilege in local.schema_write_privileges : [ 139 | for database, spec in local.databases_with_database_write_privileges : { 140 | database_name = upper(database) 141 | privilege = upper(privilege) 142 | roles = [for role in spec.privileges.database.write.roles : upper(role)] 143 | } 144 | ] 145 | ]) : lower(replace("${grant.database_name}_${grant.privilege}", " ", "_")) => grant } 146 | 147 | ### Tables 148 | 149 | databases_with_table_privileges = { 150 | for database, spec in local.databases_with_privileges : database => spec 151 | if contains(keys(spec.privileges), "tables") 152 | } 153 | 154 | databases_with_table_read_privileges = { 155 | for database, spec in local.databases_with_table_privileges : database => spec 156 | if contains(keys(spec.privileges.table), "read") 157 | } 158 | 159 | databases_with_table_write_privileges = { 160 | for database, spec in local.databases_with_table_privileges : database => spec 161 | if contains(keys(spec.privileges.table), "write") 162 | } 163 | 164 | table_read_grants = { 165 | for grant in flatten([ 166 | for privilege in local.table_read_privileges : [ 167 | for database, spec in local.databases_with_database_read_privileges : { 168 | database_name = upper(database) 169 | privilege = upper(privilege) 170 | roles = [for role in spec.privileges.database.read.roles : upper(role)] 171 | } 172 | ] 173 | ]) : lower(replace("${grant.database_name}_${grant.privilege}", " ", "_")) => grant } 174 | 175 | table_write_grants = { 176 | for grant in flatten([ 177 | for privilege in local.table_write_privileges : [ 178 | for database, spec in local.databases_with_database_write_privileges : { 179 | database_name = upper(database) 180 | privilege = upper(privilege) 181 | roles = [for role in spec.privileges.database.write.roles : upper(role)] 182 | } 183 | ] 184 | ]) : lower(replace("${grant.database_name}_${grant.privilege}", " ", "_")) => grant } 185 | 186 | ### Views 187 | 188 | databases_with_view_privileges = { 189 | for database, spec in local.databases_with_privileges : database => spec 190 | if contains(keys(spec.privileges), "views") 191 | } 192 | 193 | databases_with_view_read_privileges = { 194 | for database, spec in local.databases_with_view_privileges : database => spec 195 | if contains(keys(spec.privileges.view), "read") 196 | } 197 | 198 | databases_with_view_write_privileges = { 199 | for database, spec in local.databases_with_view_privileges : database => spec 200 | if contains(keys(spec.privileges.view), "write") 201 | } 202 | 203 | view_read_grants = { 204 | for grant in flatten([ 205 | for privilege in local.view_read_privileges : [ 206 | for database, spec in local.databases_with_database_read_privileges : { 207 | database_name = upper(database) 208 | privilege = upper(privilege) 209 | roles = [for role in spec.privileges.database.read.roles : upper(role)] 210 | } 211 | ] 212 | ]) : lower(replace("${grant.database_name}_${grant.privilege}", " ", "_")) => grant } 213 | 214 | view_write_grants = { 215 | for grant in flatten([ 216 | for privilege in local.view_write_privileges : [ 217 | for database, spec in local.databases_with_database_write_privileges : { 218 | database_name = upper(database) 219 | privilege = upper(privilege) 220 | roles = [for role in spec.privileges.database.write.roles : upper(role)] 221 | } 222 | ] 223 | ]) : lower(replace("${grant.database_name}_${grant.privilege}", " ", "_")) => grant } 224 | 225 | 226 | ## Roles 227 | 228 | roles_with_grants = { 229 | for role, spec in local.spec["roles"] : role => spec 230 | if contains(keys(spec), "grant_to") 231 | } 232 | 233 | roles_with_role_grants = { 234 | for role, spec in local.roles_with_grants : role => spec 235 | if contains(keys(spec.grant_to), "roles") 236 | } 237 | 238 | roles_with_user_grants = { 239 | for role, spec in local.roles_with_grants : role => spec 240 | if contains(keys(spec.grant_to), "users") 241 | } 242 | 243 | role_grants = { 244 | for grant in flatten([ 245 | for role, spec in local.roles_with_role_grants : { 246 | role_name = upper(role) 247 | roles = [for grant_to_role in spec.grant_to.roles : upper(grant_to_role)] 248 | } 249 | ]) : lower(replace("${grant.role_name}", " ", "_")) => grant } 250 | 251 | user_grants = { 252 | for grant in flatten([ 253 | for role, spec in local.roles_with_user_grants : { 254 | role_name = upper(role) 255 | users = [for grant_to_user in spec.grant_to.users : upper(grant_to_user)] 256 | } 257 | ]) : lower(replace("${grant.role_name}", " ", "_")) => grant } 258 | 259 | ## Warehouses 260 | 261 | warehouses_with_privileges = { 262 | for warehouse, spec in local.spec["warehouses"] : warehouse => spec 263 | if contains(keys(spec), "privileges") 264 | } 265 | 266 | warehouse_grants = { 267 | for grant in flatten([ 268 | for privilege in local.warehouse_privileges : [ 269 | for warehouse, spec in local.warehouses_with_privileges : { 270 | warehouse_name = upper(warehouse) 271 | privilege = upper(privilege) 272 | roles = [for role in spec.privileges.roles : upper(role)] 273 | } 274 | ] 275 | ]) : lower(replace("${grant.warehouse_name}_${grant.privilege}", " ", "_")) => grant } 276 | } 277 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | --------------------------------------------------------------------------------