: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(replay): authenticate the temporal worker's recording deletions (#100755)
posthog[bot]2ac0aea1bc2e
diff --git a/docker-compose.hobby.yml b/docker-compose.hobby.ymlindex e06cd6b46bb0..2e69598fed3f 100644--- a/docker-compose.hobby.yml+++ b/docker-compose.hobby.yml@@ -470,6 +470,11 @@ services:BROWSERLESS_TOKEN: ${BROWSERLESS_SECRET:-$POSTHOG_SECRET}HEATMAP_BROWSERLESS_URL: 'http://browserless:3000'HEATMAP_BROWSERLESS_TOKEN: ${BROWSERLESS_SECRET:-$POSTHOG_SECRET}+ # This worker runs the recording deletion Temporal activities, which call+ # recording-api. Without these two the calls go out unauthenticated and+ # recording-api rejects them with a 401.+ RECORDING_API_URL: http://recording-api:6738+ INTERNAL_API_SECRET: ${INTERNAL_API_SECRET:-$POSTHOG_SECRET}# Point Django at the Rust feature-flags container; the setting# otherwise defaults to localhost:3001.FEATURE_FLAGS_SERVICE_URL: 'http://feature-flags:3001'diff --git a/posthog/temporal/session_replay/delete_recordings/activities.py b/posthog/temporal/session_replay/delete_recordings/activities.pyindex b42b3deb970c..f91a1dc5c3ea 100644--- a/posthog/temporal/session_replay/delete_recordings/activities.py+++ b/posthog/temporal/session_replay/delete_recordings/activities.py@@ -28,6 +28,7 @@ from posthog.temporal.session_replay.delete_recordings.types import (LoadRecordingsPage,PurgeDeletedMetadataInput,PurgeDeletedMetadataResult,+ RecordingApiConfigurationError,RecordingsWithPersonInput,RecordingsWithQueryInput,RecordingsWithTeamInput,@@ -204,11 +205,13 @@ async def delete_recordings(input: DeleteRecordingsInput) -> DeleteRecordingsResrecording_api_url = settings.RECORDING_API_URLif not recording_api_url:- raise RuntimeError("RECORDING_API_URL is not configured")+ raise RecordingApiConfigurationError("RECORDING_API_URL is not configured")url = f"{recording_api_url}/api/projects/{input.team_id}/recordings/delete"headers = recording_api_auth_headers(input.team_id, "delete")+ if not headers:+ raise RecordingApiConfigurationError("Neither INTERNAL_API_SECRET nor RECORDING_API_JWT_SECRET is configured")async with internal_httpx_async_client(timeout=60.0, headers=headers) as client:response = await client.post(url, json={"session_ids": input.session_ids, "deleted_by": input.deleted_by})diff --git a/posthog/temporal/session_replay/delete_recordings/types.py b/posthog/temporal/session_replay/delete_recordings/types.pyindex 2fe6473109f8..e58dfc4a04e8 100644--- a/posthog/temporal/session_replay/delete_recordings/types.py+++ b/posthog/temporal/session_replay/delete_recordings/types.py@@ -114,6 +114,10 @@ class LoadRecordingError(Exception):pass+class RecordingApiConfigurationError(RuntimeError):+ """The worker cannot reach recording-api because a setting is missing. Retries cannot fix it."""++class PurgeDeletedMetadataInput(BaseModel):"""Input for the nightly metadata purge workflow."""diff --git a/posthog/temporal/session_replay/delete_recordings/workflow.py b/posthog/temporal/session_replay/delete_recordings/workflow.pyindex 45d8e83ba6d8..ee3ff3f8f24e 100644--- a/posthog/temporal/session_replay/delete_recordings/workflow.py+++ b/posthog/temporal/session_replay/delete_recordings/workflow.py@@ -72,6 +72,7 @@ async def _delete_page(retry_policy=common.RetryPolicy(maximum_attempts=3,initial_interval=timedelta(minutes=1),+ non_retryable_error_types=["RecordingApiConfigurationError"],),)progress.total_deleted += len(result.deleted)diff --git a/posthog/temporal/tests/session_replay/delete_recordings/test_activities.py b/posthog/temporal/tests/session_replay/delete_recordings/test_activities.pyindex 8e79edea90d0..c9a9000557c0 100644--- a/posthog/temporal/tests/session_replay/delete_recordings/test_activities.py+++ b/posthog/temporal/tests/session_replay/delete_recordings/test_activities.py@@ -17,6 +17,7 @@ from posthog.temporal.session_replay.delete_recordings.types import (DeleteRecordingsInput,LoadRecordingError,PurgeDeletedMetadataInput,+ RecordingApiConfigurationError,)@@ -127,7 +128,6 @@ def _mock_delete_response_client(mock_client_cls):{"X-Internal-Api-Secret", "Authorization"},id="both_during_rollout",),- pytest.param("", "", set(), id="nothing_configured"),],)async def test_delete_recordings_auth_headers(internal_secret, jwt_secret, expected_headers):@@ -179,10 +179,28 @@ async def test_delete_recordings_raises_when_no_recording_api_url():with patch("posthog.temporal.session_replay.delete_recordings.activities.settings") as mock_settings:mock_settings.RECORDING_API_URL = ""- with pytest.raises(RuntimeError, match="RECORDING_API_URL is not configured"):+ with pytest.raises(RecordingApiConfigurationError, match="RECORDING_API_URL is not configured"):await delete_recordings(DeleteRecordingsInput(team_id=1, session_ids=["s1"], deleted_by="[email protected]"))+async def test_delete_recordings_raises_when_no_credential_configured():+ with (+ override_settings(+ RECORDING_API_URL="http://recording-api:8000",+ INTERNAL_API_SECRET="",+ RECORDING_API_JWT_SECRET="",+ ),+ patch(+ "posthog.temporal.session_replay.delete_recordings.activities.internal_httpx_async_client"+ ) as mock_client_cls,+ ):+ with pytest.raises(RecordingApiConfigurationError, match="RECORDING_API_JWT_SECRET"):+ await delete_recordings(DeleteRecordingsInput(team_id=1, session_ids=["s1"], deleted_by="[email protected]"))++ mock_client_cls.assert_not_called()++@pytest.mark.asyncioasync def test_delete_recordings_raises_on_http_error():mock_response = httpx.Response(500, request=httpx.Request("POST", "http://test"))