policy-pattern 0.1.0__tar.gz
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.
- policy_pattern-0.1.0/LICENSE.md +201 -0
- policy_pattern-0.1.0/PKG-INFO +148 -0
- policy_pattern-0.1.0/README.md +123 -0
- policy_pattern-0.1.0/policy_pattern/__init__.py +0 -0
- policy_pattern-0.1.0/policy_pattern/adapters/__init__.py +3 -0
- policy_pattern-0.1.0/policy_pattern/adapters/memory.py +135 -0
- policy_pattern-0.1.0/policy_pattern/errors/__init__.py +19 -0
- policy_pattern-0.1.0/policy_pattern/errors/base_error.py +28 -0
- policy_pattern-0.1.0/policy_pattern/errors/evaluation_error.py +16 -0
- policy_pattern-0.1.0/policy_pattern/errors/identifier_error.py +15 -0
- policy_pattern-0.1.0/policy_pattern/errors/model_validation_error.py +15 -0
- policy_pattern-0.1.0/policy_pattern/errors/mutation_error.py +15 -0
- policy_pattern-0.1.0/policy_pattern/errors/relation_error.py +15 -0
- policy_pattern-0.1.0/policy_pattern/errors/unknown_name_error.py +16 -0
- policy_pattern-0.1.0/policy_pattern/errors/unknown_type_error.py +16 -0
- policy_pattern-0.1.0/policy_pattern/evaluator.py +285 -0
- policy_pattern-0.1.0/policy_pattern/models/__init__.py +34 -0
- policy_pattern-0.1.0/policy_pattern/models/_schema_validation.py +130 -0
- policy_pattern-0.1.0/policy_pattern/models/compiled_model.py +43 -0
- policy_pattern-0.1.0/policy_pattern/models/direct_subject.py +47 -0
- policy_pattern-0.1.0/policy_pattern/models/identifier.py +55 -0
- policy_pattern-0.1.0/policy_pattern/models/model.py +46 -0
- policy_pattern-0.1.0/policy_pattern/models/mutation.py +52 -0
- policy_pattern-0.1.0/policy_pattern/models/object_reference.py +42 -0
- policy_pattern-0.1.0/policy_pattern/models/operators.py +116 -0
- policy_pattern-0.1.0/policy_pattern/models/reference.py +36 -0
- policy_pattern-0.1.0/policy_pattern/models/relation.py +44 -0
- policy_pattern-0.1.0/policy_pattern/models/subject.py +22 -0
- policy_pattern-0.1.0/policy_pattern/models/subject_set.py +56 -0
- policy_pattern-0.1.0/policy_pattern/models/tuple.py +52 -0
- policy_pattern-0.1.0/policy_pattern/models/tuple_to_userset.py +51 -0
- policy_pattern-0.1.0/policy_pattern/models/type.py +71 -0
- policy_pattern-0.1.0/policy_pattern/ports/__init__.py +7 -0
- policy_pattern-0.1.0/policy_pattern/ports/tuple_store.py +118 -0
- policy_pattern-0.1.0/policy_pattern/py.typed +0 -0
- policy_pattern-0.1.0/pyproject.toml +226 -0
- policy_pattern-0.1.0/pyproject.toml.orig +198 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the
|
|
44
|
+
purposes of this License, Derivative Works shall not include works
|
|
45
|
+
that remain separable from, or merely link (or bind by name) to the
|
|
46
|
+
interfaces of, the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including the
|
|
49
|
+
original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing
|
|
141
|
+
the origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Guillem Pozo Sebastián
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: policy-pattern
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A minimal, embeddable ReBAC authorization engine for Python, inspired by Google Zanzibar.
|
|
5
|
+
Keywords: python,authorization,access-control,rebac,zanzibar,security
|
|
6
|
+
Author: Guillem Pozo Sebastián
|
|
7
|
+
Author-email: Guillem Pozo Sebastián <58363308+guillem-ps@users.noreply.github.com>
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE.md
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Typing :: Typed
|
|
13
|
+
Classifier: Topic :: Security
|
|
14
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
15
|
+
Classifier: Programming Language :: Python
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Requires-Python: >=3.12
|
|
21
|
+
Project-URL: Homepage, https://github.com/guillem-ps/policy-pattern
|
|
22
|
+
Project-URL: Repository, https://github.com/guillem-ps/policy-pattern
|
|
23
|
+
Project-URL: Issues, https://github.com/guillem-ps/policy-pattern/issues
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# policy-pattern
|
|
27
|
+
|
|
28
|
+
> A small, embeddable ReBAC authorization engine for Python, inspired by Google Zanzibar.
|
|
29
|
+
|
|
30
|
+
<p align="center">
|
|
31
|
+
<a href="https://github.com/guillem-ps/policy-pattern/actions/workflows/test.yaml" target="_blank">
|
|
32
|
+
<img src="https://github.com/guillem-ps/policy-pattern/actions/workflows/test.yaml/badge.svg" alt="Test Pipeline">
|
|
33
|
+
</a>
|
|
34
|
+
<a href="https://github.com/guillem-ps/policy-pattern/actions/workflows/lint.yaml" target="_blank">
|
|
35
|
+
<img src="https://github.com/guillem-ps/policy-pattern/actions/workflows/lint.yaml/badge.svg" alt="Lint Pipeline">
|
|
36
|
+
</a>
|
|
37
|
+
<a href="https://codecov.io/gh/guillem-ps/policy-pattern" target="_blank">
|
|
38
|
+
<img src="https://codecov.io/gh/guillem-ps/policy-pattern/graph/badge.svg" alt="Coverage Pipeline">
|
|
39
|
+
</a>
|
|
40
|
+
<a href="pyproject.toml" target="_blank">
|
|
41
|
+
<img src="https://img.shields.io/badge/version-0.1.0-blue" alt="Package Version">
|
|
42
|
+
</a>
|
|
43
|
+
<a href="pyproject.toml" target="_blank">
|
|
44
|
+
<img src="https://img.shields.io/badge/python-3.12%20%7C%203.13%20%7C%203.14-blue" alt="Supported Python Versions">
|
|
45
|
+
</a>
|
|
46
|
+
</p>
|
|
47
|
+
|
|
48
|
+
`policy-pattern` is a standalone Python library for relationship-based access control (ReBAC). It models
|
|
49
|
+
authorization as relationships between subjects and objects and evaluates those relationships through composable
|
|
50
|
+
authorization rules.
|
|
51
|
+
|
|
52
|
+
- **No authorization server**
|
|
53
|
+
- **No required framework**
|
|
54
|
+
- **No required database**
|
|
55
|
+
- **No network hop**
|
|
56
|
+
|
|
57
|
+
The library is designed to be embedded directly into Python applications that need richer authorization semantics
|
|
58
|
+
than traditional role-based access control.
|
|
59
|
+
|
|
60
|
+
> **Status:** v1 is code-complete — model, `compile()`, the storage port, `MemoryTupleStore`, and the `Evaluator`
|
|
61
|
+
> all exist and are tested.
|
|
62
|
+
|
|
63
|
+
See [Architecture § Scope](docs/architecture.md#scope) for what's planned (v1.x/v2) and unscheduled ideas.
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## Table of Contents
|
|
68
|
+
|
|
69
|
+
* [Example](#example)
|
|
70
|
+
* [Documentation](#documentation)
|
|
71
|
+
* [Contributing](#contributing)
|
|
72
|
+
* [License](#license)
|
|
73
|
+
* [References](#references)
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## Example
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
model = Model()
|
|
81
|
+
model.types["document"] = Type(
|
|
82
|
+
name="document",
|
|
83
|
+
relations={
|
|
84
|
+
"owner": Relation(allowed_subject_types=("user",)),
|
|
85
|
+
"parent": Relation(allowed_subject_types=("folder",)),
|
|
86
|
+
},
|
|
87
|
+
permissions={
|
|
88
|
+
"viewer": Union(left=Reference(name="owner"), right=TupleToUserset(tupleset="parent", computed="viewer")),
|
|
89
|
+
},
|
|
90
|
+
)
|
|
91
|
+
compiled = model.compile()
|
|
92
|
+
|
|
93
|
+
evaluator = Evaluator(model=compiled, store=store)
|
|
94
|
+
evaluator.check(alice, "viewer", document_42)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
`viewer` here means "the document's own owner, or the owner of whatever folder it lives in" — a document under
|
|
98
|
+
`folder:engineering` inherits access from that folder without copying a permission onto every document.
|
|
99
|
+
|
|
100
|
+
See the [Usage Guide](docs/usage/README.md) for the full, runnable walkthrough — every rewrite operator, nested
|
|
101
|
+
groups, `EvaluationBudget`, and error handling.
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## Documentation
|
|
106
|
+
|
|
107
|
+
The repository uses specifications and architecture documents as part of the implementation contract — they
|
|
108
|
+
describe **what must be true**, independent of any one adapter's implementation.
|
|
109
|
+
|
|
110
|
+
```text
|
|
111
|
+
docs/
|
|
112
|
+
├── README.md — documentation index, start here
|
|
113
|
+
├── usage/README.md — worked examples for every rewrite operator, budgets, error handling
|
|
114
|
+
├── architecture.md — the model shape, compile(), the storage port, the evaluator, and scope/roadmap
|
|
115
|
+
├── semantics.md — the storage contract every TupleStore adapter must satisfy
|
|
116
|
+
└── errors.md — the PPxxx error code ranges and what each one means
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Start with the [Usage Guide](docs/usage/README.md) to see the library working end to end, then
|
|
120
|
+
[Architecture](docs/architecture.md) to understand why the model is shaped the way it is.
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
## Contributing
|
|
125
|
+
|
|
126
|
+
We love community help! Before you open an issue or pull request, please read:
|
|
127
|
+
|
|
128
|
+
* [How to Contribute](.github/CONTRIBUTING.md)
|
|
129
|
+
* [Code of Conduct](.github/CODE_OF_CONDUCT.md)
|
|
130
|
+
* [Security Policy](SECURITY.md)
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
## License
|
|
135
|
+
|
|
136
|
+
Licensed under the [Apache License 2.0](LICENSE.md).
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
## References
|
|
141
|
+
|
|
142
|
+
The design is influenced by authorization systems and literature including:
|
|
143
|
+
|
|
144
|
+
* [Google Zanzibar](https://research.google/pubs/zanzibar-googles-consistent-global-authorization-system/)
|
|
145
|
+
* [OpenFGA](https://openfga.dev)
|
|
146
|
+
* [SpiceDB](https://github.com/authzed/spicedb) / [AuthZed](https://authzed.com)
|
|
147
|
+
|
|
148
|
+
`policy-pattern` is an independent project and is not an implementation of, or affiliated with, those projects.
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# policy-pattern
|
|
2
|
+
|
|
3
|
+
> A small, embeddable ReBAC authorization engine for Python, inspired by Google Zanzibar.
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a href="https://github.com/guillem-ps/policy-pattern/actions/workflows/test.yaml" target="_blank">
|
|
7
|
+
<img src="https://github.com/guillem-ps/policy-pattern/actions/workflows/test.yaml/badge.svg" alt="Test Pipeline">
|
|
8
|
+
</a>
|
|
9
|
+
<a href="https://github.com/guillem-ps/policy-pattern/actions/workflows/lint.yaml" target="_blank">
|
|
10
|
+
<img src="https://github.com/guillem-ps/policy-pattern/actions/workflows/lint.yaml/badge.svg" alt="Lint Pipeline">
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://codecov.io/gh/guillem-ps/policy-pattern" target="_blank">
|
|
13
|
+
<img src="https://codecov.io/gh/guillem-ps/policy-pattern/graph/badge.svg" alt="Coverage Pipeline">
|
|
14
|
+
</a>
|
|
15
|
+
<a href="pyproject.toml" target="_blank">
|
|
16
|
+
<img src="https://img.shields.io/badge/version-0.1.0-blue" alt="Package Version">
|
|
17
|
+
</a>
|
|
18
|
+
<a href="pyproject.toml" target="_blank">
|
|
19
|
+
<img src="https://img.shields.io/badge/python-3.12%20%7C%203.13%20%7C%203.14-blue" alt="Supported Python Versions">
|
|
20
|
+
</a>
|
|
21
|
+
</p>
|
|
22
|
+
|
|
23
|
+
`policy-pattern` is a standalone Python library for relationship-based access control (ReBAC). It models
|
|
24
|
+
authorization as relationships between subjects and objects and evaluates those relationships through composable
|
|
25
|
+
authorization rules.
|
|
26
|
+
|
|
27
|
+
- **No authorization server**
|
|
28
|
+
- **No required framework**
|
|
29
|
+
- **No required database**
|
|
30
|
+
- **No network hop**
|
|
31
|
+
|
|
32
|
+
The library is designed to be embedded directly into Python applications that need richer authorization semantics
|
|
33
|
+
than traditional role-based access control.
|
|
34
|
+
|
|
35
|
+
> **Status:** v1 is code-complete — model, `compile()`, the storage port, `MemoryTupleStore`, and the `Evaluator`
|
|
36
|
+
> all exist and are tested.
|
|
37
|
+
|
|
38
|
+
See [Architecture § Scope](docs/architecture.md#scope) for what's planned (v1.x/v2) and unscheduled ideas.
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Table of Contents
|
|
43
|
+
|
|
44
|
+
* [Example](#example)
|
|
45
|
+
* [Documentation](#documentation)
|
|
46
|
+
* [Contributing](#contributing)
|
|
47
|
+
* [License](#license)
|
|
48
|
+
* [References](#references)
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Example
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
model = Model()
|
|
56
|
+
model.types["document"] = Type(
|
|
57
|
+
name="document",
|
|
58
|
+
relations={
|
|
59
|
+
"owner": Relation(allowed_subject_types=("user",)),
|
|
60
|
+
"parent": Relation(allowed_subject_types=("folder",)),
|
|
61
|
+
},
|
|
62
|
+
permissions={
|
|
63
|
+
"viewer": Union(left=Reference(name="owner"), right=TupleToUserset(tupleset="parent", computed="viewer")),
|
|
64
|
+
},
|
|
65
|
+
)
|
|
66
|
+
compiled = model.compile()
|
|
67
|
+
|
|
68
|
+
evaluator = Evaluator(model=compiled, store=store)
|
|
69
|
+
evaluator.check(alice, "viewer", document_42)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
`viewer` here means "the document's own owner, or the owner of whatever folder it lives in" — a document under
|
|
73
|
+
`folder:engineering` inherits access from that folder without copying a permission onto every document.
|
|
74
|
+
|
|
75
|
+
See the [Usage Guide](docs/usage/README.md) for the full, runnable walkthrough — every rewrite operator, nested
|
|
76
|
+
groups, `EvaluationBudget`, and error handling.
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## Documentation
|
|
81
|
+
|
|
82
|
+
The repository uses specifications and architecture documents as part of the implementation contract — they
|
|
83
|
+
describe **what must be true**, independent of any one adapter's implementation.
|
|
84
|
+
|
|
85
|
+
```text
|
|
86
|
+
docs/
|
|
87
|
+
├── README.md — documentation index, start here
|
|
88
|
+
├── usage/README.md — worked examples for every rewrite operator, budgets, error handling
|
|
89
|
+
├── architecture.md — the model shape, compile(), the storage port, the evaluator, and scope/roadmap
|
|
90
|
+
├── semantics.md — the storage contract every TupleStore adapter must satisfy
|
|
91
|
+
└── errors.md — the PPxxx error code ranges and what each one means
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Start with the [Usage Guide](docs/usage/README.md) to see the library working end to end, then
|
|
95
|
+
[Architecture](docs/architecture.md) to understand why the model is shaped the way it is.
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## Contributing
|
|
100
|
+
|
|
101
|
+
We love community help! Before you open an issue or pull request, please read:
|
|
102
|
+
|
|
103
|
+
* [How to Contribute](.github/CONTRIBUTING.md)
|
|
104
|
+
* [Code of Conduct](.github/CODE_OF_CONDUCT.md)
|
|
105
|
+
* [Security Policy](SECURITY.md)
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## License
|
|
110
|
+
|
|
111
|
+
Licensed under the [Apache License 2.0](LICENSE.md).
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
## References
|
|
116
|
+
|
|
117
|
+
The design is influenced by authorization systems and literature including:
|
|
118
|
+
|
|
119
|
+
* [Google Zanzibar](https://research.google/pubs/zanzibar-googles-consistent-global-authorization-system/)
|
|
120
|
+
* [OpenFGA](https://openfga.dev)
|
|
121
|
+
* [SpiceDB](https://github.com/authzed/spicedb) / [AuthZed](https://authzed.com)
|
|
122
|
+
|
|
123
|
+
`policy-pattern` is an independent project and is not an implementation of, or affiliated with, those projects.
|
|
File without changes
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"""
|
|
2
|
+
In-memory TupleStore adapter — demo/test adapter, no throughput requirement.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from collections.abc import Generator
|
|
6
|
+
from contextlib import contextmanager
|
|
7
|
+
from heapq import nsmallest
|
|
8
|
+
from threading import Lock
|
|
9
|
+
from typing import override
|
|
10
|
+
|
|
11
|
+
from policy_pattern.models import Mutation, ObjectReference, Subject, Tuple
|
|
12
|
+
from policy_pattern.ports import ReadResult, TupleStore, View
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class MemoryTupleStore(TupleStore):
|
|
16
|
+
"""
|
|
17
|
+
In-memory `TupleStore` — a plain `set[Tuple]` behind a lock, no index.
|
|
18
|
+
|
|
19
|
+
`read_relation()` does a linear scan over a snapshot; no
|
|
20
|
+
`dict[(object, relation), set[Tuple]]` index is maintained. An index
|
|
21
|
+
would need to stay in lockstep with `write_tuples()` on every call —
|
|
22
|
+
real complexity bought for a performance property this adapter is
|
|
23
|
+
explicitly not required to have (`docs/semantics.md` § Threading calls
|
|
24
|
+
it a "demo/test adapter with no throughput requirement"). Real
|
|
25
|
+
throughput is `PostgresTupleStore`'s job, not this one's.
|
|
26
|
+
|
|
27
|
+
Example:
|
|
28
|
+
store = MemoryTupleStore()
|
|
29
|
+
store.write_tuples(Mutation(additions=frozenset({t}), deletions=frozenset()))
|
|
30
|
+
with store.snapshot() as view:
|
|
31
|
+
view.read_tuple(subject, "owner", object)
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def __init__(self) -> None:
|
|
35
|
+
"""
|
|
36
|
+
Starts empty.
|
|
37
|
+
"""
|
|
38
|
+
self._tuples: set[Tuple] = set()
|
|
39
|
+
self._lock = Lock()
|
|
40
|
+
|
|
41
|
+
@override
|
|
42
|
+
def write_tuples(self, mutation: Mutation) -> None:
|
|
43
|
+
"""
|
|
44
|
+
Atomically applies additions/deletions under the lock.
|
|
45
|
+
|
|
46
|
+
`Mutation.__post_init__` already guarantees `additions`/`deletions`
|
|
47
|
+
are disjoint — this method does not re-validate that.
|
|
48
|
+
"""
|
|
49
|
+
with self._lock:
|
|
50
|
+
self._tuples = (self._tuples - mutation.deletions) | mutation.additions
|
|
51
|
+
|
|
52
|
+
@override
|
|
53
|
+
@contextmanager
|
|
54
|
+
def snapshot(self) -> Generator[View, None, None]:
|
|
55
|
+
"""
|
|
56
|
+
Copies the current tuple set under the lock, then yields a view over the copy.
|
|
57
|
+
|
|
58
|
+
The copy — not a re-held lock — is what makes the view stable: once
|
|
59
|
+
the copy exists, nothing that happens to the live set afterward can
|
|
60
|
+
be observed through the already-open view, because the view never
|
|
61
|
+
looks at the live set again.
|
|
62
|
+
"""
|
|
63
|
+
with self._lock:
|
|
64
|
+
frozen = frozenset(self._tuples)
|
|
65
|
+
yield _MemoryView(frozen)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class _MemoryView(View):
|
|
69
|
+
"""
|
|
70
|
+
Read-only view over one immutable snapshot of a `MemoryTupleStore`.
|
|
71
|
+
|
|
72
|
+
Private: nothing outside `MemoryTupleStore.snapshot()` ever constructs
|
|
73
|
+
one directly.
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
def __init__(self, tuples: frozenset[Tuple]) -> None:
|
|
77
|
+
"""
|
|
78
|
+
Wraps an already-copied, immutable tuple set.
|
|
79
|
+
"""
|
|
80
|
+
self._tuples = tuples
|
|
81
|
+
|
|
82
|
+
@property
|
|
83
|
+
def tuples(self) -> frozenset[Tuple]:
|
|
84
|
+
"""
|
|
85
|
+
Returns the snapshot's tuples.
|
|
86
|
+
|
|
87
|
+
Private: nothing outside `MemoryTupleStore.snapshot()` ever needs
|
|
88
|
+
to see this.
|
|
89
|
+
"""
|
|
90
|
+
return self._tuples
|
|
91
|
+
|
|
92
|
+
@override
|
|
93
|
+
def read_tuple(self, subject: Subject, relation: str, object: ObjectReference) -> bool:
|
|
94
|
+
"""
|
|
95
|
+
Checks exact membership by constructing the equivalent Tuple.
|
|
96
|
+
|
|
97
|
+
Reuses `Tuple.__post_init__`'s own validation (delegating `relation`
|
|
98
|
+
to `Identifier`) instead of duplicating it here.
|
|
99
|
+
"""
|
|
100
|
+
return Tuple(subject=subject, relation=relation, object=object) in self.tuples
|
|
101
|
+
|
|
102
|
+
@override
|
|
103
|
+
def read_relation(self, object: ObjectReference, relation: str, limit: int) -> ReadResult:
|
|
104
|
+
"""
|
|
105
|
+
Linearly scans the snapshot for matches, bounded by `limit + 1`.
|
|
106
|
+
|
|
107
|
+
Args:
|
|
108
|
+
object: The `ObjectReference` to match.
|
|
109
|
+
relation: The relation name to match.
|
|
110
|
+
limit: The maximum number of matches to return. If more than
|
|
111
|
+
`limit` matches exist, the result is truncated to exactly
|
|
112
|
+
`limit` and `complete=False`.
|
|
113
|
+
|
|
114
|
+
Matches are ranked by `repr()` before truncating — `frozenset`
|
|
115
|
+
iteration order is not stable (hash randomization), and an
|
|
116
|
+
unordered scan would make which tuples survive truncation vary
|
|
117
|
+
between runs with identical data, breaking
|
|
118
|
+
docs/architecture.md's determinism guarantee. `heapq.nsmallest`
|
|
119
|
+
keeps only `limit + 1` candidates in memory at a time, rather than
|
|
120
|
+
`sorted()`'s full match set — with a huge match set and a small
|
|
121
|
+
limit, that is the difference between O(limit) and O(match count)
|
|
122
|
+
memory. `limit=0` needs no special-casing: it is already
|
|
123
|
+
well-defined (immediately incomplete if any match exists,
|
|
124
|
+
complete-and-empty otherwise).
|
|
125
|
+
"""
|
|
126
|
+
candidates: list[Tuple] = nsmallest(
|
|
127
|
+
limit + 1,
|
|
128
|
+
(t for t in self.tuples if t.object == object and t.relation == relation),
|
|
129
|
+
key=repr,
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
if len(candidates) > limit:
|
|
133
|
+
return ReadResult(tuples=frozenset(candidates[:limit]), complete=False)
|
|
134
|
+
|
|
135
|
+
return ReadResult(tuples=frozenset(candidates), complete=True)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from .base_error import BaseError
|
|
2
|
+
from .evaluation_error import EvaluationError
|
|
3
|
+
from .identifier_error import InvalidIdentifierError
|
|
4
|
+
from .model_validation_error import ModelValidationError
|
|
5
|
+
from .mutation_error import InvalidMutationError
|
|
6
|
+
from .relation_error import InvalidRelationError
|
|
7
|
+
from .unknown_name_error import UnknownNameError
|
|
8
|
+
from .unknown_type_error import UnknownTypeError
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"BaseError",
|
|
12
|
+
"EvaluationError",
|
|
13
|
+
"InvalidIdentifierError",
|
|
14
|
+
"InvalidMutationError",
|
|
15
|
+
"InvalidRelationError",
|
|
16
|
+
"ModelValidationError",
|
|
17
|
+
"UnknownNameError",
|
|
18
|
+
"UnknownTypeError",
|
|
19
|
+
]
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Base class for all policy pattern errors.
|
|
3
|
+
To create a standardized error response, inherit from this class and set the `code` attribute to a unique error code.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from typing import ClassVar
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class BaseError(Exception):
|
|
10
|
+
"""
|
|
11
|
+
Custom exception class for policy pattern errors.
|
|
12
|
+
It provides a standardized error response format with a unique error code, message,
|
|
13
|
+
and optional related information.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
code: ClassVar[str]
|
|
17
|
+
|
|
18
|
+
def __init__(self, *, message: str, related: str | None = None) -> None:
|
|
19
|
+
"""
|
|
20
|
+
Initialize the BaseError following the standard error response format in the policy pattern docs errors.md.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
message (str): A description of the error.
|
|
24
|
+
related (str | None): Additional information about the error, such as the field or type that caused it.
|
|
25
|
+
"""
|
|
26
|
+
self.message = message
|
|
27
|
+
self.related = related
|
|
28
|
+
super().__init__(f"[{self.code}] {message}")
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Set of errors related to Evaluator runtime failures (budget exceeded, cycle detected).
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from .base_error import BaseError
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class EvaluationError(BaseError):
|
|
9
|
+
"""
|
|
10
|
+
Error raised when check() cannot complete: a cycle was detected, or a configured
|
|
11
|
+
EvaluationBudget limit (max_depth, max_nodes, max_store_reads) was exceeded.
|
|
12
|
+
Attributes:
|
|
13
|
+
code (ClassVar[str]): A unique error code for the specific error type.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
code = "PP201"
|