dbl-policy 0.1.1__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.
- dbl_policy-0.1.1/LICENSE +21 -0
- dbl_policy-0.1.1/PKG-INFO +80 -0
- dbl_policy-0.1.1/README.md +51 -0
- dbl_policy-0.1.1/pyproject.toml +59 -0
- dbl_policy-0.1.1/setup.cfg +4 -0
- dbl_policy-0.1.1/src/dbl_policy/__init__.py +28 -0
- dbl_policy-0.1.1/src/dbl_policy/allow_all.py +42 -0
- dbl_policy-0.1.1/src/dbl_policy/deny_all.py +42 -0
- dbl_policy-0.1.1/src/dbl_policy/model.py +96 -0
- dbl_policy-0.1.1/src/dbl_policy.egg-info/PKG-INFO +80 -0
- dbl_policy-0.1.1/src/dbl_policy.egg-info/SOURCES.txt +13 -0
- dbl_policy-0.1.1/src/dbl_policy.egg-info/dependency_links.txt +1 -0
- dbl_policy-0.1.1/src/dbl_policy.egg-info/requires.txt +9 -0
- dbl_policy-0.1.1/src/dbl_policy.egg-info/top_level.txt +1 -0
- dbl_policy-0.1.1/tests/test_policy.py +89 -0
dbl_policy-0.1.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Lukas Pfister
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dbl-policy
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Deterministic policy evaluation layer for dbl-core
|
|
5
|
+
Author-email: Lukas Pfister <228201683+lukaspfisterch@users.noreply.github.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Repository, https://github.com/lukaspfisterch/dbl-policy
|
|
8
|
+
Project-URL: Issues, https://github.com/lukaspfisterch/dbl-policy/issues
|
|
9
|
+
Keywords: dbl,policy,governance,deterministic
|
|
10
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Python: >=3.11
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Requires-Dist: dbl-core<0.4,>=0.3
|
|
22
|
+
Provides-Extra: test
|
|
23
|
+
Requires-Dist: pytest<9,>=8; extra == "test"
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: pytest<9,>=8; extra == "dev"
|
|
26
|
+
Requires-Dist: ruff<1,>=0.5; extra == "dev"
|
|
27
|
+
Requires-Dist: mypy<2,>=1.10; extra == "dev"
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
|
|
30
|
+
# DBL Policy
|
|
31
|
+
|
|
32
|
+
DBL Policy provides deterministic, tenant-scoped policy evaluation that produces DECISION events only. It does not execute tasks.
|
|
33
|
+
|
|
34
|
+
## Scope
|
|
35
|
+
- Policy decisions derived only from authoritative inputs.
|
|
36
|
+
- No execution, no orchestration, no IO side effects.
|
|
37
|
+
|
|
38
|
+
## Contract
|
|
39
|
+
- docs/dbl_policy_contract.md
|
|
40
|
+
|
|
41
|
+
## Install
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install dbl-policy
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Requires `dbl-core>=0.3.0`, Python 3.11+.
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from dbl_policy import (
|
|
53
|
+
PolicyContext,
|
|
54
|
+
PolicyDecision,
|
|
55
|
+
PolicyId,
|
|
56
|
+
PolicyVersion,
|
|
57
|
+
TenantId,
|
|
58
|
+
DecisionOutcome,
|
|
59
|
+
decision_to_dbl_event,
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
context = PolicyContext(
|
|
63
|
+
tenant_id=TenantId("tenant-1"),
|
|
64
|
+
inputs={"use_case": "llm-generate"},
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
decision = PolicyDecision(
|
|
68
|
+
outcome=DecisionOutcome.ALLOW,
|
|
69
|
+
reason_code="ok",
|
|
70
|
+
policy_id=PolicyId("example"),
|
|
71
|
+
policy_version=PolicyVersion("1.0.0"),
|
|
72
|
+
tenant_id=context.tenant_id,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
event = decision_to_dbl_event(decision, correlation_id="c1")
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
|
|
80
|
+
MIT License. See LICENSE.
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# DBL Policy
|
|
2
|
+
|
|
3
|
+
DBL Policy provides deterministic, tenant-scoped policy evaluation that produces DECISION events only. It does not execute tasks.
|
|
4
|
+
|
|
5
|
+
## Scope
|
|
6
|
+
- Policy decisions derived only from authoritative inputs.
|
|
7
|
+
- No execution, no orchestration, no IO side effects.
|
|
8
|
+
|
|
9
|
+
## Contract
|
|
10
|
+
- docs/dbl_policy_contract.md
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install dbl-policy
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Requires `dbl-core>=0.3.0`, Python 3.11+.
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from dbl_policy import (
|
|
24
|
+
PolicyContext,
|
|
25
|
+
PolicyDecision,
|
|
26
|
+
PolicyId,
|
|
27
|
+
PolicyVersion,
|
|
28
|
+
TenantId,
|
|
29
|
+
DecisionOutcome,
|
|
30
|
+
decision_to_dbl_event,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
context = PolicyContext(
|
|
34
|
+
tenant_id=TenantId("tenant-1"),
|
|
35
|
+
inputs={"use_case": "llm-generate"},
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
decision = PolicyDecision(
|
|
39
|
+
outcome=DecisionOutcome.ALLOW,
|
|
40
|
+
reason_code="ok",
|
|
41
|
+
policy_id=PolicyId("example"),
|
|
42
|
+
policy_version=PolicyVersion("1.0.0"),
|
|
43
|
+
tenant_id=context.tenant_id,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
event = decision_to_dbl_event(decision, correlation_id="c1")
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## License
|
|
50
|
+
|
|
51
|
+
MIT License. See LICENSE.
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "dbl-policy"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
description = "Deterministic policy evaluation layer for dbl-core"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.11"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Lukas Pfister", email = "228201683+lukaspfisterch@users.noreply.github.com" }
|
|
14
|
+
]
|
|
15
|
+
keywords = ["dbl", "policy", "governance", "deterministic"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 2 - Pre-Alpha",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"License :: OSI Approved :: MIT License",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
22
|
+
"Programming Language :: Python :: 3.11",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Typing :: Typed",
|
|
25
|
+
]
|
|
26
|
+
dependencies = [
|
|
27
|
+
"dbl-core>=0.3,<0.4",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.optional-dependencies]
|
|
31
|
+
test = [
|
|
32
|
+
"pytest>=8,<9",
|
|
33
|
+
]
|
|
34
|
+
dev = [
|
|
35
|
+
"pytest>=8,<9",
|
|
36
|
+
"ruff>=0.5,<1",
|
|
37
|
+
"mypy>=1.10,<2",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
[project.urls]
|
|
41
|
+
Repository = "https://github.com/lukaspfisterch/dbl-policy"
|
|
42
|
+
Issues = "https://github.com/lukaspfisterch/dbl-policy/issues"
|
|
43
|
+
|
|
44
|
+
[tool.setuptools]
|
|
45
|
+
package-dir = { "" = "src" }
|
|
46
|
+
license-files = ["LICENSE"]
|
|
47
|
+
|
|
48
|
+
[tool.setuptools.packages.find]
|
|
49
|
+
where = ["src"]
|
|
50
|
+
include = ["dbl_policy*"]
|
|
51
|
+
exclude = ["tests*", "docs*", "scripts*"]
|
|
52
|
+
|
|
53
|
+
[tool.setuptools.package-data]
|
|
54
|
+
dbl_policy = ["py.typed"]
|
|
55
|
+
|
|
56
|
+
[tool.pytest.ini_options]
|
|
57
|
+
testpaths = ["tests"]
|
|
58
|
+
python_files = ["test_*.py"]
|
|
59
|
+
addopts = ["-q", "--strict-markers", "--strict-config"]
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
from .model import (
|
|
2
|
+
DecisionOutcome,
|
|
3
|
+
Policy,
|
|
4
|
+
PolicyContext,
|
|
5
|
+
PolicyDecision,
|
|
6
|
+
PolicyId,
|
|
7
|
+
PolicyVersion,
|
|
8
|
+
TenantId,
|
|
9
|
+
decision_to_dbl_event,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
from .allow_all import AllowAllPolicy
|
|
13
|
+
from .deny_all import DenyAllPolicy
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"DecisionOutcome",
|
|
17
|
+
"Policy",
|
|
18
|
+
"PolicyContext",
|
|
19
|
+
"PolicyDecision",
|
|
20
|
+
"PolicyId",
|
|
21
|
+
"PolicyVersion",
|
|
22
|
+
"TenantId",
|
|
23
|
+
"decision_to_dbl_event",
|
|
24
|
+
"AllowAllPolicy",
|
|
25
|
+
"DenyAllPolicy",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
__version__ = "0.1.1"
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import Any, Mapping
|
|
5
|
+
|
|
6
|
+
from .model import (
|
|
7
|
+
DecisionOutcome,
|
|
8
|
+
PolicyContext,
|
|
9
|
+
PolicyDecision,
|
|
10
|
+
PolicyId,
|
|
11
|
+
PolicyVersion,
|
|
12
|
+
TenantId,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True)
|
|
17
|
+
class AllowAllPolicy:
|
|
18
|
+
policy_id: PolicyId = PolicyId("dbl_policy.allow_all")
|
|
19
|
+
policy_version: PolicyVersion = PolicyVersion("1.0.0")
|
|
20
|
+
|
|
21
|
+
def evaluate(self, context: PolicyContext) -> PolicyDecision:
|
|
22
|
+
return PolicyDecision(
|
|
23
|
+
outcome=DecisionOutcome.ALLOW,
|
|
24
|
+
reason_code="allow_all",
|
|
25
|
+
reason_message="development allow-all policy",
|
|
26
|
+
policy_id=self.policy_id,
|
|
27
|
+
policy_version=self.policy_version,
|
|
28
|
+
tenant_id=context.tenant_id,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
POLICY = AllowAllPolicy()
|
|
33
|
+
policy = POLICY
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def evaluate(context: PolicyContext) -> PolicyDecision:
|
|
37
|
+
return POLICY.evaluate(context)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def decide(*, tenant_id: str, inputs: Mapping[str, Any]) -> PolicyDecision:
|
|
41
|
+
ctx = PolicyContext(tenant_id=TenantId(tenant_id), inputs=inputs)
|
|
42
|
+
return POLICY.evaluate(ctx)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import Any, Mapping
|
|
5
|
+
|
|
6
|
+
from .model import (
|
|
7
|
+
DecisionOutcome,
|
|
8
|
+
PolicyContext,
|
|
9
|
+
PolicyDecision,
|
|
10
|
+
PolicyId,
|
|
11
|
+
PolicyVersion,
|
|
12
|
+
TenantId,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True)
|
|
17
|
+
class DenyAllPolicy:
|
|
18
|
+
policy_id: PolicyId = PolicyId("dbl_policy.deny_all")
|
|
19
|
+
policy_version: PolicyVersion = PolicyVersion("1.0.0")
|
|
20
|
+
|
|
21
|
+
def evaluate(self, context: PolicyContext) -> PolicyDecision:
|
|
22
|
+
return PolicyDecision(
|
|
23
|
+
outcome=DecisionOutcome.DENY,
|
|
24
|
+
reason_code="deny_all",
|
|
25
|
+
reason_message="default deny-all policy",
|
|
26
|
+
policy_id=self.policy_id,
|
|
27
|
+
policy_version=self.policy_version,
|
|
28
|
+
tenant_id=context.tenant_id,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
POLICY = DenyAllPolicy()
|
|
33
|
+
policy = POLICY
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def evaluate(context: PolicyContext) -> PolicyDecision:
|
|
37
|
+
return POLICY.evaluate(context)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def decide(*, tenant_id: str, inputs: Mapping[str, Any]) -> PolicyDecision:
|
|
41
|
+
ctx = PolicyContext(tenant_id=TenantId(tenant_id), inputs=inputs)
|
|
42
|
+
return POLICY.evaluate(ctx)
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from enum import Enum
|
|
5
|
+
import json
|
|
6
|
+
from typing import Any, Mapping, Protocol
|
|
7
|
+
|
|
8
|
+
from dbl_core import DblEvent, DblEventKind, GateDecision
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(frozen=True)
|
|
12
|
+
class PolicyId:
|
|
13
|
+
value: str
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True)
|
|
17
|
+
class PolicyVersion:
|
|
18
|
+
value: str
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass(frozen=True)
|
|
22
|
+
class TenantId:
|
|
23
|
+
value: str
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class DecisionOutcome(str, Enum):
|
|
27
|
+
ALLOW = "ALLOW"
|
|
28
|
+
DENY = "DENY"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
_FORBIDDEN_INPUT_KEYS = {
|
|
32
|
+
"trace",
|
|
33
|
+
"trace_digest",
|
|
34
|
+
"execution",
|
|
35
|
+
"execution_trace",
|
|
36
|
+
"success",
|
|
37
|
+
"failure_code",
|
|
38
|
+
"exception_type",
|
|
39
|
+
"exception",
|
|
40
|
+
"runtime",
|
|
41
|
+
"runtime_ms",
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _canonicalize_mapping(value: Mapping[str, Any]) -> dict[str, Any]:
|
|
46
|
+
items: dict[str, Any] = {}
|
|
47
|
+
for key in sorted(value.keys(), key=lambda k: str(k)):
|
|
48
|
+
items[str(key)] = value[key]
|
|
49
|
+
return items
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass(frozen=True)
|
|
53
|
+
class PolicyContext:
|
|
54
|
+
tenant_id: TenantId
|
|
55
|
+
inputs: Mapping[str, Any]
|
|
56
|
+
|
|
57
|
+
def __post_init__(self) -> None:
|
|
58
|
+
if not isinstance(self.inputs, Mapping):
|
|
59
|
+
raise TypeError("inputs must be a mapping")
|
|
60
|
+
for key in self.inputs.keys():
|
|
61
|
+
key_str = str(key).lower()
|
|
62
|
+
if key_str in _FORBIDDEN_INPUT_KEYS or key_str.startswith("trace") or key_str.startswith("execution"):
|
|
63
|
+
raise ValueError(f"observational key not allowed: {key}")
|
|
64
|
+
json.dumps(self.to_dict(), ensure_ascii=True, sort_keys=True)
|
|
65
|
+
|
|
66
|
+
def to_dict(self) -> dict[str, Any]:
|
|
67
|
+
return {
|
|
68
|
+
"tenant_id": self.tenant_id.value,
|
|
69
|
+
"inputs": _canonicalize_mapping(self.inputs),
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@dataclass(frozen=True)
|
|
74
|
+
class PolicyDecision:
|
|
75
|
+
outcome: DecisionOutcome
|
|
76
|
+
reason_code: str
|
|
77
|
+
policy_id: PolicyId
|
|
78
|
+
policy_version: PolicyVersion
|
|
79
|
+
tenant_id: TenantId
|
|
80
|
+
reason_message: str | None = None
|
|
81
|
+
|
|
82
|
+
def __post_init__(self) -> None:
|
|
83
|
+
if self.outcome not in (DecisionOutcome.ALLOW, DecisionOutcome.DENY):
|
|
84
|
+
raise ValueError("invalid decision outcome")
|
|
85
|
+
if not self.reason_code:
|
|
86
|
+
raise ValueError("reason_code is required")
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class Policy(Protocol):
|
|
90
|
+
def evaluate(self, context: PolicyContext) -> PolicyDecision:
|
|
91
|
+
...
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def decision_to_dbl_event(decision: PolicyDecision, correlation_id: str) -> DblEvent:
|
|
95
|
+
gate = GateDecision(decision.outcome.value, decision.reason_code, decision.reason_message)
|
|
96
|
+
return DblEvent(DblEventKind.DECISION, correlation_id=correlation_id, data=gate)
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dbl-policy
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Deterministic policy evaluation layer for dbl-core
|
|
5
|
+
Author-email: Lukas Pfister <228201683+lukaspfisterch@users.noreply.github.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Repository, https://github.com/lukaspfisterch/dbl-policy
|
|
8
|
+
Project-URL: Issues, https://github.com/lukaspfisterch/dbl-policy/issues
|
|
9
|
+
Keywords: dbl,policy,governance,deterministic
|
|
10
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Python: >=3.11
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Requires-Dist: dbl-core<0.4,>=0.3
|
|
22
|
+
Provides-Extra: test
|
|
23
|
+
Requires-Dist: pytest<9,>=8; extra == "test"
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: pytest<9,>=8; extra == "dev"
|
|
26
|
+
Requires-Dist: ruff<1,>=0.5; extra == "dev"
|
|
27
|
+
Requires-Dist: mypy<2,>=1.10; extra == "dev"
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
|
|
30
|
+
# DBL Policy
|
|
31
|
+
|
|
32
|
+
DBL Policy provides deterministic, tenant-scoped policy evaluation that produces DECISION events only. It does not execute tasks.
|
|
33
|
+
|
|
34
|
+
## Scope
|
|
35
|
+
- Policy decisions derived only from authoritative inputs.
|
|
36
|
+
- No execution, no orchestration, no IO side effects.
|
|
37
|
+
|
|
38
|
+
## Contract
|
|
39
|
+
- docs/dbl_policy_contract.md
|
|
40
|
+
|
|
41
|
+
## Install
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install dbl-policy
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Requires `dbl-core>=0.3.0`, Python 3.11+.
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from dbl_policy import (
|
|
53
|
+
PolicyContext,
|
|
54
|
+
PolicyDecision,
|
|
55
|
+
PolicyId,
|
|
56
|
+
PolicyVersion,
|
|
57
|
+
TenantId,
|
|
58
|
+
DecisionOutcome,
|
|
59
|
+
decision_to_dbl_event,
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
context = PolicyContext(
|
|
63
|
+
tenant_id=TenantId("tenant-1"),
|
|
64
|
+
inputs={"use_case": "llm-generate"},
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
decision = PolicyDecision(
|
|
68
|
+
outcome=DecisionOutcome.ALLOW,
|
|
69
|
+
reason_code="ok",
|
|
70
|
+
policy_id=PolicyId("example"),
|
|
71
|
+
policy_version=PolicyVersion("1.0.0"),
|
|
72
|
+
tenant_id=context.tenant_id,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
event = decision_to_dbl_event(decision, correlation_id="c1")
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
|
|
80
|
+
MIT License. See LICENSE.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/dbl_policy/__init__.py
|
|
5
|
+
src/dbl_policy/allow_all.py
|
|
6
|
+
src/dbl_policy/deny_all.py
|
|
7
|
+
src/dbl_policy/model.py
|
|
8
|
+
src/dbl_policy.egg-info/PKG-INFO
|
|
9
|
+
src/dbl_policy.egg-info/SOURCES.txt
|
|
10
|
+
src/dbl_policy.egg-info/dependency_links.txt
|
|
11
|
+
src/dbl_policy.egg-info/requires.txt
|
|
12
|
+
src/dbl_policy.egg-info/top_level.txt
|
|
13
|
+
tests/test_policy.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
dbl_policy
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from dbl_core import DblEventKind, GateDecision
|
|
6
|
+
from dbl_policy import (
|
|
7
|
+
DecisionOutcome,
|
|
8
|
+
PolicyContext,
|
|
9
|
+
PolicyDecision,
|
|
10
|
+
PolicyId,
|
|
11
|
+
PolicyVersion,
|
|
12
|
+
TenantId,
|
|
13
|
+
decision_to_dbl_event,
|
|
14
|
+
)
|
|
15
|
+
from dbl_policy.allow_all import POLICY as ALLOW_POLICY
|
|
16
|
+
from dbl_policy.deny_all import POLICY as DENY_POLICY
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class ExamplePolicy:
|
|
20
|
+
def __init__(self, policy_id: PolicyId, policy_version: PolicyVersion) -> None:
|
|
21
|
+
self._policy_id = policy_id
|
|
22
|
+
self._policy_version = policy_version
|
|
23
|
+
|
|
24
|
+
def evaluate(self, context: PolicyContext) -> PolicyDecision:
|
|
25
|
+
if context.tenant_id.value == "tenant-deny":
|
|
26
|
+
return PolicyDecision(
|
|
27
|
+
outcome=DecisionOutcome.DENY,
|
|
28
|
+
reason_code="tenant_blocked",
|
|
29
|
+
policy_id=self._policy_id,
|
|
30
|
+
policy_version=self._policy_version,
|
|
31
|
+
tenant_id=context.tenant_id,
|
|
32
|
+
)
|
|
33
|
+
return PolicyDecision(
|
|
34
|
+
outcome=DecisionOutcome.ALLOW,
|
|
35
|
+
reason_code="ok",
|
|
36
|
+
policy_id=self._policy_id,
|
|
37
|
+
policy_version=self._policy_version,
|
|
38
|
+
tenant_id=context.tenant_id,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_determinism_same_context_same_decision():
|
|
43
|
+
policy = ExamplePolicy(PolicyId("example"), PolicyVersion("1.0.0"))
|
|
44
|
+
context = PolicyContext(tenant_id=TenantId("tenant-1"), inputs={"use_case": "x"})
|
|
45
|
+
d1 = policy.evaluate(context)
|
|
46
|
+
d2 = policy.evaluate(context)
|
|
47
|
+
assert d1 == d2
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def test_tenant_scoping_changes_decision():
|
|
51
|
+
policy = ExamplePolicy(PolicyId("example"), PolicyVersion("1.0.0"))
|
|
52
|
+
allow_ctx = PolicyContext(tenant_id=TenantId("tenant-1"), inputs={"use_case": "x"})
|
|
53
|
+
deny_ctx = PolicyContext(tenant_id=TenantId("tenant-deny"), inputs={"use_case": "x"})
|
|
54
|
+
assert policy.evaluate(allow_ctx).outcome == DecisionOutcome.ALLOW
|
|
55
|
+
assert policy.evaluate(deny_ctx).outcome == DecisionOutcome.DENY
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def test_no_observables_in_context():
|
|
59
|
+
with pytest.raises(ValueError, match="observational key not allowed"):
|
|
60
|
+
PolicyContext(tenant_id=TenantId("tenant-1"), inputs={"trace": {"x": 1}})
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def test_decision_to_dbl_event():
|
|
64
|
+
decision = PolicyDecision(
|
|
65
|
+
outcome=DecisionOutcome.ALLOW,
|
|
66
|
+
reason_code="ok",
|
|
67
|
+
policy_id=PolicyId("example"),
|
|
68
|
+
policy_version=PolicyVersion("1.0.0"),
|
|
69
|
+
tenant_id=TenantId("tenant-1"),
|
|
70
|
+
)
|
|
71
|
+
event = decision_to_dbl_event(decision, correlation_id="c1")
|
|
72
|
+
assert event.event_kind == DblEventKind.DECISION
|
|
73
|
+
assert isinstance(event.data, GateDecision)
|
|
74
|
+
assert event.data.decision == "ALLOW"
|
|
75
|
+
assert event.data.reason_code == "ok"
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_allow_all_policy() -> None:
|
|
79
|
+
ctx = PolicyContext(tenant_id=TenantId("tenant-1"), inputs={"use_case": "x"})
|
|
80
|
+
d = ALLOW_POLICY.evaluate(ctx)
|
|
81
|
+
assert d.outcome == DecisionOutcome.ALLOW
|
|
82
|
+
assert d.reason_code == "allow_all"
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def test_deny_all_policy() -> None:
|
|
86
|
+
ctx = PolicyContext(tenant_id=TenantId("tenant-1"), inputs={"use_case": "x"})
|
|
87
|
+
d = DENY_POLICY.evaluate(ctx)
|
|
88
|
+
assert d.outcome == DecisionOutcome.DENY
|
|
89
|
+
assert d.reason_code == "deny_all"
|