feat(api): organize monitors in groups with inherited permissions

Assignments scoped to a group cover all monitors and subgroups below it,
so people can be given exactly one branch of the tree. Deleting a group
moves its content one level up and drops assignments scoped to it.

Refs #57

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 09:19:55 +01:00
1 parent 2d7f326d38
commit 11482ec6d5
4 files changed
+497 -21

No files matched your search

+82 -16
View File
@@ -13,6 +13,7 @@ import (
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"git.zb-server.de/ZB-Server/uptimesebi/internal/authz"
"git.zb-server.de/ZB-Server/uptimesebi/internal/check"
"git.zb-server.de/ZB-Server/uptimesebi/internal/schedule"
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
@@ -31,6 +32,8 @@ type monitorRequest struct {
// Confirmations is how many failures in a row make a region count as
// down; 0 keeps the default of 2.
Confirmations int `json:"confirmations" binding:"min=0,max=10"`
// GroupID places the monitor in a group; nil keeps it at the top level.
GroupID *uuid.UUID `json:"group_id"`
}
type latestView struct {
@@ -52,6 +55,7 @@ type monitorView struct {
Config json.RawMessage `json:"config"`
Enabled bool `json:"enabled"`
Confirmations int32 `json:"confirmations"`
GroupID *uuid.UUID `json:"group_id"`
Regions []uuid.UUID `json:"regions"`
Latest []latestView `json:"latest"`
}
@@ -59,7 +63,7 @@ type monitorView struct {
func viewMonitor(m store.Monitor, regions []uuid.UUID) monitorView {
return monitorView{
ID: m.ID, Name: m.Name, Type: m.Type, Target: m.Target, IntervalSeconds: m.IntervalSeconds,
TimeoutMS: m.TimeoutMs, Config: m.Config, Enabled: m.Enabled, Confirmations: m.Confirmations, Regions: regions, Latest: []latestView{},
TimeoutMS: m.TimeoutMs, Config: m.Config, Enabled: m.Enabled, Confirmations: m.Confirmations, GroupID: m.GroupID, Regions: regions, Latest: []latestView{},
}
}
@@ -72,8 +76,19 @@ func (s *Server) minInterval() int {
}
// validateMonitor normalizes req and writes an error response if it is not
// acceptable.
func (s *Server) validateMonitor(c *gin.Context, req *monitorRequest) bool {
// acceptable, including when the principal may not write monitors in the
// requested group.
func (s *Server) validateMonitor(c *gin.Context, req *monitorRequest, t groupTree) bool {
if req.GroupID != nil {
if _, ok := t.byID[*req.GroupID]; !ok {
fail(c, http.StatusUnprocessableEntity, "invalid_group")
return false
}
}
if !canInGroup(current(c), authz.MonitorsWrite, t, req.GroupID) {
fail(c, http.StatusForbidden, "forbidden")
return false
}
req.Name = strings.TrimSpace(req.Name)
req.Target = strings.TrimSpace(req.Target)
if req.Name == "" {
@@ -129,11 +144,18 @@ func (s *Server) validateMonitor(c *gin.Context, req *monitorRequest) bool {
return true
}
// listMonitors returns the monitors the principal may read, org-wide or
// through a group assignment.
func (s *Server) listMonitors(c *gin.Context) {
p := current(c)
ctx := c.Request.Context()
var out []monitorView
err := s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
t, err := s.loadGroupTree(ctx, p.scope())
if err != nil {
internalError(c, err)
return
}
out := []monitorView{}
err = s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
monitors, err := q.ListMonitors(ctx)
if err != nil {
return err
@@ -156,12 +178,15 @@ func (s *Server) listMonitors(c *gin.Context) {
Region: l.Region, Time: l.Time, OK: l.Ok, LatencyMS: l.LatencyMs, StatusCode: l.StatusCode, Error: l.Error,
})
}
out = make([]monitorView, len(monitors))
for i, m := range monitors {
out[i] = viewMonitor(m, byMonitor[m.ID])
if l := latestBy[m.ID]; l != nil {
out[i].Latest = l
for _, m := range monitors {
if !canInGroup(p, authz.MonitorsRead, t, m.GroupID) {
continue
}
v := viewMonitor(m, byMonitor[m.ID])
if l := latestBy[m.ID]; l != nil {
v.Latest = l
}
out = append(out, v)
}
return nil
})
@@ -174,18 +199,27 @@ func (s *Server) listMonitors(c *gin.Context) {
func (s *Server) createMonitor(c *gin.Context) {
var req monitorRequest
if !bind(c, &req) || !s.validateMonitor(c, &req) {
if !bind(c, &req) {
return
}
p := current(c)
ctx := c.Request.Context()
t, err := s.loadGroupTree(ctx, p.scope())
if err != nil {
internalError(c, err)
return
}
if !s.validateMonitor(c, &req, t) {
return
}
var m store.Monitor
err := s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
err = s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
var err error
m, err = q.CreateMonitor(ctx, store.CreateMonitorParams{
OrgID: p.Org.ID, Name: req.Name, Type: req.Type, Target: req.Target,
IntervalSeconds: int32(req.IntervalSeconds), TimeoutMs: int32(req.TimeoutMS),
Config: req.Config, Enabled: req.Enabled == nil || *req.Enabled, Confirmations: int32(req.Confirmations),
GroupID: req.GroupID,
})
if err != nil {
return err
@@ -241,17 +275,34 @@ func (s *Server) updateMonitor(c *gin.Context) {
return
}
var req monitorRequest
if !bind(c, &req) || !s.validateMonitor(c, &req) {
if !bind(c, &req) {
return
}
p := current(c)
ctx := c.Request.Context()
t, err := s.loadGroupTree(ctx, p.scope())
if err != nil {
internalError(c, err)
return
}
if !s.validateMonitor(c, &req, t) {
return
}
var (
m store.Monitor
previous []uuid.UUID
denied bool
)
err = s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
var err error
old, err := q.GetMonitor(ctx, id)
if err != nil {
return err
}
// Moving out of a group needs write access where it was, too.
if !canInGroup(p, authz.MonitorsWrite, t, old.GroupID) {
denied = true
return nil
}
if previous, err = q.MonitorRegions(ctx, id); err != nil {
return err
}
@@ -259,13 +310,14 @@ func (s *Server) updateMonitor(c *gin.Context) {
ID: id, Name: req.Name, Type: req.Type, Target: req.Target,
IntervalSeconds: int32(req.IntervalSeconds), TimeoutMs: int32(req.TimeoutMS),
Config: req.Config, Enabled: req.Enabled == nil || *req.Enabled, Confirmations: int32(req.Confirmations),
GroupID: req.GroupID,
})
if err != nil {
return err
}
return setRegions(ctx, q, m, req.Regions)
})
if errors.Is(err, pgx.ErrNoRows) {
if errors.Is(err, pgx.ErrNoRows) || (err == nil && denied) {
fail(c, http.StatusNotFound, "not_found")
return
}
@@ -288,12 +340,26 @@ func (s *Server) deleteMonitor(c *gin.Context) {
}
p := current(c)
ctx := c.Request.Context()
t, err := s.loadGroupTree(ctx, p.scope())
if err != nil {
internalError(c, err)
return
}
var (
regions []uuid.UUID
n int64
)
err = s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
var err error
old, err := q.GetMonitor(ctx, id)
if errors.Is(err, pgx.ErrNoRows) {
return nil
}
if err != nil {
return err
}
if !canInGroup(p, authz.MonitorsWrite, t, old.GroupID) {
return nil
}
if regions, err = q.MonitorRegions(ctx, id); err != nil {
return err
}