fix(api): platform check type switch binds the platform's checkers only
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m25s
ci / test (pull_request) Successful in 12m34s
ci / build (pull_request) Successful in 18m7s
ci / image (pull_request) Successful in 5m22s
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m25s
ci / test (pull_request) Successful in 12m34s
ci / build (pull_request) Successful in 18m7s
ci / image (pull_request) Successful in 5m22s
Switching a check type off in the admin area removed it for every organization, own checkers included, and hid it from the organization's own choice. The platform switch now only keeps such monitors off the platform's regions: organizations with regions of their own still create them there (422 check_type_own_checkers_only on a platform region), and the editor marks them "own checkers only". Passive types, which no checker runs, stay switched off entirely. The organization's check types are chosen independently of the platform's: its settings list every type, and the platform switch no longer rewrites that choice. 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
12e6de77aa
commit
bd887523de
12 files changed
+2296
-2096
No files matched your search
@@ -108,10 +108,11 @@ var endpoints = []endpoint{
|
||||
{method: "GET", path: "/api/v1/org/limits", tag: "Organization", summary: "Limits in effect for the organization and its usage",
|
||||
description: "0 means unlimited. Creating beyond a limit gives 422 monitor_limit, status_page_limit, region_limit or platform_region_limit.",
|
||||
resp: usageView{}},
|
||||
{method: "GET", path: "/api/v1/org/check-types", tag: "Monitors", summary: "Monitor types this installation offers",
|
||||
resp: obj{"types": []string{}}},
|
||||
{method: "GET", path: "/api/v1/org/check-types", tag: "Monitors", summary: "Monitor types members can create",
|
||||
description: "`own_checkers_only` are the types the platform's checkers do not run: monitors of them use the organization's own regions only, or 422 check_type_own_checkers_only.",
|
||||
resp: obj{"types": []string{}, "own_checkers_only": []string{}}},
|
||||
{method: "PUT", path: "/api/v1/org/check-types", tag: "Monitors", summary: "Choose the monitor types offered to members",
|
||||
description: "Out of the types the platform offers; types left out are not offered for new monitors or type changes, existing monitors keep running.",
|
||||
description: "Independent of the types the platform's checkers run; types left out are not offered for new monitors or type changes, existing monitors keep running.",
|
||||
perm: "org.manage", sessionOnly: true, req: checkTypesRequest{}, status: http.StatusNoContent},
|
||||
{method: "GET", path: "/api/v1/org/groups", tag: "Monitors", summary: "List monitor groups", resp: obj{"groups": []groupView{}}},
|
||||
{method: "POST", path: "/api/v1/org/groups", tag: "Monitors", summary: "Create a group", perm: "monitors.write",
|
||||
@@ -302,8 +303,8 @@ var endpoints = []endpoint{
|
||||
req: legalRequest{}, status: http.StatusNoContent},
|
||||
{method: "GET", path: "/api/v1/admin/check-types", tag: "Platform", summary: "Check types and whether they are offered", perm: "platform admin",
|
||||
resp: obj{"types": []checkTypeView{}}},
|
||||
{method: "PUT", path: "/api/v1/admin/check-types", tag: "Platform", summary: "Choose the check types offered for new monitors",
|
||||
description: "Types left out are switched off: not offered for new monitors or type changes; existing monitors keep running.",
|
||||
{method: "PUT", path: "/api/v1/admin/check-types", tag: "Platform", summary: "Choose the check types the platform's checkers run",
|
||||
description: "Types left out are switched off for the platform's regions: new monitors and type changes may use them on the organizations' own regions only (passive types not at all); existing monitors keep running.",
|
||||
perm: "platform admin", req: checkTypesRequest{}, status: http.StatusNoContent},
|
||||
{method: "GET", path: "/api/v1/admin/checker-network", tag: "Platform", summary: "Whether platform checkers may check private addresses", perm: "platform admin",
|
||||
resp: checkerNetwork{}},
|
||||
|
||||
+115
-27
@@ -8,6 +8,7 @@ import (
|
||||
"slices"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/check"
|
||||
@@ -15,10 +16,12 @@ import (
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
||||
)
|
||||
|
||||
// Platform admins can switch off check types the installation cannot
|
||||
// offer, such as Docker on shared cloud checkers. Switched-off types are
|
||||
// not offered for new monitors; existing ones keep running and stay
|
||||
// editable.
|
||||
// Platform admins can switch off check types the platform's checkers
|
||||
// cannot offer, such as Docker on shared cloud checkers. That only binds
|
||||
// the platform's checkers: organizations keep such types for their own.
|
||||
// Organizations separately choose the types they offer their members.
|
||||
// Switched-off types are not offered for new monitors; existing ones keep
|
||||
// running and stay editable.
|
||||
|
||||
const settingDisabledChecks = "disabled_check_types"
|
||||
|
||||
@@ -48,34 +51,87 @@ func (s *Server) disabledChecks(ctx context.Context) map[string]bool {
|
||||
return set
|
||||
}
|
||||
|
||||
// offered reports whether org may create monitors of type t: neither the
|
||||
// platform nor the organization switched it off.
|
||||
func (s *Server) offered(ctx context.Context, org store.Org, t string) bool {
|
||||
return slices.Contains(s.enabledCheckTypes(ctx, org, ""), t)
|
||||
// checkTypeChoice is a type an organization can pick for a monitor.
|
||||
type checkTypeChoice struct {
|
||||
Type string
|
||||
// OwnOnly is set when the platform switched the type off for its
|
||||
// checkers, leaving the organization's own.
|
||||
OwnOnly bool
|
||||
}
|
||||
|
||||
// enabledCheckTypes lists the types offered to org for new monitors, plus
|
||||
// keep when a monitor of a switched-off type is being edited. The
|
||||
// organization's choice is ignored if it would leave nothing, which can
|
||||
// happen when the platform later switches off what it kept.
|
||||
func (s *Server) enabledCheckTypes(ctx context.Context, org store.Org, keep string) []string {
|
||||
// checkTypeChoices lists the types offered to org, plus keep when a
|
||||
// monitor of a switched-off type is being edited. Types the platform
|
||||
// switched off for its checkers are offered only to organizations with
|
||||
// regions of their own; passive ones, which no checker runs, not at all.
|
||||
func (s *Server) checkTypeChoices(ctx context.Context, org store.Org, keep string, ownRegions bool) []checkTypeChoice {
|
||||
off := s.disabledChecks(ctx)
|
||||
var platform, out []string
|
||||
for _, t := range check.Types {
|
||||
if off[string(t)] && string(t) != keep {
|
||||
var out, platform []checkTypeChoice
|
||||
for _, ty := range check.Types {
|
||||
t := string(ty)
|
||||
passive := check.Passive(ty)
|
||||
if off[t] && t != keep && (passive || !ownRegions) {
|
||||
continue
|
||||
}
|
||||
platform = append(platform, string(t))
|
||||
if !slices.Contains(org.DisabledCheckTypes, string(t)) || string(t) == keep {
|
||||
out = append(out, string(t))
|
||||
choice := checkTypeChoice{Type: t, OwnOnly: off[t] && !passive}
|
||||
if !off[t] || t == keep {
|
||||
platform = append(platform, choice)
|
||||
}
|
||||
if !slices.Contains(org.DisabledCheckTypes, t) || t == keep {
|
||||
out = append(out, choice)
|
||||
}
|
||||
}
|
||||
// The organization's choice can leave nothing usable when it only
|
||||
// kept types the platform's checkers no longer run.
|
||||
if len(out) == 0 {
|
||||
return platform
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
var (
|
||||
errCheckTypeDisabled = errors.New("api: check type switched off")
|
||||
errOwnCheckersOnly = errors.New("api: check type switched off for platform checkers")
|
||||
)
|
||||
|
||||
// checkTypeAllowed fails unless org may give a monitor, previously of
|
||||
// type keep in the regions previous, type t in regions; platform marks
|
||||
// the platform's regions. Monitors of a type the platform switched off
|
||||
// keep the platform regions they had but gain none.
|
||||
func (s *Server) checkTypeAllowed(ctx context.Context, org store.Org, t, keep string, regions, previous []uuid.UUID, platform map[uuid.UUID]bool) error {
|
||||
own := false
|
||||
for _, p := range platform {
|
||||
own = own || !p
|
||||
}
|
||||
choices := s.checkTypeChoices(ctx, org, keep, own)
|
||||
i := slices.IndexFunc(choices, func(c checkTypeChoice) bool { return c.Type == t })
|
||||
if i < 0 {
|
||||
return errCheckTypeDisabled
|
||||
}
|
||||
if !s.disabledChecks(ctx)[t] || check.Passive(check.Type(t)) {
|
||||
return nil
|
||||
}
|
||||
for _, r := range regions {
|
||||
if platform[r] && (t != keep || !slices.Contains(previous, r)) {
|
||||
return errOwnCheckersOnly
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// failCheckType answers a checkTypeAllowed error and reports whether it
|
||||
// did.
|
||||
func failCheckType(c *gin.Context, err error) bool {
|
||||
switch {
|
||||
case errors.Is(err, errCheckTypeDisabled):
|
||||
fail(c, http.StatusUnprocessableEntity, "check_type_disabled")
|
||||
case errors.Is(err, errOwnCheckersOnly):
|
||||
fail(c, http.StatusUnprocessableEntity, "check_type_own_checkers_only")
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
type checkTypeView struct {
|
||||
Type string `json:"type"`
|
||||
Enabled bool `json:"enabled"`
|
||||
@@ -132,13 +188,40 @@ func (s *Server) adminSetCheckTypes(c *gin.Context) {
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// orgCheckTypes tells API clients which monitor types they can create.
|
||||
// orgCheckTypes tells API clients which monitor types they can create;
|
||||
// own_checkers_only are those the platform's checkers do not run.
|
||||
func (s *Server) orgCheckTypes(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"types": s.enabledCheckTypes(c.Request.Context(), current(c).Org, "")})
|
||||
p := current(c)
|
||||
ctx := c.Request.Context()
|
||||
own, err := s.hasOwnRegions(ctx, p)
|
||||
if err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
types, ownOnly := []string{}, []string{}
|
||||
for _, ch := range s.checkTypeChoices(ctx, p.Org, "", own) {
|
||||
types = append(types, ch.Type)
|
||||
if ch.OwnOnly {
|
||||
ownOnly = append(ownOnly, ch.Type)
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"types": types, "own_checkers_only": ownOnly})
|
||||
}
|
||||
|
||||
// orgSetCheckTypes chooses which of the platform's types the organization
|
||||
// offers its members.
|
||||
func (s *Server) hasOwnRegions(ctx context.Context, p *principal) (bool, error) {
|
||||
var own bool
|
||||
err := s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
||||
regions, err := q.ListVisibleRegions(ctx)
|
||||
own = slices.ContainsFunc(regions, func(r store.Region) bool { return r.OrgID != nil })
|
||||
return err
|
||||
})
|
||||
return own, err
|
||||
}
|
||||
|
||||
// orgSetCheckTypes chooses which types the organization offers its
|
||||
// members, independently of what the platform's checkers run. Passive
|
||||
// types the platform switched off cannot be chosen and keep their
|
||||
// setting.
|
||||
func (s *Server) orgSetCheckTypes(c *gin.Context) {
|
||||
var req checkTypesRequest
|
||||
if !bind(c, &req) {
|
||||
@@ -149,12 +232,17 @@ func (s *Server) orgSetCheckTypes(c *gin.Context) {
|
||||
off := s.disabledChecks(ctx)
|
||||
disabled := []string{}
|
||||
enabled := 0
|
||||
for _, t := range check.Types {
|
||||
for _, ty := range check.Types {
|
||||
t := string(ty)
|
||||
switch {
|
||||
case slices.Contains(req.Enabled, string(t)):
|
||||
case off[t] && check.Passive(ty):
|
||||
if slices.Contains(p.Org.DisabledCheckTypes, t) {
|
||||
disabled = append(disabled, t)
|
||||
}
|
||||
case slices.Contains(req.Enabled, t):
|
||||
enabled++
|
||||
case !off[string(t)]:
|
||||
disabled = append(disabled, string(t))
|
||||
default:
|
||||
disabled = append(disabled, t)
|
||||
}
|
||||
}
|
||||
for _, t := range req.Enabled {
|
||||
|
||||
@@ -74,4 +74,29 @@ func TestCheckTypesCanBeSwitchedOff(t *testing.T) {
|
||||
if _, _, page := admin.page("/monitors/" + hb["id"].(string) + "/edit"); !strings.Contains(page, `value="heartbeat" selected`) {
|
||||
t.Error("editing a monitor of a switched-off type does not keep its type")
|
||||
}
|
||||
|
||||
// The switch binds the platform's checkers only: with a region of its
|
||||
// own the organization keeps the type there.
|
||||
st, own := member.do("POST", "/api/v1/org/regions", map[string]any{"key": uniq("own-"), "name": "Own"})
|
||||
if st != http.StatusCreated {
|
||||
t.Fatalf("own region = %d %v", st, own)
|
||||
}
|
||||
ownID := own["id"].(string)
|
||||
if _, types := member.do("GET", "/api/v1/org/check-types", nil); !strings.Contains(mustJSON(types["types"]), `"icmp"`) || mustJSON(types["own_checkers_only"]) != `["icmp"]` {
|
||||
t.Errorf("offered types with an own region = %v", types)
|
||||
}
|
||||
if _, _, page := member.page("/monitors/new"); !strings.Contains(page, `value="icmp"`) || !strings.Contains(page, "own checkers only") {
|
||||
t.Error("the editor does not offer the type for own checkers")
|
||||
}
|
||||
st, body = member.do("POST", "/api/v1/org/monitors", map[string]any{"name": "Ping", "type": "icmp", "target": "example.com", "interval_seconds": 60, "regions": []string{ownID, region}})
|
||||
expect(t, "switched-off type on a platform region", st, body, http.StatusUnprocessableEntity, "check_type_own_checkers_only")
|
||||
st, ping := member.do("POST", "/api/v1/org/monitors", map[string]any{"name": "Ping", "type": "icmp", "target": "example.com", "interval_seconds": 60, "regions": []string{ownID}})
|
||||
expect(t, "switched-off type on an own region", st, ping, http.StatusCreated, "")
|
||||
st, body = member.do("PUT", "/api/v1/org/monitors/"+ping["id"].(string), map[string]any{"name": "Ping", "type": "icmp", "target": "example.com", "interval_seconds": 60, "regions": []string{ownID, region}})
|
||||
expect(t, "adding a platform region", st, body, http.StatusUnprocessableEntity, "check_type_own_checkers_only")
|
||||
st, body = member.do("POST", "/api/v1/org/monitors", map[string]any{"name": "Cron", "type": "heartbeat", "interval_seconds": 3600})
|
||||
expect(t, "switched-off passive type", st, body, http.StatusUnprocessableEntity, "check_type_disabled")
|
||||
if _, _, page := member.page("/settings/org"); !strings.Contains(page, `value="icmp"`) || strings.Contains(page, `value="heartbeat"`) {
|
||||
t.Error("the organization settings do not offer the types for own checkers")
|
||||
}
|
||||
}
|
||||
+10
-3
@@ -674,9 +674,16 @@ func (s *Server) monitorEditor(c *gin.Context, p *principal, page web.Page, me w
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f.Types = s.enabledCheckTypes(ctx, p.Org, f.Type)
|
||||
if !slices.Contains(f.Types, f.Type) {
|
||||
f.Type = f.Types[0]
|
||||
keep := ""
|
||||
if idParam != "" {
|
||||
keep = f.Type
|
||||
}
|
||||
own := slices.ContainsFunc(regions, func(r store.Region) bool { return r.OrgID != nil })
|
||||
for _, ch := range s.checkTypeChoices(ctx, p.Org, keep, own) {
|
||||
f.Types = append(f.Types, web.TypeOption{Type: ch.Type, OwnOnly: ch.OwnOnly})
|
||||
}
|
||||
if !slices.ContainsFunc(f.Types, func(t web.TypeOption) bool { return t.Type == f.Type }) {
|
||||
f.Type = f.Types[0].Type
|
||||
}
|
||||
for i, r := range regions {
|
||||
live, err := s.sched.Instances(ctx, r.ID.String())
|
||||
|
||||
@@ -280,8 +280,7 @@ func (s *Server) createMonitor(c *gin.Context) {
|
||||
if !s.validateMonitor(c, &req, t) {
|
||||
return
|
||||
}
|
||||
if !s.offered(ctx, p.Org, req.Type) {
|
||||
fail(c, http.StatusUnprocessableEntity, "check_type_disabled")
|
||||
if failCheckType(c, s.checkTypeAllowed(ctx, p.Org, req.Type, "", req.Regions, nil, req.platform)) {
|
||||
return
|
||||
}
|
||||
req.Target = passiveTarget(check.Type(req.Type), req.Target)
|
||||
@@ -403,8 +402,8 @@ func (s *Server) updateMonitor(c *gin.Context) {
|
||||
if previous, err = q.MonitorRegions(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if old.Type != req.Type && !s.offered(ctx, p.Org, req.Type) {
|
||||
return errCheckTypeDisabled
|
||||
if err := s.checkTypeAllowed(ctx, p.Org, req.Type, old.Type, req.Regions, previous, req.platform); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := checkRegions(s.orgLimits(ctx, p.Org), req.Regions, previous, req.platform); err != nil {
|
||||
return err
|
||||
@@ -443,8 +442,7 @@ func (s *Server) updateMonitor(c *gin.Context) {
|
||||
fail(c, http.StatusUnprocessableEntity, "invalid_alert_group")
|
||||
return
|
||||
}
|
||||
if errors.Is(err, errCheckTypeDisabled) {
|
||||
fail(c, http.StatusUnprocessableEntity, "check_type_disabled")
|
||||
if failCheckType(c, err) {
|
||||
return
|
||||
}
|
||||
if failLimit(c, err) {
|
||||
@@ -611,5 +609,3 @@ func passiveRegion(t check.Type) string {
|
||||
}
|
||||
return heartbeatRegion
|
||||
}
|
||||
|
||||
var errCheckTypeDisabled = errors.New("api: check type switched off")
|
||||
@@ -383,9 +383,14 @@ func (s *Server) orgSettingsView(c *gin.Context, p *principal, page web.Page, me
|
||||
v.Usage = web.UsageView{Limits: webLimits(u.Limits), Monitors: u.Usage.Monitors, StatusPages: u.Usage.StatusPages}
|
||||
off := s.disabledChecks(c.Request.Context())
|
||||
for _, t := range check.Types {
|
||||
if !off[string(t)] {
|
||||
v.CheckTypes = append(v.CheckTypes, web.CheckTypeOption{Type: string(t), Enabled: !slices.Contains(p.Org.DisabledCheckTypes, string(t))})
|
||||
o := web.CheckTypeOption{Type: string(t), Enabled: !slices.Contains(p.Org.DisabledCheckTypes, string(t))}
|
||||
if off[string(t)] {
|
||||
if check.Passive(t) {
|
||||
continue
|
||||
}
|
||||
o.Note = "org.check_type_own_only"
|
||||
}
|
||||
v.CheckTypes = append(v.CheckTypes, o)
|
||||
}
|
||||
if p.Org.ApiTokenMaxDays != nil {
|
||||
v.TokenMaxDays = int(*p.Org.ApiTokenMaxDays)
|
||||
|
||||
Reference in new issue
Block a user