feat: backend half of the admin panel upgrade: delete the unrouted provisioning, releases, plan, discount and enterprise-inquiry admin handlers with their service and repository methods, retire the six admin permission bits nothing gated as reserved placeholders so live bit positions and existing masks are unchanged and IsSuperAdmin checks the live set, add forty admin endpoints for mailbox sync governor state with clear-throttle and restart-backfill, in-flight send reservations, cross-workspace dead letters with replay, task failures, webhook delivery health with reclaim, fleet capacity, the control loops decision log, dedicated bindings with release and the routed convert-to-dedicated, operator-driven workspace export and import, per-organization API keys and webhooks, warmup invalid-token abuse and action history, and signups by acquisition channel, and add a scheduled_job_runs table (migration 000135) with a jobrun package that every backend and consumer loop now records through and a run-now request the owning process picks up within fifteen seconds
2026-09-07 21:40:38 -07:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/warmbly/warmbly/internal/errx"
|
|
|
|
|
"github.com/warmbly/warmbly/internal/models"
|
|
|
|
|
"github.com/warmbly/warmbly/internal/repository"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
auditActionSyncClearThrottle models.AuditAction = "sync_clear_throttle"
|
|
|
|
|
auditActionSyncRestartBackfill models.AuditAction = "sync_restart_backfill"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// AdminSearchSync is GET /admin/sync: every mailbox's sync governor state.
|
|
|
|
|
func (h *Handler) AdminSearchSync(c *gin.Context) {
|
|
|
|
|
if h.AdminSyncRepo == nil {
|
feat: complete the ADA CASA v2.1.1 AL1 control set across authentication, sessions, access control, cryptography, input validation and configuration, adding a breached-password denylist and per-account login throttling, enforced multi-factor authentication on the admin panel, step-up confirmation before an action that mints a lasting credential, purpose-scoped session tokens, single-use TOTP steps, tenant verification on every cross-referenced identifier, security headers on every surface, encrypted webhook signing secrets, per-organization idempotency, PKCE and a minimal two-scope Gmail consent on the mailbox OAuth flow, bounded spreadsheet and archive decoding, a patched Go toolchain with govulncheck in CI, and the evidence pack under compliance/casa
2026-09-19 08:18:35 +02:00
|
|
|
errx.JSON(c, errx.New(errx.NotImplemented, "sync operations are not available on this instance"))
|
feat: backend half of the admin panel upgrade: delete the unrouted provisioning, releases, plan, discount and enterprise-inquiry admin handlers with their service and repository methods, retire the six admin permission bits nothing gated as reserved placeholders so live bit positions and existing masks are unchanged and IsSuperAdmin checks the live set, add forty admin endpoints for mailbox sync governor state with clear-throttle and restart-backfill, in-flight send reservations, cross-workspace dead letters with replay, task failures, webhook delivery health with reclaim, fleet capacity, the control loops decision log, dedicated bindings with release and the routed convert-to-dedicated, operator-driven workspace export and import, per-organization API keys and webhooks, warmup invalid-token abuse and action history, and signups by acquisition channel, and add a scheduled_job_runs table (migration 000135) with a jobrun package that every backend and consumer loop now records through and a run-now request the owning process picks up within fifteen seconds
2026-09-07 21:40:38 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var search models.AdminSyncSearch
|
|
|
|
|
if err := c.ShouldBindQuery(&search); err != nil {
|
|
|
|
|
errx.JSON(c, errx.New(errx.BadRequest, "invalid query parameters"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
result, err := h.AdminSyncRepo.Search(c.Request.Context(), &search)
|
|
|
|
|
if err != nil {
|
|
|
|
|
switch {
|
|
|
|
|
case errors.Is(err, repository.ErrAdminSyncBadCursor):
|
|
|
|
|
errx.JSON(c, errx.New(errx.BadRequest, "invalid cursor"))
|
|
|
|
|
case errors.Is(err, repository.ErrAdminSyncBadState):
|
|
|
|
|
errx.JSON(c, errx.New(errx.BadRequest, "invalid state filter"))
|
|
|
|
|
default:
|
|
|
|
|
errx.JSON(c, errx.New(errx.Internal, "failed to load sync state"))
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AdminSyncClearThrottle is POST /admin/sync/:id/clear-throttle.
|
|
|
|
|
func (h *Handler) AdminSyncClearThrottle(c *gin.Context) {
|
|
|
|
|
if h.AdminSyncRepo == nil {
|
feat: complete the ADA CASA v2.1.1 AL1 control set across authentication, sessions, access control, cryptography, input validation and configuration, adding a breached-password denylist and per-account login throttling, enforced multi-factor authentication on the admin panel, step-up confirmation before an action that mints a lasting credential, purpose-scoped session tokens, single-use TOTP steps, tenant verification on every cross-referenced identifier, security headers on every surface, encrypted webhook signing secrets, per-organization idempotency, PKCE and a minimal two-scope Gmail consent on the mailbox OAuth flow, bounded spreadsheet and archive decoding, a patched Go toolchain with govulncheck in CI, and the evidence pack under compliance/casa
2026-09-19 08:18:35 +02:00
|
|
|
errx.JSON(c, errx.New(errx.NotImplemented, "sync operations are not available on this instance"))
|
feat: backend half of the admin panel upgrade: delete the unrouted provisioning, releases, plan, discount and enterprise-inquiry admin handlers with their service and repository methods, retire the six admin permission bits nothing gated as reserved placeholders so live bit positions and existing masks are unchanged and IsSuperAdmin checks the live set, add forty admin endpoints for mailbox sync governor state with clear-throttle and restart-backfill, in-flight send reservations, cross-workspace dead letters with replay, task failures, webhook delivery health with reclaim, fleet capacity, the control loops decision log, dedicated bindings with release and the routed convert-to-dedicated, operator-driven workspace export and import, per-organization API keys and webhooks, warmup invalid-token abuse and action history, and signups by acquisition channel, and add a scheduled_job_runs table (migration 000135) with a jobrun package that every backend and consumer loop now records through and a run-now request the owning process picks up within fifteen seconds
2026-09-07 21:40:38 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.JSON(c, errx.New(errx.BadRequest, "invalid mailbox ID"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
cleared, err := h.AdminSyncRepo.ClearThrottle(c.Request.Context(), id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.JSON(c, errx.New(errx.Internal, "failed to clear throttle"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if !cleared {
|
|
|
|
|
errx.JSON(c, errx.New(errx.NotFound, "mailbox has no sync state or is not throttled"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
resp := gin.H{"email_id": id, "cleared": true}
|
|
|
|
|
h.reloadSyncedMailbox(c, id, resp)
|
|
|
|
|
h.audit(c, auditActionSyncClearThrottle, models.AuditEntityEmailAccount, &id, map[string]string{
|
|
|
|
|
"reloaded": boolString(resp["reloaded"].(bool)),
|
|
|
|
|
})
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AdminSyncRestartBackfill is POST /admin/sync/:id/restart-backfill.
|
|
|
|
|
func (h *Handler) AdminSyncRestartBackfill(c *gin.Context) {
|
|
|
|
|
if h.AdminSyncRepo == nil {
|
feat: complete the ADA CASA v2.1.1 AL1 control set across authentication, sessions, access control, cryptography, input validation and configuration, adding a breached-password denylist and per-account login throttling, enforced multi-factor authentication on the admin panel, step-up confirmation before an action that mints a lasting credential, purpose-scoped session tokens, single-use TOTP steps, tenant verification on every cross-referenced identifier, security headers on every surface, encrypted webhook signing secrets, per-organization idempotency, PKCE and a minimal two-scope Gmail consent on the mailbox OAuth flow, bounded spreadsheet and archive decoding, a patched Go toolchain with govulncheck in CI, and the evidence pack under compliance/casa
2026-09-19 08:18:35 +02:00
|
|
|
errx.JSON(c, errx.New(errx.NotImplemented, "sync operations are not available on this instance"))
|
feat: backend half of the admin panel upgrade: delete the unrouted provisioning, releases, plan, discount and enterprise-inquiry admin handlers with their service and repository methods, retire the six admin permission bits nothing gated as reserved placeholders so live bit positions and existing masks are unchanged and IsSuperAdmin checks the live set, add forty admin endpoints for mailbox sync governor state with clear-throttle and restart-backfill, in-flight send reservations, cross-workspace dead letters with replay, task failures, webhook delivery health with reclaim, fleet capacity, the control loops decision log, dedicated bindings with release and the routed convert-to-dedicated, operator-driven workspace export and import, per-organization API keys and webhooks, warmup invalid-token abuse and action history, and signups by acquisition channel, and add a scheduled_job_runs table (migration 000135) with a jobrun package that every backend and consumer loop now records through and a run-now request the owning process picks up within fifteen seconds
2026-09-07 21:40:38 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.JSON(c, errx.New(errx.BadRequest, "invalid mailbox ID"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
reset, err := h.AdminSyncRepo.ResetBackfill(c.Request.Context(), id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.JSON(c, errx.New(errx.Internal, "failed to reset backfill"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if !reset {
|
|
|
|
|
errx.JSON(c, errx.New(errx.NotFound, "mailbox has no sync state"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
resp := gin.H{"email_id": id, "reset": true}
|
|
|
|
|
h.reloadSyncedMailbox(c, id, resp)
|
|
|
|
|
h.audit(c, auditActionSyncRestartBackfill, models.AuditEntityEmailAccount, &id, map[string]string{
|
|
|
|
|
"reloaded": boolString(resp["reloaded"].(bool)),
|
|
|
|
|
})
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// reloadSyncedMailbox re-ships the mailbox so the worker's live copy follows
|
|
|
|
|
// the platform copy; a failure is reported in the response, not as an error.
|
|
|
|
|
func (h *Handler) reloadSyncedMailbox(c *gin.Context, id uuid.UUID, resp gin.H) {
|
|
|
|
|
resp["reloaded"] = false
|
|
|
|
|
if h.EmailService == nil {
|
|
|
|
|
resp["reload_error"] = "email service not configured"
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := h.EmailService.LoadAccountOntoWorker(c.Request.Context(), id); err != nil {
|
|
|
|
|
resp["reload_error"] = err.Error()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
resp["reloaded"] = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func boolString(b bool) string {
|
|
|
|
|
if b {
|
|
|
|
|
return "true"
|
|
|
|
|
}
|
|
|
|
|
return "false"
|
|
|
|
|
}
|