Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
Restore previous license on persistence failure instead of clearing it
rcourtman45b5c8a86124
diff --git a/internal/api/license_handlers.go b/internal/api/license_handlers.goindex 722a71dbe8..a7766b7749 100644--- a/internal/api/license_handlers.go+++ b/internal/api/license_handlers.go@@ -185,6 +185,40 @@ func (h *LicenseHandlers) effectiveState(ctx context.Context, service *license.Sreturn state, ""}+func cloneLicenseSnapshot(current *license.License) *license.License {+ if current == nil {+ return nil+ }++ snapshot := *current+ if current.GracePeriodEnd != nil {+ gracePeriodEnd := *current.GracePeriodEnd+ snapshot.GracePeriodEnd = &gracePeriodEnd+ }+ snapshot.Claims.Features = append([]string(nil), current.Claims.Features...)+ return &snapshot+}++func restoreLicenseSnapshot(service *license.Service, snapshot *license.License) error {+ if snapshot == nil {+ service.Clear()+ return nil+ }++ restored, err := service.ActivatePersisted(snapshot.Raw)+ if err != nil {+ service.Clear()+ return err+ }+ if snapshot.GracePeriodEnd != nil {+ gracePeriodEnd := *snapshot.GracePeriodEnd+ restored.GracePeriodEnd = &gracePeriodEnd+ } else {+ restored.GracePeriodEnd = nil+ }+ return nil+}+// HandleLicenseStatus handles GET /api/license/status// Returns the current license status.func (h *LicenseHandlers) HandleLicenseStatus(w http.ResponseWriter, r *http.Request) {@@ -308,6 +342,7 @@ func (h *LicenseHandlers) HandleActivateLicense(w http.ResponseWriter, r *http.R}orgID := GetOrgID(r.Context())+ previousLicense := cloneLicenseSnapshot(service.Current())lic, err := service.Activate(req.LicenseKey)if err != nil {log.Warn().Err(err).Msg("Failed to activate license")@@ -323,9 +358,13 @@ func (h *LicenseHandlers) HandleActivateLicense(w http.ResponseWriter, r *http.R// Persist the license with grace period if applicableif persistence == nil {- service.Clear()persistErr := errors.New("license persistence unavailable")- h.setLoadIssue(orgID, persistErr)+ if restoreErr := restoreLicenseSnapshot(service, previousLicense); restoreErr != nil {+ log.Error().Err(restoreErr).Msg("Failed to restore previous license after persistence failure")+ h.setLoadIssue(orgID, errors.Join(persistErr, restoreErr))+ } else {+ h.clearLoadIssue(orgID)+ }log.Error().Err(persistErr).Msg("Failed to persist license activation")w.Header().Set("Content-Type", "application/json")w.WriteHeader(http.StatusInternalServerError)@@ -341,8 +380,12 @@ func (h *LicenseHandlers) HandleActivateLicense(w http.ResponseWriter, r *http.RgracePeriodEnd = &ts}if err := persistence.SaveWithGracePeriod(req.LicenseKey, gracePeriodEnd); err != nil {- service.Clear()- h.setLoadIssue(orgID, err)+ if restoreErr := restoreLicenseSnapshot(service, previousLicense); restoreErr != nil {+ log.Error().Err(restoreErr).Msg("Failed to restore previous license after persistence failure")+ h.setLoadIssue(orgID, errors.Join(err, restoreErr))+ } else {+ h.clearLoadIssue(orgID)+ }log.Error().Err(err).Msg("Failed to persist license activation")w.Header().Set("Content-Type", "application/json")w.WriteHeader(http.StatusInternalServerError)diff --git a/internal/api/license_handlers_test.go b/internal/api/license_handlers_test.goindex 1ac77fca72..e7d65e95c5 100644--- a/internal/api/license_handlers_test.go+++ b/internal/api/license_handlers_test.go@@ -508,7 +508,7 @@ func TestHandleActivateLicense_ValidKey(t *testing.T) {}}-func TestHandleActivateLicense_PersistenceUnavailableClearsRuntimeState(t *testing.T) {+func TestHandleActivateLicense_PersistenceUnavailableLeavesNoRuntimeLicense(t *testing.T) {t.Setenv("PULSE_LICENSE_DEV_MODE", "true")handler := NewLicenseHandlers(nil)@@ -538,7 +538,76 @@ func TestHandleActivateLicense_PersistenceUnavailableClearsRuntimeState(t *testit.Fatalf("expected message %q, got %q", "License could not be persisted", resp.Message)}if handler.Service(context.Background()).Current() != nil {- t.Fatalf("expected runtime license to be cleared after persistence failure")+ t.Fatalf("expected no runtime license after persistence failure without a previous license")+ }+}++func TestHandleActivateLicense_PersistenceFailureRestoresPreviousLicense(t *testing.T) {+ t.Setenv("PULSE_LICENSE_DEV_MODE", "true")++ handler, tempDir := createTestHandlerWithDir(t)+ service := handler.Service(context.Background())++ previousKey, err := license.GenerateLicenseForTesting("[email protected]", license.TierPro, 24*time.Hour)+ if err != nil {+ t.Fatalf("failed to generate previous license: %v", err)+ }+ previousLicense, err := service.Activate(previousKey)+ if err != nil {+ t.Fatalf("failed to activate previous license: %v", err)+ }++ persistence, err := handler.getPersistenceForOrg("default")+ if err != nil {+ t.Fatalf("failed to get persistence: %v", err)+ }+ if err := persistence.SaveWithGracePeriod(previousKey, nil); err != nil {+ t.Fatalf("failed to persist previous license: %v", err)+ }++ licensePath := filepath.Join(tempDir, license.LicenseFileName)+ if err := os.Chmod(licensePath, 0400); err != nil {+ t.Fatalf("failed to make persisted license read-only: %v", err)+ }+ t.Cleanup(func() {+ _ = os.Chmod(licensePath, 0600)+ })++ newKey, err := license.GenerateLicenseForTesting("[email protected]", license.TierPro, 24*time.Hour)+ if err != nil {+ t.Fatalf("failed to generate next license: %v", err)+ }++ body, _ := json.Marshal(map[string]string{"license_key": newKey})+ req := httptest.NewRequest(http.MethodPost, "/api/license/activate", bytes.NewReader(body))+ rec := httptest.NewRecorder()++ handler.HandleActivateLicense(rec, req)++ if rec.Code != http.StatusInternalServerError {+ t.Fatalf("expected status %d, got %d: %s", http.StatusInternalServerError, rec.Code, rec.Body.String())+ }++ var resp ActivateLicenseResponse+ if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {+ t.Fatalf("failed to unmarshal response: %v", err)+ }+ if resp.Success {+ t.Fatalf("expected Success=false when persistence fails")+ }+ if resp.Message != "License could not be persisted" {+ t.Fatalf("expected message %q, got %q", "License could not be persisted", resp.Message)+ }++ current := handler.Service(context.Background()).Current()+ if current == nil {+ t.Fatal("expected previous runtime license to be restored")+ }+ if current.Raw != previousKey {+ t.Fatalf("expected previous license key to be restored")+ }+ if current.Claims.Email != previousLicense.Claims.Email {+ t.Fatalf("expected restored license email %q, got %q", previousLicense.Claims.Email, current.Claims.Email)}}