egzoscore 0.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.
Files changed (62) hide show
  1. egzos/__init__.py +11 -0
  2. egzos/_ids.py +29 -0
  3. egzos/_signals.py +39 -0
  4. egzos/_term.py +33 -0
  5. egzos/_types.py +1023 -0
  6. egzos/api/__init__.py +150 -0
  7. egzos/api/app.py +1574 -0
  8. egzos/api/blobs.py +92 -0
  9. egzos/api/throttle.py +190 -0
  10. egzos/auth.py +176 -0
  11. egzos/authz/__init__.py +5 -0
  12. egzos/authz/presence.py +778 -0
  13. egzos/authz/values.py +32 -0
  14. egzos/backends/__init__.py +8 -0
  15. egzos/backends/base.py +91 -0
  16. egzos/backends/postgres.py +490 -0
  17. egzos/backends/sqlite.py +524 -0
  18. egzos/binding.py +309 -0
  19. egzos/cli/__init__.py +1092 -0
  20. egzos/cli/__main__.py +11 -0
  21. egzos/confirm.py +455 -0
  22. egzos/container.py +222 -0
  23. egzos/identity.py +1104 -0
  24. egzos/ledger.py +167 -0
  25. egzos/mcp/__init__.py +3 -0
  26. egzos/mcp/http.py +90 -0
  27. egzos/mcp/server.py +287 -0
  28. egzos/model.py +242 -0
  29. egzos/py.typed +0 -0
  30. egzos/resolver.py +115 -0
  31. egzos/store/__init__.py +10 -0
  32. egzos/store/autotitle.py +31 -0
  33. egzos/store/blobs.py +120 -0
  34. egzos/store/find.py +120 -0
  35. egzos/store/items.py +233 -0
  36. egzos/store/nodes.py +193 -0
  37. egzos/store/objects.py +143 -0
  38. egzos/store/uploads.py +369 -0
  39. egzos/trust.py +675 -0
  40. egzos/web/__init__.py +88 -0
  41. egzos/web/app.py +965 -0
  42. egzos/web/static/htmx.LICENSE +13 -0
  43. egzos/web/static/htmx.min.js +1 -0
  44. egzos/web/static/lifeboat.css +140 -0
  45. egzos/web/static/lifeboat.js +12 -0
  46. egzos/web/strings.py +133 -0
  47. egzos/web/templates/_manifest_row.html +3 -0
  48. egzos/web/templates/_shell.html +29 -0
  49. egzos/web/templates/_window.html +7 -0
  50. egzos/web/templates/base.html +24 -0
  51. egzos/web/templates/error.html +10 -0
  52. egzos/web/templates/home.html +39 -0
  53. egzos/web/templates/item.html +91 -0
  54. egzos/web/templates/notfound.html +6 -0
  55. egzos/web/templates/pending.html +107 -0
  56. egzos/web/tokens.css +294 -0
  57. egzoscore-0.2.0.dist-info/METADATA +135 -0
  58. egzoscore-0.2.0.dist-info/RECORD +62 -0
  59. egzoscore-0.2.0.dist-info/WHEEL +4 -0
  60. egzoscore-0.2.0.dist-info/entry_points.txt +2 -0
  61. egzoscore-0.2.0.dist-info/licenses/LICENSE +65 -0
  62. egzoscore-0.2.0.dist-info/licenses/NOTICE +6 -0
