diff --git a/ops/media-alpha/README.md b/ops/media-alpha/README.md index 6fbeda6..1f4b503 100644 --- a/ops/media-alpha/README.md +++ b/ops/media-alpha/README.md @@ -11,7 +11,8 @@ ## Где ```text -127.0.0.1:9180 +Внутренний: 127.0.0.1:9180 +Через SSO: http://192.168.1.10:9161/alpha/ /opt/media-alpha/media_alpha.py media-alpha.service ``` diff --git a/ops/media-alpha/media_alpha.py b/ops/media-alpha/media_alpha.py index 91edbe2..7898010 100644 --- a/ops/media-alpha/media_alpha.py +++ b/ops/media-alpha/media_alpha.py @@ -11,6 +11,7 @@ from pathlib import Path JF = "http://192.168.1.103:8096" KEY = Path("/root/.jellyfin-key").read_text().strip() HOST, PORT = "127.0.0.1", 9180 +ALLOWED = {"maloyakapg@gmail.com"} def jf(path): @@ -55,7 +56,17 @@ class H(BaseHTTPRequestHandler): self.end_headers() self.wfile.write(data) + def allowed(self): + user = (self.headers.get("X-Audit-User") or "").strip().lower() + gateway = self.headers.get("X-Audit-Gateway") == "1" + if not gateway and self.client_address[0] in ("127.0.0.1", "::1"): + return True + return user in ALLOWED + def do_GET(self): + if not self.allowed(): + self.out(403, {"ok": False, "error": "forbidden"}) + return u = urllib.parse.urlparse(self.path) if u.path == "/health": try: @@ -98,7 +109,7 @@ a{color:#58a6ff}
""" diff --git a/ops/media-alpha/tests/test_alpha.py b/ops/media-alpha/tests/test_alpha.py new file mode 100644 index 0000000..ba254b2 --- /dev/null +++ b/ops/media-alpha/tests/test_alpha.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +"""Live read-only tests for Media Alpha (local service).""" +import json +import unittest +import urllib.error +import urllib.request + +BASE = "http://127.0.0.1:9180" + + +def get(path, headers=None): + req = urllib.request.Request(BASE + path, headers=headers or {}) + with urllib.request.urlopen(req, timeout=10) as r: + return r.status, r.read().decode() + + +class Alpha(unittest.TestCase): + def test_health(self): + code, body = get("/health") + self.assertEqual(code, 200) + self.assertTrue(json.loads(body).get("jellyfin")) + + def test_search_returns_real_items(self): + code, body = get("/v1/media/search?q=the") + self.assertEqual(code, 200) + data = json.loads(body) + self.assertTrue(data.get("ok")) + self.assertGreater(len(data.get("items") or []), 0) + + def test_other_email_forbidden(self): + with self.assertRaises(urllib.error.HTTPError) as ctx: + get("/", {"X-Audit-Gateway": "1", "X-Audit-User": "other@example.com"}) + self.assertEqual(ctx.exception.code, 403) + + def test_roman_allowed(self): + code, _ = get("/", {"X-Audit-Gateway": "1", "X-Audit-User": "maloyakapg@gmail.com"}) + self.assertEqual(code, 200) + + +if __name__ == "__main__": + unittest.main(verbosity=2)