Support local web activation over a restricted Unix socket

This commit is contained in:
parkel committed 2026-11-21 01:10:25 +01:00
1 parent 1fbe97d879
commit 7f706c39b5
6 files changed
+144 -2

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
[Unit]
Description=Coulobre local web activation socket
[Socket]
ListenStream=/run/coulobre-web-apply.sock
SocketUser=root
SocketGroup=coulobre-web-provisioning
SocketMode=0660
Accept=yes
MaxConnections=4
RemoveOnStop=true
[Install]
WantedBy=sockets.target
@@ -0,0 +1,20 @@
[Unit]
Description=Coulobre local web activation request
After=apache2.service php8.5-fpm.service
CollectMode=inactive-or-failed
[Service]
Type=exec
User=root
WorkingDirectory=/opt/coulobre
UMask=0022
ExecStart=/opt/coulobre/bin/coulobre-web-node apply-request
StandardInput=socket
StandardOutput=socket
StandardError=journal
RuntimeMaxSec=3min
KillSignal=SIGINT
KillMode=mixed
NoNewPrivileges=true
PrivateTmp=true
ProtectHome=true
+3 -1
View File
@@ -157,7 +157,9 @@ to Apache workers. The scheduler stores its state in a private directory and
there is one scheduler for the installation. there is one scheduler for the installation.
```sh ```sh
sudo install -m 0644 deploy/systemd/*.service /etc/systemd/system/ sudo install -m 0644 deploy/systemd/coulobre-panel.service \
deploy/systemd/coulobre-worker.service deploy/systemd/coulobre-beat.service \
/etc/systemd/system/
sudo systemctl daemon-reload sudo systemctl daemon-reload
sudo systemctl enable --now coulobre-panel.service coulobre-worker.service coulobre-beat.service sudo systemctl enable --now coulobre-panel.service coulobre-worker.service coulobre-beat.service
+22
View File
@@ -570,6 +570,28 @@ The status command reads local state. It exits with code 0 for `configured`,
code 2 for the other report states, and code 1 if it cannot read the manifest code 2 for the other report states, and code 1 if it cannot read the manifest
or inspect the node. Use an HTTP request to verify the site's response as well. or inspect the node. Use an HTTP request to verify the site's response as well.
### Enable local web activation
On a web node with the same Apache and PHP-FPM setup, systemd can accept local
activation requests at `/run/coulobre-web-apply.sock`. Install the matching
Coulobre package in a root-owned virtual environment at `/opt/coulobre` before
enabling the socket.
```sh
getent group coulobre-web-provisioning >/dev/null || sudo groupadd --system coulobre-web-provisioning
sudo install -m 0644 deploy/systemd/coulobre-web-apply.socket \
deploy/systemd/coulobre-web-apply@.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable --now coulobre-web-apply.socket
```
Grant group membership only to the service identity responsible for provisioning
sites. The socket accepts one version 1 site manifest as UTF-8 JSON, up to 4 KiB.
After sending the manifest, shut down the writing half of the connection and
read the JSON reply to EOF. A `configured` reply includes the account ID and
domain. A `failed` reply includes a diagnostic in `problem`. Retry the same
manifest if the connection closes before a reply arrives.
## Run an isolated PHP test site ## Run an isolated PHP test site
Open a completed preview for the account's current domain and select **Download Open a completed preview for the account's current domain and select **Download
+55 -1
View File
@@ -1,6 +1,12 @@
import json
from contextlib import redirect_stdout
from io import BytesIO, StringIO
from types import SimpleNamespace
from unittest.mock import patch
from django.test import SimpleTestCase from django.test import SimpleTestCase
from coulobre.web_node import WebSite from coulobre.web_node import MAX_REQUEST_BYTES, WebSite, main
class WebSiteManifestTests(SimpleTestCase): class WebSiteManifestTests(SimpleTestCase):
@@ -32,3 +38,51 @@ class WebSiteManifestTests(SimpleTestCase):
for change in invalid: for change in invalid:
with self.subTest(change=change), self.assertRaises(ValueError): with self.subTest(change=change), self.assertRaises(ValueError):
WebSite.from_manifest(manifest | change) WebSite.from_manifest(manifest | change)
class WebActivationRequestTests(SimpleTestCase):
def setUp(self):
self.site = WebSite(42, "example.org")
self.apply = self.enterContext(patch("coulobre.web_node_apply.apply_site"))
def request(self, data):
output = StringIO()
with (
patch("sys.stdin", SimpleNamespace(buffer=BytesIO(data))),
redirect_stdout(output),
):
code = main(["apply-request"])
return code, json.loads(output.getvalue())
def test_replies_with_the_identity_of_the_applied_manifest(self):
code, reply = self.request(json.dumps(self.site.manifest()).encode())
self.apply.assert_called_once_with(self.site)
self.assertEqual(code, 0)
self.assertEqual(
reply, self.site.manifest() | {"state": "configured", "problem": None}
)
def test_rejects_invalid_or_oversized_input_before_apply(self):
for data in (
b"",
b"\xff",
b"[" * MAX_REQUEST_BYTES,
b" " * (MAX_REQUEST_BYTES + 1),
json.dumps(self.site.manifest() | {"document_root": "/tmp"}).encode(),
):
with self.subTest(length=len(data)):
code, reply = self.request(data)
self.assertEqual(code, 1)
self.assertEqual(reply["state"], "failed")
self.assertTrue(reply["problem"])
self.apply.assert_not_called()
def test_reports_node_failures_as_json_for_the_requested_site(self):
self.apply.side_effect = ValueError("The primary domain is already reserved.")
code, reply = self.request(json.dumps(self.site.manifest()).encode())
self.assertEqual(code, 1)
self.assertEqual(
reply,
self.site.manifest()
| {"state": "failed", "problem": "The primary domain is already reserved."},
)
+30
View File
@@ -10,6 +10,7 @@ from .directadmin import DOCUMENT_ROOTS
from .domain_names import canonical_domain from .domain_names import canonical_domain
MANIFEST_VERSION = 1 MANIFEST_VERSION = 1
MAX_REQUEST_BYTES = 4096
CONFIGURATIONS = {"apache": "apache-vhost.conf", "php-fpm": "php-fpm-pool.conf"} CONFIGURATIONS = {"apache": "apache-vhost.conf", "php-fpm": "php-fpm-pool.conf"}
@@ -66,6 +67,28 @@ class WebSite:
) )
def apply_request():
from .web_node_apply import apply_site
report = {"version": MANIFEST_VERSION}
try:
body = sys.stdin.buffer.read(MAX_REQUEST_BYTES + 1)
if len(body) > MAX_REQUEST_BYTES:
raise ValueError(f"The manifest exceeds {MAX_REQUEST_BYTES} bytes.")
try:
payload = json.loads(body.decode("utf-8"))
except (ValueError, RecursionError):
raise ValueError("Expected a UTF-8 JSON manifest.") from None
site = WebSite.from_manifest(payload)
report = site.manifest()
apply_site(site)
except (OSError, ValueError) as error:
print(json.dumps({**report, "state": "failed", "problem": str(error)}))
return 1
print(json.dumps({**report, "state": "configured", "problem": None}))
return 0
def main(argv=None): def main(argv=None):
parser = argparse.ArgumentParser(prog="coulobre-web-node") parser = argparse.ArgumentParser(prog="coulobre-web-node")
commands = parser.add_subparsers(dest="command", required=True) commands = parser.add_subparsers(dest="command", required=True)
@@ -76,6 +99,10 @@ def main(argv=None):
render.add_argument("configuration", choices=CONFIGURATIONS) render.add_argument("configuration", choices=CONFIGURATIONS)
apply = commands.add_parser("apply", help="Install a site on an Ubuntu web node.") apply = commands.add_parser("apply", help="Install a site on an Ubuntu web node.")
apply.add_argument("manifest", type=Path) apply.add_argument("manifest", type=Path)
commands.add_parser(
"apply-request",
help="Apply one site manifest from standard input and report JSON.",
)
status = commands.add_parser( status = commands.add_parser(
"status", help="Check a site's local configuration and services." "status", help="Check a site's local configuration and services."
) )
@@ -89,6 +116,9 @@ def main(argv=None):
restore.add_argument("--source-root", choices=DOCUMENT_ROOTS, default="public_html") restore.add_argument("--source-root", choices=DOCUMENT_ROOTS, default="public_html")
args = parser.parse_args(argv) args = parser.parse_args(argv)
if args.command == "apply-request":
return apply_request()
try: try:
payload = json.loads(args.manifest.read_text(encoding="utf-8")) payload = json.loads(args.manifest.read_text(encoding="utf-8"))
site = WebSite.from_manifest(payload) site = WebSite.from_manifest(payload)