Files
uptimesebi/internal/api/checktypes.go
T
Sebastian UnterschützandClaude Opus 5.5 465fda60dc
ci / image (pull_request) Successful in 3m33s
ci / lint (pull_request) Successful in 6m25s
ci / test (pull_request) Successful in 11m34s
ci / build (pull_request) Successful in 11m54s
feat: choose which checkers may check private addresses, check types per organization
Whether platform checkers may check private addresses, including the
host's Docker socket, is now a platform setting; unset, it stays as
before: allowed self-hosted, forbidden in the cloud edition. Each of an
organization's checkers has its own switch, on by default, settable at
creation and toggled in the checker list. Both apply from the checker's
next poll.

Organizations choose in their settings which of the platform's check
types their members are offered; existing monitors keep running, and if
the platform later switches off everything an organization kept, it is
offered the platform's types again rather than none. Forms marked
data-autosave are sent when a field changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 17:18:22 +01:00

180 lines
5.1 KiB
Go

package api
import (
"context"
"encoding/json"
"errors"
"net/http"
"slices"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5"
"git.zb-server.de/ZB-Server/uptimesebi/internal/check"
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
"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.
const settingDisabledChecks = "disabled_check_types"
func (s *Server) disabledChecks(ctx context.Context) map[string]bool {
set, err := s.checkTypes.get(ctx, s.rdb, func(ctx context.Context) (map[string]bool, error) {
out := map[string]bool{}
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
raw, err := q.GetPlatformSetting(ctx, settingDisabledChecks)
if errors.Is(err, pgx.ErrNoRows) {
return nil
}
if err != nil {
return err
}
var list []string
_ = json.Unmarshal(raw, &list)
for _, t := range list {
out[t] = true
}
return nil
})
return out, err
})
if err != nil {
s.log.WarnContext(ctx, "loading disabled check types failed", "error", err)
}
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)
}
// 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 {
off := s.disabledChecks(ctx)
var platform, out []string
for _, t := range check.Types {
if off[string(t)] && string(t) != keep {
continue
}
platform = append(platform, string(t))
if !slices.Contains(org.DisabledCheckTypes, string(t)) || string(t) == keep {
out = append(out, string(t))
}
}
if len(out) == 0 {
return platform
}
return out
}
type checkTypeView struct {
Type string `json:"type"`
Enabled bool `json:"enabled"`
}
func (s *Server) adminListCheckTypes(c *gin.Context) {
off := s.disabledChecks(c.Request.Context())
out := make([]checkTypeView, len(check.Types))
for i, t := range check.Types {
out[i] = checkTypeView{Type: string(t), Enabled: !off[string(t)]}
}
c.JSON(http.StatusOK, gin.H{"types": out})
}
type checkTypesRequest struct {
// Enabled lists the types to offer; all others are switched off.
Enabled []string `json:"enabled" binding:"max=100"`
}
func (s *Server) adminSetCheckTypes(c *gin.Context) {
var req checkTypesRequest
if !bind(c, &req) {
return
}
var disabled []string
for _, t := range check.Types {
if !slices.Contains(req.Enabled, string(t)) {
disabled = append(disabled, string(t))
}
}
for _, t := range req.Enabled {
if !slices.Contains(check.Types, check.Type(t)) {
fail(c, http.StatusUnprocessableEntity, "invalid_type")
return
}
}
if len(disabled) == len(check.Types) {
fail(c, http.StatusUnprocessableEntity, "all_check_types_disabled")
return
}
ctx := c.Request.Context()
raw, _ := json.Marshal(append([]string{}, disabled...))
auditDetail(c, "disabled", disabled)
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
return q.SetPlatformSetting(ctx, store.SetPlatformSettingParams{Key: settingDisabledChecks, Value: raw})
})
if err != nil {
internalError(c, err)
return
}
if err := s.checkTypes.bump(ctx, s.rdb); err != nil {
s.log.ErrorContext(ctx, "announcing check type change failed", "error", err)
}
c.Status(http.StatusNoContent)
}
// orgCheckTypes tells API clients which monitor types they can create.
func (s *Server) orgCheckTypes(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"types": s.enabledCheckTypes(c.Request.Context(), current(c).Org, "")})
}
// orgSetCheckTypes chooses which of the platform's types the organization
// offers its members.
func (s *Server) orgSetCheckTypes(c *gin.Context) {
var req checkTypesRequest
if !bind(c, &req) {
return
}
p := current(c)
ctx := c.Request.Context()
off := s.disabledChecks(ctx)
disabled := []string{}
enabled := 0
for _, t := range check.Types {
switch {
case slices.Contains(req.Enabled, string(t)):
enabled++
case !off[string(t)]:
disabled = append(disabled, string(t))
}
}
for _, t := range req.Enabled {
if !slices.Contains(check.Types, check.Type(t)) {
fail(c, http.StatusUnprocessableEntity, "invalid_type")
return
}
}
if enabled == 0 {
fail(c, http.StatusUnprocessableEntity, "all_check_types_disabled")
return
}
auditDetail(c, "disabled", disabled)
err := s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
return q.SetOrgDisabledCheckTypes(ctx, store.SetOrgDisabledCheckTypesParams{ID: p.Org.ID, DisabledCheckTypes: disabled})
})
if err != nil {
internalError(c, err)
return
}
c.Status(http.StatusNoContent)
}