Files
uptimesebi/internal/api/monitorsecrets.go
T
Sebastian UnterschützandClaude Opus 5.5 d3cf9f48e1
ci / image (pull_request) Successful in 2m38s
ci / build (pull_request) Successful in 7m5s
ci / lint (pull_request) Successful in 10m15s
ci / test (pull_request) Failing after 11m53s
feat: database monitors with sealed credentials
Secret monitor settings such as database passwords are stored sealed,
bound to monitor type and field, never returned by the API, the console
or exports, kept when an edit leaves them empty, and opened only for the
checker's job. Monitor types are validated by the check package; the
database only checks their format. Compose gets a checks profile with
MariaDB, MongoDB and SQL Server, and CI tests against MariaDB and
MongoDB.

Closes #93

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

99 lines
2.4 KiB
Go

package api
import (
"encoding/base64"
"encoding/json"
"slices"
"strings"
"git.zb-server.de/ZB-Server/uptimesebi/internal/check"
)
// Monitor settings listed by check.SecretFields, such as database
// passwords, are stored as "sealed_<field>": encrypted and bound to the
// monitor type and field. They are never returned; checkers get them in
// plain text with their jobs.
const sealedPrefix = "sealed_"
func monitorSecretAAD(t check.Type, field string) []byte {
return []byte("monitor:" + string(t) + ":" + field)
}
// sealMonitorConfig replaces secret settings in cfg by their sealed form.
// A secret left empty keeps its value from previous, so forms need not ask
// for passwords again.
func (s *Server) sealMonitorConfig(t check.Type, cfg, previous []byte) []byte {
fields := check.SecretFields(t)
if len(fields) == 0 {
return cfg
}
var m, old map[string]any
if json.Unmarshal(cfg, &m) != nil {
return cfg
}
_ = json.Unmarshal(previous, &old)
for _, f := range fields {
v, _ := m[f].(string)
delete(m, f)
switch {
case v != "":
m[sealedPrefix+f] = base64.StdEncoding.EncodeToString(s.keys.Seal([]byte(v), monitorSecretAAD(t, f)))
case old[sealedPrefix+f] != nil:
m[sealedPrefix+f] = old[sealedPrefix+f]
}
}
out, _ := json.Marshal(m)
return out
}
// openMonitorConfig restores secret settings for a checker.
func (s *Server) openMonitorConfig(t check.Type, cfg []byte) ([]byte, error) {
fields := check.SecretFields(t)
if len(fields) == 0 {
return cfg, nil
}
var m map[string]any
if err := json.Unmarshal(cfg, &m); err != nil {
return nil, err
}
for _, f := range fields {
sealed, ok := m[sealedPrefix+f].(string)
delete(m, sealedPrefix+f)
if !ok {
continue
}
raw, err := base64.StdEncoding.DecodeString(sealed)
if err != nil {
return nil, err
}
plain, err := s.keys.Open(raw, monitorSecretAAD(t, f))
if err != nil {
return nil, err
}
m[f] = string(plain)
}
return json.Marshal(m)
}
// publicMonitorConfig drops sealed secrets from cfg and names those set.
func publicMonitorConfig(cfg []byte) ([]byte, []string) {
var m map[string]any
if json.Unmarshal(cfg, &m) != nil {
return cfg, nil
}
var set []string
for k := range m {
if f, ok := strings.CutPrefix(k, sealedPrefix); ok {
set = append(set, f)
delete(m, k)
}
}
if set == nil {
return cfg, nil
}
slices.Sort(set)
out, _ := json.Marshal(m)
return out, set
}