sentrykit 0.1.0rc1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. sentrykit/__init__.py +200 -0
  2. sentrykit/audit.py +967 -0
  3. sentrykit/cedar.py +750 -0
  4. sentrykit/cli.py +1405 -0
  5. sentrykit/config.py +300 -0
  6. sentrykit/constraints.py +928 -0
  7. sentrykit/context.py +393 -0
  8. sentrykit/decorator.py +252 -0
  9. sentrykit/engine.py +1509 -0
  10. sentrykit/envelope.py +513 -0
  11. sentrykit/errors.py +252 -0
  12. sentrykit/integrations/__init__.py +1 -0
  13. sentrykit/integrations/base.py +767 -0
  14. sentrykit/integrations/fastapi.py +361 -0
  15. sentrykit/integrations/flask.py +342 -0
  16. sentrykit/integrations/mcp.py +929 -0
  17. sentrykit/integrations/pydantic_ai.py +728 -0
  18. sentrykit/integrations/tools.py +479 -0
  19. sentrykit/masking.py +459 -0
  20. sentrykit/policy/__init__.py +40 -0
  21. sentrykit/policy/loader.py +764 -0
  22. sentrykit/policy/pack/10-rbac.cedar +9 -0
  23. sentrykit/policy/pack/20-groups.cedar +9 -0
  24. sentrykit/policy/pack/30-ownership.cedar +8 -0
  25. sentrykit/policy/pack/40-delegation.cedar +40 -0
  26. sentrykit/policy/pack/50-expiry.cedar +24 -0
  27. sentrykit/policy/pack/60-break-glass.cedar +34 -0
  28. sentrykit/policy/pack/70-separation-of-duties.cedar +22 -0
  29. sentrykit/policy/pack/80-jit-elevation.cedar +32 -0
  30. sentrykit/policy/pack/entities.json +3 -0
  31. sentrykit/policy/pack/pack.cedarschema +45 -0
  32. sentrykit/policy/pack/templates/time-boxed-grant.cedartemplate +5 -0
  33. sentrykit/py.typed +0 -0
  34. sentrykit/registry.py +666 -0
  35. sentrykit/stores/__init__.py +116 -0
  36. sentrykit/stores/base.py +870 -0
  37. sentrykit/stores/composite.py +635 -0
  38. sentrykit/stores/filesystem.py +400 -0
  39. sentrykit/stores/jsonfile.py +701 -0
  40. sentrykit/stores/jsonl.py +357 -0
  41. sentrykit/stores/memory.py +500 -0
  42. sentrykit-0.1.0rc1.dist-info/METADATA +541 -0
  43. sentrykit-0.1.0rc1.dist-info/RECORD +46 -0
  44. sentrykit-0.1.0rc1.dist-info/WHEEL +4 -0
  45. sentrykit-0.1.0rc1.dist-info/entry_points.txt +20 -0
  46. sentrykit-0.1.0rc1.dist-info/licenses/LICENSE +201 -0
