Files
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

118 lines
3.2 KiB
Go

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