From 1e99b53d518428ab97405b4cbdd107c9428a2269 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Tue, 24 Nov 2026 02:55:37 +0000 Subject: [PATCH] fix(auth): accept the bootstrap admin key on the boot that creates it The server loaded the API key cache before it created the admin key from GOHOARDER_BOOTSTRAP_ADMIN_KEY. The new key was therefore refused until the next restart. Bootstrap now runs before the cache loads. --- pkg/app/app.go | 11 ++++++----- pkg/app/bootstrap_admin_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 pkg/app/bootstrap_admin_test.go diff --git a/pkg/app/app.go b/pkg/app/app.go index d336733..c2cb6dd 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -309,17 +309,18 @@ func (a *App) initializeComponents() error { log.Info().Msg("Initializing authentication manager") authCfg := auth.Config{BcryptCost: a.config.Auth.BcryptCost} a.authManager = auth.NewWithStore(authCfg, a.metadata) + // Bootstrap before Load: a key saved after the cache is hydrated stays invalid until the next restart. + bootCtx, bootCancel := context.WithTimeout(context.Background(), 30*time.Second) + if err := auth.BootstrapAdminFromEnv(bootCtx, a.metadata, authCfg); err != nil { + log.Warn().Err(err).Msg("Failed to bootstrap admin API key from environment") + } + bootCancel() loadCtx, loadCancel := context.WithTimeout(context.Background(), 30*time.Second) if err := a.authManager.Load(loadCtx); err != nil { // Empty key set is recoverable: log and continue rather than fail boot. log.Warn().Err(err).Msg("Failed to load API keys from metadata store; starting with empty key set") } loadCancel() - bootCtx, bootCancel := context.WithTimeout(context.Background(), 30*time.Second) - if err := auth.BootstrapAdminFromEnv(bootCtx, a.metadata, authCfg); err != nil { - log.Warn().Err(err).Msg("Failed to bootstrap admin API key from environment") - } - bootCancel() a.scanManager.SetFileSource(a.cache.ScanFile) diff --git a/pkg/app/bootstrap_admin_test.go b/pkg/app/bootstrap_admin_test.go new file mode 100644 index 0000000..d29c44a --- /dev/null +++ b/pkg/app/bootstrap_admin_test.go @@ -0,0 +1,29 @@ +package app + +import ( + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/gofiber/fiber/v3" + "github.com/lukaszraczylo/gohoarder/pkg/auth" +) + +// The env-bootstrapped admin key must work on the boot that creates it, not only after a restart. +func TestBootstrapAdminKeyWorksOnFirstBoot(t *testing.T) { + const key = "bootstrap-admin-key-for-test" + t.Setenv(auth.BootstrapAdminEnvVar, key) + a := newTestApp(t, nil) + + req := httptest.NewRequest(http.MethodGet, "/api/admin/prewarming/status", nil) + req.Header.Set("X-API-Key", key) + resp, err := a.app.Test(req, fiber.TestConfig{Timeout: 10 * time.Second}) + if err != nil { + t.Fatal(err) + } + _ = resp.Body.Close() + if resp.StatusCode != http.StatusOK { + t.Fatalf("admin route with the bootstrap key = %d, want 200", resp.StatusCode) + } +}