Skip to content

feat(grant): support temporary role grants (FOR INTERVAL / UNTIL) - #108

Open
sgoley wants to merge 1 commit into
datacoves:mainfrom
sgoley:feat/temporary-role-grants
Open

sgoley wants to merge 1 commit into
datacoves:mainfrom
sgoley:feat/temporary-role-grants

Conversation

@sgoley

@sgoley sgoley commented Nov 18, 2026

Copy link
Copy Markdown

Summary

Refs #107 — a first-draft implementation of temporary role grants for review.

Adds for_interval and until to RoleGrant, covering the Snowflake
preview syntax:

role_grants:
  - role: oncall
    to_user: alice
    for_interval: 24 HOURS
  - role: oncall
    to_user: bob
    until: "2026-12-01T00:00:00Z"
  • _RoleGrant gains for_interval (config-only, fetchable: False — same
    precedent as UserKeyPair.days_to_expiry), until, and a computed
    has_expiration. Intervals and timestamps are normalized to canonical
    forms so the remote expires_on compares cleanly against config.
  • create_role_grant renders FOR INTERVAL/UNTIL; the clauses are
    rejected on role-to-role grants, matching Snowflake's restriction.
  • update_role_grant (previously NotImplementedError) re-GRANTs with
    RESET EXPIRATION so the expiration is set to exactly the desired value
    — required, since re-granting a timed clause onto a permanent grant
    leaves it permanent otherwise. A plain re-GRANT (no clause) makes a
    temporary grant permanent again.
  • ACCOUNT_USAGE.GRANTS_TO_USERS selects EXPIRES_ON, retrying without
    the column on accounts where the preview column does not exist yet
    (INVALID_COLUMN_ERR/INVALID_IDENTIFIER, the same pair used
    elsewhere); SHOW GRANTS reads expires_on too. A grant whose
    expiration has already passed is treated as gone.
  • parse_grant understands the new syntax, including RESET EXPIRATION;
    role_grants: YAML accepts for_interval/until on user grants.

Notable semantics worth review:

  • An interval-managed grant shows a diff on every plan (until drifts as
    time passes) and each apply re-arms the interval — the honest
    representation of "lasts N hours from when applied". Documented in the
    resource docstring and docs/resources/role_grant.md.
  • Live Snowflake behavior (actual expiry, multi-grantor grants) is not
    covered — integration tests would need an account on the preview.

Test plan

  • uv run pytest tests/ — 2412 passed
  • black, ruff, codespell, mypy clean
  • New unit tests: spec validation/normalization, SQL rendering,
    update paths (permanent/temporary transitions, reset), parser,
    ACCOUNT_USAGE EXPIRES_ON handling + column-missing fallback
  • Integration run against an account with the preview enabled

Generated with Devin

Snowflake's temporary role grants preview lets GRANT ROLE ... TO USER
carry FOR INTERVAL '<n> <MINUTES|HOURS|DAYS>' or UNTIL '<timestamp>', and
reports expires_on in SHOW GRANTS and ACCOUNT_USAGE.GRANTS_TO_USERS.

- _RoleGrant gains for_interval (config-only, like UserKeyPair's
  days_to_expiry, until, and has_expiration; intervals and timestamps
  are normalized to canonical forms so remote expires_on compares
  cleanly against config.
- create_role_grant renders the clause; update_role_grant re-GRANTs
  with RESET EXPIRATION so an expiration is set to exactly the desired
  value, and a plain re-GRANT makes a grant permanent again.
- ACCOUNT_USAGE.GRANTS_TO_USERS selects EXPIRES_ON, retrying without it
  on accounts where the preview column does not exist yet; SHOW GRANTS
  reads expires_on too. A grant whose expiration already passed is
  treated as gone, matching the access Snowflake actually gives.
- parse_grant understands the new syntax; role_grants YAML accepts
  for_interval/until (user grants only).

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
EOF
)
@noel

noel commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

@sgoley thanks for this. I reviewed the branch at 45679ec and checked the expiration handling against the GRANT ROLE, SHOW GRANTS and GRANTS_TO_USERS docs. The core behavior matches what Snowflake documents: reading expires_on from SHOW, a plain GRANT making a temporary grant permanent, RESET EXPIRATION to shorten or make a permanent grant temporary, and the single-unit MINUTES|HOURS|DAYS interval rule. I haven't run it against a live account.

A few things to tighten:

  1. Unquoted naive YAML timestamps are taken as UTC. normalize_grant_expiration treats any datetime without a tzinfo as UTC, which is right for what the connector returns. YAML also produces a naive datetime for until: 2026-12-01 00:00:00 (unquoted), so that config is accepted as UTC while the quoted "2026-12-01 00:00:00" is rejected for missing a timezone. Snowflake rejects timestamps without an offset, so the config side should too. Splitting the connector path from the config path (or rejecting naive datetimes in _RoleGrant.__post_init__ / to_dict) would fix it.

  2. A past until only fails at apply time. Snowflake requires the timestamp to be in the future, and the fetch side reports expired grants as absent, so a stale until shows a pending create on every plan and then errors on apply. A plan-time check with a clear message would surface it earlier.

  3. The ACCOUNT_USAGE fallback query depends on exact whitespace. query.replace(",\n EXPIRES_ON", "") matches nothing if the query is reindented, so the retry sends the same failing SQL and grants-to-users silently falls back to SHOW. Building the column list in code avoids that. The fallback-and-return-None block also appears several times and could share one exit.

  4. ACCOUNT_USAGE lag with use_account_usage=True. GRANTS_TO_USERS can lag up to 2 hours, so after an until change the next plan in that window reads the old EXPIRES_ON and proposes the update again. Worth a line in the docs at minimum.

Smaller items:

  • The FOR INTERVAL / UNTIL clause is built separately in create_role_grant and update_role_grant. One helper, with update appending RESET EXPIRATION, keeps them from drifting.
  • A templated for_interval skips normalize_grant_interval and isn't checked after vars resolve, while until is normalized again in to_dict. A bad value like "36" then fails in Snowflake at apply.
  • has_expiration is derived from for_interval / until but shows up as a settable spec field and in to_dict output. Marking it init=False would keep callers from setting it inconsistently.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants