Sandbox Lifecycle Events and Signed Webhook Delivery
- Source:
docs/enhancements/0028-sandbox-events-and-webhooks/README.md- Status:
implemented- Area:
gateway- Milestone:
v0.7.0
| Stage | Status | Created | Updated | Owning Area |
|---|---|---|---|---|
stable |
implemented |
2026-11-16 |
2026-11-18 |
gateway |
Table of Contents
Section titled “Table of Contents”- Release Signoff Checklist
- Summary
- Motivation
- Proposal
- Design Details
- Production Readiness Review Questionnaire (PRR)
- Implementation History
- Drawbacks
- Alternatives
Release Signoff Checklist
Section titled “Release Signoff Checklist”- Enhancement is scoped clearly with explicit Goals and Non-Goals.
- API / OpenAPI changes are additive and backward-compatible.
- Security boundary (SSRF protection, HMAC-SHA256 webhook signing, encrypted secret storage, tenant isolation) is reviewed.
- Dual-runtime behavior (Production Kubernetes
Kata + CSI Blockand Local DevelopmentDocker + Loopback ext4) is specified. - Test plan covers unit tests (
go test -race) and end-to-end delivery/retry/SSRF verification. - Graduation criteria for Alpha, Beta, and Stable are defined.
- Beta (
SandboxWebhookCRD +Secretbackend, PodBox KubernetesEvents) and Stable (dead-letter inspection/retry API, PrometheusRule alerts) delivered inv0.7.0.
Summary
Section titled “Summary”CEP-0028 introduces a tenant-isolated Sandbox Lifecycle Events store and Signed Webhook Delivery subsystem (pkg/events) in cubepod-gateway. It enables users and external orchestrators to query sandbox lifecycle audit history (GET /events, GET /sandboxes/{sandboxID}/events) and subscribe to real-time HTTP webhook notifications (POST/GET/PATCH/DELETE /webhooks) for created, paused, resumed, updated, forked, and killed transitions.
As of v0.7.0 (Stable), Kubernetes deployments persist webhooks as SandboxWebhook CRs (HMAC key isolated in a companion Secret) and lifecycle events as labeled core/v1 Events on PodBox, so multi-replica HA gateways share webhook and event state with zero external database. Failed or dropped deliveries are captured as tenant-scoped dead letters that can be listed, retried, or discarded via /webhooks/dead-letters APIs, and a reference PrometheusRule (deploy/monitoring/webhook-alerts.yaml) alerts on failed/dropped deliveries.
Motivation
Section titled “Motivation”Currently, clients that need to track sandbox lifecycle transitions (for billing, idle-reaper notifications, audit trails, or UI updates) must poll GET /sandboxes or GET /sandboxes/{id}. Polling adds unnecessary API load and misses transient transitions such as transparent auto-resume or idle-timeout pauses.
- Tenant-Scoped Lifecycle Event Log: Record structured lifecycle events (
sandbox.lifecycle.created,paused,resumed,updated,forked,killed) with cursor-paginated query APIs (GET /events,GET /sandboxes/{sandboxID}/events). - Webhook Registration & Management API: Provide
/webhooksCRUD (POST /webhooks,GET /webhooks,GET /webhooks/{webhookID},PATCH /webhooks/{webhookID},DELETE /webhooks/{webhookID}) scoped bysha256(APIKey)tenant hash. - Cryptographically Signed Delivery: Sign every webhook payload with HMAC-SHA256 (
t=<unix>,v1=<hex>) using a per-webhook secret encrypted at rest. - SSRF-Safe Bounded Async Dispatcher: Deliver events asynchronously with bounded queues, exponential backoff retries, and dial-time IP validation blocking private/loopback/metadata ranges in production.
- Dual-Runtime Parity: Emit identical events across both Local Docker and Production Kubernetes (
Kata + CSI Block) runtimes.
Non-Goals
Section titled “Non-Goals”- Streaming in-guest stdout/stderr or filesystem watch events over webhooks (handled by Connect-RPC streaming).
- Guaranteeing unbounded persistent replay across multi-month retention (Alpha uses a bounded ring/retention window; long-term archival belongs to external SIEM/observability sinks).
- Modifying
PodBoxCRD schema in Alpha (events are emitted bycubepod-gatewaylifecycle handlers and idle-reaper sync).
Proposal
Section titled “Proposal”User Stories
Section titled “User Stories”- Story 1 (Billing & Usage Metering): As a platform integrator, I register a webhook subscribed to
sandbox.lifecycle.created,paused,resumed, andkilledso my billing service receives signed notifications without pollingGET /sandboxes. - Story 2 (Audit & Debugging): As a tenant developer, I call
GET /sandboxes/{id}/eventsto inspect when a sandbox was paused by the idle reaper and when it auto-resumed on incoming traffic.
Notes / Constraints / Caveats
Section titled “Notes / Constraints / Caveats”- Idle Reaper Visibility: In Kubernetes mode,
cubepod-controllermay transitionPodBox.Status.PhasetoPausedor delete expired sandboxes asynchronously. Gateway’sresolveSandbox/syncloop should emit correspondingpaused/killedevents when observing a state transition not already recorded. - Dual-Runtime Behavior:
- Production Kubernetes (
Kata + CSI Block): Webhook subscriptions are persisted asSandboxWebhookCRDs (cubepod.io/v1alpha1) with a companion KubernetesSecret(secretRef) holding only the HMACsignatureKey, and lifecycle events are emitted as labeled KubernetesEvents onPodBox(zero external database dependency, consistent with CEP-0022). SSRF dialer blocks RFC1918, loopback, and cloud metadata IPs (169.254.169.254). - Local Development (
Docker + Loopback ext4): Identical/eventsand/webhooksAPIs backed by<state-dir>/events/(AES-256-GCM encrypted webhook store + bounded event ring); operators may setCUBEPOD_WEBHOOK_ALLOW_PRIVATE=1in local test harnesses to deliver to127.0.0.1mock receivers.
- Production Kubernetes (
Risks and Mitigations
Section titled “Risks and Mitigations”| Risk | Likelihood | Impact | Mitigation |
|---|---|---|---|
SSRF against internal cluster services or cloud metadata (169.254.169.254) |
High | Critical | Custom net.Dialer.Control verifies resolved IP is global unicast and not loopback/private/link-local before TCP connect (redirect following disabled). |
| Slow or unreachable webhook receiver blocking sandbox creation/pause | Medium | High | Strictly non-blocking bounded worker queue (chan with drop-oldest/drop-new metric) + 5s per-attempt HTTP timeout; lifecycle handlers never await delivery. |
| Webhook signing secret leakage | Low | High | Encrypt signatureKey at rest (AES-256-GCM) and return it only on POST /webhooks creation or explicit rotation (POST /webhooks/{id} / PATCH). |
| Multi-replica duplicate event emission on K8s watch | Medium | Low | Include deterministic eventID (evt_...) and idempotencyKey in payload so receivers deduplicate cleanly. |
Design Details
Section titled “Design Details”API / CRD / Wire Changes
Section titled “API / CRD / Wire Changes”0. SandboxWebhook CRD (api/v1alpha1/types.go & deploy/helm/cubepod/crds/cubepod.io_sandboxwebhooks.yaml)
Section titled “0. SandboxWebhook CRD (api/v1alpha1/types.go & deploy/helm/cubepod/crds/cubepod.io_sandboxwebhooks.yaml)”In Kubernetes mode, each registered webhook is stored as a SandboxWebhook custom resource (cubepod.io/v1alpha1, sandboxwebhooks.cubepod.io) labeled with cubepod.io/tenant=<tenantPrefix>, keeping configuration & delivery status in CRD etcd and isolating only the HMAC signatureKey in a referenced Kubernetes Secret (secretRef):
type SandboxWebhookSpec struct { TenantHash string `json:"tenantHash"` Name string `json:"name"` URL string `json:"url"` Events []string `json:"events"` Enabled bool `json:"enabled"` SecretName string `json:"secretName"` // K8s Secret holding key "signatureKey"}
type SandboxWebhookStatus struct { LastDeliveryTime *time.Time `json:"lastDeliveryTime,omitempty"` ConsecutiveFailures int `json:"consecutiveFailures,omitempty"` // Bounded (max 50 per webhook); never contains the signatureKey. Added in v0.7.0. DeadLetters []SandboxWebhookDeadLetter `json:"deadLetters,omitempty"`}
type SandboxWebhookDeadLetter struct { ID string `json:"id"` // dlq_... EventID string `json:"eventId"` EventType string `json:"eventType"` SandboxID string `json:"sandboxId,omitempty"` Reason string `json:"reason"` // delivery_failed | queue_full Attempts int `json:"attempts,omitempty"` LastStatusCode int `json:"lastStatusCode,omitempty"` LastError string `json:"lastError,omitempty"` // sanitized class RetryCount int `json:"retryCount,omitempty"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` Event json.RawMessage `json:"event,omitempty"`}Concrete Kubernetes object layout (v0.7.0):
| Object | Name | Labels / Annotations | Contents |
|---|---|---|---|
SandboxWebhook CR |
cubepod-whk-<hex> |
cubepod.io/resource=sandbox-webhook, cubepod.io/tenant=<16-char tenant prefix>, annotation cubepod.io/webhook-id |
spec (name, URL, events, enabled, secretName) and gateway-maintained status.lastDeliveryTime, status.consecutiveFailures, status.deadLetters |
Secret |
cubepod-whk-sec-<hex> |
cubepod.io/resource=sandbox-webhook-secret |
data key signatureKey only (referenced via spec.secretName) |
core/v1 Event |
deterministic per transition | cubepod.io/resource=sandbox-event, cubepod.io/tenant, cubepod.io/sandbox, cubepod.io/source=gateway, annotation cubepod.io/lifecycle-event (JSON payload) |
involvedObject = PodBox <sandboxID> |
Gateways resync SandboxWebhook CRs every 5s and reload on cache miss, so a webhook created on one replica is visible on all replicas.
In Kubernetes mode, cubepod-controller / cubepod-gateway also emits labeled Kubernetes Events (core/v1) attached to PodBox (involvedObject.name = <sandboxID>, label cubepod.io/tenant=<tenantPrefix>), allowing GET /events and GET /sandboxes/{id}/events to query recent lifecycle events directly from Kubernetes without an external database.
1. Webhooks Endpoints (pkg/gateway/webhooks.go & api/openapi/cubepod-v1.yaml)
Section titled “1. Webhooks Endpoints (pkg/gateway/webhooks.go & api/openapi/cubepod-v1.yaml)”All routes require X-API-Key (requireManagementKey).
POST /webhooks:- Request:
{ "name": "billing-hook", "url": "https://example.com/hook", "events": ["sandbox.lifecycle.created", "sandbox.lifecycle.killed"], "enabled": true } - Response (
201 Created):{ "id": "whk_...", "name": "...", "url": "...", "events": [...], "enabled": true, "signatureKey": "whsec_...", "createdAt": "..." }(signatureKeyreturned only at creation).
- Request:
GET /webhooks: List tenant webhooks (omittingsignatureKey).GET /webhooks/{webhookID}: Get single webhook metadata (omittingsignatureKey).PATCH /webhooks/{webhookID}: Updateurl,events,enabled, or"rotateSecret": true.DELETE /webhooks/{webhookID}: Remove webhook and its dead letters (204 No Content).WebhookInfoadditionally exposes optionallastDeliveryTimeandconsecutiveFailures(v0.7.0).
1a. Dead-Letter Endpoints (v0.7.0)
Section titled “1a. Dead-Letter Endpoints (v0.7.0)”All routes require X-API-Key and return 501 when events are not configured or the feature gate is off.
GET /webhooks/dead-letters?webhookID=...&sandboxID=...&reason=...&limit=<=100&nextToken=...: Paginated tenant dead letters (X-Next-Tokenheader).GET /webhooks/{webhookID}/dead-letters: Same filters minuswebhookID;404if the webhook is not owned by the tenant.POST /webhooks/{webhookID}/dead-letters/{deadLetterID}/retry: Re-sign with the currentsignatureKeyand re-enqueue (202+DeadLetter);404not found,409webhook disabled,503delivery queue full. A failed retry re-records the same id withretryCount+1.DELETE /webhooks/{webhookID}/dead-letters/{deadLetterID}: Discard (204);404not found.
DeadLetter schema: id (dlq_...), webhookId, eventId, eventType, sandboxId, reason (delivery_failed | queue_full), attempts, optional lastStatusCode, optional sanitized lastError class (timeout, connection_error, http_status, ssrf_blocked), optional retryCount, createdAt, updatedAt, and the original lifecycle event. It never contains the signatureKey or webhook URL.
2. Events Endpoints & E2B-Compatible Event Payload Schema
Section titled “2. Events Endpoints & E2B-Compatible Event Payload Schema”GET /events?sandboxID=...&eventType=...&limit=50&nextToken=...: Paginated tenant lifecycle events (X-Next-Tokenheader).GET /sandboxes/{sandboxID}/events: Convenience route scoped to one sandbox.
Event payload schema (compatible with E2B v1 Sandbox Lifecycle Webhooks):
{ "version": "v1", "id": "f5911677-cb60-498f-afed-f68143b3cc59", "type": "sandbox.lifecycle.paused", "timestamp": "2026-11-16T01:15:00Z", "eventCategory": "lifecycle", "eventLabel": "pause", "sandboxId": "cpod-123", "sandboxTeamId": "team_abc...", "sandboxTemplateId": "base", "sandboxBuildId": "a979a14b-bdcc-49e6-bc04-1189fc9fe7c2", "sandboxExecutionId": "1dae9e1c-9957-4ce7-a236-a99d5779aadf", "eventData": { "pause_mode": "filesystem", "execution": { "started_at": "2026-11-16T01:10:00Z", "vcpu_count": 2, "memory_mb": 512, "execution_time": 300000 } }}Notes on E2B compatibility:
- CubePod emits
pause_mode: "filesystem"onpausedandresume_mode: "reboot"onresumed(reflecting CubePod’s scale-to-zero PVC retention rather than RAM snapshot restore). eventDatamay benulloncreated/updated/forked, or carryexecutionusage metadata onpaused/killed.
3. Signature Verification Header
Section titled “3. Signature Verification Header”Every delivery sends:
e2b-signature: <hmac-sha256-hex-or-v1>(official E2B webhook signature header)X-CubePod-Event-ID: <event-uuid>X-CubePod-Signature: t=1794816900,v1=<hmac-sha256-hex(signatureKey, "1794816900." + rawBody)>
State Machine & Data Flow
Section titled “State Machine & Data Flow”flowchart LR
GW["cubepod-gateway Lifecycle Handler"] -->|"Emit(event)"| Store["pkg/events Ring/File Store"]
GW -->|"Enqueue (non-blocking)"| Queue["Bounded Delivery Queue"]
Queue --> Worker["Async Webhook Worker"]
Worker -->|"SSRF-safe Dialer + HMAC Sign"| Target["HTTPS Receiver"]
Target -->|"5xx / Timeout"| Retry["Exponential Backoff (max 3 retries)"]
Retry --> Worker
Retry -->|"max retries exhausted"| DLQ["Dead Letters (CR status / file)"]
Queue -->|"queue full"| DLQ
DLQ -->|"POST .../retry (re-sign)"| QueueBoundary Conditions & Failure Recovery
Section titled “Boundary Conditions & Failure Recovery”- Queue Saturation: If the delivery queue is full, new deliveries are dropped and
cubepod_webhook_deliveries_total{result="dropped"}increments;/sandboxeslatency is never impacted. Since v0.7.0 the dropped delivery is recorded as a dead letter with reasonqueue_full. - Retry Exhaustion: After max retries the delivery is recorded as a dead letter with reason
delivery_failedandcubepod_webhook_dead_letters_totalincrements. Dead letters are bounded to 500 per tenant (file/memory backend) and 50 per webhook (SandboxWebhookstatus.deadLetters); oldest entries are evicted first. - Multi-Replica Deduplication: Lifecycle
Eventnames are deterministic, so concurrent replicas observing the same sync transition collide on create and only the replica that wins delivers webhooks. - Event Retention: Kubernetes
Events are subject to the apiserver event TTL (default1h); the gateway additionally keeps a bounded in-memory ring. Long-term archival remains a non-goal. - HTTP Redirects: Webhook client disables automatic
3xxredirects (CheckRedirect: func(...) error { return http.ErrUseLastResponse }) to prevent redirect-based SSRF bypasses. - Gateway Restart: Registered webhooks persist in
SandboxWebhookCRs +Secrets (K8s) or encrypted on disk (file backend); in-flight retries are best-effort, but exhausted or dropped deliveries survive as dead letters.
Test Plan
Section titled “Test Plan”- Unit Tests (
go test -race):pkg/events: Event emission, per-tenant & per-sandbox filtering, cursor pagination, webhook CRUD, HMAC-SHA256 signature verification, SSRF dialer rejecting127.0.0.1,10.0.0.1,169.254.169.254, andhttp://when TLS-only is enforced, redirect blocking, and retry backoff.pkg/gateway:/webhooksand/eventsHTTP handlers, tenant isolation (404), and end-to-endPOST /sandboxes->POST /pause->POST /connect->DELETE /sandboxes/{id}delivering signed events to a test receiver.- Beta/Stable (v0.7.0): Kubernetes backend against a fake client (CR + Secret create/sync/delete,
signatureKeynever in CR, cross-replica visibility, deterministicEventdedupe), dead-letter recording on retry exhaustion and queue saturation, list/filter/pagination, retry (202/404/409/503), delete, bounds, and tenant isolation.
Graduation Criteria
Section titled “Graduation Criteria”- Alpha (
v0.6.0) — delivered:pkg/events+/webhooks+/eventsroutes + SSRF-safe HMAC dispatcher passingmake test(local file store). - Beta (
v0.7.0) — delivered:SandboxWebhookCRD + companionSecretbackend (CUBEPOD_EVENTS_BACKEND=k8s, Helmevents.backend: k8s) and labeled core/v1Events onPodBoxfor multi-replica HA gateways (zero external DB);cubepod-controlleralso emits PodBoxEvents (PodBoxRunning,PodBoxPaused,IdlePaused,PausedTTLExpired,PodBoxDeleted). - Stable (
v0.7.0) — delivered: Dead-letter recording (delivery_failed,queue_full) with list/retry/delete APIs,lastDeliveryTime/consecutiveFailureson webhook info,cubepod_webhook_dead_letters_total/cubepod_webhook_dead_letters_pendingmetrics, and the referencePrometheusRuleindeploy/monitoring/webhook-alerts.yaml.
Upgrade / Downgrade Strategy
Section titled “Upgrade / Downgrade Strategy”-
Upgrade: Additive gateway routes; no storage PVC impact. In v0.7.0 Kubernetes gateways default to the
k8sbackend and need theSandboxWebhookCRD with thestatus.deadLettersschema. Helm does not upgrade CRDs shipped incrds/, so on upgrade run:Terminal window kubectl apply -f deploy/helm/cubepod/crds/cubepod.io_sandboxwebhooks.yamlWebhooks previously stored in the Alpha file backend (
<state-dir>/events/webhooks.enc.json) are not migrated automatically; re-register them, or pinevents.backend: fileto keep using the file store. -
Downgrade: Disabling
SandboxEventsAndWebhooks(orevents.enabled=false) stops event recording and returns501 Not Implementedon/events,/webhooks, and dead-letter routes. Downgrading to v0.6.x leavesSandboxWebhookCRs and Secrets in place (unused); v0.6.x gateways ignorestatus.deadLetters.
Production Readiness Review Questionnaire (PRR)
Section titled “Production Readiness Review Questionnaire (PRR)”- Feature Enablement and Rollback: Feature gate
SandboxEventsAndWebhooksplusCUBEPOD_EVENTS_BACKEND(k8s|file|disabled). Default isk8swhen the gateway has a Kubernetes client andCUBEPOD_EVENTS_DIRis unset; otherwisefileunder<state-dir>/events(webhooks.enc.json,events.json,dead_letters.json).CUBEPOD_EVENTS_DIR=disabledstill disables. Helm:events.enabled,events.backend,events.dir.CUBEPOD_WEBHOOK_ALLOW_PRIVATE=1is for local test harnesses only. - Rollout, Upgrade and Rollback Planning: Existing sandboxes and PVCs are unaffected.
- Monitoring Requirements:
cubepod_events_recorded_total,cubepod_webhook_deliveries_total{result="ok|failed|dropped"},cubepod_webhook_queue_depth,cubepod_webhook_dead_letters_total,cubepod_webhook_dead_letters_pending.deploy/monitoring/webhook-alerts.yamlships aPrometheusRulewithCubePodWebhookDeliveriesFailed,CubePodWebhookDeliveriesDropped, andCubePodWebhookDeadLettersPending. - Dependencies: Go stdlib
crypto/hmac,crypto/sha256,net; ink8smode, the Kubernetes API server (gateway RBAC onsandboxwebhooks,secrets, andevents). - Scalability: Bounded event retention per tenant, bounded delivery queue, and bounded dead letters (500 per tenant file/memory, 50 per webhook CR) protect gateway memory and etcd object size.
- Troubleshooting: Inspect
cubepod_webhook_deliveries_total,GET /webhooks/dead-letters(sanitizedlastError/lastStatusCode),kubectl get sandboxwebhooks -o yaml(status.consecutiveFailures),kubectl get events -l cubepod.io/resource=sandbox-event, and structured logs (webhookID,eventID,statusCode).
Implementation History
Section titled “Implementation History”2026-11-16: CEP-0028 drafted and markedimplementable.2026-11-16: Alpha implemented inv0.6.0(file backend,/webhooks,/events, SSRF-safe HMAC dispatcher).2026-11-18: Beta and Stable implemented forv0.7.0(GitHub Issue #97):SandboxWebhookCR +Secretbackend, PodBox KubernetesEvents with multi-replica dedupe, controller PodBoxEvents, dead-letter API, new metrics, Helmevents.*values, andPrometheusRulealerts. Stage promoted tostable.
Drawbacks
Section titled “Drawbacks”- The
filebackend remains single-instance and node-local; multi-replica gateways must use thek8sbackend. - Kubernetes
Events expire with the apiserver event TTL (default1h), so/eventshistory ink8smode is short-lived; the in-memory ring is per replica. - Dead letters are bounded (50 per webhook in CR status, 500 per tenant otherwise); sustained receiver outages evict the oldest entries, so alerting on
cubepod_webhook_dead_letters_pendingmatters. - Webhook status writes add API-server traffic proportional to delivery volume.
Alternatives
Section titled “Alternatives”- Synchronous webhook calls inside
POST /sandboxes/DELETE /sandboxes/{id}: Rejected; slow receivers would stall sandbox lifecycle APIs. - External PostgreSQL / Redis database: Rejected per CEP-0022; CubePod avoids extra stateful database dependencies by using
SandboxWebhookCRDs (secretReffor HMAC keys) and KubernetesEvents onPodBox. - Storing entire webhook configs inside Kubernetes
Secrets: Rejected;Secrets are meant for credentials (signatureKey), whereas aSandboxWebhookCRD gives clean schema validation,kubectlvisibility, andStatustracking.