2026-09-22 14:33:03 +02:00
|
|
|
// Package webui serves a small read-only web UI over discovered cloud assets
|
|
|
|
|
// and their content index. It is dependency-free (stdlib + embedded HTML) and
|
2026-09-22 13:58:28 +02:00
|
|
|
// exposes no mutation endpoints.
|
|
|
|
|
package webui
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-09-22 14:33:03 +02:00
|
|
|
"embed"
|
2026-09-22 13:58:28 +02:00
|
|
|
"encoding/json"
|
2026-09-22 14:33:03 +02:00
|
|
|
"io/fs"
|
2026-09-22 15:09:34 +02:00
|
|
|
"log/slog"
|
2026-09-22 13:58:28 +02:00
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2026-09-22 15:09:34 +02:00
|
|
|
"sync/atomic"
|
|
|
|
|
"time"
|
2026-09-22 13:58:28 +02:00
|
|
|
|
2026-09-22 14:47:49 +02:00
|
|
|
"tea.karlcom.de/karl/bucketeer/internal/scan"
|
2026-09-22 13:58:28 +02:00
|
|
|
"tea.karlcom.de/karl/bucketeer/internal/store"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//go:embed index.html
|
|
|
|
|
var indexHTML []byte
|
|
|
|
|
|
2026-09-22 14:33:03 +02:00
|
|
|
//go:embed assets
|
|
|
|
|
var assetsFS embed.FS
|
|
|
|
|
|
2026-09-22 13:58:28 +02:00
|
|
|
// Store is the read-only subset of the database the UI needs.
|
|
|
|
|
type Store interface {
|
|
|
|
|
ListAssets(ctx context.Context, f store.AssetFilter) ([]store.Asset, error)
|
2026-09-22 14:33:03 +02:00
|
|
|
CountAssets(ctx context.Context, f store.AssetFilter) (int, error)
|
|
|
|
|
SearchEntries(ctx context.Context, query string, limit, offset int) ([]store.IndexEntry, int, error)
|
2026-09-22 13:58:28 +02:00
|
|
|
Stats(ctx context.Context) (store.Stats, error)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-22 14:47:49 +02:00
|
|
|
// Scanner probes and indexes a user-supplied host/URL on demand, and can
|
|
|
|
|
// re-probe already-discovered assets.
|
|
|
|
|
type Scanner interface {
|
|
|
|
|
Scan(ctx context.Context, target string) []scan.Result
|
|
|
|
|
Backfill(ctx context.Context, exposure string, limit int) (scan.BackfillReport, error)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-22 13:58:28 +02:00
|
|
|
type Config struct {
|
2026-09-22 14:47:49 +02:00
|
|
|
User string // optional HTTP basic auth username
|
|
|
|
|
Pass string // optional HTTP basic auth password
|
|
|
|
|
Scanner Scanner
|
2026-09-22 13:58:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New returns the UI handler. If Config.User is set, all routes require basic auth.
|
|
|
|
|
func New(s Store, cfg Config) http.Handler {
|
|
|
|
|
mux := http.NewServeMux()
|
2026-09-22 14:33:03 +02:00
|
|
|
assets, _ := fs.Sub(assetsFS, "assets")
|
|
|
|
|
fileServer := http.FileServer(http.FS(assets))
|
|
|
|
|
mux.Handle("/assets/", http.StripPrefix("/assets/", cacheForever(fileServer)))
|
2026-09-22 13:58:28 +02:00
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.URL.Path != "/" {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
|
_, _ = w.Write(indexHTML)
|
|
|
|
|
})
|
|
|
|
|
mux.HandleFunc("/api/assets", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
q := r.URL.Query()
|
|
|
|
|
limit, _ := strconv.Atoi(q.Get("limit"))
|
|
|
|
|
offset, _ := strconv.Atoi(q.Get("offset"))
|
2026-09-22 14:33:03 +02:00
|
|
|
f := store.AssetFilter{
|
2026-09-22 13:58:28 +02:00
|
|
|
Provider: strings.TrimSpace(q.Get("provider")),
|
|
|
|
|
Service: strings.TrimSpace(q.Get("service")),
|
|
|
|
|
Exposure: strings.TrimSpace(q.Get("exposure")),
|
|
|
|
|
Query: strings.TrimSpace(q.Get("q")),
|
2026-09-22 14:33:03 +02:00
|
|
|
Sort: strings.TrimSpace(q.Get("sort")),
|
2026-09-22 13:58:28 +02:00
|
|
|
Limit: limit,
|
|
|
|
|
Offset: offset,
|
2026-09-22 14:33:03 +02:00
|
|
|
}
|
|
|
|
|
assets, err := s.ListAssets(r.Context(), f)
|
2026-09-22 13:58:28 +02:00
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-09-22 14:33:03 +02:00
|
|
|
total, err := s.CountAssets(r.Context(), f)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
writeJSON(w, map[string]any{"assets": assets, "total": total, "limit": limit, "offset": offset})
|
|
|
|
|
})
|
|
|
|
|
mux.HandleFunc("/api/entries", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
q := r.URL.Query()
|
|
|
|
|
limit, _ := strconv.Atoi(q.Get("limit"))
|
|
|
|
|
offset, _ := strconv.Atoi(q.Get("offset"))
|
|
|
|
|
entries, total, err := s.SearchEntries(r.Context(), strings.TrimSpace(q.Get("q")), limit, offset)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
writeJSON(w, map[string]any{"entries": entries, "total": total, "limit": limit, "offset": offset})
|
2026-09-22 13:58:28 +02:00
|
|
|
})
|
2026-09-22 14:47:49 +02:00
|
|
|
if cfg.Scanner != nil {
|
|
|
|
|
mux.HandleFunc("/api/scan", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
target := strings.TrimSpace(r.URL.Query().Get("target"))
|
|
|
|
|
if r.Method == http.MethodPost {
|
|
|
|
|
var body struct {
|
|
|
|
|
Target string `json:"target"`
|
|
|
|
|
}
|
|
|
|
|
_ = json.NewDecoder(http.MaxBytesReader(w, r.Body, 4096)).Decode(&body)
|
|
|
|
|
if body.Target != "" {
|
|
|
|
|
target = strings.TrimSpace(body.Target)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if target == "" || len(target) > 512 {
|
|
|
|
|
http.Error(w, "target required (<=512 chars)", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
writeJSON(w, cfg.Scanner.Scan(r.Context(), target))
|
|
|
|
|
})
|
2026-09-22 15:09:34 +02:00
|
|
|
var backfillRunning atomic.Bool
|
2026-09-22 14:47:49 +02:00
|
|
|
mux.HandleFunc("/api/reprobe", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
|
http.Error(w, "POST only", http.StatusMethodNotAllowed)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-09-22 15:09:34 +02:00
|
|
|
if !backfillRunning.CompareAndSwap(false, true) {
|
|
|
|
|
http.Error(w, "backfill already running", http.StatusConflict)
|
2026-09-22 14:47:49 +02:00
|
|
|
return
|
|
|
|
|
}
|
2026-09-22 15:09:34 +02:00
|
|
|
q := r.URL.Query()
|
|
|
|
|
limit, _ := strconv.Atoi(q.Get("limit"))
|
|
|
|
|
exposure := strings.TrimSpace(q.Get("exposure"))
|
|
|
|
|
go func() {
|
|
|
|
|
defer backfillRunning.Store(false)
|
|
|
|
|
start := time.Now()
|
|
|
|
|
rep, err := cfg.Scanner.Backfill(context.Background(), exposure, limit)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Warn("backfill failed", "event", "backfill", "error", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
slog.Info("backfill complete", "event", "backfill", "exposure", exposure,
|
|
|
|
|
"probed", rep.Probed, "readable", rep.Readable, "listable", rep.Listable,
|
|
|
|
|
"indexed", rep.Indexed, "errors", rep.Errors, "took", time.Since(start).String())
|
|
|
|
|
}()
|
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
|
|
|
|
writeJSON(w, map[string]any{"started": true, "exposure": exposure, "limit": limit})
|
2026-09-22 14:47:49 +02:00
|
|
|
})
|
|
|
|
|
}
|
2026-09-22 13:58:28 +02:00
|
|
|
mux.HandleFunc("/api/stats", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
st, err := s.Stats(r.Context())
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
writeJSON(w, st)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if cfg.User != "" {
|
|
|
|
|
return basicAuth(cfg.User, cfg.Pass, mux)
|
|
|
|
|
}
|
|
|
|
|
return mux
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-22 14:33:03 +02:00
|
|
|
// cacheForever marks embedded, content-addressed-by-name assets as long-lived,
|
|
|
|
|
// since they only change with a new binary.
|
|
|
|
|
func cacheForever(next http.Handler) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("Cache-Control", "public, max-age=86400")
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-22 13:58:28 +02:00
|
|
|
func writeJSON(w http.ResponseWriter, v any) {
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
|
_ = json.NewEncoder(w).Encode(v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func basicAuth(user, pass string, next http.Handler) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
u, p, ok := r.BasicAuth()
|
|
|
|
|
if !ok || u != user || p != pass {
|
2026-09-22 14:33:03 +02:00
|
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="bucketeer"`)
|
2026-09-22 13:58:28 +02:00
|
|
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
|
})
|
|
|
|
|
}
|