package api import ( "context" "encoding/json" "errors" "net/http" "slices" "github.com/gin-gonic/gin" "github.com/google/uuid" "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 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" 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 } // 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 } // 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 out, platform []checkTypeChoice for _, ty := range check.Types { t := string(ty) passive := check.Passive(ty) if off[t] && t != keep && (passive || !ownRegions) { continue } 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"` } 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; // own_checkers_only are those the platform's checkers do not run. func (s *Server) orgCheckTypes(c *gin.Context) { 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}) } 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) { return } p := current(c) ctx := c.Request.Context() off := s.disabledChecks(ctx) disabled := []string{} enabled := 0 for _, ty := range check.Types { t := string(ty) switch { case off[t] && check.Passive(ty): if slices.Contains(p.Org.DisabledCheckTypes, t) { disabled = append(disabled, t) } case slices.Contains(req.Enabled, t): enabled++ default: disabled = append(disabled, 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) }