release / go (push) Successful in 2m7s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 4m30s
release / android (push) Successful in 4m39s
release / Build signed APK (push) Successful in 5m10s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
- Module path: github.com/Fabled-Sword/minstrel, in go.mod, every import and the Dockerfile's version ldflags. - buildinfo.ProjectURL, the contact outside services see in Minstrel's User-Agent: https://github.com/Fabled-Sword/minstrel. - README's compose example pulls ghcr.io/fabled-sword/minstrel:latest. - docs/security.md sends problem reports to the GitHub issues. - The demo-library stack builds Minstrel from the checkout, since :dev is not published to GHCR. - hygiene.go's same-site example uses example.com names. The workbench CI (.gitea/, ci/, export-ignored) still builds and publishes :dev to the private registry. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
95 lines
3.6 KiB
Go
95 lines
3.6 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/Fabled-Sword/minstrel/internal/apierror"
|
|
"github.com/Fabled-Sword/minstrel/internal/backup"
|
|
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
|
|
)
|
|
|
|
// SetupTokenHeader carries the setup token on a restore upload, whose body is
|
|
// the backup file itself.
|
|
const SetupTokenHeader = "X-Setup-Token"
|
|
|
|
type restoreResp struct {
|
|
// Accounts is how many accounts came back; everyone signs in with the
|
|
// password they had.
|
|
Accounts int64 `json:"accounts"`
|
|
// Restarting says the server restarts now to load the restored settings.
|
|
// The scan at its start finds each music file and fills in what the
|
|
// backup leaves out.
|
|
Restarting bool `json:"restarting"`
|
|
}
|
|
|
|
// handleRestore implements POST /api/auth/restore: a new server, with no
|
|
// accounts yet, loads a backup file sent as the request body. It asks for the
|
|
// setup token from the server log, as creating the first account does, since
|
|
// the backup's admins become this server's admins. A library the server's
|
|
// first scan found is replaced by the backup's.
|
|
//
|
|
// Then the server restarts. Settings are read once, at boot, so the restored
|
|
// ones (the library folders among them) take effect the same way they do
|
|
// after `minstrel restore`, and the startup scan finds each file at its path.
|
|
//
|
|
// 200 restored · 400 not_a_backup · 403 setup_token_invalid ·
|
|
// 409 not_empty (accounts already) · 409 newer_backup
|
|
func (h *handlers) handleRestore(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
q := dbq.New(h.pool)
|
|
n, err := q.CountUsers(ctx)
|
|
if err != nil {
|
|
writeErrWithLog(w, h.logger, "restore: count users", apierror.Internal(err))
|
|
return
|
|
}
|
|
if n > 0 {
|
|
writeErr(w, apierror.Conflict("not_empty", "this server already has accounts; a backup restores into a new one"))
|
|
return
|
|
}
|
|
if h.requireSetupToken && !h.setupToken.Matches(r.Header.Get(SetupTokenHeader)) {
|
|
h.logger.Warn("restore: needs the setup token", "setup_token", h.setupToken.Value())
|
|
writeErr(w, apierror.Forbidden("setup_token_invalid", "the setup token from the server log is required to restore a backup"))
|
|
return
|
|
}
|
|
|
|
manifest, contents, err := backup.Read(r.Body)
|
|
if err != nil {
|
|
var tooBig *http.MaxBytesError
|
|
if errors.As(err, &tooBig) {
|
|
writeErr(w, &apierror.Error{Status: http.StatusRequestEntityTooLarge, Code: "too_large", Message: "the file is larger than a backup can be"})
|
|
return
|
|
}
|
|
writeErr(w, apierror.BadRequest("not_a_backup", "that file isn't a Minstrel backup"))
|
|
return
|
|
}
|
|
switch err := backup.Restore(ctx, h.pool, contents); {
|
|
case errors.Is(err, backup.ErrNotEmpty):
|
|
writeErr(w, apierror.Conflict("not_empty", "this server already has accounts; a backup restores into a new one"))
|
|
return
|
|
case errors.Is(err, backup.ErrNewerSchema):
|
|
writeErr(w, apierror.Conflict("newer_backup", "the backup was made by a newer Minstrel; update this server first"))
|
|
return
|
|
case errors.Is(err, backup.ErrNotABackup):
|
|
writeErr(w, apierror.BadRequest("not_a_backup", "that file isn't a Minstrel backup"))
|
|
return
|
|
case err != nil:
|
|
writeErrWithLog(w, h.logger, "restore: load the backup", apierror.Internal(err))
|
|
return
|
|
}
|
|
|
|
out := restoreResp{}
|
|
if out.Accounts, err = q.CountUsers(ctx); err != nil {
|
|
h.logger.Warn("restore: count restored accounts", "err", err)
|
|
}
|
|
h.logger.Info("restored a backup", "made_at", manifest.CreatedAt, "build", manifest.Build,
|
|
"schema", manifest.Schema, "accounts", out.Accounts)
|
|
out.Restarting = h.restart != nil
|
|
writeJSON(w, http.StatusOK, out)
|
|
if h.restart != nil {
|
|
// The server drains this request before it stops, so the answer
|
|
// reaches the browser first.
|
|
h.restart()
|
|
}
|
|
}
|