permiscope 2.2.0__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.
permiscope/__init__.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"""Tiny stdlib-only client for the Permiscope governance sidecar."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import urllib.error
|
|
7
|
+
import urllib.request
|
|
8
|
+
from typing import Any, Dict, Optional
|
|
9
|
+
|
|
10
|
+
__all__ = ["Permiscope", "PermiscopeError"]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class PermiscopeError(Exception):
|
|
14
|
+
"""Raised when the sidecar cannot be reached or rejects a request."""
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Permiscope:
|
|
18
|
+
def __init__(
|
|
19
|
+
self,
|
|
20
|
+
base_url: str = "http://127.0.0.1:8787",
|
|
21
|
+
token: Optional[str] = None,
|
|
22
|
+
timeout: float = 10.0,
|
|
23
|
+
) -> None:
|
|
24
|
+
if not token:
|
|
25
|
+
raise ValueError("A sidecar bearer token is required")
|
|
26
|
+
self.base_url = base_url.rstrip("/")
|
|
27
|
+
self.token = token
|
|
28
|
+
self.timeout = timeout
|
|
29
|
+
|
|
30
|
+
def decide(
|
|
31
|
+
self,
|
|
32
|
+
action_name: str,
|
|
33
|
+
parameters: Optional[Dict[str, Any]] = None,
|
|
34
|
+
reason: Optional[str] = None,
|
|
35
|
+
) -> str:
|
|
36
|
+
"""Ask the sidecar for a policy decision without executing anything."""
|
|
37
|
+
payload = self._payload(action_name, parameters, reason)
|
|
38
|
+
response = self._post("/v1/decide", payload)
|
|
39
|
+
return str(response["decision"])
|
|
40
|
+
|
|
41
|
+
def execute(
|
|
42
|
+
self,
|
|
43
|
+
action_name: str,
|
|
44
|
+
parameters: Optional[Dict[str, Any]] = None,
|
|
45
|
+
reason: Optional[str] = None,
|
|
46
|
+
) -> Dict[str, Any]:
|
|
47
|
+
"""Ask the sidecar to govern and execute a built-in action."""
|
|
48
|
+
return self._post("/v1/execute", self._payload(action_name, parameters, reason))
|
|
49
|
+
|
|
50
|
+
def allowed(
|
|
51
|
+
self,
|
|
52
|
+
action_name: str,
|
|
53
|
+
parameters: Optional[Dict[str, Any]] = None,
|
|
54
|
+
reason: Optional[str] = None,
|
|
55
|
+
) -> bool:
|
|
56
|
+
return self.decide(action_name, parameters, reason) == "ALLOW"
|
|
57
|
+
|
|
58
|
+
@staticmethod
|
|
59
|
+
def _payload(
|
|
60
|
+
action_name: str,
|
|
61
|
+
parameters: Optional[Dict[str, Any]],
|
|
62
|
+
reason: Optional[str],
|
|
63
|
+
) -> Dict[str, Any]:
|
|
64
|
+
payload: Dict[str, Any] = {
|
|
65
|
+
"actionName": action_name,
|
|
66
|
+
"parameters": parameters or {},
|
|
67
|
+
}
|
|
68
|
+
if reason:
|
|
69
|
+
payload["reason"] = reason
|
|
70
|
+
return payload
|
|
71
|
+
|
|
72
|
+
def _post(self, path: str, payload: Dict[str, Any]) -> Dict[str, Any]:
|
|
73
|
+
request = urllib.request.Request(
|
|
74
|
+
self.base_url + path,
|
|
75
|
+
data=json.dumps(payload).encode("utf-8"),
|
|
76
|
+
headers={
|
|
77
|
+
"Content-Type": "application/json",
|
|
78
|
+
"Authorization": f"Bearer {self.token}",
|
|
79
|
+
},
|
|
80
|
+
method="POST",
|
|
81
|
+
)
|
|
82
|
+
try:
|
|
83
|
+
with urllib.request.urlopen(request, timeout=self.timeout) as response:
|
|
84
|
+
return json.loads(response.read().decode("utf-8"))
|
|
85
|
+
except urllib.error.HTTPError as error:
|
|
86
|
+
body = error.read().decode("utf-8", errors="replace")
|
|
87
|
+
raise PermiscopeError(f"HTTP {error.code}: {body}") from error
|
|
88
|
+
except urllib.error.URLError as error:
|
|
89
|
+
raise PermiscopeError(f"Cannot reach Permiscope sidecar: {error.reason}") from error
|
|
90
|
+
except json.JSONDecodeError as error:
|
|
91
|
+
raise PermiscopeError("Sidecar returned invalid JSON") from error
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: permiscope
|
|
3
|
+
Version: 2.2.0
|
|
4
|
+
Summary: Dependency-free Python client for the Permiscope governance sidecar
|
|
5
|
+
Author: Shubham Sharma
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/shubhusion/Permiscope
|
|
8
|
+
Project-URL: Documentation, https://github.com/shubhusion/Permiscope/blob/main/python/README.md
|
|
9
|
+
Keywords: agents,governance,policy,audit,sidecar
|
|
10
|
+
Requires-Python: >=3.10
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# Permiscope Python SDK
|
|
14
|
+
|
|
15
|
+
A dependency-free client for the Permiscope governance sidecar. It uses only the
|
|
16
|
+
Python standard library.
|
|
17
|
+
|
|
18
|
+
> **Release status:** packaging is prepared but not yet published to PyPI. Once
|
|
19
|
+
> v2.2.0 is published, install with `pip install permiscope`. Until then, run from a
|
|
20
|
+
> checkout with `PYTHONPATH=python python python/example_agent.py`.
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install permiscope
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Start the sidecar
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
PERMISCOPE_SIDECAR_TOKEN=dev-token npx permiscope serve
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
It binds to `127.0.0.1:8787` by default. Override with `PERMISCOPE_SIDECAR_HOST`
|
|
35
|
+
and `PERMISCOPE_SIDECAR_PORT`. Non-loopback binding is refused unless the sidecar
|
|
36
|
+
is created programmatically with `allowRemote: true`.
|
|
37
|
+
|
|
38
|
+
## Use it
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from permiscope import Permiscope
|
|
42
|
+
|
|
43
|
+
scope = Permiscope(token="dev-token")
|
|
44
|
+
|
|
45
|
+
decision = scope.decide("run_command", {"command": "echo hello"})
|
|
46
|
+
if scope.allowed("read_file", {"path": "config.json"}):
|
|
47
|
+
...
|
|
48
|
+
|
|
49
|
+
result = scope.execute("run_command", {"command": "echo governed"})
|
|
50
|
+
print(result["decision"], result["success"])
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Run the complete example from a checkout with
|
|
54
|
+
`PYTHONPATH=python python python/example_agent.py`, or `python example_agent.py` from this
|
|
55
|
+
directory once the package is installed. The sidecar must be running.
|
|
56
|
+
|
|
57
|
+
## API
|
|
58
|
+
|
|
59
|
+
- `decide(action_name, parameters=None, reason=None) -> str` — policy decision only.
|
|
60
|
+
- `allowed(...) -> bool` — convenience wrapper for `decide(...) == "ALLOW"`.
|
|
61
|
+
- `execute(...) -> dict` — governed execution with `{ decision, success, output, error }`.
|
|
62
|
+
- `PermiscopeError` — raised on HTTP errors, unreachable sidecar, or invalid JSON.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
permiscope/__init__.py,sha256=ZJkfjl-mIAlmofqc5XgtAG-CLNKZddg9HYUN7oXbLBw,3085
|
|
2
|
+
permiscope-2.2.0.dist-info/METADATA,sha256=k1GL9YP98MKCAbPkzu5U0IE0Q_RZiLJjIFQ6Mpwn6d8,2079
|
|
3
|
+
permiscope-2.2.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
4
|
+
permiscope-2.2.0.dist-info/top_level.txt,sha256=TbiQN7tse3XU6fiFVvnTDQEw-R6E_Blou3I2NDLtAp4,11
|
|
5
|
+
permiscope-2.2.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
permiscope
|