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) }