Support local web activation over a restricted Unix socket
This commit is contained in:
1 parent
1fbe97d879
commit
7f706c39b5
6 files changed
+144
-2
No files matched your search
@@ -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 coulobre.web_node import WebSite
|
||||
from coulobre.web_node import MAX_REQUEST_BYTES, WebSite, main
|
||||
|
||||
|
||||
class WebSiteManifestTests(SimpleTestCase):
|
||||
@@ -32,3 +38,51 @@ class WebSiteManifestTests(SimpleTestCase):
|
||||
for change in invalid:
|
||||
with self.subTest(change=change), self.assertRaises(ValueError):
|
||||
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."},
|
||||
)
|
||||
@@ -10,6 +10,7 @@ from .directadmin import DOCUMENT_ROOTS
|
||||
from .domain_names import canonical_domain
|
||||
|
||||
MANIFEST_VERSION = 1
|
||||
MAX_REQUEST_BYTES = 4096
|
||||
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):
|
||||
parser = argparse.ArgumentParser(prog="coulobre-web-node")
|
||||
commands = parser.add_subparsers(dest="command", required=True)
|
||||
@@ -76,6 +99,10 @@ def main(argv=None):
|
||||
render.add_argument("configuration", choices=CONFIGURATIONS)
|
||||
apply = commands.add_parser("apply", help="Install a site on an Ubuntu web node.")
|
||||
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", 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")
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
if args.command == "apply-request":
|
||||
return apply_request()
|
||||
|
||||
try:
|
||||
payload = json.loads(args.manifest.read_text(encoding="utf-8"))
|
||||
site = WebSite.from_manifest(payload)
|
||||
|
||||
Reference in new issue
Block a user