Repository navigation
feat(domain): add permission_grant validation and principal existence check (BDMS-1534) - #1039
likithabommasani21 wants to merge 2 commits into
Conversation
Coverage✅ 81.81% total — gate is 75%. Coverage for the Python files changed in this PR
|
ksmuczynski
left a comment
There was a problem hiding this comment.
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.
|
|
||
| from enum import Enum | ||
|
|
||
| from core.enums import Capability, PrincipalType, ScopeType |
There was a problem hiding this comment.
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.jsonwith thejsonmodule. - Delete this module and use
Capability,ScopeType, andPrincipalTypeas 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.
| return False | ||
| return session.query(ApiKey).filter(ApiKey.id == api_key_id).first() is not None | ||
|
|
||
| return True |
There was a problem hiding this comment.
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| api_key_id = int(principal_id) | ||
| except ValueError: | ||
| return False | ||
| return session.query(ApiKey).filter(ApiKey.id == api_key_id).first() is not None |
There was a problem hiding this comment.
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)
)| 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}") |
There was a problem hiding this comment.
This seems like a very useful function for other places in the API. I'd recommend putting it in services/util.py instead
There was a problem hiding this comment.
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.
… check (BDMS-1534)
…al_exists (BDMS-1534)
9ae924a to
3e23216
Compare
Coverage✅ 82.08% total — gate is 75%. Coverage for the Python files changed in this PR
|
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: thatcapability/scope_type/principal_typeare 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 functionsvalidate_capability,validate_scope_type,validate_principal_type-- check a value againstcore/enums.py(lexicon-backed), no database.services/permission_grant_helper.py:principal_exists(), the one DB-dependent check. Onlyapi_keyhas a local table to check against;user/principal_grouppass through unchecked today.tests/test_permission_grant.py: 10 tests covering both the domain validators andprincipal_exists.Author Checklist
Reviewer Checklist
validate_capability/validate_scope_type/validate_principal_typecorrectly reject invalid values and accept all current lexicon terms.principal_existshandles a non-numericapi_keyid without raising (returnsFalse).user/principal_grouppassing through unchecked is acceptable for now, given no local table exists for either.domain/permission_grant.pystays free of database/service imports.Notes
Any special considerations, known limitations, risks, workarounds, or follow-up work to note?
principal_existscurrently assumes Authentik is the sole owner ofuser/principal_groupmembership, and that no local table for either exists in our database -- so there's nothing to check them against, and they pass through unchecked.api_keyhas one; if not, no change needed.