Lot 3 (4/5) : API admin S3, variables S3_*, tests et documentation
GET/PUT /api/admin/s3 et POST /api/admin/s3/test (administrateurs) ; S3_ENDPOINT, S3_REGION, S3_BUCKET, S3_ACCESS_KEY, S3_SECRET_KEY (_FILE), S3_INSECURE ; miniatures S3 sous DATA_DIR/.s3cache. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LNfwm4pfYR9C914C2EeiuB
This commit is contained in:
1 parent
53972f9762
commit
e79a813272
6 files changed
+95
No files matched your search
@@ -32,6 +32,7 @@
|
||||
// POST /api/public/{jeton}/unlock mot de passe d'un lien protégé
|
||||
// GET /api/public/{jeton}/file|zip|thumb|preview contenu d'un lien de partage
|
||||
// GET /api/settings / PUT paramètres (écriture : administrateur)
|
||||
// GET /api/admin/s3 / PUT, POST /api/admin/s3/test racine S3 (administrateur)
|
||||
// GET /api/logo / PUT (image/png) / DELETE logo 64x64
|
||||
// POST /api/account/password changement du mot de passe (compte local)
|
||||
// PATCH /api/account nom et e-mail (compte local)
|
||||
@@ -173,6 +174,9 @@ func (h *Handler) Register(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/logo", h.readLogo)
|
||||
mux.HandleFunc("PUT /api/logo", h.writeLogo)
|
||||
mux.HandleFunc("DELETE /api/logo", h.deleteLogo)
|
||||
mux.HandleFunc("GET /api/admin/s3", h.adminS3)
|
||||
mux.HandleFunc("PUT /api/admin/s3", h.adminSaveS3)
|
||||
mux.HandleFunc("POST /api/admin/s3/test", h.adminTestS3)
|
||||
if h.Accounts != nil {
|
||||
mux.HandleFunc("POST /api/account/password", h.changePassword)
|
||||
mux.HandleFunc("PATCH /api/account", h.patchAccount)
|
||||
|
||||
@@ -502,6 +502,10 @@ func TestSettingsAndAdminPermissions(t *testing.T) {
|
||||
want(t, bob.json("PUT", "/api/settings", map[string]any{"title": "X"}), 403)
|
||||
want(t, bob.do("DELETE", "/api/logo", nil), 403)
|
||||
want(t, bob.get("/api/admin/users"), 403)
|
||||
want(t, bob.get("/api/admin/s3"), 403)
|
||||
want(t, bob.json("PUT", "/api/admin/s3", map[string]any{"bucket": "b"}), 403)
|
||||
want(t, admin.get("/api/admin/s3"), 200)
|
||||
want(t, admin.json("PUT", "/api/admin/s3", map[string]any{"bucket": "b"}), 400) // sans catalogue
|
||||
want(t, bob.json("POST", "/api/admin/users", map[string]any{"username": "eve", "password": "mot-de-passe-eve"}), 403)
|
||||
want(t, bob.json("PATCH", "/api/admin/users/alice", map[string]any{"role": "user"}), 403)
|
||||
want(t, bob.do("DELETE", "/api/admin/users/alice", nil), 403)
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/vblogio/upandown/internal/storage"
|
||||
)
|
||||
|
||||
// Configuration de la racine S3 (administrateurs). La clé secrète n'est
|
||||
// jamais renvoyée ; un champ fixé par l'environnement ne peut être modifié.
|
||||
|
||||
func (h *Handler) adminS3(w http.ResponseWriter, r *http.Request) {
|
||||
if !h.admin(w, r) {
|
||||
return
|
||||
}
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
writeJSON(w, http.StatusOK, h.Spaces.S3Status())
|
||||
}
|
||||
|
||||
func (h *Handler) adminSaveS3(w http.ResponseWriter, r *http.Request) {
|
||||
if !h.admin(w, r) {
|
||||
return
|
||||
}
|
||||
var p storage.S3Patch
|
||||
if !h.decode(w, r, &p) {
|
||||
return
|
||||
}
|
||||
st, err := h.Spaces.SaveS3(p)
|
||||
if err != nil {
|
||||
s3Fail(w, err)
|
||||
return
|
||||
}
|
||||
slog.Info("racine S3 : configuration modifiée", "by", userID(r))
|
||||
writeJSON(w, http.StatusOK, st)
|
||||
}
|
||||
|
||||
func (h *Handler) adminTestS3(w http.ResponseWriter, r *http.Request) {
|
||||
if !h.admin(w, r) {
|
||||
return
|
||||
}
|
||||
var p storage.S3Patch
|
||||
if !h.decode(w, r, &p) {
|
||||
return
|
||||
}
|
||||
if err := h.Spaces.TestS3(p); err != nil {
|
||||
s3Fail(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]bool{"ok": true})
|
||||
}
|
||||
|
||||
// s3Fail : les erreurs de connexion au bucket sont renvoyées telles quelles
|
||||
// (utiles à l'administrateur).
|
||||
func s3Fail(w http.ResponseWriter, err error) {
|
||||
switch {
|
||||
case errors.Is(err, storage.ErrS3NotEmpty):
|
||||
writeErr(w, http.StatusConflict, err.Error())
|
||||
case errors.Is(err, storage.ErrInvalidPath):
|
||||
writeErr(w, http.StatusBadRequest, err.Error())
|
||||
default:
|
||||
writeErr(w, http.StatusBadGateway, err.Error())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user