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