Buckets : navigateur d'objets en lecture seule

L'API d'administration ne liste pas les objets : le serveur passe par
l'API S3 (ListObjectsV2, délimiteur « / », 500 entrées par page) avec une
clé temporaire garage-ui-lecture-... limitée à la lecture sur le bucket.
La clé est réutilisée pendant la navigation, supprimée après 10 minutes
sans requête, à l'arrêt (SIGTERM/SIGINT) et, si le serveur s'est arrêté
brutalement, au démarrage suivant.
Route GET /api/buckets/objects?id=&prefix=&token=. Interface : panneau
« Objets » chargé à la demande (fil d'Ariane, filtre, pagination).
Mock : ListObjectsV2 avec préfixe, délimiteur, taille et date.
Tests : listing, pagination, droits en lecture seule, aucune écriture S3,
expiration et recréation de la clé, purge des clés orphelines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LNfwm38nQMsuGSPgFuRqBy
This commit is contained in:
claude BotandClaude Opus 5.5 committed 2026-11-27 12:51:01 +01:00
1 parent 3ff8bc2355
commit 57188f5bf5
10 files changed
+713 -23

No files matched your search

+39 -8
View File
@@ -8,6 +8,8 @@ import (
"encoding/json"
"encoding/xml"
"fmt"
"hash/fnv"
"html"
"io"
"log"
"net/http"
@@ -43,8 +45,15 @@ type bucket struct {
func (b *bucket) s3Names() map[string]bool {
if b.Names == nil {
b.Names = map[string]bool{}
// Arborescence factice : quelques fichiers à la racine, le reste réparti dans des « dossiers ».
dirs := []string{"donnees/", "donnees/2026/11/", "images/", "sauvegardes/vm-101/", "sauvegardes/vm-102/"}
for i := 0; i < b.Objects; i++ {
b.Names[fmt.Sprintf("donnees/objet-%06d.bin", i)] = true
switch {
case i < 3:
b.Names[[]string{"index.html", "LISEZMOI.md", "robots.txt"}[i]] = true
default:
b.Names[fmt.Sprintf("%sobjet-%06d.bin", dirs[i%len(dirs)], i)] = true
}
}
}
return b.Names
@@ -345,7 +354,8 @@ func handle(token string) http.HandlerFunc {
}
}
// handleS3 imite juste ce dont le vidage a besoin : ListObjectsV2 et DeleteObjects, en style path.
// handleS3 imite juste ce dont l'interface a besoin : ListObjectsV2 (préfixe, délimiteur,
// pagination) et DeleteObjects, en style path.
// La signature n'est pas vérifiée, seule la présence d'une clé connue l'est.
func handleS3(w http.ResponseWriter, r *http.Request) {
mu.Lock()
@@ -381,20 +391,41 @@ func handleS3(w http.ResponseWriter, r *http.Request) {
log.Printf("S3 %s /%s?%s", r.Method, name, r.URL.RawQuery)
switch {
case r.Method == "GET" && q.Get("list-type") == "2":
var all []string
max := 1000
fmt.Sscan(q.Get("max-keys"), &max)
prefix, delim := q.Get("prefix"), q.Get("delimiter")
seen := map[string]bool{}
var all []string // objets, et préfixes communs suffixés de \x00
for k := range names {
if k > q.Get("continuation-token") {
all = append(all, k)
if !strings.HasPrefix(k, prefix) {
continue
}
e := k
if i := strings.Index(k[len(prefix):], delim); delim != "" && i >= 0 {
e = k[:len(prefix)+i+1] + "\x00"
}
if !seen[e] && e > q.Get("continuation-token") {
seen[e] = true
all = append(all, e)
}
}
sort.Strings(all)
trunc := len(all) > 1000
trunc := len(all) > max
if trunc {
all = all[:1000]
all = all[:max]
}
fmt.Fprint(w, `<ListBucketResult>`)
for _, k := range all {
fmt.Fprintf(w, `<Contents><Key>%s</Key></Contents>`, k)
if p, ok := strings.CutSuffix(k, "\x00"); ok {
fmt.Fprintf(w, `<CommonPrefixes><Prefix>%s</Prefix></CommonPrefixes>`, html.EscapeString(p))
continue
}
h := fnv.New32a()
h.Write([]byte(k))
n := h.Sum32()
mod := time.Date(2026, 11, 27, 9, 0, 0, 0, time.UTC).Add(-time.Duration(n%(90*24*3600)) * time.Second)
fmt.Fprintf(w, `<Contents><Key>%s</Key><Size>%d</Size><LastModified>%s</LastModified><ETag>"%08x"</ETag></Contents>`,
html.EscapeString(k), int64(n%(64<<20)), mod.Format("2006-01-02T15:04:05.000Z"), n)
}
if trunc {
fmt.Fprintf(w, `<IsTruncated>true</IsTruncated><NextContinuationToken>%s</NextContinuationToken>`, all[len(all)-1])