feat(check): PostgreSQL, MySQL/MariaDB, SQL Server, MongoDB and Redis checks
Each connects to host:port with the configured credentials and TLS mode, signs in and runs a query (a ping for MongoDB and Redis), optionally comparing the first value. Connections go through the network guard, and errors never repeat the password. Tested against real servers. Refs #93 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
b01eefdbce
commit
a62ffb43de
5 files changed
+522
-2
No files matched your search
@@ -0,0 +1,122 @@
|
||||
package check
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// dbTarget returns the address of a test database or skips.
|
||||
func dbTarget(t *testing.T, env string) string {
|
||||
t.Helper()
|
||||
v := os.Getenv(env)
|
||||
if v == "" {
|
||||
t.Skip(env + " is not set")
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func runDB(t *testing.T, typ Type, target string, cfg map[string]any, allowPrivate bool) Outcome {
|
||||
t.Helper()
|
||||
raw, _ := json.Marshal(cfg)
|
||||
c, err := Validate(typ, target, raw)
|
||||
if err != nil {
|
||||
t.Fatalf("Validate(%s) = %v", raw, err)
|
||||
}
|
||||
timeout := 15 * time.Second
|
||||
if !allowPrivate {
|
||||
// MongoDB retries a refused server until the timeout.
|
||||
timeout = 3 * time.Second
|
||||
}
|
||||
return Run(context.Background(), Spec{Type: typ, Target: target, Timeout: timeout, Config: c, AllowPrivate: allowPrivate})
|
||||
}
|
||||
|
||||
func TestDatabaseChecks(t *testing.T) {
|
||||
pgTarget := func() string {
|
||||
u, err := url.Parse(os.Getenv("UPTIME_TEST_DATABASE_URL"))
|
||||
if err != nil || u.Host == "" {
|
||||
t.Skip("UPTIME_TEST_DATABASE_URL is not set")
|
||||
}
|
||||
return u.Host
|
||||
}
|
||||
redisTarget := func() string {
|
||||
u, err := url.Parse(os.Getenv("UPTIME_TEST_REDIS_URL"))
|
||||
if err != nil || u.Host == "" {
|
||||
t.Skip("UPTIME_TEST_REDIS_URL is not set")
|
||||
}
|
||||
return u.Host
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
typ Type
|
||||
target func() string
|
||||
good map[string]any
|
||||
bad map[string]any
|
||||
}{
|
||||
{"postgres", Postgres, pgTarget,
|
||||
map[string]any{"user": "uptime", "password": "uptime", "database": "uptime_test", "tls": "disable", "query": "SELECT 41 + 1", "expect": "42"},
|
||||
map[string]any{"user": "uptime", "password": "wrong-pw-123", "database": "uptime_test", "tls": "disable"}},
|
||||
{"mariadb", MySQL, func() string { return dbTarget(t, "UPTIME_TEST_MYSQL_ADDR") },
|
||||
map[string]any{"user": "root", "password": "checkpw", "database": "shop", "tls": "disable", "query": "SELECT 41 + 1", "expect": "42"},
|
||||
map[string]any{"user": "root", "password": "wrong-pw-123", "tls": "disable"}},
|
||||
{"mssql", MSSQL, func() string { return dbTarget(t, "UPTIME_TEST_MSSQL_ADDR") },
|
||||
map[string]any{"user": "sa", "password": "Check-pw-2026", "database": "master", "tls": "require", "query": "SELECT 41 + 1", "expect": "42"},
|
||||
map[string]any{"user": "sa", "password": "wrong-pw-123", "tls": "require"}},
|
||||
{"mongodb", MongoDB, func() string { return dbTarget(t, "UPTIME_TEST_MONGO_ADDR") },
|
||||
map[string]any{"user": "root", "password": "checkpw", "database": "admin", "tls": "disable"},
|
||||
map[string]any{"user": "root", "password": "wrong-pw-123", "database": "admin", "tls": "disable"}},
|
||||
{"redis", Redis, redisTarget,
|
||||
map[string]any{"database": "15", "tls": "disable"},
|
||||
map[string]any{"user": "nobody", "password": "wrong-pw-123", "tls": "disable"}},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
target := tc.target()
|
||||
if o := runDB(t, tc.typ, target, tc.good, true); !o.OK {
|
||||
t.Fatalf("good credentials: %+v", o)
|
||||
}
|
||||
o := runDB(t, tc.typ, target, tc.bad, true)
|
||||
if o.OK {
|
||||
t.Fatal("wrong password accepted")
|
||||
}
|
||||
if strings.Contains(o.Error, "wrong-pw-123") {
|
||||
t.Errorf("error repeats the password: %s", o.Error)
|
||||
}
|
||||
// Shared checkers must not reach private networks.
|
||||
if o := runDB(t, tc.typ, target, tc.good, false); o.OK || !strings.Contains(o.Error, "non-public") && !strings.Contains(o.Error, "timeout") {
|
||||
t.Errorf("private address reached without permission: %+v", o)
|
||||
}
|
||||
})
|
||||
}
|
||||
t.Run("unexpected result", func(t *testing.T) {
|
||||
o := runDB(t, Postgres, pgTarget(), map[string]any{"user": "uptime", "password": "uptime", "database": "uptime_test", "tls": "disable", "query": "SELECT 'degraded'", "expect": "ok"}, true)
|
||||
if o.OK || !strings.Contains(o.Error, `"degraded"`) {
|
||||
t.Errorf("Run() = %+v", o)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestDatabaseValidate(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
typ Type
|
||||
target string
|
||||
cfg string
|
||||
}{
|
||||
{Postgres, "db.example.com", `{}`},
|
||||
{Postgres, "db.example.com:99999", `{}`},
|
||||
{Postgres, "db.example.com:5432", `{"tls":"maybe"}`},
|
||||
{Redis, "cache:6379", `{"query":"INFO"}`},
|
||||
{Redis, "cache:6379", `{"database":"16"}`},
|
||||
} {
|
||||
if _, err := Validate(tc.typ, tc.target, json.RawMessage(tc.cfg)); err == nil {
|
||||
t.Errorf("Validate(%s, %s, %s) accepted", tc.typ, tc.target, tc.cfg)
|
||||
}
|
||||
}
|
||||
if got := SecretFields(MySQL); len(got) != 1 || got[0] != "password" {
|
||||
t.Errorf("SecretFields(mysql) = %v", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user