Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions tests/integration/test_private_database_log_boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from __future__ import annotations

import os
from contextlib import contextmanager
from contextvars import ContextVar
from uuid import uuid4

import httpx
Expand All @@ -11,6 +13,30 @@
from tests.integration._resources import ch_client_from_env, unique_org


@contextmanager
def _observe_request_capture(patch, capture):
from server.core import ch
from server.settings import settings

# Child work belongs to this request; tasks already running do not.
request = ContextVar("private_log_test_capture", default=False)
original_path = settings.ch_capture_statements
original_capture = ch.capture_statement

def capture_request(sql, _path):
path = str(capture) if request.get() else original_path
if path:
original_capture(sql, path)

patch.setattr(ch, "capture_statement", capture_request)
patch.setattr(settings, "ch_capture_statements", str(capture))
token = request.set(True)
try:
yield
finally:
request.reset(token)


async def test_native_private_query_log_is_inaccessible_to_sql_and_analyst(
control_plane_url, mint_test_token, monkeypatch, tmp_path
):
Expand All @@ -20,7 +46,6 @@ async def test_native_private_query_log_is_inaccessible_to_sql_and_analyst(
from server.core.diagnostics import private_work
from server.insights.app import sql_access
from server.insights.domain.sql_surface import Scope
from server.settings import settings

org, namespace = unique_org("private-log"), "docs"
token = mint_test_token(org=org, namespaces=[namespace], roles=["writer"])
Expand All @@ -32,8 +57,7 @@ async def test_native_private_query_log_is_inaccessible_to_sql_and_analyst(
try:
await ch.connect()
capture = tmp_path / "statements.jsonl"
with monkeypatch.context() as patch:
patch.setattr(settings, "ch_capture_statements", str(capture))
with monkeypatch.context() as patch, _observe_request_capture(patch, capture):
query_id = "docsrc-private-log-" + uuid4().hex
with private_work():
value = await ch.raw_bytes(
Expand Down
117 changes: 117 additions & 0 deletions tests/unit/test_private_capture_observation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
"""Exercise the native-log test's capture boundary without a database."""

from __future__ import annotations

import asyncio
import json

import pytest

from server.core import ch, diagnostics
from server.settings import settings
from tests.integration import test_private_database_log_boundary as boundary


class NativeBoundaryReached(Exception):
"""The capture assertions passed; the real database checks start here."""


class CaptureDriver:
def __init__(self, background, *, leak=None):
self.background = background
self.leak = leak
self.ready = asyncio.Event()
self.finished = asyncio.Event()
self.disconnected = False

async def connect(self):
pass

async def disconnect(self):
self.disconnected = True

async def public_work(self):
await self.ready.wait()
try:
assert not diagnostics.codes_only()
if self.background:
ch.set_query_tag("test.unrelated_public_capture")
ch.statement_settings("SELECT 1 AS unrelated_public_control")
finally:
self.finished.set()

async def raw_bytes(self, sql, *, settings):
assert diagnostics.codes_only()
ch.statement_settings(sql, settings, cls="read")
self.ready.set()
await self.finished.wait()
if self.leak == "owner":
self.force_private_capture(sql)
elif self.leak == "child":
await asyncio.create_task(self.private_child(sql))
return b"synthetic-private-code\n"

@staticmethod
def force_private_capture(sql):
assert diagnostics.codes_only()
ch.capture_statement(sql, settings.ch_capture_statements)

async def private_child(self, sql):
self.force_private_capture(sql)

async def query_one(self, sql):
assert not diagnostics.codes_only()
ch.statement_settings(sql)
return {"public_control": 1}

async def execute(self, sql):
assert sql == "SYSTEM FLUSH LOGS"
raise NativeBoundaryReached


def captured_sql(path):
return [json.loads(line)["sql"] for line in path.read_text().splitlines()]


async def run_boundary(monkeypatch, tmp_path, *, background, leak=None):
driver = CaptureDriver(background, leak=leak)
monkeypatch.setenv("LOOM_CH_HOST", "unit-capture-only")
monkeypatch.setattr(ch, "get_client", lambda: driver)
# This task predates the private request and must not inherit its scope.
public = asyncio.create_task(driver.public_work())
tag = ch.query_tag_var.set("")
try:
await boundary.test_native_private_query_log_is_inaccessible_to_sql_and_analyst(
"http://unused.invalid", lambda **_: "unit-token", monkeypatch, tmp_path
)
finally:
driver.ready.set()
await public
ch.query_tag_var.reset(tag)
assert driver.disconnected


@pytest.mark.parametrize("background", [False, True])
@pytest.mark.parametrize("original_sink", [False, True])
async def test_capture_observes_only_the_logical_request(
monkeypatch, tmp_path, background, original_sink
):
original = tmp_path / "original.jsonl"
monkeypatch.setattr(settings, "ch_capture_statements", str(original) if original_sink else "")
with pytest.raises(NativeBoundaryReached):
await run_boundary(monkeypatch, tmp_path, background=background)
assert captured_sql(tmp_path / "statements.jsonl") == ["SELECT 1 AS public_control"]
if original_sink and background:
assert captured_sql(original) == ["SELECT 1 AS unrelated_public_control"]
else:
assert not original.exists()


@pytest.mark.parametrize("leak", ["owner", "child"])
async def test_private_capture_from_owner_or_child_still_fails(monkeypatch, tmp_path, leak):
monkeypatch.setattr(settings, "ch_capture_statements", "")
with pytest.raises(AssertionError):
await run_boundary(monkeypatch, tmp_path, background=True, leak=leak)
assert "SELECT 'synthetic-private-code' FORMAT TabSeparated" in captured_sql(
tmp_path / "statements.jsonl"
)
Loading