feat: admins suspend or delete status pages, organizations and accounts
The admin panel lists every published status page, and every suspended page, organization or account, with the organization and owner behind it, searchable by title, address, organization or email. Each can be suspended with a reason, reinstated, or deleted. A suspended page is not served; status page servers drop it when they next ask. A suspended organization's monitors leave the schedule, its pages go offline, heartbeat pings and Alertmanager webhooks are no longer recorded, and its members and API tokens get 403 org_suspended; the console explains why. Reinstating restarts heartbeat waits so they do not fire at once. A suspended account is signed out everywhere and can neither sign in nor use its tokens; only someone who knows the password learns of the suspension. Platform admins cannot be suspended or deleted. Account deletion shares its logic with the GDPR erasure, including the refusal while the account owns shared organizations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
db37060c70
commit
90e74d091a
37 files changed
+3109
-1140
No files matched your search
@@ -123,6 +123,9 @@ func (s *Server) resolvePrincipal(c *gin.Context) (*principal, int, string, erro
|
||||
if err != nil {
|
||||
return nil, 0, "", err
|
||||
}
|
||||
if p.Org.SuspendedAt != nil {
|
||||
return nil, http.StatusForbidden, "org_suspended", nil
|
||||
}
|
||||
if code := policyViolation(p.Org, p.Grants.Owner, sess); code != "" {
|
||||
return nil, http.StatusForbidden, code, nil
|
||||
}
|
||||
|
||||
@@ -241,6 +241,23 @@ var endpoints = []endpoint{
|
||||
{method: "PUT", path: "/api/v1/admin/checker-network", tag: "Platform", summary: "Let platform checkers check private addresses or not",
|
||||
description: "Unset, they may on self-hosted installations and may not in the cloud edition. Applies from each checker's next poll.",
|
||||
perm: "platform admin", req: checkerNetwork{}, status: http.StatusNoContent},
|
||||
{method: "GET", path: "/api/v1/admin/status-pages", tag: "Platform", summary: "Public and suspended status pages with organization and owner",
|
||||
description: "Newest first, at most 500. `q` searches title, slug, domain, organization and owner email.",
|
||||
perm: "platform admin", resp: obj{"status_pages": []moderatedPage{}}},
|
||||
{method: "PUT", path: "/api/v1/admin/status-pages/:id/suspension", tag: "Platform", summary: "Suspend or reinstate a status page",
|
||||
description: "A suspended page is not served; status page servers drop it when they next ask for it.",
|
||||
perm: "platform admin", req: suspensionRequest{}, status: http.StatusNoContent},
|
||||
{method: "DELETE", path: "/api/v1/admin/status-pages/:id", tag: "Platform", summary: "Delete a status page", perm: "platform admin", status: http.StatusNoContent},
|
||||
{method: "PUT", path: "/api/v1/admin/orgs/:id/suspension", tag: "Platform", summary: "Suspend or reinstate an organization",
|
||||
description: "A suspended organization's monitors are not checked, its status pages are not served, heartbeats and Alertmanager webhooks are refused, and its members and API tokens get 403 org_suspended.",
|
||||
perm: "platform admin", req: suspensionRequest{}, status: http.StatusNoContent},
|
||||
{method: "DELETE", path: "/api/v1/admin/orgs/:id", tag: "Platform", summary: "Delete an organization with all its data", perm: "platform admin", status: http.StatusNoContent},
|
||||
{method: "PUT", path: "/api/v1/admin/users/:id/suspension", tag: "Platform", summary: "Suspend or reinstate an account",
|
||||
description: "A suspended account is signed out everywhere and can neither sign in nor use its API tokens. Platform admins cannot be suspended.",
|
||||
perm: "platform admin", req: suspensionRequest{}, status: http.StatusNoContent},
|
||||
{method: "DELETE", path: "/api/v1/admin/users/:id", tag: "Platform", summary: "Delete an account",
|
||||
description: "Organizations only the account belongs to are deleted with it; owning one shared with others gives 409 owns_shared_orgs. Platform admins cannot be deleted.",
|
||||
perm: "platform admin", status: http.StatusNoContent},
|
||||
{method: "GET", path: "/api/v1/admin/regions", tag: "Platform", summary: "List platform regions", perm: "platform admin",
|
||||
resp: obj{"regions": []regionView{}}},
|
||||
{method: "POST", path: "/api/v1/admin/regions", tag: "Platform", summary: "Create a platform region", perm: "platform admin",
|
||||
|
||||
+16
-10
@@ -89,16 +89,22 @@ var auditActions = map[string]string{
|
||||
"POST /api/v1/org/tokens": "token.create",
|
||||
"DELETE /api/v1/org/tokens/:id": "token.delete",
|
||||
|
||||
"PUT /api/v1/admin/legal/:kind": "platform.legal_update",
|
||||
"PUT /api/v1/org/legal/:kind": "org.legal_update",
|
||||
"PUT /api/v1/admin/check-types": "platform.check_types_update",
|
||||
"PUT /api/v1/admin/checker-network": "platform.checker_network_update",
|
||||
"PUT /api/v1/org/check-types": "org.check_types_update",
|
||||
"PATCH /api/v1/org/agents/:id": "agent.update",
|
||||
"POST /api/v1/admin/regions": "platform.region_create",
|
||||
"DELETE /api/v1/admin/regions/:id": "platform.region_delete",
|
||||
"POST /api/v1/admin/agents": "platform.agent_create",
|
||||
"DELETE /api/v1/admin/agents/:id": "platform.agent_delete",
|
||||
"PUT /api/v1/admin/legal/:kind": "platform.legal_update",
|
||||
"PUT /api/v1/org/legal/:kind": "org.legal_update",
|
||||
"PUT /api/v1/admin/check-types": "platform.check_types_update",
|
||||
"PUT /api/v1/admin/checker-network": "platform.checker_network_update",
|
||||
"PUT /api/v1/admin/status-pages/:id/suspension": "platform.status_page_suspension",
|
||||
"DELETE /api/v1/admin/status-pages/:id": "platform.status_page_delete",
|
||||
"PUT /api/v1/admin/orgs/:id/suspension": "platform.org_suspension",
|
||||
"DELETE /api/v1/admin/orgs/:id": "platform.org_delete",
|
||||
"PUT /api/v1/admin/users/:id/suspension": "platform.user_suspension",
|
||||
"DELETE /api/v1/admin/users/:id": "platform.user_delete",
|
||||
"PUT /api/v1/org/check-types": "org.check_types_update",
|
||||
"PATCH /api/v1/org/agents/:id": "agent.update",
|
||||
"POST /api/v1/admin/regions": "platform.region_create",
|
||||
"DELETE /api/v1/admin/regions/:id": "platform.region_delete",
|
||||
"POST /api/v1/admin/agents": "platform.agent_create",
|
||||
"DELETE /api/v1/admin/agents/:id": "platform.agent_delete",
|
||||
}
|
||||
|
||||
// Failed attempts of these actions are recorded too.
|
||||
|
||||
+10
-1
@@ -155,10 +155,15 @@ func (s *Server) login(c *gin.Context) {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
if !ok || user.DisabledAt != nil {
|
||||
if !ok {
|
||||
invalidCredentials(c)
|
||||
return
|
||||
}
|
||||
// Only someone who knows the password learns of the suspension.
|
||||
if user.DisabledAt != nil {
|
||||
fail(c, http.StatusForbidden, "account_suspended")
|
||||
return
|
||||
}
|
||||
if rehash {
|
||||
s.rehashPassword(ctx, user.ID, req.Password)
|
||||
}
|
||||
@@ -243,6 +248,10 @@ func (s *Server) finishLogin(c *gin.Context, userID uuid.UUID, method auth.Metho
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
if user.DisabledAt != nil {
|
||||
fail(c, http.StatusForbidden, "account_suspended")
|
||||
return
|
||||
}
|
||||
var org uuid.UUID
|
||||
if len(orgs) > 0 {
|
||||
org = orgs[0].ID
|
||||
|
||||
@@ -819,7 +819,7 @@ func (s *Server) statusPagesView(c *gin.Context, p *principal, page web.Page, me
|
||||
}
|
||||
v.Pages = append(v.Pages, web.PageRow{
|
||||
ID: r.ID.String(), Title: r.Title, Slug: r.Slug, Published: r.Published,
|
||||
URL: s.pageURL(r), Domain: deref(r.CustomDomain),
|
||||
URL: s.pageURL(r), Domain: deref(r.CustomDomain), Suspended: r.SuspendedAt != nil,
|
||||
})
|
||||
}
|
||||
return web.StatusPagesPage(page, me, v), nil
|
||||
@@ -865,6 +865,7 @@ func (s *Server) statusPageEditor(c *gin.Context, p *principal, page web.Page, m
|
||||
e := web.PageEditor{
|
||||
ID: row.ID.String(), Title: row.Title, Slug: row.Slug, Description: row.Description,
|
||||
Published: row.Published, ShowDetails: row.ShowComponentDetails,
|
||||
Suspended: row.SuspendedAt != nil, SuspendedReason: row.SuspendedReason,
|
||||
Accent: v.Theme.Accent, LogoURL: v.Theme.LogoURL, CSS: v.Theme.CSS,
|
||||
URL: s.pageURL(row), Domain: deref(row.CustomDomain), DomainVerified: v.DomainVerified,
|
||||
HasOrgGroups: len(orgGroups) > 0,
|
||||
|
||||
+25
-16
@@ -609,8 +609,28 @@ func (s *Server) deleteAccount(c *gin.Context) {
|
||||
fail(c, http.StatusForbidden, "confirmation_failed")
|
||||
return
|
||||
}
|
||||
var shared []string
|
||||
var orgsDeleted int
|
||||
shared, orgsDeleted, err := s.eraseAccount(ctx, u)
|
||||
if err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
if len(shared) > 0 {
|
||||
c.JSON(http.StatusConflict, gin.H{
|
||||
"error": "owns_shared_orgs", "message": i18n.T(ctx, "error.owns_shared_orgs"), "organizations": shared,
|
||||
})
|
||||
return
|
||||
}
|
||||
auth.ClearCookie(c)
|
||||
// Kept as proof of erasure, without saying whose.
|
||||
s.writeAudit(c, store.InsertAuditEventParams{Kind: "platform", Action: "account.delete", Status: http.StatusOK},
|
||||
map[string]any{"organizations_deleted": orgsDeleted})
|
||||
c.JSON(http.StatusOK, gin.H{"status": "deleted"})
|
||||
}
|
||||
|
||||
// eraseAccount deletes u with the organizations only u belongs to and
|
||||
// ends u's sessions. If u owns organizations shared with others, nothing
|
||||
// is deleted and their slugs are returned.
|
||||
func (s *Server) eraseAccount(ctx context.Context, u store.User) (shared []string, orgsDeleted int, err error) {
|
||||
err = s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||||
owned, err := q.OwnedOrgs(ctx, u.ID)
|
||||
if err != nil {
|
||||
@@ -643,24 +663,13 @@ func (s *Server) deleteAccount(c *gin.Context) {
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
if len(shared) > 0 {
|
||||
c.JSON(http.StatusConflict, gin.H{
|
||||
"error": "owns_shared_orgs", "message": i18n.T(ctx, "error.owns_shared_orgs"), "organizations": shared,
|
||||
})
|
||||
return
|
||||
if err != nil || len(shared) > 0 {
|
||||
return shared, 0, err
|
||||
}
|
||||
if err := s.sessions.DeleteAllForUser(ctx, u.ID); err != nil {
|
||||
s.log.ErrorContext(ctx, "ending sessions of deleted account failed", "error", err)
|
||||
}
|
||||
auth.ClearCookie(c)
|
||||
// Kept as proof of erasure, without saying whose.
|
||||
s.writeAudit(c, store.InsertAuditEventParams{Kind: "platform", Action: "account.delete", Status: http.StatusOK},
|
||||
map[string]any{"organizations_deleted": orgsDeleted})
|
||||
c.JSON(http.StatusOK, gin.H{"status": "deleted"})
|
||||
return nil, orgsDeleted, nil
|
||||
}
|
||||
|
||||
type orgDeletionRequest struct {
|
||||
|
||||
@@ -0,0 +1,307 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/web"
|
||||
)
|
||||
|
||||
// Platform admins moderate public status pages and the organizations and
|
||||
// accounts behind them: suspending hides or stops them and can be undone,
|
||||
// deleting cannot. Suspended pages leave status page servers when these
|
||||
// next ask for them, within half a minute.
|
||||
|
||||
type moderatedPage struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Slug string `json:"slug"`
|
||||
Title string `json:"title"`
|
||||
Published bool `json:"published"`
|
||||
Domain string `json:"custom_domain,omitempty"`
|
||||
SuspendedAt *time.Time `json:"suspended_at"`
|
||||
Reason string `json:"suspended_reason,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Org moderatedOrg `json:"org"`
|
||||
Owner *moderatedUser `json:"owner"`
|
||||
}
|
||||
|
||||
type moderatedOrg struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Slug string `json:"slug"`
|
||||
Name string `json:"name"`
|
||||
SuspendedAt *time.Time `json:"suspended_at"`
|
||||
Reason string `json:"suspended_reason,omitempty"`
|
||||
}
|
||||
|
||||
type moderatedUser struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Email string `json:"email"`
|
||||
SuspendedAt *time.Time `json:"suspended_at"`
|
||||
Reason string `json:"suspended_reason,omitempty"`
|
||||
// Admin marks platform admins, who cannot be suspended or deleted.
|
||||
Admin bool `json:"admin,omitempty"`
|
||||
}
|
||||
|
||||
const moderationRows = 500
|
||||
|
||||
func (s *Server) moderatedPages(c *gin.Context) ([]moderatedPage, error) {
|
||||
ctx := c.Request.Context()
|
||||
var rows []store.AdminListStatusPagesRow
|
||||
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||||
var err error
|
||||
rows, err = q.AdminListStatusPages(ctx, store.AdminListStatusPagesParams{
|
||||
Search: truncate(strings.TrimSpace(c.Query("q")), 200), MaxRows: moderationRows,
|
||||
})
|
||||
return err
|
||||
})
|
||||
out := make([]moderatedPage, len(rows))
|
||||
for i, r := range rows {
|
||||
p := &out[i]
|
||||
p.ID, p.Slug, p.Title, p.Published, p.SuspendedAt, p.Reason, p.CreatedAt = r.ID, r.Slug, r.Title, r.Published, r.SuspendedAt, r.SuspendedReason, r.CreatedAt
|
||||
if r.CustomDomain != nil && r.DomainVerifiedAt != nil {
|
||||
p.Domain = *r.CustomDomain
|
||||
}
|
||||
p.Org = moderatedOrg{r.OrgID, r.OrgSlug, r.OrgName, r.OrgSuspendedAt, r.OrgSuspendedReason}
|
||||
if r.OwnerID != uuid.Nil {
|
||||
p.Owner = &moderatedUser{r.OwnerID, r.OwnerEmail, r.OwnerDisabledAt, r.OwnerDisabledReason, r.OwnerIsAdmin}
|
||||
}
|
||||
}
|
||||
return out, err
|
||||
}
|
||||
|
||||
func (s *Server) adminListStatusPages(c *gin.Context) {
|
||||
pages, err := s.moderatedPages(c)
|
||||
if err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"status_pages": pages})
|
||||
}
|
||||
|
||||
type suspensionRequest struct {
|
||||
Suspended bool `json:"suspended"`
|
||||
Reason string `json:"reason" binding:"max=500"`
|
||||
}
|
||||
|
||||
// suspension binds a suspension request and the target ID.
|
||||
func suspension(c *gin.Context) (uuid.UUID, suspensionRequest, bool) {
|
||||
id, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
fail(c, http.StatusNotFound, "not_found")
|
||||
return uuid.Nil, suspensionRequest{}, false
|
||||
}
|
||||
var req suspensionRequest
|
||||
if !bind(c, &req) {
|
||||
return uuid.Nil, req, false
|
||||
}
|
||||
req.Reason = strings.TrimSpace(req.Reason)
|
||||
auditDetail(c, "suspended", req.Suspended)
|
||||
if req.Reason != "" {
|
||||
auditDetail(c, "reason", req.Reason)
|
||||
}
|
||||
return id, req, true
|
||||
}
|
||||
|
||||
// moderated finishes a moderation request: 404 for unknown targets.
|
||||
func moderated(c *gin.Context, err error) bool {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
fail(c, http.StatusNotFound, "not_found")
|
||||
return false
|
||||
}
|
||||
if err != nil {
|
||||
internalError(c, err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *Server) adminSuspendStatusPage(c *gin.Context) {
|
||||
id, req, ok := suspension(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||||
row, err := q.SuspendStatusPage(ctx, store.SuspendStatusPageParams{ID: id, Suspend: req.Suspended, Reason: req.Reason})
|
||||
auditDetail(c, "slug", row.Slug)
|
||||
return err
|
||||
})
|
||||
if moderated(c, err) {
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) adminDeleteStatusPage(c *gin.Context) {
|
||||
id, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
fail(c, http.StatusNotFound, "not_found")
|
||||
return
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
err = s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||||
page, err := q.GetStatusPage(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
auditDetail(c, "slug", page.Slug)
|
||||
_, err = q.DeleteStatusPage(ctx, id)
|
||||
return err
|
||||
})
|
||||
if moderated(c, err) {
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) adminSuspendOrg(c *gin.Context) {
|
||||
id, req, ok := suspension(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||||
row, err := q.SuspendOrg(ctx, store.SuspendOrgParams{ID: id, Suspend: req.Suspended, Reason: req.Reason})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
auditDetail(c, "slug", row.Slug)
|
||||
if !req.Suspended {
|
||||
return q.RestartHeartbeatWait(ctx, id)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if !moderated(c, err) {
|
||||
return
|
||||
}
|
||||
// Monitors leave or rejoin the schedule with its next reconciliation.
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (s *Server) adminDeleteOrg(c *gin.Context) {
|
||||
id, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
fail(c, http.StatusNotFound, "not_found")
|
||||
return
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
err = s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||||
org, err := q.GetOrg(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
auditDetail(c, "slug", org.Slug)
|
||||
auditDetail(c, "name", org.Name)
|
||||
return purgeOrg(ctx, q, id)
|
||||
})
|
||||
if moderated(c, err) {
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
// moderatedUser loads the target account, refusing platform admins: they
|
||||
// would otherwise lock each other, or themselves, out.
|
||||
func (s *Server) moderatedUser(c *gin.Context) (store.User, bool) {
|
||||
id, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
fail(c, http.StatusNotFound, "not_found")
|
||||
return store.User{}, false
|
||||
}
|
||||
u, err := s.loadUser(c.Request.Context(), id)
|
||||
if !moderated(c, err) {
|
||||
return u, false
|
||||
}
|
||||
auditDetail(c, "email", u.Email)
|
||||
if u.IsAdmin {
|
||||
fail(c, http.StatusConflict, "target_is_admin")
|
||||
return u, false
|
||||
}
|
||||
return u, true
|
||||
}
|
||||
|
||||
func (s *Server) adminSuspendUser(c *gin.Context) {
|
||||
id, req, ok := suspension(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
u, ok := s.moderatedUser(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||||
_, err := q.SuspendUser(ctx, store.SuspendUserParams{ID: id, Suspend: req.Suspended, Reason: req.Reason})
|
||||
return err
|
||||
})
|
||||
if !moderated(c, err) {
|
||||
return
|
||||
}
|
||||
if req.Suspended {
|
||||
if err := s.sessions.DeleteAllForUser(ctx, u.ID); err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (s *Server) adminDeleteUser(c *gin.Context) {
|
||||
u, ok := s.moderatedUser(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
shared, orgsDeleted, err := s.eraseAccount(ctx, u)
|
||||
if err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
if len(shared) > 0 {
|
||||
c.JSON(http.StatusConflict, gin.H{
|
||||
"error": "owns_shared_orgs", "message": i18n.T(ctx, "error.user_owns_shared_orgs"), "organizations": shared,
|
||||
})
|
||||
return
|
||||
}
|
||||
auditDetail(c, "organizations_deleted", orgsDeleted)
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (s *Server) moderationView(c *gin.Context) (web.ModerationView, error) {
|
||||
pages, err := s.moderatedPages(c)
|
||||
v := web.ModerationView{Search: strings.TrimSpace(c.Query("q")), Limited: len(pages) == moderationRows}
|
||||
state := func(at *time.Time, reason string) string {
|
||||
switch {
|
||||
case at == nil:
|
||||
return ""
|
||||
case reason == "":
|
||||
return "-"
|
||||
}
|
||||
return reason
|
||||
}
|
||||
for _, p := range pages {
|
||||
u := strings.TrimSuffix(s.cfg.StatusPageURL, "/") + "/" + p.Slug
|
||||
if p.Domain != "" {
|
||||
u = "https://" + p.Domain + "/"
|
||||
}
|
||||
mp := web.ModeratedPage{
|
||||
ID: p.ID.String(), Title: p.Title, Slug: p.Slug, URL: u, Created: p.CreatedAt.Format(time.DateOnly),
|
||||
Published: p.Published, Suspended: state(p.SuspendedAt, p.Reason),
|
||||
Org: web.ModeratedTarget{ID: p.Org.ID.String(), Name: p.Org.Name, Detail: p.Org.Slug, Suspended: state(p.Org.SuspendedAt, p.Org.Reason)},
|
||||
}
|
||||
if p.Domain != "" {
|
||||
mp.Slug = p.Domain
|
||||
}
|
||||
if p.Owner != nil {
|
||||
mp.Owner = &web.ModeratedTarget{ID: p.Owner.ID.String(), Name: p.Owner.Email, Suspended: state(p.Owner.SuspendedAt, p.Owner.Reason), Admin: p.Owner.Admin}
|
||||
}
|
||||
v.Pages = append(v.Pages, mp)
|
||||
}
|
||||
return v, err
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
package api_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
||||
)
|
||||
|
||||
const testPassword = "correct horse battery"
|
||||
|
||||
func TestModeration(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
ctx := context.Background()
|
||||
admin := e.browser(t)
|
||||
admin.signup("admin@"+uniq("mod")+".test", uniq("mod-admin-"))
|
||||
if err := tenant.New(e.dbPool).Tx(ctx, tenant.Global(), func(q *store.Queries) error {
|
||||
return q.MakeAdmin(ctx, admin.userIDSelf())
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ownerEmail := "owner@" + uniq("mod") + ".test"
|
||||
owner := e.browser(t)
|
||||
owner.signup(ownerEmail, uniq("mod-org-"))
|
||||
slug := uniq("acme-")
|
||||
_, page := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": slug, "title": "Acme Status", "published": true})
|
||||
_, draft := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": uniq("draft-"), "title": "Draft", "published": false})
|
||||
_, hb := owner.do("POST", "/api/v1/org/monitors", map[string]any{"name": "Backup", "type": "heartbeat", "interval_seconds": 3600})
|
||||
_, region := e.platformChecker(t, uniq("eu-"))
|
||||
_, web := owner.do("POST", "/api/v1/org/monitors", map[string]any{"name": "Web", "type": "tcp", "target": "example.com:443", "interval_seconds": 60, "regions": []string{region}})
|
||||
scheduled := func() bool {
|
||||
var rows []store.ListEnabledMonitorsForScheduleRow
|
||||
_ = tenant.New(e.dbPool).Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
||||
var err error
|
||||
rows, err = q.ListEnabledMonitorsForSchedule(ctx)
|
||||
return err
|
||||
})
|
||||
for _, r := range rows {
|
||||
if r.ID.String() == web["id"] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
_, tok := owner.do("POST", "/api/v1/org/tokens", map[string]any{"name": "ci", "permissions": []string{"monitors.read"}})
|
||||
api := &browser{t: t, base: owner.base, hc: e.srv.Client(), bearer: tok["token"].(string)}
|
||||
sp := e.statuspageClient(t, nil)
|
||||
served := func() bool {
|
||||
_, err := sp.Page(ctx, "", slug)
|
||||
if err != nil && !errors.Is(err, agentapi.ErrNotFound) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return err == nil
|
||||
}
|
||||
// Pings of paused monitors are answered, so cron jobs do not fail, but
|
||||
// not recorded.
|
||||
recorded := func() bool {
|
||||
resp, err := e.srv.Client().Post(e.srv.URL+"/hb/"+hb["target"].(string), "text/plain", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
return resp.StatusCode == http.StatusOK && !strings.Contains(string(b), "paused")
|
||||
}
|
||||
|
||||
// Only admins see the list; it holds published pages with their
|
||||
// organization and owner, and search narrows it.
|
||||
if st, _ := owner.do("GET", "/api/v1/admin/status-pages", nil); st != http.StatusForbidden {
|
||||
t.Errorf("non-admin listing = %d", st)
|
||||
}
|
||||
_, list := admin.do("GET", "/api/v1/admin/status-pages?q="+slug, nil)
|
||||
raw := mustJSON(list)
|
||||
if !strings.Contains(raw, slug) || !strings.Contains(raw, ownerEmail) || strings.Contains(raw, draft["slug"].(string)) {
|
||||
t.Fatalf("list = %s", raw)
|
||||
}
|
||||
pg := list["status_pages"].([]any)[0].(map[string]any)
|
||||
orgID := pg["org"].(map[string]any)["id"].(string)
|
||||
userID := pg["owner"].(map[string]any)["id"].(string)
|
||||
if _, other := admin.do("GET", "/api/v1/admin/status-pages?q=no-such-page-"+slug, nil); len(other["status_pages"].([]any)) != 0 {
|
||||
t.Error("search does not narrow the list")
|
||||
}
|
||||
if !served() {
|
||||
t.Fatal("the page is not served before suspension")
|
||||
}
|
||||
|
||||
// A suspended page goes offline and its organization learns why.
|
||||
suspend := func(path string, on bool, reason string) {
|
||||
t.Helper()
|
||||
if st, body := admin.do("PUT", "/api/v1/admin/"+path+"/suspension", map[string]any{"suspended": on, "reason": reason}); st != http.StatusNoContent {
|
||||
t.Fatalf("suspend %s = %d %v", path, st, body)
|
||||
}
|
||||
}
|
||||
suspend("status-pages/"+page["id"].(string), true, "phishing")
|
||||
if served() {
|
||||
t.Error("a suspended page is served")
|
||||
}
|
||||
_, pages := owner.do("GET", "/api/v1/org/statuspages", nil)
|
||||
if raw := mustJSON(pages); !strings.Contains(raw, `"suspended":true`) || !strings.Contains(raw, `"suspended_reason":"phishing"`) {
|
||||
t.Errorf("the organization does not see the suspension: %s", raw)
|
||||
}
|
||||
if _, _, html := owner.page("/statuspages"); !strings.Contains(html, "suspended by the operator") {
|
||||
t.Error("the page list does not mark the suspension")
|
||||
}
|
||||
suspend("status-pages/"+page["id"].(string), false, "")
|
||||
if !served() {
|
||||
t.Error("a reinstated page is not served")
|
||||
}
|
||||
|
||||
// A suspended organization is locked out, checks and pages stop.
|
||||
suspend("orgs/"+orgID, true, "unpaid")
|
||||
st, body := owner.do("GET", "/api/v1/org/monitors", nil)
|
||||
expect(t, "member of a suspended org", st, body, http.StatusForbidden, "org_suspended")
|
||||
st, body = api.do("GET", "/api/v1/org/monitors", nil)
|
||||
expect(t, "token of a suspended org", st, body, http.StatusForbidden, "org_suspended")
|
||||
if served() {
|
||||
t.Error("a suspended organization's page is served")
|
||||
}
|
||||
if recorded() {
|
||||
t.Error("a heartbeat of a suspended org is recorded")
|
||||
}
|
||||
if scheduled() {
|
||||
t.Error("monitors of a suspended org are scheduled")
|
||||
}
|
||||
if _, _, html := owner.page("/"); !strings.Contains(html, "The operator suspended this organization") || !strings.Contains(html, "unpaid") {
|
||||
t.Error("the console does not explain the suspension")
|
||||
}
|
||||
suspend("orgs/"+orgID, false, "")
|
||||
if st, _ := owner.do("GET", "/api/v1/org/monitors", nil); st != http.StatusOK || !scheduled() {
|
||||
t.Errorf("member of a reinstated org = %d, scheduled = %v", st, scheduled())
|
||||
}
|
||||
if !recorded() {
|
||||
t.Error("a heartbeat after reinstatement is not recorded")
|
||||
}
|
||||
|
||||
// A suspended account is signed out and kept out, tokens included.
|
||||
suspend("users/"+userID, true, "")
|
||||
if st, _ := owner.do("GET", "/api/v1/me", nil); st != http.StatusUnauthorized {
|
||||
t.Errorf("session of a suspended account = %d", st)
|
||||
}
|
||||
if st, _ := api.do("GET", "/api/v1/org/monitors", nil); st != http.StatusUnauthorized {
|
||||
t.Errorf("token of a suspended account = %d", st)
|
||||
}
|
||||
fresh := e.browser(t)
|
||||
st, body = fresh.do("POST", "/api/v1/auth/login", map[string]any{"email": ownerEmail, "password": testPassword})
|
||||
expect(t, "sign-in of a suspended account", st, body, http.StatusForbidden, "account_suspended")
|
||||
if st, _ := e.browser(t).do("POST", "/api/v1/auth/login", map[string]any{"email": ownerEmail, "password": "wrong password"}); st != http.StatusUnauthorized {
|
||||
t.Error("a wrong password reveals the suspension")
|
||||
}
|
||||
suspend("users/"+userID, false, "")
|
||||
if st, _ := fresh.do("POST", "/api/v1/auth/login", map[string]any{"email": ownerEmail, "password": testPassword}); st != http.StatusOK {
|
||||
t.Errorf("sign-in after reinstatement = %d", st)
|
||||
}
|
||||
|
||||
// Admins are out of reach, also for themselves.
|
||||
st, body = admin.do("PUT", "/api/v1/admin/users/"+admin.userIDSelf().String()+"/suspension", map[string]any{"suspended": true})
|
||||
expect(t, "suspend an admin", st, body, http.StatusConflict, "target_is_admin")
|
||||
st, body = admin.do("DELETE", "/api/v1/admin/users/"+admin.userIDSelf().String(), nil)
|
||||
expect(t, "delete an admin", st, body, http.StatusConflict, "target_is_admin")
|
||||
|
||||
// An owner of a shared organization cannot be deleted before it.
|
||||
fresh.do("POST", "/api/v1/me/org", map[string]any{"org_id": orgID})
|
||||
e.joinVia(t, fresh.invite("m@"+uniq("mod")+".test", fresh.roleID("viewer")))
|
||||
st, body = admin.do("DELETE", "/api/v1/admin/users/"+userID, nil)
|
||||
expect(t, "delete the owner of a shared org", st, body, http.StatusConflict, "owns_shared_orgs")
|
||||
|
||||
// Deleting: page, organization, account.
|
||||
if st, _ := admin.do("DELETE", "/api/v1/admin/status-pages/"+page["id"].(string), nil); st != http.StatusNoContent || served() {
|
||||
t.Errorf("delete page = %d", st)
|
||||
}
|
||||
if st, _ := admin.do("DELETE", "/api/v1/admin/orgs/"+orgID, nil); st != http.StatusNoContent {
|
||||
t.Errorf("delete org = %d", st)
|
||||
}
|
||||
if st, body := admin.do("DELETE", "/api/v1/admin/users/"+userID, nil); st != http.StatusNoContent {
|
||||
t.Errorf("delete account = %d %v", st, body)
|
||||
}
|
||||
if st, _ := e.browser(t).do("POST", "/api/v1/auth/login", map[string]any{"email": ownerEmail, "password": testPassword}); st != http.StatusUnauthorized {
|
||||
t.Error("the deleted account can still sign in")
|
||||
}
|
||||
if st, _ := admin.do("DELETE", "/api/v1/admin/orgs/"+orgID, nil); st != http.StatusNotFound {
|
||||
t.Error("deleting a deleted org is not 404")
|
||||
}
|
||||
|
||||
// The admin page shows the list.
|
||||
if _, _, html := admin.page("/admin"); !strings.Contains(html, `id="moderation"`) {
|
||||
t.Error("the admin page lacks the status page list")
|
||||
}
|
||||
}
|
||||
+11
-2
@@ -50,7 +50,7 @@ var flashErrors = map[ssoError]bool{
|
||||
"sso_unavailable": true, "sso_provider_error": true, "sso_expired": true,
|
||||
"sso_denied": true, "sso_not_member": true, "sso_not_provisioned": true,
|
||||
"sso_link_required": true, "sso_identity_taken": true, "invalid_credentials": true,
|
||||
"totp_required_by_org": true, "sso_required_by_org": true,
|
||||
"totp_required_by_org": true, "sso_required_by_org": true, "account_suspended": true,
|
||||
}
|
||||
|
||||
func (s *Server) pageFor(c *gin.Context, titleID string) web.Page {
|
||||
@@ -134,6 +134,10 @@ func (s *Server) consoleMe(c *gin.Context, sess *auth.Session) (web.Me, *princip
|
||||
if err != nil || !member {
|
||||
return me, nil, err
|
||||
}
|
||||
if p.Org.SuspendedAt != nil {
|
||||
me.OrgSuspended, me.SuspendedReason = true, p.Org.SuspendedReason
|
||||
return me, nil, nil
|
||||
}
|
||||
for _, perm := range authz.All {
|
||||
me.Can[string(perm)] = p.Grants.Can(perm)
|
||||
}
|
||||
@@ -502,7 +506,12 @@ func (s *Server) adminPage(c *gin.Context) {
|
||||
for _, t := range check.Types {
|
||||
types = append(types, web.CheckTypeOption{Type: string(t), Enabled: !off[string(t)]})
|
||||
}
|
||||
web.Render(c, http.StatusOK, web.AdminPage(s.pageFor(c, "admin.title"), me, v, storageView(c, stats), types, s.platformPrivate(ctx)))
|
||||
mod, err := s.moderationView(c)
|
||||
if err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
web.Render(c, http.StatusOK, web.AdminPage(s.pageFor(c, "admin.title"), me, v, storageView(c, stats), types, s.platformPrivate(ctx), mod))
|
||||
}
|
||||
|
||||
func (s *Server) buildRegionsView(ctx context.Context, regions []store.Region, agents []store.ListAgentsRow, apiBase, next string, manageable func(store.Region) bool) (web.RegionsView, error) {
|
||||
|
||||
@@ -167,6 +167,13 @@ func (s *Server) Mount(r gin.IRouter) {
|
||||
admin.GET("/check-types", s.adminListCheckTypes)
|
||||
admin.PUT("/check-types", s.adminSetCheckTypes)
|
||||
admin.GET("/checker-network", s.adminGetCheckerNetwork)
|
||||
admin.GET("/status-pages", s.adminListStatusPages)
|
||||
admin.PUT("/status-pages/:id/suspension", s.adminSuspendStatusPage)
|
||||
admin.DELETE("/status-pages/:id", s.adminDeleteStatusPage)
|
||||
admin.PUT("/orgs/:id/suspension", s.adminSuspendOrg)
|
||||
admin.DELETE("/orgs/:id", s.adminDeleteOrg)
|
||||
admin.PUT("/users/:id/suspension", s.adminSuspendUser)
|
||||
admin.DELETE("/users/:id", s.adminDeleteUser)
|
||||
admin.PUT("/checker-network", s.adminSetCheckerNetwork)
|
||||
admin.GET("/legal", s.adminListLegal)
|
||||
admin.PUT("/legal/:kind", s.adminSaveLegal)
|
||||
|
||||
+1
-1
@@ -255,7 +255,7 @@ func (s *Server) ssoCallback(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if user.DisabledAt != nil {
|
||||
s.ssoAbort(c, uuid.Nil, "invalid_credentials")
|
||||
s.ssoAbort(c, uuid.Nil, "account_suspended")
|
||||
return
|
||||
}
|
||||
if old := auth.Current(c); old != nil {
|
||||
|
||||
@@ -392,6 +392,10 @@ type statusPageView struct {
|
||||
DomainVerified bool `json:"domain_verified"`
|
||||
// DomainRecord is the TXT record that proves control of CustomDomain.
|
||||
DomainRecord *dnsRecord `json:"domain_record,omitempty"`
|
||||
// Suspended is set when platform admins took the page offline; it is
|
||||
// not served whatever Published says.
|
||||
Suspended bool `json:"suspended,omitempty"`
|
||||
SuspendedReason string `json:"suspended_reason,omitempty"`
|
||||
}
|
||||
|
||||
type dnsRecord struct {
|
||||
@@ -405,6 +409,7 @@ func viewStatusPage(p store.StatusPage) statusPageView {
|
||||
ID: p.ID, Slug: p.Slug, Title: p.Title, Description: p.Description,
|
||||
Published: p.Published, ShowComponentDetails: p.ShowComponentDetails,
|
||||
CustomDomain: p.CustomDomain, DomainVerified: p.DomainVerifiedAt != nil,
|
||||
Suspended: p.SuspendedAt != nil, SuspendedReason: p.SuspendedReason,
|
||||
}
|
||||
_ = json.Unmarshal(p.Theme, &v.Theme)
|
||||
if p.CustomDomain != nil {
|
||||
|
||||
@@ -79,6 +79,16 @@ func (s *Server) tokenPrincipal(ctx context.Context, tok string) (*principal, in
|
||||
if err != nil {
|
||||
return nil, 0, "", err
|
||||
}
|
||||
if p.Org.SuspendedAt != nil {
|
||||
return nil, http.StatusForbidden, "org_suspended", nil
|
||||
}
|
||||
// Tokens act for their creator and stop with the creator's account.
|
||||
if u, err := s.loadUser(ctx, row.UserID); err != nil || u.DisabledAt != nil {
|
||||
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, 0, "", err
|
||||
}
|
||||
return nil, http.StatusUnauthorized, "unauthenticated", nil
|
||||
}
|
||||
p.UserID, p.TokenID = row.UserID, row.ID
|
||||
p.Grants = p.Grants.Restrict(authz.NewSet(row.Permissions...))
|
||||
return p, 0, "", nil
|
||||
|
||||
Reference in new issue
Block a user