Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

test(docker): drive every secret name the container entrypoint claims

Aashish6288ed94fe6f
diff --git a/tests/test_docker_entrypoint.py b/tests/test_docker_entrypoint.py
index ed2e1d28..379b6231 100644
--- a/tests/test_docker_entrypoint.py
+++ b/tests/test_docker_entrypoint.py
@@ -1,57 +1,152 @@
-"""Exercise secret-file handling across the container command boundary."""
+"""Exercise secret-file handling across the container command boundary.
+
+`docker/entrypoint.py` loops over `SECRET_NAMES`, and the HTTP service's documented way to hand
+over its bearer key is the second name in that loop -- `docs/docker.md`, "Bearer token from a
+file": "`LAYA_API_KEY_FILE` is read once at startup, moved into `LAYA_API_KEY`, and the `_FILE`
+variable is removed before the server execs." Every test in this file used to name `HF_TOKEN`
+in its harness, its expected value and its error assertion, so the loop was exercised through
+one of its two arms:
+
+ $ sed 's/SECRET_NAMES = ("HF_TOKEN", "LAYA_API_KEY")/SECRET_NAMES = ("HF_TOKEN",)/' docker/entrypoint.py > ...
+ $ python tests/test_docker_entrypoint.py
+ Ran 5 tests in 0.165s
+ OK
+
+Run that mutated script against a live `laya-serve` (mounted over the image's own entrypoint) and
+the service is left unauthenticated: `POST /v1/systemone` with no `Authorization` header answers
+400 instead of 401. Nothing in the suite said so. The names are therefore read out of the script
+itself and each one is driven through the whole contract, including the `_FILE` removal, which no
+test checked for either name.
+"""
+import importlib.util
+import json
import os
from pathlib import Path
+import re
import subprocess
import sys
import tempfile
import unittest
-ENTRYPOINT = Path(__file__).resolve().parents[1] / "docker" / "entrypoint.py"
+ROOT = Path(__file__).resolve().parents[1]
+ENTRYPOINT = ROOT / "docker" / "entrypoint.py"
+COMPOSE_FILES = tuple(sorted(ROOT.glob("compose*.y*ml")))
+
+
+def _secret_names():
+ """The names the container entrypoint claims to load, read from the entrypoint itself."""
+ spec = importlib.util.spec_from_file_location("laya_entrypoint_under_test", ENTRYPOINT)
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module) # the script only defines main() under a __main__ guard
+ return tuple(module.SECRET_NAMES)
+
+
+SECRET_NAMES = _secret_names()
+
+
+def file_backed_secrets(text):
+ """Names a Compose file forwards as `<NAME>_FILE` next to `<NAME>`.
+
+ The pairing is what makes one: `<NAME>` is where the loaded value has to land, so a `_FILE`
+ whose base name is not forwarded at all is a path handed to the program itself rather than a
+ secret to read. That excludes `LAYA_REQUEST_FILE`, which the quickstart opens directly and
+ which must *not* appear in `SECRET_NAMES`.
+ """
+ forwarded = set(re.findall(r"^\s+([A-Z][A-Z0-9_]*):", text, re.MULTILINE))
+ return {key[:-len("_FILE")] for key in forwarded
+ if key.endswith("_FILE") and key[:-len("_FILE")] in forwarded}
class EntrypointTests(unittest.TestCase):
- def invoke(self, extra, expected="direct"):
- env = {key: value for key, value in os.environ.items()
- if key not in {"HF_TOKEN", "HF_TOKEN_FILE"}}
+ def invoke(self, name, extra, expected="direct"):
+ """Run the entrypoint with `extra`, and report what the child saw without echoing it."""
+ blocked = {prefix for secret in SECRET_NAMES for prefix in (secret, secret + "_FILE")}
+ env = {key: value for key, value in os.environ.items() if key not in blocked}
env.update(extra)
- child = "import os, sys; assert os.environ.get('HF_TOKEN') == sys.argv[1]; print('child ran')"
+ child = ("import json, os, sys;"
+ "n = sys.argv[1];"
+ "print(json.dumps({'name': n,"
+ " 'matched': os.environ.get(n) == sys.argv[2],"
+ " 'file_var': n + '_FILE' in os.environ}))")
return subprocess.run(
- [sys.executable, str(ENTRYPOINT), sys.executable, "-c", child, expected],
+ [sys.executable, str(ENTRYPOINT), sys.executable, "-c", child, name, expected],
env=env, capture_output=True, text=True, check=False,
)
- def test_direct_value(self):
- result = self.invoke({"HF_TOKEN": "direct"})
+ def report(self, result, name):
+ """The child's own account of its environment, or the entrypoint's failure."""
self.assertEqual(result.returncode, 0, result.stderr)
- self.assertEqual(result.stdout.strip(), "child ran")
+ self.assertEqual(result.stdout.count("\n"), 1, result.stdout)
+ seen = json.loads(result.stdout)
+ self.assertEqual(seen.get("name"), name, result.stdout)
+ return seen
+
+ def test_every_secret_the_deployment_forwards_is_loaded(self):
+ """A `<NAME>` / `<NAME>_FILE` pair in a shipped Compose file is a name the loop must have."""
+ for compose in COMPOSE_FILES:
+ with self.subTest(compose=compose.name):
+ self.assertEqual(file_backed_secrets(compose.read_text(encoding="utf-8"))
+ - set(SECRET_NAMES), set())
+
+ def test_every_loaded_secret_is_reachable_from_the_deployment(self):
+ """The reverse: a name in the loop that no Compose file forwards is one nobody can set."""
+ for name in SECRET_NAMES:
+ with self.subTest(secret=name):
+ self.assertTrue(any(name in file_backed_secrets(compose.read_text(encoding="utf-8"))
+ for compose in COMPOSE_FILES),
+ "%s is loaded by the entrypoint but forwarded by no Compose file"
+ % name)
+
+ def test_direct_value(self):
+ for name in SECRET_NAMES:
+ with self.subTest(secret=name):
+ result = self.invoke(name, {name: "direct"})
+ self.assertTrue(self.report(result, name)["matched"], result.stdout)
def test_file_overrides_direct_and_trims_whitespace(self):
- with tempfile.TemporaryDirectory() as directory:
- secret = Path(directory) / "token"
- secret.write_text(" synthetic-file-value\n", encoding="utf-8")
- result = self.invoke({"HF_TOKEN": "direct", "HF_TOKEN_FILE": str(secret)},
- "synthetic-file-value")
- self.assertEqual(result.returncode, 0, result.stderr)
- self.assertNotIn("synthetic-file-value", result.stdout + result.stderr)
+ for name in SECRET_NAMES:
+ with self.subTest(secret=name):
+ with tempfile.TemporaryDirectory() as directory:
+ secret = Path(directory) / "token"
+ secret.write_text(" synthetic-file-value\n", encoding="utf-8")
+ result = self.invoke(name, {name: "direct", name + "_FILE": str(secret)},
+ "synthetic-file-value")
+ self.assertTrue(self.report(result, name)["matched"], result.stdout)
+ self.assertNotIn("synthetic-file-value", result.stdout + result.stderr)
+
+ def test_file_variable_is_removed_before_the_command_runs(self):
+ """What "and the `_FILE` variable is removed before the server execs" promises, per name."""
+ for name in SECRET_NAMES:
+ with self.subTest(secret=name):
+ with tempfile.TemporaryDirectory() as directory:
+ secret = Path(directory) / "token"
+ secret.write_text("synthetic-file-value\n", encoding="utf-8")
+ result = self.invoke(name, {name + "_FILE": str(secret)}, "synthetic-file-value")
+ self.assertFalse(self.report(result, name)["file_var"], result.stdout)
def test_invalid_files_stop_before_child_without_disclosure(self):
- with tempfile.TemporaryDirectory() as directory:
- secret = Path(directory) / "private-filename"
- for contents in (None, b"", b" \n", b"synthetic-secret\x00", b"\xff"):
- with self.subTest(contents=contents):
- if contents is not None:
- secret.write_bytes(contents)
- result = self.invoke({"HF_TOKEN": "direct", "HF_TOKEN_FILE": str(secret)})
- self.assertNotEqual(result.returncode, 0)
- self.assertEqual(result.stdout, "")
- self.assertIn("HF_TOKEN_FILE", result.stderr)
- self.assertNotIn(str(secret), result.stderr)
- self.assertNotIn("synthetic-secret", result.stderr)
- self.assertNotIn("direct", result.stderr)
+ for name in SECRET_NAMES:
+ with self.subTest(secret=name):
+ with tempfile.TemporaryDirectory() as directory:
+ secret = Path(directory) / "private-filename"
+ for contents in (None, b"", b" \n", b"synthetic-secret\x00", b"\xff"):
+ with self.subTest(contents=contents):
+ if contents is not None:
+ secret.write_bytes(contents)
+ result = self.invoke(name, {name: "direct", name + "_FILE": str(secret)})
+ self.assertNotEqual(result.returncode, 0)
+ self.assertEqual(result.stdout, "")
+ self.assertIn(name + "_FILE", result.stderr)
+ self.assertNotIn(str(secret), result.stderr)
+ self.assertNotIn("synthetic-secret", result.stderr)
+ self.assertNotIn("direct", result.stderr)
def test_empty_file_setting_and_unknown_suffix_are_ignored(self):
- result = self.invoke({"HF_TOKEN": "direct", "HF_TOKEN_FILE": "", "UNKNOWN_FILE": "/missing"})
- self.assertEqual(result.returncode, 0, result.stderr)
+ for name in SECRET_NAMES:
+ with self.subTest(secret=name):
+ result = self.invoke(name, {name: "direct", name + "_FILE": "",
+ "UNKNOWN_FILE": "/missing"})
+ self.assertTrue(self.report(result, name)["matched"], result.stdout)
def test_missing_command_fails(self):
result = subprocess.run([sys.executable, str(ENTRYPOINT)], env={},