serve: an empty api_key in the config is a warning at start, not silence (#569)
Cut down from the PR as the plan asked (recommend, never force): an empty --api-key or STRATA_API_KEY is already refused (#213) and a blank config key is refused with #956; an api_key that is present but empty now prints that the server has no key and starts. The setup-side refusal is left out (the final line already says no API key is set). Co-authored-by: Guillaume PUTIER <guillaume@putier.fr>
This commit is contained in:
1 parent
8d8e165148
commit
ecdc028c73
2 files changed
+50
No files matched your search
@@ -5057,6 +5057,9 @@ def main() -> int:
|
||||
except ValueError as e:
|
||||
print(f'[strata] {e}: set a key, or leave --api-key / STRATA_API_KEY / "api_key" out', file=sys.stderr)
|
||||
return 2
|
||||
if not svc.api_key and "api_key" in cfg: # #569: written, but empty: the server has none (a warning, not a stop)
|
||||
print("[strata] the api_key in the config is empty: this server has no API key (anyone who can reach it can "
|
||||
"use it); set one, or leave api_key out", file=sys.stderr, flush=True)
|
||||
svc.cors_origins = origins_of(cfg.get("cors_origins"), "cors_origins", wildcard=True)
|
||||
svc.trusted_origins = origins_of(cfg.get("trusted_origins"), "trusted_origins", wildcard=False)
|
||||
try:
|
||||
|
||||
@@ -847,6 +847,53 @@ class StatusNeedsTheKey(unittest.TestCase):
|
||||
httpd.server_close()
|
||||
|
||||
|
||||
class _Reached(Exception):
|
||||
"""raised by the patched serve(): main() got past the API key checks."""
|
||||
|
||||
|
||||
class ConfigApiKey(unittest.TestCase):
|
||||
"""#569 (and #213): the key in the config, through main(): empty is a warning, a blank one is refused."""
|
||||
|
||||
def run_main(self, cfg):
|
||||
"""-> (return code, the API key the service got or None when main() stopped before serving, stderr)."""
|
||||
import serve.server as S
|
||||
seen = {}
|
||||
|
||||
def fake_serve(svc, host=None, port=None):
|
||||
seen["key"] = svc.api_key
|
||||
raise _Reached
|
||||
|
||||
err = io.StringIO()
|
||||
with tempfile.TemporaryDirectory() as d, mock.patch.dict(os.environ), \
|
||||
mock.patch.object(S, "serve", fake_serve), mock.patch.object(sys, "stderr", new=err):
|
||||
os.environ.pop("STRATA_API_KEY", None)
|
||||
p = Path(d) / "cfg.json"
|
||||
p.write_text(json.dumps(cfg), encoding="utf-8")
|
||||
with mock.patch.object(sys, "argv", ["server.py", "--engine", "mock", "--port", "0", "--config", str(p)]):
|
||||
try:
|
||||
code = S.main()
|
||||
except _Reached:
|
||||
code = 0
|
||||
return code, seen.get("key"), err.getvalue()
|
||||
|
||||
def test_an_empty_key_is_a_warning(self):
|
||||
code, key, err = self.run_main({"api_key": ""})
|
||||
self.assertEqual((code, key), (0, ""))
|
||||
self.assertIn("api_key in the config is empty", err)
|
||||
|
||||
def test_a_blank_key_is_refused(self):
|
||||
for value in (" ", "\r\n"):
|
||||
self.assertEqual(self.run_main({"api_key": value})[:2], (2, None), repr(value))
|
||||
|
||||
def test_a_key_and_no_key(self):
|
||||
code, key, err = self.run_main({"api_key": "cfg"})
|
||||
self.assertEqual((code, key), (0, "cfg"))
|
||||
self.assertNotIn("is empty", err)
|
||||
code, key, err = self.run_main({})
|
||||
self.assertEqual((code, key), (0, ""))
|
||||
self.assertNotIn("is empty", err)
|
||||
|
||||
|
||||
class ApiKeyForms(unittest.TestCase):
|
||||
"""#725: a key no client could send (spaces or a line end around it), and a key outside ASCII."""
|
||||
|
||||
|
||||
Reference in new issue
Block a user