Buckets : bouton Vider (objets et envois multipart inachevés)
L'API d'administration ne sait pas supprimer d'objets : le serveur passe par l'API S3 avec une clé temporaire (CreateKey + AllowBucketKey), supprime les objets par lots de 1000 (ListObjectsV2 + DeleteObjects, signature v4 en bibliothèque standard), nettoie les envois inachevés (CleanupIncompleteUploads, olderThanSecs 0) puis supprime la clé. Avancement diffusé en NDJSON (POST /api/buckets/empty?id=...). Confirmation par saisie du nom, interruption et reprise possibles. Nouvelle variable GARAGE_S3_API_URL (défaut : hôte admin, port 3900). Mock : API S3 minimale sur :3900. Tests : signature (exemple AWS) et vidage complet simulé. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LNfwm2bSk3o3YKYUP5P691
This commit is contained in:
1 parent
ea2058b6c1
commit
e21ef321f9
7 files changed
+278
-8
No files matched your search
+100
-1
@@ -6,10 +6,13 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -33,6 +36,18 @@ type bucket struct {
|
||||
MaxSize *int64
|
||||
MaxObjs *int64
|
||||
Keys map[string]perm
|
||||
Names map[string]bool // objets côté S3, créés à la première requête S3 (Objects noms factices)
|
||||
}
|
||||
|
||||
// s3Names renvoie les objets du bucket, générés à partir de Objects au premier appel.
|
||||
func (b *bucket) s3Names() map[string]bool {
|
||||
if b.Names == nil {
|
||||
b.Names = map[string]bool{}
|
||||
for i := 0; i < b.Objects; i++ {
|
||||
b.Names[fmt.Sprintf("donnees/objet-%06d.bin", i)] = true
|
||||
}
|
||||
}
|
||||
return b.Names
|
||||
}
|
||||
|
||||
type key struct {
|
||||
@@ -330,6 +345,85 @@ func handle(token string) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// handleS3 imite juste ce dont le vidage a besoin : ListObjectsV2 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()
|
||||
defer mu.Unlock()
|
||||
auth := r.Header.Get("Authorization")
|
||||
known := false
|
||||
for id := range keys {
|
||||
if strings.Contains(auth, "Credential="+id+"/") {
|
||||
known = true
|
||||
}
|
||||
}
|
||||
if !known {
|
||||
w.WriteHeader(403)
|
||||
fmt.Fprint(w, `<Error><Code>InvalidAccessKeyId</Code><Message>clé inconnue</Message></Error>`)
|
||||
return
|
||||
}
|
||||
name := strings.Trim(r.URL.Path, "/")
|
||||
var b *bucket
|
||||
for _, x := range buckets {
|
||||
for _, a := range x.Aliases {
|
||||
if a == name {
|
||||
b = x
|
||||
}
|
||||
}
|
||||
}
|
||||
if b == nil {
|
||||
w.WriteHeader(404)
|
||||
fmt.Fprint(w, `<Error><Code>NoSuchBucket</Code><Message>bucket introuvable</Message></Error>`)
|
||||
return
|
||||
}
|
||||
q := r.URL.Query()
|
||||
names := b.s3Names()
|
||||
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
|
||||
for k := range names {
|
||||
if k > q.Get("continuation-token") {
|
||||
all = append(all, k)
|
||||
}
|
||||
}
|
||||
sort.Strings(all)
|
||||
trunc := len(all) > 1000
|
||||
if trunc {
|
||||
all = all[:1000]
|
||||
}
|
||||
fmt.Fprint(w, `<ListBucketResult>`)
|
||||
for _, k := range all {
|
||||
fmt.Fprintf(w, `<Contents><Key>%s</Key></Contents>`, k)
|
||||
}
|
||||
if trunc {
|
||||
fmt.Fprintf(w, `<IsTruncated>true</IsTruncated><NextContinuationToken>%s</NextContinuationToken>`, all[len(all)-1])
|
||||
}
|
||||
fmt.Fprint(w, `</ListBucketResult>`)
|
||||
case r.Method == "POST" && q.Has("delete"):
|
||||
var in struct {
|
||||
Objects []struct{ Key string } `xml:"Object"`
|
||||
}
|
||||
data, _ := io.ReadAll(r.Body)
|
||||
_ = xml.Unmarshal(data, &in)
|
||||
for _, o := range in.Objects {
|
||||
if names[o.Key] {
|
||||
delete(names, o.Key)
|
||||
b.Objects--
|
||||
if b.Objects > 0 {
|
||||
b.Bytes -= b.Bytes / int64(b.Objects+1)
|
||||
} else {
|
||||
b.Bytes = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
time.Sleep(300 * time.Millisecond) // pour voir l'avancement dans l'interface
|
||||
fmt.Fprint(w, `<DeleteResult></DeleteResult>`)
|
||||
default:
|
||||
w.WriteHeader(501)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
token := "test"
|
||||
if v := os.Getenv("MOCK_TOKEN"); v != "" {
|
||||
@@ -339,7 +433,12 @@ func main() {
|
||||
if v := os.Getenv("MOCK_ADDR"); v != "" {
|
||||
addr = v
|
||||
}
|
||||
log.Printf("mockgarage sur %s (jeton %q)", addr, token)
|
||||
s3Addr := ":3900"
|
||||
if v := os.Getenv("MOCK_S3_ADDR"); v != "" {
|
||||
s3Addr = v
|
||||
}
|
||||
log.Printf("mockgarage sur %s (jeton %q), S3 sur %s", addr, token, s3Addr)
|
||||
go func() { log.Fatal(http.ListenAndServe(s3Addr, http.HandlerFunc(handleS3))) }()
|
||||
http.Handle("/v2/", handle(token))
|
||||
log.Fatal(http.ListenAndServe(addr, nil))
|
||||
}
|
||||
@@ -51,6 +51,7 @@ type config struct {
|
||||
title string
|
||||
s3Endpoint string
|
||||
s3Region string
|
||||
s3URL *url.URL // API S3 vue du serveur (vidage des buckets), distincte de l'endpoint public
|
||||
cliBin string
|
||||
cliConfig string
|
||||
rpcHost string // GARAGE_RPC_HOST : [identifiant@]hôte:port
|
||||
@@ -91,6 +92,10 @@ func loadConfig() (*config, error) {
|
||||
return nil, fmt.Errorf("GARAGE_ADMIN_URL invalide")
|
||||
}
|
||||
c.adminURL = u
|
||||
s3 := env("GARAGE_S3_API_URL", "http://"+u.Hostname()+":3900")
|
||||
if c.s3URL, err = url.Parse(s3); err != nil || c.s3URL.Host == "" {
|
||||
return nil, fmt.Errorf("GARAGE_S3_API_URL invalide")
|
||||
}
|
||||
if d, err := time.ParseDuration(env("SESSION_TTL", "24h")); err == nil && d > 0 {
|
||||
c.sessionTTL = d
|
||||
}
|
||||
@@ -437,6 +442,7 @@ func (c *config) routes() http.Handler {
|
||||
mux.HandleFunc("POST /api/login", c.handleLogin)
|
||||
mux.HandleFunc("POST /api/logout", c.handleLogout)
|
||||
mux.HandleFunc("POST /api/cli", c.guard(c.handleCLI))
|
||||
mux.HandleFunc("POST /api/buckets/empty", c.guard(c.handleEmptyBucket))
|
||||
mux.Handle("/api/garage/", c.guard(c.garageProxy().ServeHTTP))
|
||||
mux.Handle("/", spa())
|
||||
return mux
|
||||
|
||||
Reference in new issue
Block a user