Setting up roles and grants as code¶
This page builds the project in the order you'd learn it: the access model, the one-time bootstrap, a plan and deploy from your own terminal, then the pipeline that does it on every pull request.
The access model as data¶
A DCM project is a folder with a manifest.yml and SQL under sources/. The
manifest's templating section can hold any data, and the SQL files are Jinja
templates over it. That lets the access model live as YAML, with SQL generated
from it:
templating:
defaults:
role_parent: SYSADMIN
databases:
- name: SANDBOX
comment: Example database for the access model.
schemas:
- { name: RAW, comment: Data as loaded. }
- { name: CURATED, comment: Cleaned data for analysis. }
roles:
- name: ANALYST
comment: Reads curated data.
read: [SANDBOX.CURATED]
write: []
granted_to: [] # e.g. a role SCIM created from an Entra group
- name: DATA_ENGINEER
comment: Loads and transforms data.
read: []
write: [SANDBOX.RAW, SANDBOX.CURATED]
granted_to: []
Two layers of roles:
- Access roles are database roles, one
_READand one_WRITEper schema. They live in the database and are dropped with it._WRITEincludes_READ. - Functional roles are account roles that people and services receive.
Each gets the access roles of the schemas it reads or writes,
USAGEon those databases, and is granted torole_parentso administrators can use it.
A macro writes each schema's access roles:
{% macro schema_access_roles(db, schema) %}
define database role {{ db }}.{{ schema }}_READ;
define database role {{ db }}.{{ schema }}_WRITE;
grant usage on schema {{ db }}.{{ schema }} to database role {{ db }}.{{ schema }}_READ;
{% for kind in ['tables', 'views'] %}
grant select on all {{ kind }} in schema {{ db }}.{{ schema }} to database role {{ db }}.{{ schema }}_READ;
grant select on future {{ kind }} in schema {{ db }}.{{ schema }} to database role {{ db }}.{{ schema }}_READ;
{% endfor %}
grant database role {{ db }}.{{ schema }}_READ to database role {{ db }}.{{ schema }}_WRITE;
grant create table, create view on schema {{ db }}.{{ schema }} to database role {{ db }}.{{ schema }}_WRITE;
grant insert, update, delete, truncate on all tables in schema {{ db }}.{{ schema }} to database role {{ db }}.{{ schema }}_WRITE;
grant insert, update, delete, truncate on future tables in schema {{ db }}.{{ schema }} to database role {{ db }}.{{ schema }}_WRITE;
{% endmacro %}
Definition files may only contain DEFINE, GRANT and ATTACH statements,
and DCM's Jinja doesn't support import, include or extends. Macros in
sources/macros/ are visible to every file without importing them.
Future grants, and why the schemas are managed access¶
Snowflake now recommends inherited grants (GRANT INHERITED SELECT ON ALL
TABLES IN SCHEMA ...) over future grants, and DCM's docs say future grants
will be deprecated. In the account we tested, plain SQL accepted the inherited
syntax but DCM PLAN rejected it with Unsupported feature 'INHERITED'.
The cause is an opt-in: inherited grants (and container-level MANAGE GRANTS)
are off until an administrator sets the account parameter
FEATURE_RBAC_INHERITED_GRANTS = 'ENABLED', and it defaults to DISABLED.
With it enabled we found two more obstacles:
- Owning the schema isn't enough. An inherited grant needs
MANAGE GRANTSon the schema or a containing object, and the project can't grant that to itself, so an administrator would have to grant it for every database the project creates. - Plans left inherited grants out. A plan with an inherited grant completed, but its changeset didn't list it. Reviewers can't approve what they can't see.
So the macro uses ON ALL plus ON FUTURE, which work without either.
Future grants in a schema normally need MANAGE GRANTS on the account, which
is SECURITYADMIN-level power. The exception is a managed access schema:
its owner may create future grants. So every schema is defined
WITH MANAGED ACCESS, and the project's role owns it:
define schema {{ db.name }}.{{ schema.name }} with managed access
comment = '{{ schema.comment }}';
Managed access also means only the schema owner can grant on objects inside it. A data engineer who creates a table can't quietly share it; access goes through the repository.
The bootstrap¶
Run once, as a person with ACCOUNTADMIN. It creates two roles, the database that holds the project object, and the project:
USE ROLE USERADMIN;
CREATE ROLE IF NOT EXISTS DCM_ADMIN; -- owns and deploys the project
CREATE ROLE IF NOT EXISTS DCM_PLANNER; -- can plan, never deploy
GRANT ROLE DCM_ADMIN TO ROLE SECURITYADMIN;
GRANT ROLE DCM_PLANNER TO ROLE DCM_ADMIN;
USE ROLE SECURITYADMIN;
GRANT CREATE ROLE ON ACCOUNT TO ROLE DCM_ADMIN;
USE ROLE ACCOUNTADMIN; -- SECURITYADMIN was refused this one
GRANT CREATE DATABASE ON ACCOUNT TO ROLE DCM_ADMIN;
USE ROLE DCM_ADMIN;
CREATE DATABASE IF NOT EXISTS DCM;
CREATE SCHEMA IF NOT EXISTS DCM.PROJECTS;
CREATE DCM PROJECT IF NOT EXISTS DCM.PROJECTS.ACCESS_CONTROL;
GRANT USAGE ON DATABASE DCM TO ROLE DCM_PLANNER;
GRANT USAGE ON SCHEMA DCM.PROJECTS TO ROLE DCM_PLANNER;
GRANT PLAN ON DCM PROJECT DCM.PROJECTS.ACCESS_CONTROL TO ROLE DCM_PLANNER;
- Only the project's owner can deploy.
PLANis a separate privilege: a plan runs with the owner's privileges but changes nothing. That split is what lets pull requests see a plan without being able to deploy. DCM_ADMINhas noMANAGE GRANTS. It owns everything the project creates and grants only on that. When the definitions need to grant on objects created elsewhere, addMANAGE GRANTSon that database or schema (container-levelMANAGE GRANTS) rather than on the account.- The project's database isn't in the definitions, so no deploy can drop it.
DCM_ADMINrolls up to SECURITYADMIN, which fewer people hold than SYSADMIN. The functional roles it creates roll up to SYSADMIN.
The basic version: plan and deploy from a terminal¶
With your own Snowflake connection (any user that can use DCM_ADMIN):
snow dcm plan --from . --role DCM_PLANNER --save-output
snow dcm deploy --from . --role DCM_ADMIN
The plan uploads the folder to a temporary stage, renders the Jinja, and
prints each step. --save-output writes out/plan_result.json, a changeset
listing every object and grant it would create, alter or drop. The reference
repository's scripts/plan_summary.py turns it into a readable diff, with
drops and revokes listed first:
CREATE role ANALYST
~ comment = 'Reads curated data.'
+ database role SANDBOX.CURATED_READ
+ USAGE ON DATABASE SANDBOX
CREATE database role SANDBOX.CURATED_READ
+ SELECT ON FUTURE TABLES IN SCHEMA SANDBOX.CURATED
+ SELECT ON FUTURE VIEWS IN SCHEMA SANDBOX.CURATED
+ USAGE ON SCHEMA SANDBOX.CURATED
ALTER role SYSADMIN
+ role ANALYST
+ role DATA_ENGINEER
ON ALL grants don't appear while the schemas are empty; there is nothing
for them to cover yet.
Read every plan before deploying. plan --delta is faster during
development, but it only looks at changed definitions, so it can't see changes
made outside DCM. Always run a full plan before a deploy.
The pipeline¶
Two identities¶
| Job | Runs on | Azure identity trusts | Snowflake user | Snowflake role |
|---|---|---|---|---|
| plan | every pull request; manual runs on main |
...:pull_request and ...:ref:refs/heads/main |
SVC_DCM_PLAN |
DCM_PLANNER |
| deploy | push to main; manual runs |
...:environment:snowflake-dcm |
SVC_DCM_DEPLOY |
DCM_ADMIN |
A pull request can change the workflow file, so anything a pull request's job
can reach, the pull request can use. Its identity can only plan. The deploy
identity's federated credential trusts only the snowflake-dcm environment,
and the environment's branch policy admits only main.
infra/main.bicep creates both identities and their federated credentials.
Azure rejects concurrent federated-credential writes on one identity, so the
Bicep chains them with dependsOn. The subjects use the repository's
immutable prefix; read it with
gh api repos/OWNER/REPO/actions/oidc/customization/sub -q .sub_claim_prefix.
The bootstrap then creates one service user per identity:
CREATE USER IF NOT EXISTS SVC_DCM_PLAN
TYPE = SERVICE
WORKLOAD_IDENTITY = (TYPE = AZURE
ISSUER = 'https://login.microsoftonline.com/<tenant-id>/v2.0'
SUBJECT = '<plan identity principalId>')
DEFAULT_ROLE = DCM_PLANNER
DEFAULT_SECONDARY_ROLES = ();
GRANT ROLE DCM_PLANNER TO USER SVC_DCM_PLAN;
-- and SVC_DCM_DEPLOY, with the deploy identity and DCM_ADMIN
Signing in with the Snowflake CLI¶
The CLI signs in as a managed identity the same way the Python connector does:
it reads a GitHub OIDC token from the file named by
AZURE_FEDERATED_TOKEN_FILE and exchanges it for the identity's Entra token.
Snowflake CLI 3.33.0 bundles connector 4.8.1, which needs azure-identity
for that path. Without it the sign-in fails with MissingDependencyError.
Install both, pinned by hash:
snowflake-cli==3.33.0
azure-identity
The job writes the token just before running snow, then uses a temporary
connection:
snow dcm plan --from . --save-output -x \
--account "$SNOWFLAKE_ACCOUNT" --user SVC_DCM_PLAN --role DCM_PLANNER \
--authenticator WORKLOAD_IDENTITY --workload-identity-provider AZURE
The workflow then runs plan_summary.py and appends the result to the job
summary, so a reviewer sees the changeset on the pull request's checks page,
and uploads out/ as an artifact.
Hardening¶
- Use ephemeral self-hosted runners, or GitHub-hosted ones. Plan jobs run pull-request code. On a runner that keeps its disk between jobs, a malicious branch could leave something behind that reads a later deploy job's token. The identity split protects Snowflake, not the runner.
- Require reviews on
main, and add CODEOWNERS formanifest.yml,sources/and.github/. Merging is deploying. - Run a plan of
mainon a schedule to see drift: changes made by hand to objects the project manages show up as differences. - Pin everything: actions to commit SHAs, the CLI and its dependencies by
hash, and a per-job virtual environment and
SNOWFLAKE_HOMEso a self-hosted runner's own~/.snowflakeis never read. - Keep secrets out of templating variables. DCM doesn't redact them in the rendered SQL, which lands in plan output and artifacts.