Files
uptimesebi/internal/api/dbcheck_test.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

74 lines
2.7 KiB
Go

package api_test
import (
"context"
"net/http"
"net/url"
"os"
"strings"
"testing"
"time"
"github.com/jackc/pgx/v5"
)
func TestDatabaseMonitorSecrets(t *testing.T) {
e := newEnv(t)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
go e.api.RunScheduler(ctx)
tok, region := e.platformChecker(t, uniq("eu-"))
e.startChecker(t, tok)
pg, _ := url.Parse(os.Getenv("UPTIME_TEST_DATABASE_URL"))
owner := e.browser(t)
owner.signup("o@"+uniq("db")+".test", uniq("db-"))
monitor := map[string]any{
"name": "Main DB", "type": "postgres", "target": pg.Host, "interval_seconds": 10, "timeout_ms": 5000, "confirmations": 1,
"regions": []string{region},
"config": map[string]any{"user": "uptime", "password": "uptime", "database": "uptime_test", "tls": "disable", "query": "SELECT 41 + 1", "expect": "42"},
}
st, body := owner.do("POST", "/api/v1/org/monitors", monitor)
if st != http.StatusCreated {
t.Fatalf("create = %d %v", st, body)
}
id := body["id"].(string)
if _, hasPassword := body["config"].(map[string]any)["password"]; hasPassword || mustJSON(body["secrets_set"]) != `["password"]` {
t.Errorf("create answer exposes or misses the password: %s", mustJSON(body))
}
var stored string
_ = e.asSystem(t, func(tx pgx.Tx) error {
return tx.QueryRow(context.Background(), `SELECT config::text FROM monitors WHERE id = $1`, id).Scan(&stored)
})
if strings.Contains(stored, `"password"`) || !strings.Contains(stored, "sealed_password") {
t.Errorf("stored config = %s", stored)
}
latestOK := func() bool {
_, list := owner.do("GET", "/api/v1/org/monitors", nil)
for _, m := range list["monitors"].([]any) {
if l := m.(map[string]any)["latest"].([]any); len(l) > 0 && l[0].(map[string]any)["ok"] == true {
return true
}
}
return false
}
waitFor(t, 15*time.Second, "the checker signing in with the sealed password", latestOK)
// Saving without the password keeps it.
monitor["config"] = map[string]any{"user": "uptime", "database": "uptime_test", "tls": "disable", "query": "SELECT 41 + 1", "expect": "42"}
if st, body := owner.do("PUT", "/api/v1/org/monitors/"+id, monitor); st != http.StatusOK || mustJSON(body["secrets_set"]) != `["password"]` {
t.Fatalf("update without password = %d %v", st, body)
}
if _, _, page := owner.page("/monitors/" + id + "/edit"); strings.Contains(page, `value="uptime_test"`) == false || strings.Contains(page, "sealed_") {
t.Error("the editor lacks the settings or shows the sealed password")
}
_, list := owner.do("GET", "/api/v1/org/monitors", nil)
_, _, export := owner.download("/api/v1/org/export?format=json")
for _, raw := range []string{mustJSON(list), string(export)} {
if strings.Contains(raw, "sealed_password") {
t.Error("a sealed password leaves the API")
}
}
}