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") } } } func TestProtocolMonitorSecrets(t *testing.T) { e := newEnv(t) _, region := e.platformChecker(t, uniq("eu-")) owner := e.browser(t) owner.signup("o@"+uniq("proto")+".test", uniq("proto-")) for _, tc := range []struct { typ, target string config map[string]any secrets string }{ {"radius", "radius.example.com:1812", map[string]any{"secret": "radius-shared-1", "user": "probe", "password": "radius-pw-1"}, `["password","secret"]`}, {"snmp", "switch.example.com:161", map[string]any{"community": "snmp-comm-1", "oid": "1.3.6.1.2.1.1.5.0"}, `["community"]`}, } { st, body := owner.do("POST", "/api/v1/org/monitors", map[string]any{ "name": tc.typ, "type": tc.typ, "target": tc.target, "interval_seconds": 60, "regions": []string{region}, "config": tc.config, }) if st != http.StatusCreated || mustJSON(body["secrets_set"]) != tc.secrets { t.Fatalf("create %s = %d %s", tc.typ, st, mustJSON(body)) } _, _, page := owner.page("/monitors/" + body["id"].(string) + "/edit") for _, v := range tc.config { if s := v.(string); strings.Contains(s, "-1") && (strings.Contains(page, s) || strings.Contains(mustJSON(body), s)) { t.Errorf("%s: secret %q leaves the server", tc.typ, s) } } if !strings.Contains(page, `name="config.`) { t.Errorf("%s: editor lacks the settings", tc.typ) } } }