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:
1 parent
2d7f326d38
commit
11482ec6d5
4 files changed
+497
-21
No files matched your search
@@ -0,0 +1,275 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
|
"github.com/jackc/pgx/v5/pgconn"
|
||||||
|
|
||||||
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/authz"
|
||||||
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
||||||
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
||||||
|
)
|
||||||
|
|
||||||
|
// groupTree is an organization's monitor groups. Role assignments scoped to
|
||||||
|
// a group cover its whole subtree, so permission checks look at a group and
|
||||||
|
// all its ancestors.
|
||||||
|
type groupTree struct {
|
||||||
|
byID map[uuid.UUID]store.MonitorGroup
|
||||||
|
children map[uuid.UUID][]uuid.UUID
|
||||||
|
roots []uuid.UUID
|
||||||
|
}
|
||||||
|
|
||||||
|
func newGroupTree(groups []store.MonitorGroup) groupTree {
|
||||||
|
t := groupTree{byID: map[uuid.UUID]store.MonitorGroup{}, children: map[uuid.UUID][]uuid.UUID{}}
|
||||||
|
for _, g := range groups {
|
||||||
|
t.byID[g.ID] = g
|
||||||
|
}
|
||||||
|
for _, g := range groups {
|
||||||
|
if g.ParentID != nil {
|
||||||
|
if _, ok := t.byID[*g.ParentID]; ok {
|
||||||
|
t.children[*g.ParentID] = append(t.children[*g.ParentID], g.ID)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.roots = append(t.roots, g.ID)
|
||||||
|
}
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) loadGroupTree(ctx context.Context, scope tenant.Scope) (groupTree, error) {
|
||||||
|
var groups []store.MonitorGroup
|
||||||
|
err := s.db.Tx(ctx, scope, func(q *store.Queries) error {
|
||||||
|
var err error
|
||||||
|
groups, err = q.ListGroups(ctx)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return newGroupTree(groups), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// resources returns the group and its ancestors as authorization scopes,
|
||||||
|
// nearest first. A monitor without a group has none: only org-wide rights
|
||||||
|
// cover it.
|
||||||
|
func (t groupTree) resources(group *uuid.UUID) []authz.Resource {
|
||||||
|
var out []authz.Resource
|
||||||
|
for id := group; id != nil; {
|
||||||
|
g, ok := t.byID[*id]
|
||||||
|
if !ok || len(out) > 20 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
out = append(out, authz.Resource{Type: authz.MonitorGroup, ID: g.ID})
|
||||||
|
id = g.ParentID
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// depth returns how many ancestors a group has.
|
||||||
|
func (t groupTree) depth(id uuid.UUID) int { return len(t.resources(&id)) - 1 }
|
||||||
|
|
||||||
|
// walk visits groups depth-first in name order.
|
||||||
|
func (t groupTree) walk(fn func(g store.MonitorGroup, depth int)) {
|
||||||
|
var visit func(ids []uuid.UUID, depth int)
|
||||||
|
visit = func(ids []uuid.UUID, depth int) {
|
||||||
|
for _, id := range ids {
|
||||||
|
fn(t.byID[id], depth)
|
||||||
|
visit(t.children[id], depth+1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
visit(t.roots, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// canInGroup reports whether p holds perm for things in group.
|
||||||
|
func canInGroup(p *principal, perm authz.Permission, t groupTree, group *uuid.UUID) bool {
|
||||||
|
return p.Grants.Can(perm, t.resources(group)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
type groupView struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
ParentID *uuid.UUID `json:"parent_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) listGroups(c *gin.Context) {
|
||||||
|
p := current(c)
|
||||||
|
t, err := s.loadGroupTree(c.Request.Context(), p.scope())
|
||||||
|
if err != nil {
|
||||||
|
internalError(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
out := []groupView{}
|
||||||
|
t.walk(func(g store.MonitorGroup, _ int) {
|
||||||
|
out = append(out, groupView{ID: g.ID, Name: g.Name, ParentID: g.ParentID})
|
||||||
|
})
|
||||||
|
c.JSON(http.StatusOK, gin.H{"groups": out})
|
||||||
|
}
|
||||||
|
|
||||||
|
type groupRequest struct {
|
||||||
|
Name string `json:"name" binding:"required,max=100"`
|
||||||
|
ParentID *uuid.UUID `json:"parent_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func groupError(c *gin.Context, err error) bool {
|
||||||
|
var pgErr *pgconn.PgError
|
||||||
|
if !errors.As(err, &pgErr) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case pgErr.ConstraintName == "monitor_groups_cycle", pgErr.ConstraintName == "monitor_groups_check":
|
||||||
|
fail(c, http.StatusUnprocessableEntity, "group_cycle")
|
||||||
|
case pgErr.ConstraintName == "monitor_groups_depth":
|
||||||
|
fail(c, http.StatusUnprocessableEntity, "group_too_deep")
|
||||||
|
case pgErr.Code == "23503":
|
||||||
|
fail(c, http.StatusUnprocessableEntity, "invalid_group")
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) createGroup(c *gin.Context) {
|
||||||
|
var req groupRequest
|
||||||
|
if !bind(c, &req) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req.Name = strings.TrimSpace(req.Name)
|
||||||
|
if req.Name == "" {
|
||||||
|
fail(c, http.StatusUnprocessableEntity, "invalid_input")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
p := current(c)
|
||||||
|
ctx := c.Request.Context()
|
||||||
|
t, err := s.loadGroupTree(ctx, p.scope())
|
||||||
|
if err != nil {
|
||||||
|
internalError(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if req.ParentID != nil {
|
||||||
|
if _, ok := t.byID[*req.ParentID]; !ok {
|
||||||
|
fail(c, http.StatusUnprocessableEntity, "invalid_group")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !canInGroup(p, authz.MonitorsWrite, t, req.ParentID) {
|
||||||
|
fail(c, http.StatusForbidden, "forbidden")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var g store.MonitorGroup
|
||||||
|
err = s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
||||||
|
var err error
|
||||||
|
g, err = q.CreateGroup(ctx, store.CreateGroupParams{OrgID: p.Org.ID, ParentID: req.ParentID, Name: req.Name})
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
if groupError(c, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
internalError(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusCreated, groupView{ID: g.ID, Name: g.Name, ParentID: g.ParentID})
|
||||||
|
}
|
||||||
|
|
||||||
|
// updateGroup renames or moves a group. Moving needs write access both where
|
||||||
|
// the group is and where it goes.
|
||||||
|
func (s *Server) updateGroup(c *gin.Context) {
|
||||||
|
id, err := uuid.Parse(c.Param("id"))
|
||||||
|
if err != nil {
|
||||||
|
fail(c, http.StatusNotFound, "not_found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var req groupRequest
|
||||||
|
if !bind(c, &req) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req.Name = strings.TrimSpace(req.Name)
|
||||||
|
p := current(c)
|
||||||
|
ctx := c.Request.Context()
|
||||||
|
t, err := s.loadGroupTree(ctx, p.scope())
|
||||||
|
if err != nil {
|
||||||
|
internalError(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, ok := t.byID[id]; !ok || req.Name == "" {
|
||||||
|
fail(c, http.StatusNotFound, "not_found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if req.ParentID != nil {
|
||||||
|
if _, ok := t.byID[*req.ParentID]; !ok {
|
||||||
|
fail(c, http.StatusUnprocessableEntity, "invalid_group")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !canInGroup(p, authz.MonitorsWrite, t, &id) || !canInGroup(p, authz.MonitorsWrite, t, req.ParentID) {
|
||||||
|
fail(c, http.StatusForbidden, "forbidden")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var g store.MonitorGroup
|
||||||
|
err = s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
||||||
|
var err error
|
||||||
|
g, err = q.UpdateGroup(ctx, store.UpdateGroupParams{ID: id, Name: req.Name, ParentID: req.ParentID})
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
if groupError(c, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
internalError(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, groupView{ID: g.ID, Name: g.Name, ParentID: g.ParentID})
|
||||||
|
}
|
||||||
|
|
||||||
|
// deleteGroup moves the group's subgroups and monitors to its parent and
|
||||||
|
// removes assignments scoped to it. As things move up, write access to the
|
||||||
|
// parent is required.
|
||||||
|
func (s *Server) deleteGroup(c *gin.Context) {
|
||||||
|
id, err := uuid.Parse(c.Param("id"))
|
||||||
|
if err != nil {
|
||||||
|
fail(c, http.StatusNotFound, "not_found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
p := current(c)
|
||||||
|
ctx := c.Request.Context()
|
||||||
|
t, err := s.loadGroupTree(ctx, p.scope())
|
||||||
|
if err != nil {
|
||||||
|
internalError(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
g, ok := t.byID[id]
|
||||||
|
if !ok {
|
||||||
|
fail(c, http.StatusNotFound, "not_found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !canInGroup(p, authz.MonitorsWrite, t, g.ParentID) {
|
||||||
|
fail(c, http.StatusForbidden, "forbidden")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
||||||
|
if err := q.ReparentGroups(ctx, store.ReparentGroupsParams{OldParent: &id, NewParent: g.ParentID}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := q.RegroupMonitors(ctx, store.RegroupMonitorsParams{OldGroup: &id, NewGroup: g.ParentID}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rt := string(authz.MonitorGroup)
|
||||||
|
if err := q.DeleteAssignmentsForResource(ctx, store.DeleteAssignmentsForResourceParams{ResourceType: &rt, ResourceID: &id}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err := q.DeleteGroup(ctx, id)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
|
fail(c, http.StatusNotFound, "not_found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
internalError(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Status(http.StatusNoContent)
|
||||||
|
}
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
package api_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (b *browser) group(name string, parent string) string {
|
||||||
|
b.t.Helper()
|
||||||
|
body := map[string]any{"name": name}
|
||||||
|
if parent != "" {
|
||||||
|
body["parent_id"] = parent
|
||||||
|
}
|
||||||
|
st, resp := b.do("POST", "/api/v1/org/groups", body)
|
||||||
|
if st != http.StatusCreated {
|
||||||
|
b.t.Fatalf("create group %s = %d %v", name, st, resp)
|
||||||
|
}
|
||||||
|
return resp["id"].(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
func monitorBody(name, group, region string) map[string]any {
|
||||||
|
b := map[string]any{"name": name, "type": "tcp", "target": "example.com:443", "interval_seconds": 3600, "regions": []string{region}}
|
||||||
|
if group != "" {
|
||||||
|
b["group_id"] = group
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMonitorGroups(t *testing.T) {
|
||||||
|
e := newEnv(t)
|
||||||
|
_, region := e.platformChecker(t, uniq("eu-"))
|
||||||
|
owner := e.browser(t)
|
||||||
|
owner.signup("o@"+uniq("g")+".test", uniq("grp-"))
|
||||||
|
|
||||||
|
prod := owner.group("Prod", "")
|
||||||
|
eu := owner.group("EU", prod)
|
||||||
|
fra := owner.group("Frankfurt", eu)
|
||||||
|
staging := owner.group("Staging", "")
|
||||||
|
|
||||||
|
newMonitor := func(b *browser, name, group string) (int, map[string]any) {
|
||||||
|
return b.do("POST", "/api/v1/org/monitors", monitorBody(name, group, region))
|
||||||
|
}
|
||||||
|
for name, g := range map[string]string{"fra-web": fra, "eu-db": eu, "staging-web": staging, "loose": ""} {
|
||||||
|
if st, body := newMonitor(owner, name, g); st != http.StatusCreated {
|
||||||
|
t.Fatalf("create monitor %s = %d %v", name, st, body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("no cycles and limited depth", func(t *testing.T) {
|
||||||
|
st, body := owner.do("PUT", "/api/v1/org/groups/"+prod, map[string]any{"name": "Prod", "parent_id": fra})
|
||||||
|
expect(t, "move group into its descendant", st, body, http.StatusUnprocessableEntity, "group_cycle")
|
||||||
|
parent := staging
|
||||||
|
for i := 0; i < 7; i++ {
|
||||||
|
parent = owner.group("level", parent)
|
||||||
|
}
|
||||||
|
st, body = owner.do("POST", "/api/v1/org/groups", map[string]any{"name": "too deep", "parent_id": parent})
|
||||||
|
expect(t, "ninth level", st, body, http.StatusUnprocessableEntity, "group_too_deep")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("scoped permissions follow the subtree", func(t *testing.T) {
|
||||||
|
owner.do("POST", "/api/v1/org/roles", map[string]any{"name": "Readers", "permissions": []string{"members.read"}})
|
||||||
|
owner.do("POST", "/api/v1/org/roles", map[string]any{"name": "Monitor editor", "permissions": []string{"monitors.write"}})
|
||||||
|
email := "ed@" + uniq("ed") + ".test"
|
||||||
|
ed := e.joinVia(t, owner.invite(email, owner.roleID("Readers")))
|
||||||
|
st, body := owner.do("POST", "/api/v1/org/members/"+owner.userID(email)+"/roles", map[string]any{
|
||||||
|
"role_id": owner.roleID("Monitor editor"), "resource_type": "monitor_group", "resource_id": prod,
|
||||||
|
})
|
||||||
|
expect(t, "assign scoped role", st, body, http.StatusCreated, "")
|
||||||
|
|
||||||
|
_, list := ed.do("GET", "/api/v1/org/monitors", nil)
|
||||||
|
var names []string
|
||||||
|
for _, m := range list["monitors"].([]any) {
|
||||||
|
names = append(names, m.(map[string]any)["name"].(string))
|
||||||
|
}
|
||||||
|
if got := strings.Join(names, ","); !strings.Contains(got, "fra-web") || !strings.Contains(got, "eu-db") || len(names) != 2 {
|
||||||
|
t.Errorf("scoped user sees %v, want only the Prod subtree", names)
|
||||||
|
}
|
||||||
|
st, body = newMonitor(ed, "x", staging)
|
||||||
|
expect(t, "create outside scope", st, body, http.StatusForbidden, "forbidden")
|
||||||
|
st, body = newMonitor(ed, "x", "")
|
||||||
|
expect(t, "create at top level", st, body, http.StatusForbidden, "forbidden")
|
||||||
|
st, body = newMonitor(ed, "fra-cache", fra)
|
||||||
|
expect(t, "create deep inside scope", st, body, http.StatusCreated, "")
|
||||||
|
created := body["id"].(string)
|
||||||
|
st, body = ed.do("PUT", "/api/v1/org/monitors/"+created, monitorBody("fra-cache", staging, region))
|
||||||
|
expect(t, "move out of scope", st, body, http.StatusForbidden, "forbidden")
|
||||||
|
st, body = ed.do("POST", "/api/v1/org/groups", map[string]any{"name": "Root"})
|
||||||
|
expect(t, "create root group", st, body, http.StatusForbidden, "forbidden")
|
||||||
|
st, body = ed.do("POST", "/api/v1/org/groups", map[string]any{"name": "Paris", "parent_id": eu})
|
||||||
|
expect(t, "create subgroup inside scope", st, body, http.StatusCreated, "")
|
||||||
|
|
||||||
|
_, _, html := ed.page("/")
|
||||||
|
if !strings.Contains(html, "Frankfurt") || strings.Contains(html, "staging-web") {
|
||||||
|
t.Errorf("scoped dashboard: Frankfurt shown %t, staging-web hidden %t",
|
||||||
|
strings.Contains(html, "Frankfurt"), !strings.Contains(html, "staging-web"))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("deleting a group moves its content up", func(t *testing.T) {
|
||||||
|
st, body := owner.do("DELETE", "/api/v1/org/groups/"+eu, nil)
|
||||||
|
expect(t, "delete EU", st, body, http.StatusNoContent, "")
|
||||||
|
_, groups := owner.do("GET", "/api/v1/org/groups", nil)
|
||||||
|
for _, g := range groups["groups"].([]any) {
|
||||||
|
g := g.(map[string]any)
|
||||||
|
if g["id"] == fra && g["parent_id"] != prod {
|
||||||
|
t.Errorf("Frankfurt parent = %v, want Prod", g["parent_id"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_, list := owner.do("GET", "/api/v1/org/monitors", nil)
|
||||||
|
for _, m := range list["monitors"].([]any) {
|
||||||
|
m := m.(map[string]any)
|
||||||
|
if m["name"] == "eu-db" && m["group_id"] != prod {
|
||||||
|
t.Errorf("eu-db group = %v, want Prod", m["group_id"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("groups stay in their org", func(t *testing.T) {
|
||||||
|
other := e.browser(t)
|
||||||
|
other.signup("x@"+uniq("gx")+".test", uniq("grp-x-"))
|
||||||
|
st, body := newMonitor(other, "x", prod)
|
||||||
|
expect(t, "foreign group", st, body, http.StatusUnprocessableEntity, "invalid_group")
|
||||||
|
})
|
||||||
|
|
||||||
|
if st, final, html := owner.page("/groups"); st != http.StatusOK || final != "/groups" || !strings.Contains(html, "Frankfurt") {
|
||||||
|
t.Errorf("GET /groups = %d at %s", st, final)
|
||||||
|
}
|
||||||
|
}
|
||||||
+82
-16
@@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/jackc/pgx/v5"
|
"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/check"
|
||||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/schedule"
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/schedule"
|
||||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
"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
|
// Confirmations is how many failures in a row make a region count as
|
||||||
// down; 0 keeps the default of 2.
|
// down; 0 keeps the default of 2.
|
||||||
Confirmations int `json:"confirmations" binding:"min=0,max=10"`
|
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 {
|
type latestView struct {
|
||||||
@@ -52,6 +55,7 @@ type monitorView struct {
|
|||||||
Config json.RawMessage `json:"config"`
|
Config json.RawMessage `json:"config"`
|
||||||
Enabled bool `json:"enabled"`
|
Enabled bool `json:"enabled"`
|
||||||
Confirmations int32 `json:"confirmations"`
|
Confirmations int32 `json:"confirmations"`
|
||||||
|
GroupID *uuid.UUID `json:"group_id"`
|
||||||
Regions []uuid.UUID `json:"regions"`
|
Regions []uuid.UUID `json:"regions"`
|
||||||
Latest []latestView `json:"latest"`
|
Latest []latestView `json:"latest"`
|
||||||
}
|
}
|
||||||
@@ -59,7 +63,7 @@ type monitorView struct {
|
|||||||
func viewMonitor(m store.Monitor, regions []uuid.UUID) monitorView {
|
func viewMonitor(m store.Monitor, regions []uuid.UUID) monitorView {
|
||||||
return monitorView{
|
return monitorView{
|
||||||
ID: m.ID, Name: m.Name, Type: m.Type, Target: m.Target, IntervalSeconds: m.IntervalSeconds,
|
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
|
// validateMonitor normalizes req and writes an error response if it is not
|
||||||
// acceptable.
|
// acceptable, including when the principal may not write monitors in the
|
||||||
func (s *Server) validateMonitor(c *gin.Context, req *monitorRequest) bool {
|
// 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.Name = strings.TrimSpace(req.Name)
|
||||||
req.Target = strings.TrimSpace(req.Target)
|
req.Target = strings.TrimSpace(req.Target)
|
||||||
if req.Name == "" {
|
if req.Name == "" {
|
||||||
@@ -129,11 +144,18 @@ func (s *Server) validateMonitor(c *gin.Context, req *monitorRequest) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// listMonitors returns the monitors the principal may read, org-wide or
|
||||||
|
// through a group assignment.
|
||||||
func (s *Server) listMonitors(c *gin.Context) {
|
func (s *Server) listMonitors(c *gin.Context) {
|
||||||
p := current(c)
|
p := current(c)
|
||||||
ctx := c.Request.Context()
|
ctx := c.Request.Context()
|
||||||
var out []monitorView
|
t, err := s.loadGroupTree(ctx, p.scope())
|
||||||
err := s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
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)
|
monitors, err := q.ListMonitors(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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,
|
Region: l.Region, Time: l.Time, OK: l.Ok, LatencyMS: l.LatencyMs, StatusCode: l.StatusCode, Error: l.Error,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
out = make([]monitorView, len(monitors))
|
for _, m := range monitors {
|
||||||
for i, m := range monitors {
|
if !canInGroup(p, authz.MonitorsRead, t, m.GroupID) {
|
||||||
out[i] = viewMonitor(m, byMonitor[m.ID])
|
continue
|
||||||
if l := latestBy[m.ID]; l != nil {
|
|
||||||
out[i].Latest = l
|
|
||||||
}
|
}
|
||||||
|
v := viewMonitor(m, byMonitor[m.ID])
|
||||||
|
if l := latestBy[m.ID]; l != nil {
|
||||||
|
v.Latest = l
|
||||||
|
}
|
||||||
|
out = append(out, v)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@@ -174,18 +199,27 @@ func (s *Server) listMonitors(c *gin.Context) {
|
|||||||
|
|
||||||
func (s *Server) createMonitor(c *gin.Context) {
|
func (s *Server) createMonitor(c *gin.Context) {
|
||||||
var req monitorRequest
|
var req monitorRequest
|
||||||
if !bind(c, &req) || !s.validateMonitor(c, &req) {
|
if !bind(c, &req) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
p := current(c)
|
p := current(c)
|
||||||
ctx := c.Request.Context()
|
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
|
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
|
var err error
|
||||||
m, err = q.CreateMonitor(ctx, store.CreateMonitorParams{
|
m, err = q.CreateMonitor(ctx, store.CreateMonitorParams{
|
||||||
OrgID: p.Org.ID, Name: req.Name, Type: req.Type, Target: req.Target,
|
OrgID: p.Org.ID, Name: req.Name, Type: req.Type, Target: req.Target,
|
||||||
IntervalSeconds: int32(req.IntervalSeconds), TimeoutMs: int32(req.TimeoutMS),
|
IntervalSeconds: int32(req.IntervalSeconds), TimeoutMs: int32(req.TimeoutMS),
|
||||||
Config: req.Config, Enabled: req.Enabled == nil || *req.Enabled, Confirmations: int32(req.Confirmations),
|
Config: req.Config, Enabled: req.Enabled == nil || *req.Enabled, Confirmations: int32(req.Confirmations),
|
||||||
|
GroupID: req.GroupID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -241,17 +275,34 @@ func (s *Server) updateMonitor(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
var req monitorRequest
|
var req monitorRequest
|
||||||
if !bind(c, &req) || !s.validateMonitor(c, &req) {
|
if !bind(c, &req) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
p := current(c)
|
p := current(c)
|
||||||
ctx := c.Request.Context()
|
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 (
|
var (
|
||||||
m store.Monitor
|
m store.Monitor
|
||||||
previous []uuid.UUID
|
previous []uuid.UUID
|
||||||
|
denied bool
|
||||||
)
|
)
|
||||||
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
|
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 {
|
if previous, err = q.MonitorRegions(ctx, id); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -259,13 +310,14 @@ func (s *Server) updateMonitor(c *gin.Context) {
|
|||||||
ID: id, Name: req.Name, Type: req.Type, Target: req.Target,
|
ID: id, Name: req.Name, Type: req.Type, Target: req.Target,
|
||||||
IntervalSeconds: int32(req.IntervalSeconds), TimeoutMs: int32(req.TimeoutMS),
|
IntervalSeconds: int32(req.IntervalSeconds), TimeoutMs: int32(req.TimeoutMS),
|
||||||
Config: req.Config, Enabled: req.Enabled == nil || *req.Enabled, Confirmations: int32(req.Confirmations),
|
Config: req.Config, Enabled: req.Enabled == nil || *req.Enabled, Confirmations: int32(req.Confirmations),
|
||||||
|
GroupID: req.GroupID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return setRegions(ctx, q, m, req.Regions)
|
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")
|
fail(c, http.StatusNotFound, "not_found")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -288,12 +340,26 @@ func (s *Server) deleteMonitor(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
p := current(c)
|
p := current(c)
|
||||||
ctx := c.Request.Context()
|
ctx := c.Request.Context()
|
||||||
|
t, err := s.loadGroupTree(ctx, p.scope())
|
||||||
|
if err != nil {
|
||||||
|
internalError(c, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
var (
|
var (
|
||||||
regions []uuid.UUID
|
regions []uuid.UUID
|
||||||
n int64
|
n int64
|
||||||
)
|
)
|
||||||
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
|
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 {
|
if regions, err = q.MonitorRegions(ctx, id); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-5
@@ -107,12 +107,18 @@ func (s *Server) Mount(r gin.IRouter) {
|
|||||||
org := v1.Group("/org", s.orgAccess())
|
org := v1.Group("/org", s.orgAccess())
|
||||||
org.PUT("/policy", sessionOnly(), can(authz.OrgManage), s.updatePolicy)
|
org.PUT("/policy", sessionOnly(), can(authz.OrgManage), s.updatePolicy)
|
||||||
|
|
||||||
org.GET("/monitors", can(authz.MonitorsRead), s.listMonitors)
|
// Monitor and group handlers check permissions themselves: assignments
|
||||||
org.POST("/monitors", can(authz.MonitorsWrite), s.createMonitor)
|
// scoped to a group grant access below that group only.
|
||||||
org.PUT("/monitors/:id", can(authz.MonitorsWrite), s.updateMonitor)
|
org.GET("/monitors", s.listMonitors)
|
||||||
org.DELETE("/monitors/:id", can(authz.MonitorsWrite), s.deleteMonitor)
|
org.POST("/monitors", s.createMonitor)
|
||||||
|
org.PUT("/monitors/:id", s.updateMonitor)
|
||||||
|
org.DELETE("/monitors/:id", s.deleteMonitor)
|
||||||
|
org.GET("/groups", s.listGroups)
|
||||||
|
org.POST("/groups", s.createGroup)
|
||||||
|
org.PUT("/groups/:id", s.updateGroup)
|
||||||
|
org.DELETE("/groups/:id", s.deleteGroup)
|
||||||
|
|
||||||
org.GET("/regions", can(authz.MonitorsRead), s.listOrgRegions)
|
org.GET("/regions", s.listOrgRegions)
|
||||||
org.POST("/regions", sessionOnly(), can(authz.OrgManage), s.createOrgRegion)
|
org.POST("/regions", sessionOnly(), can(authz.OrgManage), s.createOrgRegion)
|
||||||
org.DELETE("/regions/:id", sessionOnly(), can(authz.OrgManage), s.deleteOrgRegion)
|
org.DELETE("/regions/:id", sessionOnly(), can(authz.OrgManage), s.deleteOrgRegion)
|
||||||
org.GET("/agents", can(authz.OrgManage), s.listOrgAgents)
|
org.GET("/agents", can(authz.OrgManage), s.listOrgAgents)
|
||||||
|
|||||||
Reference in new issue
Block a user