Skip to content

feat(domain): add permission_grant validation and principal existence check (BDMS-1534) - #1039

Open
likithabommasani21 wants to merge 2 commits into
stagingfrom
BDMS-1534-validation-for-permission-grant-input
Open

likithabommasani21 wants to merge 2 commits into
stagingfrom
BDMS-1534-validation-for-permission-grant-input

Conversation

@likithabommasani21

Copy link
Copy Markdown
Contributor

BDMS-1534: Add permission_grant validation and principal existence check

Why

This PR addresses the following problem / context:

  • permission_grant (BDMS-1532) needs validation before a grant is persisted: that capability/scope_type/principal_type are real values, and that the principal being granted access actually exists where that's checkable.

How

Implementation summary: the following was changed / added / removed:

  • domain/permission_grant.py: pure functions validate_capability, validate_scope_type, validate_principal_type -- check a value against core/enums.py (lexicon-backed), no database.
  • services/permission_grant_helper.py: principal_exists(), the one DB-dependent check. Only api_key has a local table to check against; user/principal_group pass through unchecked today.
  • tests/test_permission_grant.py: 10 tests covering both the domain validators and principal_exists.

Author Checklist

  • Changes are limited to the intended scope.
  • Tests were added or updated where appropriate.
  • Relevant tests and lint checks pass.
  • No secrets, credentials, or sensitive data were publicly exposed.
  • [N/A] Documentation was updated where necessary.

Reviewer Checklist

  • validate_capability/validate_scope_type/validate_principal_type correctly reject invalid values and accept all current lexicon terms.
  • principal_exists handles a non-numeric api_key id without raising (returns False).
  • Agree that user/principal_group passing through unchecked is acceptable for now, given no local table exists for either.
  • domain/permission_grant.py stays free of database/service imports.

Notes

Any special considerations, known limitations, risks, workarounds, or follow-up work to note?

  • principal_exists currently assumes Authentik is the sole owner of user/principal_group membership, and that no local table for either exists in our database -- so there's nothing to check them against, and they pass through unchecked.
  • Posted a comment on ADR6 to confirm this assumption, since some of its wording could be read as Ocotillo owning group membership instead. Once that's resolved: if Ocotillo ends up with a real local table for groups, add an existence check here the same way api_key has one; if not, no change needed.

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Coverage

✅ 81.81% total — gate is 75%.

Coverage for the Python files changed in this PR
Name Stmts Miss Cover Missing
domain/permission_grant.py 13 0 100%
services/permission_grant_helper.py 10 0 100%
TOTAL 23 0 100%

@ksmuczynski ksmuczynski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I think just one change is needed before merge: domain/permission_grant.py imports core.enums, which imports SQLAlchemy and the db package. ADR4 and CLAUDE.md say modules in domain/ must not import sqlalchemy, db/, or services/, so that domain rules can be tested without a database. The other comments are non-blocking.

Comment thread domain/permission_grant.py Outdated

from enum import Enum

from core.enums import Capability, PrincipalType, ScopeType

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core/enums.py imports services.lexicon_helper, which imports sqlalchemy and db.lexicon. Importing this module therefore loads SQLAlchemy and the whole db package. ADR4 and CLAUDE.md say modules in domain/ must not import sqlalchemy, db/, or services/, so that domain rules can be tested without a database.

Any of these would fix it:

  • Pass the allowed values in as a set from the caller.
  • Read the three categories from core/lexicon.json with the json module.
  • Delete this module and use Capability, ScopeType, and PrincipalType as field types on the future create schema. Pydantic would make the same check and return a 422, which is where CLAUDE.md puts input validation.

Comment thread services/permission_grant_helper.py Outdated
return False
return session.query(ApiKey).filter(ApiKey.id == api_key_id).first() is not None

return True

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For every principal_type except api_key, this returns True, including invalid types: principal_exists(session, "bogus", "x") is True. The docstring only says user and principal_group skip the check. I suggest returning True for those two and False for anything else, so a caller that skips validate_principal_type still rejects a bad type.

    if principal_type in ("user", "principal_group"):
        # No local table exists for either type.
        return True
    return False

Comment thread services/permission_grant_helper.py Outdated
api_key_id = int(principal_id)
except ValueError:
return False
return session.query(ApiKey).filter(ApiKey.id == api_key_id).first() is not None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns True for revoked and expired keys, so a grant could be saved for a key that can no longer authenticate. resolve_api_key() uses domain.api_key.is_usable() to reject those keys. Consider using the same function here:

        key = session.get(ApiKey, api_key_id)
        return key is not None and is_usable(
            key.revoked_at, key.expires_at, datetime.now(timezone.utc)
        )

Comment thread domain/permission_grant.py Outdated
Comment on lines +12 to +16
def _validate_against_enum(value: str, enum_cls: type[Enum], field_name: str) -> None:
try:
enum_cls(value)
except ValueError:
raise ValueError(f"Invalid {field_name}: {value!r}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a very useful function for other places in the API. I'd recommend putting it in services/util.py instead

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or core/enums.py

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this to either services/util.py or core/enums.py would reintroduce the SQLAlchemy/db import that domain/ modules aren't allowed to have.

@likithabommasani21
likithabommasani21 force-pushed the BDMS-1534-validation-for-permission-grant-input branch from 9ae924a to 3e23216 Compare November 24, 2026 20:35
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

✅ 82.08% total — gate is 75%.

Coverage for the Python files changed in this PR
Name Stmts Miss Cover Missing
domain/permission_grant.py 22 0 100%
services/permission_grant_helper.py 19 0 100%
TOTAL 41 0 100%

@jacob-a-brown jacob-a-brown left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me!

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.

4 participants