@@ -0,0 +1,9 @@
1
+ // Role-based access, without the pack ever naming a role.
2
+ //
3
+ // A shipped policy cannot know that your roles are called `support` and `oncall`, so the
4
+ // *resource* declares which role it requires and the grant store supplies the principal's
5
+ // edge to it. `principal in resource.required_role` is transitive, so a role hierarchy --
6
+ // `Role::"lead" in Role::"support"` -- resolves in Cedar rather than in application code.
7
+ @id("rbac-required-role")
8
+ permit (principal, action, resource)
9
+ when { resource has required_role && principal in resource.required_role };
@@ -0,0 +1,9 @@
1
+ // Membership of a team, project or tenant, resolved through Cedar's parent closure.
2
+ //
3
+ // The resource names the group it belongs to; a principal reaches it through however many
4
+ // hops its own edges take. A member of `Group::"eu-west"` therefore satisfies a resource
5
+ // declaring `Group::"eu"` with no policy per level -- which is the multi-hop relationship
6
+ // resolution sentrykit exists to offload.
7
+ @id("group-membership")
8
+ permit (principal, action, resource)
9
+ when { resource has group && principal in resource.group };
@@ -0,0 +1,8 @@
1
+ // The owner of a thing may act on it.
2
+ //
3
+ // Equality, not `in`: ownership is not inherited. A team the owner belongs to does not
4
+ // become an owner, and handing ownership on is a separate grant rather than a side effect
5
+ // of a group edge.
6
+ @id("resource-owner")
7
+ permit (principal, action, resource)
8
+ when { resource has owner && resource.owner == principal };
@@ -0,0 +1,40 @@
1
+ // Acting on behalf of someone else, within a scope that binds rather than adds.
2
+ //
3
+ // The delegated principal -- typically an agent -- arrives from the grant store already
4
+ // carrying its own edges: who delegated to it, which actions it may take, and when the
5
+ // delegation lapses. The caller must also *declare* the delegation in the context, so a
6
+ // delegated identity is never used silently.
7
+ //
8
+ // The two forbids are what make the scope a boundary. Without them a delegated agent whose
9
+ // scope excludes an action would still reach the plain RBAC permit and take it anyway,
10
+ // because a Cedar permit needs no other permit's agreement -- only the absence of a forbid.
11
+ @id("delegation-on-behalf-of")
12
+ @obligations("record:on_behalf_of")
13
+ permit (principal, action, resource)
14
+ when
15
+ {
16
+ context has on_behalf_of &&
17
+ principal has delegated_by &&
18
+ principal.delegated_by == context.on_behalf_of &&
19
+ principal has delegated_actions &&
20
+ action in principal.delegated_actions &&
21
+ context has now &&
22
+ principal has delegation_expires &&
23
+ context.now <= principal.delegation_expires &&
24
+ resource has required_role &&
25
+ principal in resource.required_role
26
+ };
27
+
28
+ @id("delegation-outside-scope-forbidden")
29
+ forbid (principal, action, resource)
30
+ when
31
+ { principal has delegated_actions && !(action in principal.delegated_actions) };
32
+
33
+ @id("delegation-expired-forbidden")
34
+ forbid (principal, action, resource)
35
+ when
36
+ {
37
+ principal has delegation_expires &&
38
+ context has now &&
39
+ context.now > principal.delegation_expires
40
+ };
@@ -0,0 +1,24 @@
1
+ // An entitlement that stops on its own, at both ends.
2
+ //
3
+ // A grant store already drops expired grants when it materializes a principal, so this is
4
+ // the belt to that store's braces: a principal assembled by a host's own directory, or one
5
+ // carrying an entitlement window as a plain attribute, still stops being entitled at the
6
+ // declared moment. Expressed as a forbid so it beats every permit in the pack -- an expiry
7
+ // that could be out-voted is not an expiry.
8
+ @id("entitlement-expired-forbidden")
9
+ forbid (principal, action, resource)
10
+ when
11
+ {
12
+ principal has entitlement_expires &&
13
+ context has now &&
14
+ context.now > principal.entitlement_expires
15
+ };
16
+
17
+ @id("entitlement-not-yet-valid-forbidden")
18
+ forbid (principal, action, resource)
19
+ when
20
+ {
21
+ principal has entitlement_starts &&
22
+ context has now &&
23
+ context.now < principal.entitlement_starts
24
+ };
@@ -0,0 +1,34 @@
1
+ // Emergency access: an entitlement, a flag *and* a justification -- never a flag alone.
2
+ //
3
+ // The obvious draft -- a permit conditioned on `context.break_glass` only -- authorizes
4
+ // literally anyone: the flag is caller-supplied, and a
5
+ // Cedar permit needs no base entitlement to fire. So the resource declares an
6
+ // `emergency_role`, the principal must be in it, and the context must carry a non-empty
7
+ // justification.
8
+ //
9
+ // The forbid half is what makes an emergency-only resource emergency-only: it fires whenever
10
+ // the flag is absent, and forbid beats every other permit in the pack. The permit carries a
11
+ // mandatory obligation, so a call that proceeds this way cannot proceed quietly.
12
+ @id("emergency-resource-forbidden-without-flag")
13
+ forbid (principal, action, resource)
14
+ when
15
+ {
16
+ resource has emergency_only &&
17
+ resource.emergency_only &&
18
+ !(context has break_glass && context.break_glass)
19
+ };
20
+
21
+ @id("break-the-glass")
22
+ @obligations("notify:secops,record:justification")
23
+ permit (principal, action, resource)
24
+ when
25
+ {
26
+ resource has emergency_only &&
27
+ resource.emergency_only &&
28
+ resource has emergency_role &&
29
+ principal in resource.emergency_role &&
30
+ context has break_glass &&
31
+ context.break_glass &&
32
+ context has justification &&
33
+ context.justification != ""
34
+ };
@@ -0,0 +1,22 @@
1
+ // Two duties that must not land on one person.
2
+ //
3
+ // The first half is static incompatibility: the resource declares the role that must *not*
4
+ // hold it, so "nobody in the audit role may touch settlement" is a resource attribute rather
5
+ // than a cross-product of policies.
6
+ //
7
+ // The second is the four-eyes rule, and it needs the approval action to be identifiable. The
8
+ // pack ships `Action::"approve"` in its entity graph precisely so a host can map its own
9
+ // actions into it -- `Action::"payments.approve" in Action::"approve"` -- and have this
10
+ // forbid apply without editing reviewed policy logic.
11
+ @id("sod-conflicting-role")
12
+ forbid (principal, action, resource)
13
+ when
14
+ { resource has conflicting_role && principal in resource.conflicting_role };
15
+
16
+ @id("sod-same-actor-approval")
17
+ forbid (
18
+ principal,
19
+ action in Action::"approve",
20
+ resource
21
+ )
22
+ when { resource has prepared_by && resource.prepared_by == principal };
@@ -0,0 +1,32 @@
1
+ // Just-in-time elevation: a role held for a window, and only for that window.
2
+ //
3
+ // The elevation is two attributes on the principal -- which role, and until when -- so
4
+ // granting it is a store write rather than a policy change. The permit matches the elevated
5
+ // role against what the resource requires, which keeps elevation from becoming a blanket
6
+ // escalation: holding an elevation to one role says nothing about a resource that requires
7
+ // another.
8
+ //
9
+ // The forbid closes the window from the other side. Without it a lapsed elevation would
10
+ // merely stop matching this permit, leaving every other permit the principal has intact --
11
+ // which is right for a role they still hold and wrong for one that has just run out.
12
+ @id("jit-elevation")
13
+ @obligations("notify:secops")
14
+ permit (principal, action, resource)
15
+ when
16
+ {
17
+ principal has elevation_role &&
18
+ principal has elevation_until &&
19
+ context has now &&
20
+ context.now <= principal.elevation_until &&
21
+ resource has required_role &&
22
+ principal.elevation_role == resource.required_role
23
+ };
24
+
25
+ @id("jit-elevation-expired-forbidden")
26
+ forbid (principal, action, resource)
27
+ when
28
+ {
29
+ principal has elevation_until &&
30
+ context has now &&
31
+ context.now > principal.elevation_until
32
+ };
@@ -0,0 +1,3 @@
1
+ [
2
+ {"uid": {"type": "Action", "id": "approve"}, "attrs": {}, "parents": []}
3
+ ]
@@ -0,0 +1,45 @@
1
+ // The shipped pack's Cedar schema.
2
+ //
3
+ // Every attribute is optional, because a pattern is adopted by *adding* an attribute: a
4
+ // resource that declares no `owner` simply never reaches the ownership permit. Declaring
5
+ // them required would make the pack an all-or-nothing commitment instead of a menu.
6
+ //
7
+ // Times are `Long` — epoch seconds — because `sentrykit.cedar.coerce_value` renders every
8
+ // `datetime` that way and Cedar has no ordering on strings.
9
+
10
+ type PackContext = {
11
+ now?: Long,
12
+ break_glass?: Bool,
13
+ justification?: String,
14
+ on_behalf_of?: User,
15
+ };
16
+
17
+ entity Role in [Role];
18
+
19
+ entity Group in [Group];
20
+
21
+ entity User in [Role, Group] {
22
+ entitlement_starts?: Long,
23
+ entitlement_expires?: Long,
24
+ delegated_by?: User,
25
+ delegated_actions?: Set<Action>,
26
+ delegation_expires?: Long,
27
+ elevation_role?: Role,
28
+ elevation_until?: Long,
29
+ };
30
+
31
+ entity Resource in [Group] {
32
+ owner?: User,
33
+ group?: Group,
34
+ required_role?: Role,
35
+ emergency_only?: Bool,
36
+ emergency_role?: Role,
37
+ conflicting_role?: Role,
38
+ prepared_by?: User,
39
+ };
40
+
41
+ action "access", "approve" appliesTo {
42
+ principal: [User],
43
+ resource: [Resource],
44
+ context: PackContext
45
+ };
@@ -0,0 +1,5 @@
1
+ permit (
2
+ principal == ?principal,
3
+ action,
4
+ resource == ?resource
5
+ );
sentrykit/py.typed ADDED
File without changes