Readability capabilities (exists/object_readable/listable) are now tracked
separately instead of a single exposure flag, matching how providers differ:
- Azure 'Blob' mode = object readable, not listable; 'Container' = listable
- Cloudflare R2 public URLs are never anonymously listable (root 500); R2 is
object-read only and is no longer probed for listing
- S3/GCS: 200 list = listable; 403 + readable index.html/robots.txt =
object readable only
New columns: cloud_assets.listable, cloud_assets.object_readable.
Azure: account-level container listing is never anonymous; discover containers
by probing a bounded common-name set (or the container from a URL path), then
list blobs.
Classifier: added the legacy AWS '.s3-<region>.amazonaws.com' bucket form.
On-demand scan: POST /api/scan {target} classifies a host or URL, follows CNAME
for custom domains, probes, and indexes readable resources; Azure container
comes from the URL path. UI has a Scan box.
Backfill: POST /api/reprobe re-probes already-discovered assets with the current
checks and indexes any readable ones, recovering findings from earlier probes.
DNS: CNAME-only enrichment (A/AAAA resolved only for matches), 64 workers,
larger queue.
Verified live against real public resources:
veteran1.nyc3.digitaloceanspaces.com listable, entries
bucket-01.s3-eu-west-1.amazonaws.com listable, entries
tartugeo-podcast.storage.googleapis.com listable, entries
eroticfeeldev.blob.core.windows.net/images listable, entries
169 lines
5.4 KiB
Go
169 lines
5.4 KiB
Go
// 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
|
|
// exposes no mutation endpoints.
|
|
package webui
|
|
|
|
import (
|
|
"context"
|
|
"embed"
|
|
"encoding/json"
|
|
"io/fs"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"tea.karlcom.de/karl/bucketeer/internal/scan"
|
|
"tea.karlcom.de/karl/bucketeer/internal/store"
|
|
)
|
|
|
|
//go:embed index.html
|
|
var indexHTML []byte
|
|
|
|
//go:embed assets
|
|
var assetsFS embed.FS
|
|
|
|
// 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)
|
|
CountAssets(ctx context.Context, f store.AssetFilter) (int, error)
|
|
SearchEntries(ctx context.Context, query string, limit, offset int) ([]store.IndexEntry, int, error)
|
|
Stats(ctx context.Context) (store.Stats, error)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
type Config struct {
|
|
User string // optional HTTP basic auth username
|
|
Pass string // optional HTTP basic auth password
|
|
Scanner Scanner
|
|
}
|
|
|
|
// 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()
|
|
assets, _ := fs.Sub(assetsFS, "assets")
|
|
fileServer := http.FileServer(http.FS(assets))
|
|
mux.Handle("/assets/", http.StripPrefix("/assets/", cacheForever(fileServer)))
|
|
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"))
|
|
f := store.AssetFilter{
|
|
Provider: strings.TrimSpace(q.Get("provider")),
|
|
Service: strings.TrimSpace(q.Get("service")),
|
|
Exposure: strings.TrimSpace(q.Get("exposure")),
|
|
Query: strings.TrimSpace(q.Get("q")),
|
|
Sort: strings.TrimSpace(q.Get("sort")),
|
|
Limit: limit,
|
|
Offset: offset,
|
|
}
|
|
assets, err := s.ListAssets(r.Context(), f)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
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})
|
|
})
|
|
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))
|
|
})
|
|
mux.HandleFunc("/api/reprobe", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "POST only", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
q := r.URL.Query()
|
|
limit, _ := strconv.Atoi(q.Get("limit"))
|
|
rep, err := cfg.Scanner.Backfill(r.Context(), strings.TrimSpace(q.Get("exposure")), limit)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
writeJSON(w, rep)
|
|
})
|
|
}
|
|
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
|
|
}
|
|
|
|
// 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)
|
|
})
|
|
}
|
|
|
|
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 {
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="bucketeer"`)
|
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|