egzos/api/__init__.py ADDED
@@ -0,0 +1,150 @@
1
+ # Copyright 2026 Ali Sasanian
2
+ # SPDX-License-Identifier: LicenseRef-PolyForm-Strict-1.0.0
3
+ """
4
+ The REST door (`egzoscore serve --http`; spec/contracts/rest.md): an HTTP resource server,
5
+ in-process with the container, under `/v1/`. It accepts the container's own `Token` and access
6
+ tokens from the container's identity provider (`identity.md`), and is where egzos.io and any
7
+ fork's UI read and write the person's container.
8
+
9
+ TLS (rest.md §1): plain HTTP only when the listening socket is bound to loopback, decided from the
10
+ socket and never from a forwarded header. Anywhere else the door serves TLS, or does not start.
11
+ Behind a TLS-terminating proxy on loopback every caller arrives from the proxy's address, so the
12
+ throttle's per-source bucket (rest.md §2 item 7) counts the proxy as one source: the container-
13
+ global bucket still bounds a sweep, and a proxy that throttles per client is the place to split
14
+ callers apart. A forwarded header is never read to do it here.
15
+
16
+ The origin `/mcp`'s protected-resource metadata names (identity.md §7) is `identity.origin`, the
17
+ one clients reach (§2), when the owner sets it, as behind a proxy or on a wildcard address. Absent,
18
+ it is the one `serve` binds: the socket's scheme, host and port. Never a request's `Host` or a
19
+ forwarded header, so the document is the same for every caller.
20
+ """
21
+
22
+ from __future__ import annotations
23
+
24
+ import ipaddress
25
+ import os
26
+ from collections.abc import Mapping
27
+ from pathlib import Path
28
+ from urllib.parse import urlsplit
29
+
30
+ from egzos._signals import sigterm_as_sigint
31
+ from egzos.container import Container
32
+
33
+ DEFAULT_PORT = 7426
34
+ #: Seconds a stopping door waits for requests in flight before it closes them: a held request must
35
+ #: not keep a host's stop waiting until a hard kill, which skips whatever runs after the door exits
36
+ #: (a replicator's last sync, say).
37
+ SHUTDOWN_GRACE = 5
38
+ #: The host setting that opts a container into plain HTTP behind its host's TLS-terminating proxy.
39
+ EDGE_PROXY = "EGZOS_EDGE_PROXY"
40
+ #: Not globally routable: private, loopback, link-local and unique-local (fc00::/7 holds Fly's
41
+ #: fdaa::/16 6PN). Anything else is treated as routable, so an address in doubt refuses.
42
+ _NOT_ROUTABLE = tuple(ipaddress.ip_network(n) for n in (
43
+ "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "127.0.0.0/8", "169.254.0.0/16",
44
+ "::1/128", "fe80::/10", "fc00::/7",
45
+ ))
46
+
47
+
48
+ def loopback(host: str) -> bool:
49
+ if host == "localhost":
50
+ return True
51
+ try:
52
+ return ipaddress.ip_address(host).is_loopback
53
+ except ValueError:
54
+ return False
55
+
56
+
57
+ def bound_origin(host: str, port: int, *, tls: bool) -> str:
58
+ """The origin a door bound to `host:port` serves: `https` when it holds TLS, a default port
59
+ left out, an IPv6 address in brackets."""
60
+ scheme = "https" if tls else "http"
61
+ name = f"[{host}]" if ":" in host else host
62
+ return f"{scheme}://{name}" if port == (443 if tls else 80) else f"{scheme}://{name}:{port}"
63
+
64
+
65
+ def edge_proxy(environ: Mapping[str, str] | None = None) -> bool:
66
+ """Whether the host opted in to TLS at its edge: `EGZOS_EDGE_PROXY=1`, and nothing else."""
67
+ value = (os.environ if environ is None else environ).get(EDGE_PROXY)
68
+ if value is None:
69
+ return False
70
+ if value != "1":
71
+ raise ValueError(f"{EDGE_PROXY}: 1, or unset")
72
+ return True
73
+
74
+
75
+ def globally_routable(address: str) -> bool:
76
+ """Whether `address` (an IP literal, IPv6 with or without a zone) is globally routable: any
77
+ address outside `_NOT_ROUTABLE`, an IPv4-mapped IPv6 address judged as its IPv4."""
78
+ ip = ipaddress.ip_address(address.split("%", 1)[0])
79
+ if isinstance(ip, ipaddress.IPv6Address) and ip.ipv4_mapped is not None:
80
+ ip = ip.ipv4_mapped
81
+ if ip.is_unspecified:
82
+ return False
83
+ return not any(ip.version == n.version and ip in n for n in _NOT_ROUTABLE)
84
+
85
+
86
+ def check_edge(container: Container, host: str) -> None:
87
+ """Refuse (`PermissionError`) a plain-HTTP door at a host's edge unless the socket binds no
88
+ globally routable address and is no unspecified address, and
89
+ `identity.origin` is set explicitly with scheme `https` (rest.md §1)."""
90
+ try:
91
+ ip = ipaddress.ip_address(host.split("%", 1)[0])
92
+ except ValueError:
93
+ raise PermissionError(
94
+ f"{EDGE_PROXY}: bind an address, not a name, so the socket decides (rest.md §1)"
95
+ ) from None
96
+ if isinstance(ip, ipaddress.IPv6Address) and ip.ipv4_mapped is not None:
97
+ ip = ip.ipv4_mapped # judged as its IPv4, as globally_routable judges it
98
+ if ip.is_unspecified: # a host's own address list can be incomplete, so no list decides
99
+ raise PermissionError(
100
+ f"{EDGE_PROXY}: {host} binds every address; bind the host's private address itself"
101
+ )
102
+ if globally_routable(host):
103
+ raise PermissionError(f"{EDGE_PROXY}: {host} is globally routable; bind a private address")
104
+ origin = container.config_value("identity.origin")
105
+ parsed = urlsplit(origin) if isinstance(origin, str) else None
106
+ if parsed is None or parsed.scheme != "https" or not parsed.hostname:
107
+ raise PermissionError(
108
+ f"{EDGE_PROXY}: set identity.origin to the proxy's https origin "
109
+ "(`egzoscore config set identity.origin https://…`)"
110
+ )
111
+
112
+
113
+ def serve_http(
114
+ container: Container,
115
+ *,
116
+ host: str = "127.0.0.1",
117
+ port: int = DEFAULT_PORT,
118
+ tls_cert: Path | None = None,
119
+ tls_key: Path | None = None,
120
+ environ: Mapping[str, str] | None = None,
121
+ ) -> None:
122
+ import uvicorn
123
+
124
+ from egzos.api.app import create_app
125
+
126
+ if (tls_cert is None) != (tls_key is None):
127
+ raise ValueError("TLS needs both a certificate and its key")
128
+ edge = edge_proxy(environ) # read first, so a mistyped value stops on every bind (a6 on #353)
129
+ if tls_cert is None and not loopback(host) and edge:
130
+ check_edge(container, host)
131
+ elif tls_cert is None and not loopback(host):
132
+ raise PermissionError(
133
+ "plain HTTP is for a loopback socket only (rest.md §1): give --tls-cert and --tls-key, "
134
+ "or bind 127.0.0.1 behind a TLS-terminating proxy"
135
+ )
136
+ origin = bound_origin(host, port, tls=tls_cert is not None)
137
+ print(f"egzoscore REST door on {origin}/v1/, MCP on {origin}/mcp — Ctrl-C to stop.", flush=True)
138
+ with sigterm_as_sigint(): # a SIGTERM stop closes the container as Ctrl-C does
139
+ uvicorn.run(
140
+ create_app(container, origin=origin),
141
+ host=host,
142
+ port=port,
143
+ log_level="warning",
144
+ access_log=False, # a URL is not logged: the door's own record is the chain
145
+ ssl_certfile=str(tls_cert) if tls_cert else None,
146
+ ssl_keyfile=str(tls_key) if tls_key else None,
147
+ proxy_headers=False, # rest.md §1: the socket decides, never a forwarded header
148
+ server_header=False,
149
+ timeout_graceful_shutdown=SHUTDOWN_GRACE,
150
+ )