package api import ( "context" "encoding/json" "errors" "net/http" "github.com/gin-gonic/gin" "github.com/google/uuid" "github.com/jackc/pgx/v5" "git.zb-server.de/ZB-Server/uptimesebi/internal/store" "git.zb-server.de/ZB-Server/uptimesebi/internal/tenant" ) // Whether checkers may check private addresses: platform checkers follow // a platform setting, organizations' checkers their own flag. const settingPlatformPrivate = "platform_checkers_private" // platformPrivate reports whether platform checkers may reach private // addresses. Unset, they may on self-hosted installations only: in the // cloud they share the operator's network. func (s *Server) platformPrivate(ctx context.Context) bool { v, err := s.checkerNet.get(ctx, s.rdb, func(ctx context.Context) (bool, error) { allow := !s.cloud() err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error { raw, err := q.GetPlatformSetting(ctx, settingPlatformPrivate) if errors.Is(err, pgx.ErrNoRows) { return nil } if err != nil { return err } return json.Unmarshal(raw, &allow) }) return allow, err }) if err != nil { s.log.WarnContext(ctx, "loading checker network setting failed", "error", err) return false } return v } func (s *Server) allowPrivate(ctx context.Context, a store.GetAgentByHashRow) bool { if a.OrgID != nil { return a.AllowPrivate } return s.platformPrivate(ctx) } type checkerNetwork struct { // AllowPrivate lets platform checkers check private addresses. AllowPrivate bool `json:"allow_private"` } func (s *Server) adminGetCheckerNetwork(c *gin.Context) { c.JSON(http.StatusOK, checkerNetwork{AllowPrivate: s.platformPrivate(c.Request.Context())}) } func (s *Server) adminSetCheckerNetwork(c *gin.Context) { var req checkerNetwork if !bind(c, &req) { return } ctx := c.Request.Context() raw, _ := json.Marshal(req.AllowPrivate) auditDetail(c, "allow_private", req.AllowPrivate) err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error { return q.SetPlatformSetting(ctx, store.SetPlatformSettingParams{Key: settingPlatformPrivate, Value: raw}) }) if err != nil { internalError(c, err) return } if err := s.checkerNet.bump(ctx, s.rdb); err != nil { s.log.ErrorContext(ctx, "announcing checker network change failed", "error", err) } c.Status(http.StatusNoContent) } type agentUpdate struct { AllowPrivate bool `json:"allow_private"` } // updateOrgAgent changes whether one of the organization's checkers may // check private addresses; it applies from the checker's next poll. func (s *Server) updateOrgAgent(c *gin.Context) { id, err := uuid.Parse(c.Param("id")) if err != nil { fail(c, http.StatusNotFound, "not_found") return } var req agentUpdate if !bind(c, &req) { return } p := current(c) ctx := c.Request.Context() auditDetail(c, "allow_private", req.AllowPrivate) var n int64 err = s.db.Tx(ctx, p.scope(), func(q *store.Queries) error { n, err = q.SetAgentAllowPrivate(ctx, store.SetAgentAllowPrivateParams{ID: id, OrgID: &p.Org.ID, AllowPrivate: req.AllowPrivate}) return err }) if err != nil { internalError(c, err) return } if n == 0 { fail(c, http.StatusNotFound, "not_found") return } c.Status(http.StatusNoContent) }