phabricator-proxy 0.1.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.
phabricator_proxy/app.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""A read-only Conduit proxy: enough of Phabricator to be talked to, no key handed out.
|
|
2
|
+
|
|
3
|
+
:func:`create_app` returns an app to mount wherever a caller can reach it (for
|
|
4
|
+
hackbot, on the broker sidecar the agent talks to over loopback). Point a Conduit
|
|
5
|
+
client at the mount point and it behaves like a Phabricator instance, except that
|
|
6
|
+
the proxy holds the API key and the caller does not. Two rules keep that safe:
|
|
7
|
+
|
|
8
|
+
* only allow-listed methods are forwarded, so the key cannot be used to write;
|
|
9
|
+
and
|
|
10
|
+
* whatever ``__conduit__`` token the caller sends is discarded and replaced with
|
|
11
|
+
the real key, so a caller chooses the method and its arguments, never the
|
|
12
|
+
credentials.
|
|
13
|
+
|
|
14
|
+
Requests and responses are otherwise relayed as-is — including Conduit's own
|
|
15
|
+
error envelope — so a client's existing error handling keeps working.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
import json
|
|
19
|
+
import logging
|
|
20
|
+
import urllib.parse
|
|
21
|
+
|
|
22
|
+
import httpx
|
|
23
|
+
from fastapi import FastAPI, Request
|
|
24
|
+
from fastapi.responses import JSONResponse
|
|
25
|
+
from phabricator_client import PhabricatorClient
|
|
26
|
+
|
|
27
|
+
log = logging.getLogger(__name__)
|
|
28
|
+
|
|
29
|
+
# Everything needed to reconstruct a revision's stack and its diffs, and nothing
|
|
30
|
+
# that writes. An allow list rather than a deny list, so a Conduit method added
|
|
31
|
+
# upstream is refused by default instead of silently exposed with the key.
|
|
32
|
+
READ_ONLY_METHODS = frozenset(
|
|
33
|
+
{
|
|
34
|
+
# A revision's metadata and the stack graph it sits in.
|
|
35
|
+
"differential.revision.search",
|
|
36
|
+
# A revision's diffs, and the base commit each was built on.
|
|
37
|
+
"differential.querydiffs",
|
|
38
|
+
# The patch text itself.
|
|
39
|
+
"differential.getrawdiff",
|
|
40
|
+
# Expanding an abbreviated base commit to a full, fetchable hash.
|
|
41
|
+
"diffusion.querycommits",
|
|
42
|
+
}
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _conduit_error(code: str, info: str, status: int) -> JSONResponse:
|
|
47
|
+
"""A Conduit-shaped error body, so a Conduit client can read the refusal."""
|
|
48
|
+
return JSONResponse(
|
|
49
|
+
{"result": None, "error_code": code, "error_info": info}, status_code=status
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _parse_params(body: bytes) -> dict:
|
|
54
|
+
"""Read Conduit's ``params`` field out of a url-encoded request body.
|
|
55
|
+
|
|
56
|
+
Parsed directly rather than via ``request.form()``, which needs a
|
|
57
|
+
``Content-Type`` to recognise a form and would otherwise read the body as
|
|
58
|
+
empty. Not every Conduit client sends one — moz-phab's does not — and this
|
|
59
|
+
is a façade for Conduit clients in general, not only for the one hackbot
|
|
60
|
+
happens to use.
|
|
61
|
+
|
|
62
|
+
Raises :class:`ValueError` if ``params`` is not a JSON object.
|
|
63
|
+
"""
|
|
64
|
+
fields = urllib.parse.parse_qs(body.decode())
|
|
65
|
+
params = json.loads(fields.get("params", ["{}"])[0])
|
|
66
|
+
if not isinstance(params, dict):
|
|
67
|
+
raise ValueError("params must be a JSON object")
|
|
68
|
+
return params
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def create_app(
|
|
72
|
+
client: PhabricatorClient,
|
|
73
|
+
*,
|
|
74
|
+
allowed_methods: frozenset[str] = READ_ONLY_METHODS,
|
|
75
|
+
) -> FastAPI:
|
|
76
|
+
"""Build the proxy app, forwarding through ``client`` (which holds the key).
|
|
77
|
+
|
|
78
|
+
Mount it at the path a Conduit client will treat as the API root, e.g.
|
|
79
|
+
``Mount("/phabricator/api", app=create_app(client))`` makes a call to
|
|
80
|
+
``differential.revision.search`` land on
|
|
81
|
+
``/phabricator/api/differential.revision.search``.
|
|
82
|
+
"""
|
|
83
|
+
# No docs/OpenAPI routes: this app fronts a credential, so it serves the
|
|
84
|
+
# Conduit methods it was asked to serve and nothing else.
|
|
85
|
+
app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)
|
|
86
|
+
|
|
87
|
+
@app.post("/{method}")
|
|
88
|
+
async def proxy(method: str, request: Request) -> JSONResponse:
|
|
89
|
+
if method not in allowed_methods:
|
|
90
|
+
log.warning("refusing to proxy Conduit method %s", method)
|
|
91
|
+
return _conduit_error(
|
|
92
|
+
"ERR-CONDUIT-METHOD-NOT-ALLOWED",
|
|
93
|
+
f"{method} is not on the proxy's read-only allow list",
|
|
94
|
+
403,
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
try:
|
|
98
|
+
params = _parse_params(await request.body())
|
|
99
|
+
except ValueError as exc:
|
|
100
|
+
return _conduit_error("ERR-CONDUIT-CORE", f"Malformed params: {exc}", 400)
|
|
101
|
+
|
|
102
|
+
try:
|
|
103
|
+
return JSONResponse(await client.conduit_call(method, params))
|
|
104
|
+
except httpx.HTTPError as exc:
|
|
105
|
+
log.warning("proxied Conduit call %s failed: %s", method, exc)
|
|
106
|
+
return _conduit_error(
|
|
107
|
+
"ERR-CONDUIT-CORE", f"Upstream Phabricator call failed: {exc}", 502
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
return app
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: phabricator-proxy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Mountable read-only Conduit proxy that holds the Phabricator key
|
|
5
|
+
Requires-Python: >=3.14
|
|
6
|
+
Requires-Dist: fastapi>=0.109.0
|
|
7
|
+
Requires-Dist: httpx>=0.26.0
|
|
8
|
+
Requires-Dist: moz-phabricator-client
|
|
9
|
+
Provides-Extra: dev
|
|
10
|
+
Requires-Dist: pytest-asyncio>=0.23.0; extra == 'dev'
|
|
11
|
+
Requires-Dist: pytest>=8.0.0; extra == 'dev'
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
phabricator_proxy/__init__.py,sha256=2_gZKscZfAFWxUgUWI_rXL-6fRf5HiMY6EWrMmTZSoI,111
|
|
2
|
+
phabricator_proxy/app.py,sha256=9be173AfAK1eK83pMxHw8bMIXFtg0MvrrCnmBwOODcg,4345
|
|
3
|
+
phabricator_proxy-0.1.0.dist-info/METADATA,sha256=_OmNHhPUbHxd0pF4_lpaH3wBdXVwCeKQYsI0qTI1WLI,377
|
|
4
|
+
phabricator_proxy-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
5
|
+
phabricator_proxy-0.1.0.dist-info/RECORD,,
|