feat(logshipper): support dynamic OmniLogger credentials
- Add per-service OmniLogger credentials to core config and update subscriptions. - Follow config pushes and enforce entry and request size limits. - Requeue batches after transient send failures.
This commit is contained in:
1 parent
76bf5916fe
commit
8701874efb
20 files changed
+4908
-61
No files matched your search
@@ -1,9 +1,12 @@
|
||||
import json
|
||||
import unittest
|
||||
|
||||
from shared_backend.coreconfig import (
|
||||
MIN_INTERNAL_SECRET_LENGTH,
|
||||
Info,
|
||||
Store,
|
||||
follow_omnilogger,
|
||||
handle_core_services_push,
|
||||
internal_secret_matches,
|
||||
internal_secret_usable,
|
||||
)
|
||||
@@ -117,5 +120,192 @@ class UrlValidationTests(unittest.TestCase):
|
||||
self.assertEqual(store.auth_guard_url, "http://auth.example.com")
|
||||
|
||||
|
||||
class OmniLoggerStoreTests(unittest.TestCase):
|
||||
def test_from_json_reads_omni_fields_and_to_json_never_carries_the_token(self):
|
||||
info = Info.from_json(
|
||||
{"omniLoggerUrl": "http://omni.example.com", "omniLoggerIngestToken": "tok-123"}
|
||||
)
|
||||
self.assertEqual(info.omni_logger_url, "http://omni.example.com")
|
||||
self.assertEqual(info.omni_logger_ingest_token, "tok-123")
|
||||
self.assertEqual(info.to_json()["omniLoggerUrl"], "http://omni.example.com")
|
||||
self.assertNotIn("omniLoggerIngestToken", info.to_json())
|
||||
self.assertNotIn("tok-123", json.dumps(info.to_json()))
|
||||
self.assertNotIn("tok-123", repr(info))
|
||||
|
||||
def test_keeps_last_non_blank_values_and_strips_the_trailing_slash(self):
|
||||
store = Store()
|
||||
store.set(
|
||||
Info(omni_logger_url="http://omni.example.com/", omni_logger_ingest_token="tok-1")
|
||||
)
|
||||
store.set(Info(omni_logger_url=" ", omni_logger_ingest_token=""))
|
||||
info = store.get()
|
||||
self.assertEqual(info.omni_logger_url, "http://omni.example.com")
|
||||
self.assertEqual(info.omni_logger_ingest_token, "tok-1")
|
||||
|
||||
def test_a_bad_omni_url_is_refused_and_nothing_changes(self):
|
||||
store = Store()
|
||||
store.set(Info(omni_logger_url="http://omni.example.com", omni_logger_ingest_token="tok-1"))
|
||||
for bad in ["omni.example.com", "ftp://omni.example.com", "http://"]:
|
||||
with self.subTest(url=bad), self.assertRaises(ValueError):
|
||||
store.set(Info(omni_logger_url=bad, omni_logger_ingest_token="tok-2"))
|
||||
self.assertEqual(store.get().omni_logger_url, "http://omni.example.com")
|
||||
self.assertEqual(store.get().omni_logger_ingest_token, "tok-1")
|
||||
|
||||
def test_subscribers_and_on_update_both_see_every_set(self):
|
||||
store = Store()
|
||||
from_on_update, from_subscriber = [], []
|
||||
store.on_update(from_on_update.append)
|
||||
store.subscribe(from_subscriber.append)
|
||||
store.set(Info(auth_guard_url="http://auth.example.com"))
|
||||
self.assertEqual(len(from_on_update), 1)
|
||||
self.assertEqual(len(from_subscriber), 1)
|
||||
self.assertEqual(from_subscriber[0].auth_guard_url, "http://auth.example.com")
|
||||
|
||||
|
||||
class _RecordingShipper:
|
||||
def __init__(self):
|
||||
self.calls = []
|
||||
|
||||
def set_credentials(self, base_url, token):
|
||||
self.calls.append((base_url, token))
|
||||
|
||||
|
||||
class FollowOmniLoggerTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.store = Store()
|
||||
self.shipper = _RecordingShipper()
|
||||
follow_omnilogger(self.store, self.shipper)
|
||||
|
||||
def test_a_push_with_both_values_sets_the_shipper(self):
|
||||
self.store.set(
|
||||
Info(omni_logger_url="http://omni.example.com", omni_logger_ingest_token="tok-1")
|
||||
)
|
||||
self.assertEqual(self.shipper.calls, [("http://omni.example.com", "tok-1")])
|
||||
|
||||
def test_a_token_without_a_url_leaves_the_shipper_alone(self):
|
||||
self.store.set(Info(omni_logger_ingest_token="tok-1"))
|
||||
self.assertEqual(self.shipper.calls, [])
|
||||
|
||||
def test_a_url_pushed_after_a_token_applies_both(self):
|
||||
self.store.set(Info(omni_logger_ingest_token="tok-1"))
|
||||
self.store.set(Info(omni_logger_url="http://omni.example.com"))
|
||||
self.assertEqual(self.shipper.calls, [("http://omni.example.com", "tok-1")])
|
||||
|
||||
def test_a_later_token_only_push_reuses_the_stored_url(self):
|
||||
self.store.set(
|
||||
Info(omni_logger_url="http://omni.example.com", omni_logger_ingest_token="tok-1")
|
||||
)
|
||||
self.store.set(Info(omni_logger_ingest_token="tok-2"))
|
||||
self.assertEqual(
|
||||
self.shipper.calls,
|
||||
[("http://omni.example.com", "tok-1"), ("http://omni.example.com", "tok-2")],
|
||||
)
|
||||
|
||||
def test_pushes_without_omni_fields_do_not_touch_the_shipper(self):
|
||||
self.store.set(Info(auth_guard_url="http://auth.example.com"))
|
||||
self.assertEqual(self.shipper.calls, [])
|
||||
|
||||
def test_a_store_without_omni_values_does_not_apply_at_startup(self):
|
||||
follow_omnilogger(Store(), self.shipper)
|
||||
self.assertEqual(self.shipper.calls, [])
|
||||
|
||||
|
||||
INTERNAL_SECRET = "a-sufficiently-long-secret-value"
|
||||
PUSH_BODY = {"omniLoggerUrl": "http://omni.example.com", "omniLoggerIngestToken": "tok-secret-1"}
|
||||
AUTH_ERROR = {"error": "internal service authentication required"}
|
||||
JSON_ERROR = {"error": "invalid JSON body"}
|
||||
URL_ERROR = {"error": "core service URL must be an http or https URL with a host"}
|
||||
|
||||
|
||||
class HandleCoreServicesPushTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.store = Store()
|
||||
self.shipper = _RecordingShipper()
|
||||
follow_omnilogger(self.store, self.shipper)
|
||||
|
||||
def push(self, body, auth=INTERNAL_SECRET, secret=INTERNAL_SECRET):
|
||||
raw = body if isinstance(body, bytes) else json.dumps(body).encode("utf-8")
|
||||
return handle_core_services_push(self.store, secret, auth, raw)
|
||||
|
||||
def test_a_missing_or_wrong_x_internal_auth_gets_401(self):
|
||||
for auth in (None, "", "wrong-secret-value-here", INTERNAL_SECRET + "x"):
|
||||
with self.subTest(auth=auth):
|
||||
self.assertEqual(self.push(PUSH_BODY, auth=auth), (401, AUTH_ERROR))
|
||||
self.assertEqual(self.shipper.calls, [])
|
||||
self.assertEqual(self.store.get().omni_logger_url, "")
|
||||
|
||||
def test_an_unusable_secret_refuses_every_caller(self):
|
||||
for secret in (None, "", "SECRET", "change-me-to-a-long-random-value", "short"):
|
||||
with self.subTest(secret=secret):
|
||||
status, _ = self.push(PUSH_BODY, auth=secret, secret=secret)
|
||||
self.assertEqual(status, 401)
|
||||
status, _ = self.push(PUSH_BODY, auth=INTERNAL_SECRET, secret=secret)
|
||||
self.assertEqual(status, 401)
|
||||
self.assertEqual(self.shipper.calls, [])
|
||||
|
||||
def test_a_valid_push_sets_the_shipper_and_never_echoes_the_token(self):
|
||||
status, body = self.push(PUSH_BODY)
|
||||
self.assertEqual((status, body), (200, {"ok": True}))
|
||||
self.assertEqual(self.shipper.calls, [("http://omni.example.com", "tok-secret-1")])
|
||||
self.assertNotIn("tok-secret-1", json.dumps(body))
|
||||
|
||||
def test_a_token_without_a_url_does_not_change_the_shipper(self):
|
||||
status, body = self.push({"omniLoggerIngestToken": "tok-secret-1"})
|
||||
self.assertEqual((status, body), (200, {"ok": True}))
|
||||
self.assertEqual(self.shipper.calls, [])
|
||||
|
||||
def test_a_bad_omni_url_gets_400_and_changes_nothing(self):
|
||||
for bad in ["omni.example.com", "ftp://omni.example.com", "javascript:alert(1)"]:
|
||||
with self.subTest(url=bad):
|
||||
body = {"omniLoggerUrl": bad, "omniLoggerIngestToken": "tok-secret-1"}
|
||||
self.assertEqual(self.push(body), (400, URL_ERROR))
|
||||
self.assertEqual(self.shipper.calls, [])
|
||||
|
||||
def test_a_bad_auth_guard_url_gets_400(self):
|
||||
self.assertEqual(self.push({"authGuardUrl": "auth.example.com"}), (400, URL_ERROR))
|
||||
|
||||
def test_a_field_of_the_wrong_type_gets_400_not_a_server_error(self):
|
||||
for body in (
|
||||
{"carrierNodeUrls": 5},
|
||||
{"carrierNodeUrls": True},
|
||||
{"carrierNodeUrls": "http://carrier.example.com"},
|
||||
{"carrierNodeUrl": ["http://carrier.example.com"]},
|
||||
{"authGuardUrl": 5},
|
||||
):
|
||||
with self.subTest(body=body):
|
||||
self.assertEqual(self.push(body), (400, JSON_ERROR))
|
||||
self.assertEqual(self.shipper.calls, [])
|
||||
|
||||
def test_an_oversized_body_or_a_body_that_is_not_a_json_object_gets_400(self):
|
||||
cases = [
|
||||
json.dumps({"x": "a" * 4096}).encode("utf-8"),
|
||||
b"not json",
|
||||
b"[1, 2]",
|
||||
b'"a string"',
|
||||
b"\xff\xfe\x00",
|
||||
json.dumps({"omniLoggerUrl": 5}).encode("utf-8"),
|
||||
json.dumps({"omniLoggerIngestToken": ["tok"]}).encode("utf-8"),
|
||||
]
|
||||
for raw in cases:
|
||||
with self.subTest(raw=raw[:20]):
|
||||
self.assertEqual(self.push(raw), (400, JSON_ERROR))
|
||||
|
||||
def test_a_body_of_exactly_4096_bytes_is_read(self):
|
||||
raw = json.dumps({"x": "a" * 4087}).encode("utf-8")
|
||||
self.assertEqual(len(raw), 4096)
|
||||
self.assertEqual(self.push(raw), (200, {"ok": True}))
|
||||
|
||||
def test_other_fields_and_null_values_are_ignored(self):
|
||||
body = {
|
||||
"contentScannerUrl": "not a url",
|
||||
"comfyUiUrl": 7,
|
||||
"carrierNodeSecret": "not-used-here",
|
||||
"omniLoggerIngestToken": None,
|
||||
"omniLoggerUrl": "http://omni.example.com",
|
||||
}
|
||||
self.assertEqual(self.push(body), (200, {"ok": True}))
|
||||
self.assertEqual(self.shipper.calls, [])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user