: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
feat(workflows): add the transactional email jwt, cdp client and api key scope (#116188)
Harley Alexander165cd1ebea13
diff --git a/frontend/src/lib/scopes.tsx b/frontend/src/lib/scopes.tsxindex 22d6db810f22..9a45c17ece31 100644--- a/frontend/src/lib/scopes.tsx+++ b/frontend/src/lib/scopes.tsx@@ -379,6 +379,7 @@ export const PROJECT_SECRET_API_KEY_ALLOWED_API_SCOPE_ACTION = ['billing:read','replay_scanner:write','session_recording:read',+ 'hog_flow:write',] as constexport type ProjectSecretAPIKeyAllowedScope = (typeof PROJECT_SECRET_API_KEY_ALLOWED_API_SCOPE_ACTION)[number]diff --git a/posthog/jwt.py b/posthog/jwt.pyindex 0414c1eb594b..7909419b1fc4 100644--- a/posthog/jwt.py+++ b/posthog/jwt.py@@ -31,6 +31,7 @@ class PosthogJwtAudience(Enum):WORKFLOWS_CANCEL_INVOCATIONS = "posthog:workflows:cancel_invocations"WORKFLOWS_CANCEL_BATCH = "posthog:workflows:cancel_batch"WORKFLOWS_STEP_RESUME = "posthog:workflows:step_resume"+ WORKFLOWS_SEND_TRANSACTIONAL_EMAIL = "posthog:workflows:send_transactional_email"CREDENTIALS_SERVICE = "posthog:credentials_service"TASKS_CREATE = "posthog:tasks:create"CUSTOMER_TASKS_CREATE = "posthog:customer-tasks:create"diff --git a/posthog/plugins/plugin_server_api.py b/posthog/plugins/plugin_server_api.pyindex 55f4036867b4..0e17c68a090c 100644--- a/posthog/plugins/plugin_server_api.py+++ b/posthog/plugins/plugin_server_api.py@@ -248,6 +248,32 @@ def resume_workflow_step(team_id: int, origin_key: str, status: str, result: dic)+WORKFLOWS_TRANSACTIONAL_EMAIL_JWT_PURPOSE = ScopedServiceJwtPurpose(+ audience=PosthogJwtAudience.WORKFLOWS_SEND_TRANSACTIONAL_EMAIL,+ settings_name="WORKFLOWS_TRANSACTIONAL_EMAIL_JWT_SECRETS",+ default_ttl=timedelta(minutes=2),+)+++def _mint_transactional_email_jwt(team_id: int, hog_flow_id: str) -> str:+ """Short-lived scoped JWT for one transactional email send, pinned to the team and the transactional email's+ workflow so a leaked token can only send that email. Verified in the plugin server's CdpApi."""+ if not WORKFLOWS_TRANSACTIONAL_EMAIL_JWT_PURPOSE.enabled():+ raise RuntimeError("WORKFLOWS_TRANSACTIONAL_EMAIL_JWT_SECRET is not configured — cannot send email")+ return WORKFLOWS_TRANSACTIONAL_EMAIL_JWT_PURPOSE.mint({"team_id": team_id, "hog_flow_id": hog_flow_id})+++def send_transactional_email(team_id: int, hog_flow_id: str, payload: dict) -> requests.Response:+ """Run a transactional email's workflow once. `payload` carries the recipient, the recipient's distinct_id and+ the template variables. 202 means queued, 429 means the team is over its email quota."""+ return internal_requests.post(+ CDP_API_URL + f"/api/projects/{team_id}/hog_flows/{hog_flow_id}/transactional_email",+ json=payload,+ headers={"Authorization": f"Bearer {_mint_transactional_email_jwt(team_id, hog_flow_id)}"},+ timeout=10,+ )++def cancel_hog_flow_invocations(team_id: int, hog_flow_id: str, payload: dict) -> requests.Response:"""Flag a workflow's in-flight invocations for cancellation. `payload` carries exactly oneselector: {"invocation_ids": [...]} or {"all": true}. The Node side only marks rows and wakesdiff --git a/posthog/scopes.py b/posthog/scopes.pyindex 41b5763b93bf..be2969a6c023 100644--- a/posthog/scopes.py+++ b/posthog/scopes.py@@ -271,6 +271,9 @@ PROJECT_SECRET_API_KEY_ALLOWED_API_SCOPE_ACTION: list[tuple[APIScopeObject, APIS# Both are needed together; every other replay_scanner and session_recording route stays PSAK-denied.("replay_scanner", "write"),("session_recording", "read"),+ # Lets a service send transactional email through `messaging_templates/:id/send/`. Every other+ # hog_flow route stays PSAK-denied, because no other hog_flow viewset lists PSAK actions.+ ("hog_flow", "write"),]# Server-side scope assignment string-set constants (see RFC: server-side scopediff --git a/posthog/settings/data_stores.py b/posthog/settings/data_stores.pyindex 86ffe7a41fce..55ff5ce94547 100644--- a/posthog/settings/data_stores.py+++ b/posthog/settings/data_stores.py@@ -601,6 +601,17 @@ WORKFLOWS_STEP_RESUME_JWT_SECRETS = get_list(get_from_env("WORKFLOWS_STEP_RESUME_JWT_SECRET", "local-dev-workflows-step-resume-jwt" if DEBUG or TEST else ""))+# Scoped JWT keys for the transactional email route (an API call sending one email from a message+# template). The web tier mints, the plugin server verifies. Its own key per the one-key-per-surface+# rule above. Comma-separated, newest first. Empty outside dev/test, so sends fail closed until+# provisioned. The dev/test value must match the plugin server's default (nodejs/src/cdp/config.ts).+WORKFLOWS_TRANSACTIONAL_EMAIL_JWT_SECRETS = get_list(+ get_from_env(+ "WORKFLOWS_TRANSACTIONAL_EMAIL_JWT_SECRET",+ "local-dev-workflows-transactional-email-jwt" if DEBUG or TEST else "",+ )+)+# Signs the tokens a workflow's "Create AI task" action calls back with. The dev/test value# must match the plugin server's minting default so local workflows work with no setup.TASKS_CREATE_JWT_SECRETS = get_list(