feat(check): add http, tcp, tls, dns and icmp checks
Validate normalizes settings for the API; Run executes them on checkers. Targets on private networks are refused unless explicitly allowed. Refs #12 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
bc8e934568
commit
55559c8b0a
6 files changed
+710
-1
No files matched your search
@@ -0,0 +1,143 @@
|
||||
package check
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func mustValidate(t *testing.T, typ Type, target, config string) json.RawMessage {
|
||||
t.Helper()
|
||||
c, err := Validate(typ, target, json.RawMessage(config))
|
||||
if err != nil {
|
||||
t.Fatalf("Validate(%s, %q, %s) failed: %v", typ, target, config, err)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func run(t *testing.T, typ Type, target, config string) Outcome {
|
||||
t.Helper()
|
||||
return Run(context.Background(), Spec{
|
||||
Type: typ, Target: target, Timeout: 2 * time.Second,
|
||||
Config: mustValidate(t, typ, target, config), AllowPrivate: true,
|
||||
})
|
||||
}
|
||||
|
||||
func TestHTTP(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/ok":
|
||||
_, _ = w.Write([]byte("all systems operational"))
|
||||
case "/fail":
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
case "/moved":
|
||||
http.Redirect(w, r, "/ok", http.StatusMovedPermanently)
|
||||
case "/slow":
|
||||
time.Sleep(3 * time.Second)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
tests := []struct {
|
||||
name, path, config string
|
||||
wantOK bool
|
||||
wantErr string
|
||||
}{
|
||||
{"ok", "/ok", `{}`, true, ""},
|
||||
{"server error", "/fail", `{}`, false, "unexpected status 503"},
|
||||
{"accepted error code", "/fail", `{"expect_status":["503"]}`, true, ""},
|
||||
{"keyword present", "/ok", `{"keyword":"operational"}`, true, ""},
|
||||
{"keyword missing", "/ok", `{"keyword":"outage"}`, false, "keyword not found"},
|
||||
{"keyword must be absent", "/ok", `{"keyword":"operational","keyword_absent":true}`, false, "keyword present"},
|
||||
{"redirect followed", "/moved", `{"expect_status":["200"]}`, true, ""},
|
||||
{"redirect not followed", "/moved", `{"expect_status":["200"],"follow_redirects":false}`, false, "unexpected status 301"},
|
||||
{"timeout", "/slow", `{}`, false, "timeout after 2s"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
o := run(t, HTTP, srv.URL+tt.path, tt.config)
|
||||
if o.OK != tt.wantOK || o.Error != tt.wantErr {
|
||||
t.Errorf("Run() = ok %t err %q, want ok %t err %q", o.OK, o.Error, tt.wantOK, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrivateTargetsBlocked(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
||||
defer srv.Close()
|
||||
host := strings.TrimPrefix(srv.URL, "http://")
|
||||
for _, s := range []Spec{
|
||||
{Type: HTTP, Target: srv.URL, Config: mustValidate(t, HTTP, srv.URL, `{}`)},
|
||||
{Type: TCP, Target: host, Config: json.RawMessage(`{}`)},
|
||||
} {
|
||||
s.Timeout = time.Second
|
||||
if o := Run(context.Background(), s); o.OK || o.Error != "target resolves to a non-public address" {
|
||||
t.Errorf("Run(%s %s) without AllowPrivate = %+v, want blocked", s.Type, s.Target, o)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTCP(t *testing.T) {
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
addr := ln.Addr().String()
|
||||
if o := run(t, TCP, addr, `{}`); !o.OK {
|
||||
t.Errorf("Run(tcp open port) = %+v, want ok", o)
|
||||
}
|
||||
_ = ln.Close()
|
||||
if o := run(t, TCP, addr, `{}`); o.OK || !strings.HasPrefix(o.Error, "connection failed") {
|
||||
t.Errorf("Run(tcp closed port) = %+v, want connection failed", o)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTLS(t *testing.T) {
|
||||
srv := httptest.NewTLSServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
||||
defer srv.Close()
|
||||
host := strings.TrimPrefix(srv.URL, "https://")
|
||||
// The httptest certificate is self-signed, so verification fails; that
|
||||
// is reported like any other TLS problem.
|
||||
if o := run(t, TLS, host, `{"server_name":"example.com"}`); o.OK || !strings.Contains(o.Error, "certificate") {
|
||||
t.Errorf("Run(tls self-signed) = %+v, want certificate error", o)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate(t *testing.T) {
|
||||
tests := []struct {
|
||||
typ Type
|
||||
target, config string
|
||||
want string
|
||||
}{
|
||||
{HTTP, "ftp://x", `{}`, "invalid_target"},
|
||||
{HTTP, "https://user:pw@x.test", `{}`, "invalid_target"},
|
||||
{HTTP, "https://x.test", `{"method":"POST"}`, "invalid_method"},
|
||||
{HTTP, "https://x.test", `{"method":"HEAD","keyword":"x"}`, "keyword_needs_get"},
|
||||
{HTTP, "https://x.test", `{"expect_status":["6xx"]}`, "invalid_expect_status"},
|
||||
{HTTP, "https://x.test", `{"headers":{"X":"a\r\nb"}}`, "invalid_header"},
|
||||
{TCP, "x.test", `{}`, "invalid_target"},
|
||||
{TCP, "x.test:0", `{}`, "invalid_target"},
|
||||
{DNS, "x.test", `{"record":"SRV"}`, "invalid_record"},
|
||||
{TLS, "x.test:443", `{"min_days_valid":-1}`, "invalid_config"},
|
||||
{"smtp", "x.test", `{}`, "invalid_type"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
_, err := Validate(tt.typ, tt.target, json.RawMessage(tt.config))
|
||||
var inv ErrInvalid
|
||||
if !errors.As(err, &inv) || inv.Code != tt.want {
|
||||
t.Errorf("Validate(%s, %q, %s) = %v, want %s", tt.typ, tt.target, tt.config, err, tt.want)
|
||||
}
|
||||
}
|
||||
got := mustValidate(t, HTTP, "https://x.test", `{}`)
|
||||
want := `{"method":"GET","expect_status":["2xx","3xx"],"follow_redirects":true,"verify_tls":true}`
|
||||
if string(got) != want {
|
||||
t.Errorf("Validate(http defaults) = %s, want %s", got, want)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user