3800b34a2b
CI / Test (rest) (pull_request) Successful in 29s
CI / Lint (pull_request) Successful in 32s
CI / Build (windows/amd64) (pull_request) Successful in 22s
CI / Test (store) (pull_request) Successful in 1m22s
CI / Test (server-http) (pull_request) Successful in 1m30s
CI / Build (linux/amd64) (pull_request) Successful in 22s
CI / Build (linux/arm64) (pull_request) Successful in 41s
Smoothes the rough edges that came up exercising a live deployment.
First-run bootstrap UI: /bootstrap renders a username + password form
that uses the in-memory token directly (operator no longer copies it
out of the log); /login redirects there while bootstrap is available.
Agent reliability: failJob synthetic envelopes so command.run early
returns no longer hang the server-side job; runtime probe of restic
restore --help drives --no-ownership instead of version sniffing
(0.18.x had it removed). Server unit re-shaped: ProtectSystem=full
plus ReadWritePaths=/etc/restic-manager, no ProtectHome — restore
can now write anywhere a user might want.
Restore wizard: default target is /root/rm-restore/<job-id>/ with
clearer help text. Re-init confirm input uses .field (was .input,
which doesn't exist — text was invisible).
NS-01 host delete: store DeleteHost, admin-band /hosts/{id}/delete
with hostname-confirm danger zone, audit, FK cascade, live WS close.
NS-02 enrollment-token recovery: outstanding-tokens panel on
/hosts/new, regenerate (preserves attachments) and revoke handlers
+ audit, store-level ListOutstandingEnrollmentTokens and
DeleteEnrollmentToken.
NS-03 repo init / probe surface: migration 0020 adds
hosts.repo_status + repo_status_error; WS handler projects every
init job's outcome onto the host row (idempotent already-initialised
collapses to ready); creds-save resets status and dispatches a fresh
probe; /hosts/{id}/repo/probe retry endpoint with banner.
NS-04 dashboard live + sort + filter: query-string filter
(q/status/repo_status/tag/sort/dir), 5s htmx live poll mirroring the
alerts pattern with a localStorage live toggle, sortable column
headers, filter row + clear.
Alerts page: ack'd-by line resolves user_id ULID to username.
Compose.yaml ignored — host-specific.
159 lines
5.1 KiB
Go
159 lines
5.1 KiB
Go
// ui_enrollment_tokens_test.go — covers NS-02 token-recovery handlers:
|
|
// revoke deletes the row, regenerate swaps the row out for a fresh
|
|
// raw token redirected to /hosts/pending/{newToken}.
|
|
package http
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
stdhttp "net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.dcglab.co.uk/steve/restic-manager/internal/auth"
|
|
"gitea.dcglab.co.uk/steve/restic-manager/internal/store"
|
|
)
|
|
|
|
// mintTestToken seeds an enrolment token via the same helper the live
|
|
// /hosts/new flow uses, returning the (raw, hash) pair.
|
|
func mintTestToken(t *testing.T, srv *Server) (raw, hash string) {
|
|
t.Helper()
|
|
tok, _, err := srv.mintEnrollmentToken(context.Background(),
|
|
"rest:http://r:8000/x/", "u", "p", []string{"/etc"})
|
|
if err != nil {
|
|
t.Fatalf("mint: %v", err)
|
|
}
|
|
return tok, auth.HashToken(tok)
|
|
}
|
|
|
|
// TestEnrollmentTokenRevokeDeletesRow: POST .../revoke removes the
|
|
// row and 303s back to /hosts/new.
|
|
func TestEnrollmentTokenRevokeDeletesRow(t *testing.T) {
|
|
t.Parallel()
|
|
srv, ts, st := rawTestServerWithUI(t)
|
|
_, hash := mintTestToken(t, srv)
|
|
|
|
cookie := loginAsAdmin(t, st)
|
|
req, _ := stdhttp.NewRequest("POST",
|
|
ts.URL+"/hosts/enrollment-tokens/"+hash+"/revoke",
|
|
strings.NewReader(""))
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
req.AddCookie(cookie)
|
|
cli := &stdhttp.Client{
|
|
CheckRedirect: func(*stdhttp.Request, []*stdhttp.Request) error {
|
|
return stdhttp.ErrUseLastResponse
|
|
},
|
|
}
|
|
res, err := cli.Do(req)
|
|
if err != nil {
|
|
t.Fatalf("do: %v", err)
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != stdhttp.StatusSeeOther {
|
|
t.Fatalf("status: got %d, want 303", res.StatusCode)
|
|
}
|
|
if loc := res.Header.Get("Location"); loc != "/hosts/new" {
|
|
t.Errorf("Location: got %q, want /hosts/new", loc)
|
|
}
|
|
if _, err := st.GetEnrollmentTokenAttachments(context.Background(), hash); !errors.Is(err, store.ErrNotFound) {
|
|
t.Errorf("post-revoke lookup: want ErrNotFound, got %v", err)
|
|
}
|
|
var n int
|
|
if err := st.DB().QueryRow(
|
|
`SELECT COUNT(*) FROM audit_log WHERE action = 'enrollment_token.revoked'`).Scan(&n); err != nil {
|
|
t.Fatalf("count audit: %v", err)
|
|
}
|
|
if n != 1 {
|
|
t.Errorf("audit rows: got %d, want 1", n)
|
|
}
|
|
}
|
|
|
|
// TestEnrollmentTokenRegenerateSwapsRow: POST .../regenerate revokes
|
|
// the old hash, mints a fresh raw token preserving the repo URL/user/
|
|
// password attachments, and 303s to the new pending page.
|
|
func TestEnrollmentTokenRegenerateSwapsRow(t *testing.T) {
|
|
t.Parallel()
|
|
srv, ts, st := rawTestServerWithUI(t)
|
|
oldRaw, oldHash := mintTestToken(t, srv)
|
|
|
|
cookie := loginAsAdmin(t, st)
|
|
req, _ := stdhttp.NewRequest("POST",
|
|
ts.URL+"/hosts/enrollment-tokens/"+oldHash+"/regenerate",
|
|
strings.NewReader(""))
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
req.AddCookie(cookie)
|
|
cli := &stdhttp.Client{
|
|
CheckRedirect: func(*stdhttp.Request, []*stdhttp.Request) error {
|
|
return stdhttp.ErrUseLastResponse
|
|
},
|
|
}
|
|
res, err := cli.Do(req)
|
|
if err != nil {
|
|
t.Fatalf("do: %v", err)
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != stdhttp.StatusSeeOther {
|
|
t.Fatalf("status: got %d, want 303", res.StatusCode)
|
|
}
|
|
loc := res.Header.Get("Location")
|
|
if !strings.HasPrefix(loc, "/hosts/pending/") {
|
|
t.Fatalf("Location: got %q, want /hosts/pending/<token>", loc)
|
|
}
|
|
newRaw := strings.TrimPrefix(loc, "/hosts/pending/")
|
|
if newRaw == "" || newRaw == oldRaw {
|
|
t.Fatalf("regenerate produced same/empty token (old=%q, new=%q)", oldRaw, newRaw)
|
|
}
|
|
|
|
// Old hash gone; new hash present with the same paths attachment.
|
|
if _, err := st.GetEnrollmentTokenAttachments(context.Background(), oldHash); !errors.Is(err, store.ErrNotFound) {
|
|
t.Errorf("old hash should be gone; got %v", err)
|
|
}
|
|
att, err := st.GetEnrollmentTokenAttachments(context.Background(), auth.HashToken(newRaw))
|
|
if err != nil {
|
|
t.Fatalf("new hash lookup: %v", err)
|
|
}
|
|
if len(att.InitialPaths) != 1 || att.InitialPaths[0] != "/etc" {
|
|
t.Errorf("attachments: got paths %v, want [/etc]", att.InitialPaths)
|
|
}
|
|
|
|
var n int
|
|
if err := st.DB().QueryRow(
|
|
`SELECT COUNT(*) FROM audit_log WHERE action = 'enrollment_token.regenerated'`).Scan(&n); err != nil {
|
|
t.Fatalf("count audit: %v", err)
|
|
}
|
|
if n != 1 {
|
|
t.Errorf("audit rows: got %d, want 1", n)
|
|
}
|
|
}
|
|
|
|
// TestEnrollmentTokenRegenerateMissingTokenRedirects: hitting
|
|
// regenerate with an unknown hash 303s back to /hosts/new without a
|
|
// 5xx (idempotent re-submit safety).
|
|
func TestEnrollmentTokenRegenerateMissingTokenRedirects(t *testing.T) {
|
|
t.Parallel()
|
|
_, ts, st := rawTestServerWithUI(t)
|
|
cookie := loginAsAdmin(t, st)
|
|
|
|
req, _ := stdhttp.NewRequest("POST",
|
|
ts.URL+"/hosts/enrollment-tokens/deadbeef/regenerate",
|
|
strings.NewReader(""))
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
req.AddCookie(cookie)
|
|
cli := &stdhttp.Client{
|
|
CheckRedirect: func(*stdhttp.Request, []*stdhttp.Request) error {
|
|
return stdhttp.ErrUseLastResponse
|
|
},
|
|
}
|
|
res, err := cli.Do(req)
|
|
if err != nil {
|
|
t.Fatalf("do: %v", err)
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != stdhttp.StatusSeeOther {
|
|
t.Fatalf("status: got %d, want 303", res.StatusCode)
|
|
}
|
|
if loc := res.Header.Get("Location"); loc != "/hosts/new" {
|
|
t.Errorf("Location: got %q, want /hosts/new", loc)
|
|
}
|
|
}
|