2026-09-22 13:13:10 +02:00
|
|
|
package probe
|
|
|
|
|
|
|
|
|
|
import (
|
2026-09-22 13:58:28 +02:00
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"net"
|
2026-09-22 13:13:10 +02:00
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"tea.karlcom.de/karl/bucketeer/internal/model"
|
|
|
|
|
)
|
|
|
|
|
|
2026-09-22 13:58:28 +02:00
|
|
|
func serverHost(t *testing.T, h http.Handler) string {
|
2026-09-22 13:13:10 +02:00
|
|
|
t.Helper()
|
2026-09-22 13:58:28 +02:00
|
|
|
srv := httptest.NewTLSServer(h)
|
2026-09-22 13:13:10 +02:00
|
|
|
t.Cleanup(srv.Close)
|
2026-09-22 13:58:28 +02:00
|
|
|
return strings.TrimPrefix(srv.URL, "https://")
|
2026-09-22 13:13:10 +02:00
|
|
|
}
|
|
|
|
|
|
2026-09-22 13:58:28 +02:00
|
|
|
func statusHandler(status int) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(status) })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestProbeStorageStatuses(t *testing.T) {
|
2026-09-22 13:13:10 +02:00
|
|
|
cases := []struct {
|
|
|
|
|
status int
|
|
|
|
|
want model.ExposureState
|
|
|
|
|
}{
|
|
|
|
|
{http.StatusOK, model.ExposurePublicRead},
|
|
|
|
|
{http.StatusForbidden, model.ExposureAuthRequired},
|
|
|
|
|
{http.StatusUnauthorized, model.ExposureAuthRequired},
|
|
|
|
|
{http.StatusNotFound, model.ExposureNotFound},
|
|
|
|
|
{http.StatusInternalServerError, model.ExposureReachable},
|
|
|
|
|
}
|
2026-09-22 13:58:28 +02:00
|
|
|
for _, tc := range cases {
|
|
|
|
|
p := New(Config{Workers: 1, Timeout: time.Second})
|
|
|
|
|
host := serverHost(t, statusHandler(tc.status))
|
2026-09-22 17:12:30 +02:00
|
|
|
r := p.ProbeDetailed(context.Background(), model.Candidate{
|
2026-09-22 13:58:28 +02:00
|
|
|
Hostname: host, Provider: model.ProviderAWS, Service: model.ServiceS3,
|
|
|
|
|
})
|
2026-09-22 17:12:30 +02:00
|
|
|
if r.Exposure != tc.want {
|
|
|
|
|
t.Errorf("status %d: got %s (%s), want %s", tc.status, r.Exposure, r.Detail, tc.want)
|
2026-09-22 13:13:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-22 13:58:28 +02:00
|
|
|
|
|
|
|
|
func TestProbeS3RegionRedirect(t *testing.T) {
|
|
|
|
|
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("x-amz-bucket-region", "eu-central-1")
|
|
|
|
|
w.WriteHeader(http.StatusMovedPermanently)
|
|
|
|
|
})
|
|
|
|
|
p := New(Config{Workers: 1, Timeout: time.Second})
|
|
|
|
|
host := serverHost(t, h)
|
2026-09-22 17:12:30 +02:00
|
|
|
r := p.ProbeDetailed(context.Background(), model.Candidate{
|
2026-09-22 13:58:28 +02:00
|
|
|
Hostname: host, Provider: model.ProviderAWS, Service: model.ServiceS3,
|
|
|
|
|
})
|
2026-09-22 17:12:30 +02:00
|
|
|
if r.Exposure != model.ExposureReachable || !strings.Contains(r.Detail, "eu-central-1") {
|
|
|
|
|
t.Fatalf("got %s (%s), want reachable with region", r.Exposure, r.Detail)
|
2026-09-22 13:58:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestProbeFirebase(t *testing.T) {
|
|
|
|
|
p := New(Config{Workers: 1, Timeout: time.Second})
|
|
|
|
|
host := serverHost(t, statusHandler(http.StatusUnauthorized))
|
2026-09-22 17:12:30 +02:00
|
|
|
r := p.ProbeDetailed(context.Background(), model.Candidate{
|
2026-09-22 13:58:28 +02:00
|
|
|
Hostname: host, Provider: model.ProviderGCP, Service: model.ServiceFirebaseDB,
|
|
|
|
|
})
|
2026-09-22 17:12:30 +02:00
|
|
|
if r.Exposure != model.ExposureAuthRequired {
|
|
|
|
|
t.Fatalf("got %s, want auth_required", r.Exposure)
|
2026-09-22 13:58:28 +02:00
|
|
|
}
|
|
|
|
|
host2 := serverHost(t, statusHandler(http.StatusOK))
|
2026-09-22 17:12:30 +02:00
|
|
|
r = p.ProbeDetailed(context.Background(), model.Candidate{
|
2026-09-22 13:58:28 +02:00
|
|
|
Hostname: host2, Provider: model.ProviderGCP, Service: model.ServiceFirebaseDB,
|
|
|
|
|
})
|
2026-09-22 17:12:30 +02:00
|
|
|
if r.Exposure != model.ExposurePublicRead {
|
|
|
|
|
t.Fatalf("got %s, want public_read", r.Exposure)
|
2026-09-22 13:58:28 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-22 14:47:49 +02:00
|
|
|
func TestProbeAzureContainerDiscovery(t *testing.T) {
|
|
|
|
|
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.URL.Path == "/images" && r.URL.Query().Get("comp") == "list" {
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
|
})
|
|
|
|
|
p := New(Config{Workers: 1, Timeout: time.Second})
|
|
|
|
|
host := serverHost(t, h)
|
2026-09-22 17:12:30 +02:00
|
|
|
r := p.ProbeDetailed(context.Background(), model.Candidate{
|
2026-09-22 14:47:49 +02:00
|
|
|
Hostname: host, Resource: "acct", Provider: model.ProviderAzure, Service: model.ServiceBlobStorage,
|
|
|
|
|
})
|
2026-09-22 17:12:30 +02:00
|
|
|
if r.Exposure != model.ExposurePublicRead || !strings.Contains(r.Detail, "images") {
|
|
|
|
|
t.Fatalf("got %s (%s), want public_read via container 'images'", r.Exposure, r.Detail)
|
2026-09-22 14:47:49 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-22 13:58:28 +02:00
|
|
|
func TestProbeDatabaseTCP(t *testing.T) {
|
|
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer ln.Close()
|
|
|
|
|
go func() {
|
|
|
|
|
for {
|
|
|
|
|
conn, err := ln.Accept()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
conn.Close()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
host, port, _ := net.SplitHostPort(ln.Addr().String())
|
|
|
|
|
p := New(Config{Workers: 1, Timeout: time.Second})
|
2026-09-22 14:47:49 +02:00
|
|
|
r := p.checkTCP(context.Background(), host, mustAtoi(port))
|
|
|
|
|
if r.Exposure != model.ExposureReachable {
|
|
|
|
|
t.Fatalf("got %+v, want reachable", r)
|
2026-09-22 13:58:28 +02:00
|
|
|
}
|
2026-09-22 14:47:49 +02:00
|
|
|
if r.ObjectReadable || r.Listable {
|
|
|
|
|
t.Fatalf("tcp check must not claim readability: %+v", r)
|
2026-09-22 13:58:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A closed port yields unknown, never readable.
|
2026-09-22 14:47:49 +02:00
|
|
|
if r := p.checkTCP(context.Background(), "127.0.0.1", 1); r.Exposure == model.ExposurePublicRead {
|
2026-09-22 13:58:28 +02:00
|
|
|
t.Fatal("closed port must never be public_read")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestClassifyErrorNXDOMAIN(t *testing.T) {
|
|
|
|
|
state, detail := classifyError(&net.DNSError{Err: "no such host", IsNotFound: true})
|
|
|
|
|
if state != model.ExposurePrivate || !strings.Contains(detail, "no public record") {
|
|
|
|
|
t.Fatalf("got %s (%s)", state, detail)
|
|
|
|
|
}
|
|
|
|
|
if state, _ := classifyError(errors.New("boom")); state != model.ExposureUnknown {
|
|
|
|
|
t.Fatalf("got %s, want unknown", state)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mustAtoi(s string) int {
|
|
|
|
|
n := 0
|
|
|
|
|
for _, c := range s {
|
|
|
|
|
n = n*10 + int(c-'0')
|
|
|
|
|
}
|
|
|
|
|
return n
|
|
|
|
|
}
|