Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(ducklake): pin HTTPS for delta-kernel staging reads via duckgres (#73027)

Eric Duongcc5cfb19a9a0
diff --git a/posthog/ducklake/storage.py b/posthog/ducklake/storage.py
index 7f2f8a4fe762..87fcf955d300 100644
--- a/posthog/ducklake/storage.py
+++ b/posthog/ducklake/storage.py
@@ -679,6 +679,26 @@ def setup_duckgres_session(
conn.execute(f"LOAD {ext}")
+def create_staging_read_secret(conn: psycopg.Connection, catalog_bucket: str) -> None:
+ """Pin HTTPS for delta-kernel reads of the staging tree on this session.
+
+ On cache-proxy-enabled duckgres clusters the org's ambient S3 secret carries
+ USE_SSL false so httpfs traffic flows through the logging proxy — but
+ delta_scan's delta-kernel object store ignores DuckDB's proxy and dials the
+ plain-HTTP S3 endpoint directly, which worker egress silently drops (10
+ retries, ~58s, then failure — on every attempt). This longer-SCOPE session
+ secret wins prefix resolution for the staging tree only and forces direct
+ HTTPS (allowed egress) using the worker's own ambient credentials; the
+ region also resolves from the worker's chain. Session-scoped on purpose:
+ duckgres wipes non-persistent secrets at the next session create.
+ """
+ conn.execute(
+ "CREATE OR REPLACE SECRET posthog_staging_delta_https ("
+ "TYPE S3, PROVIDER credential_chain, USE_SSL true, "
+ f"SCOPE 's3://{catalog_bucket}/{STAGING_PREFIX}')"
+ )
+
+
def connect_to_duckgres(server: DuckgresServer) -> psycopg.Connection:
"""Open a psycopg connection to a duckgres server."""
return psycopg.connect(
@@ -699,6 +719,7 @@ __all__ = [
"configure_connection",
"configure_cross_account_connection",
"connect_to_duckgres",
+ "create_staging_read_secret",
"ensure_ducklake_bucket_exists",
"get_deltalake_storage_options",
"normalize_endpoint",
diff --git a/posthog/temporal/ducklake/ducklake_copy_data_imports_workflow.py b/posthog/temporal/ducklake/ducklake_copy_data_imports_workflow.py
index b5c3202c7da3..817d13152835 100644
--- a/posthog/temporal/ducklake/ducklake_copy_data_imports_workflow.py
+++ b/posthog/temporal/ducklake/ducklake_copy_data_imports_workflow.py
@@ -3,6 +3,7 @@ import uuid
import typing
import datetime as dt
import dataclasses
+from urllib.parse import urlparse
from django.conf import settings
@@ -28,6 +29,7 @@ from posthog.ducklake.storage import (
compute_staging_uri,
configure_connection,
connect_to_duckgres,
+ create_staging_read_secret,
ensure_ducklake_bucket_exists,
get_deltalake_storage_options,
setup_duckgres_session,
@@ -352,6 +354,7 @@ def _copy_data_imports_via_duckgres(inputs: DuckLakeCopyDataImportsActivityInput
with connect_to_duckgres(server) as conn:
setup_duckgres_session(conn)
+ create_staging_read_secret(conn, bucket)
logger.info(
"Creating DuckLake table from staged Delta snapshot via duckgres",
ducklake_table=table,
@@ -511,6 +514,7 @@ def _verify_data_imports_ducklake_copy_via_duckgres(
with connect_to_duckgres(server) as conn:
setup_duckgres_session(conn)
+ create_staging_read_secret(conn, urlparse(inputs.model.staging_uri).netloc)
return _run_data_imports_verification_checks(
conn,
inputs,
diff --git a/posthog/temporal/ducklake/ducklake_copy_data_modeling_workflow.py b/posthog/temporal/ducklake/ducklake_copy_data_modeling_workflow.py
index 99a21a921cef..146f10f44b41 100644
--- a/posthog/temporal/ducklake/ducklake_copy_data_modeling_workflow.py
+++ b/posthog/temporal/ducklake/ducklake_copy_data_modeling_workflow.py
@@ -24,6 +24,7 @@ from posthog.ducklake.storage import (
compute_staging_uri,
configure_connection,
connect_to_duckgres,
+ create_staging_read_secret,
ensure_ducklake_bucket_exists,
get_deltalake_storage_options,
setup_duckgres_session,
@@ -525,6 +526,7 @@ def _copy_data_modeling_via_duckgres(inputs: DuckLakeCopyActivityInputs, logger)
with connect_to_duckgres(server) as conn:
setup_duckgres_session(conn)
+ create_staging_read_secret(conn, bucket)
logger.info(
"Creating DuckLake table from staged Delta snapshot via duckgres",
ducklake_table=table,
diff --git a/posthog/temporal/tests/ducklake/test_ducklake_copy_data_imports_workflow.py b/posthog/temporal/tests/ducklake/test_ducklake_copy_data_imports_workflow.py
index 068ffb6f028b..79affa76fa0c 100644
--- a/posthog/temporal/tests/ducklake/test_ducklake_copy_data_imports_workflow.py
+++ b/posthog/temporal/tests/ducklake/test_ducklake_copy_data_imports_workflow.py
@@ -547,6 +547,20 @@ def test_copy_data_imports_to_ducklake_activity_via_duckgres(monkeypatch):
assert any("CREATE SCHEMA IF NOT EXISTS" in str(call) for call in execute_calls)
assert any("CREATE OR REPLACE TABLE" in str(call) for call in execute_calls)
assert any("delta_scan" in str(call) for call in execute_calls)
+
+ # delta-kernel ignores DuckDB's proxy transport and dials the org secret's
+ # plain-HTTP endpoint directly, which worker egress silently drops (~58s
+ # failure per attempt). The session must pin an HTTPS credential-chain
+ # secret over the staging tree BEFORE any delta_scan runs.
+ secret_idx = next(
+ i for i, call in enumerate(execute_calls) if "CREATE OR REPLACE SECRET posthog_staging_delta_https" in str(call)
+ )
+ secret_sql = str(execute_calls[secret_idx])
+ assert "PROVIDER credential_chain" in secret_sql
+ assert "USE_SSL true" in secret_sql
+ assert "SCOPE 's3://test-bucket/__posthog_staging'" in secret_sql
+ first_delta_idx = next(i for i, call in enumerate(execute_calls) if "delta_scan" in str(call))
+ assert secret_idx < first_delta_idx
# Verify staging URI used, not source URI
table_call = next(call for call in execute_calls if "delta_scan" in str(call))
assert "s3://test-bucket/__posthog_staging/team_1/customers" in str(table_call)
@@ -780,6 +794,10 @@ def test_verify_data_imports_ducklake_copy_activity_handles_query_failure(monkey
"posthog.temporal.ducklake.ducklake_copy_data_imports_workflow.setup_duckgres_session",
MagicMock(),
)
+ monkeypatch.setattr(
+ "posthog.temporal.ducklake.ducklake_copy_data_imports_workflow.create_staging_read_secret",
+ MagicMock(),
+ )
monkeypatch.setattr(
"posthog.temporal.ducklake.ducklake_copy_data_imports_workflow._run_data_imports_schema_verification",
MagicMock(return_value=None),
@@ -855,6 +873,10 @@ def test_verify_data_imports_ducklake_copy_activity_tolerance_comparison(monkeyp
"posthog.temporal.ducklake.ducklake_copy_data_imports_workflow.setup_duckgres_session",
MagicMock(),
)
+ monkeypatch.setattr(
+ "posthog.temporal.ducklake.ducklake_copy_data_imports_workflow.create_staging_read_secret",
+ MagicMock(),
+ )
monkeypatch.setattr(
"posthog.temporal.ducklake.ducklake_copy_data_imports_workflow._run_data_imports_schema_verification",
MagicMock(return_value=None),