Skip to content

armgit / Pulse

Public Imported from GitHub
0

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.

Parse stored subscription states for entitlement refresh

rcourtmanf16aa8a65c9f
diff --git a/docs/release-control/v6/internal/subsystems/cloud-paid.md b/docs/release-control/v6/internal/subsystems/cloud-paid.md
index c40d85d4b3..81d46ab1f3 100644
--- a/docs/release-control/v6/internal/subsystems/cloud-paid.md
+++ b/docs/release-control/v6/internal/subsystems/cloud-paid.md
@@ -224,6 +224,12 @@ or other self-hosted uncapped continuity plans.
in any non-`active` lifecycle state must return an inactive hosted entitlement
result even when the owning account's Stripe subscription is still active, so
soft-deleted or suspended workspaces cannot keep refreshing runtime leases.
+ Hosted entitlement refresh must parse `stripe_accounts.subscription_state`
+ as the normalized stored vocabulary (`active`, `trial`, `past_due`,
+ `canceled`, and migration-tolerated terminal states), not as raw Stripe API
+ statuses such as `trialing`. Raw Stripe status mapping remains limited to
+ webhook/provisioner ingress; stored-row reads must use the licensing-owned
+ stored-state parser so active trials do not fail closed during lease refresh.
3. Add or change control-plane plan storage through `internal/cloudcp/registry/models.go` and `internal/cloudcp/registry/registry.go`
4. Add or change MSP account-scoped workspace provisioning entry handlers through `internal/cloudcp/account/tenant_handlers.go`
5. Add or change public cloud self-serve signup price configuration,
diff --git a/internal/cloudcp/entitlements/service.go b/internal/cloudcp/entitlements/service.go
index 20950347d8..fdc1a3c677 100644
--- a/internal/cloudcp/entitlements/service.go
+++ b/internal/cloudcp/entitlements/service.go
@@ -249,7 +249,11 @@ func (s *Service) resolveTenantLeaseContext(tenant *registry.Tenant, requestedSu
ctx.planVersion = pkglicensing.CanonicalizePlanVersion(stripeAccount.PlanVersion)
}
if ctx.subscriptionState == "" && stripeAccount != nil {
- ctx.subscriptionState = pkglicensing.MapStripeSubscriptionStatusToState(stripeAccount.SubscriptionState)
+ // stripeAccount.SubscriptionState holds the normalized stored form
+ // (e.g. "trial", "past_due") written by
+ // normalizeStripeAccountSubscriptionState; use the stored-form
+ // parser, not the raw Stripe-status mapper.
+ ctx.subscriptionState = pkglicensing.ParseStoredSubscriptionState(stripeAccount.SubscriptionState)
if ctx.subscriptionState == pkglicensing.SubStateTrial && stripeAccount.TrialEndsAt == nil {
ctx.subscriptionState = pkglicensing.SubStateActive
}
diff --git a/internal/cloudcp/entitlements/service_test.go b/internal/cloudcp/entitlements/service_test.go
index c76ac9d1fb..403debeda7 100644
--- a/internal/cloudcp/entitlements/service_test.go
+++ b/internal/cloudcp/entitlements/service_test.go
@@ -262,6 +262,69 @@ func TestRefreshPaidEntitlementRejectsDeletedTenantWithActiveBilling(t *testing.
}
}
+func TestRefreshPaidEntitlementGrantsLeaseForNormalizedTrialStored(t *testing.T) {
+ // Regression: stripe_accounts.subscription_state is stored in the
+ // normalized form ("trial"), not the raw Stripe form ("trialing"). The
+ // refresh path used to pass that stored form to the raw-Stripe mapper,
+ // which fail-closed to SubStateExpired and rejected the refresh with
+ // HTTP 410 Gone, silently demoting paying trial tenants to community
+ // tier mid-trial. Once the resolver uses ParseStoredSubscriptionState,
+ // a normalized "trial" with no trial_ends_at must produce a valid lease.
+ svc, pub, reg := newTestServiceWithBaseURL(t, "https://cloud.example.com")
+
+ accountID, err := registry.GenerateAccountID()
+ if err != nil {
+ t.Fatalf("GenerateAccountID: %v", err)
+ }
+ if err := reg.CreateAccount(&registry.Account{
+ ID: accountID,
+ Kind: registry.AccountKindIndividual,
+ DisplayName: "Pulse Labs",
+ }); err != nil {
+ t.Fatalf("CreateAccount: %v", err)
+ }
+ if err := reg.CreateStripeAccount(&registry.StripeAccount{
+ AccountID: accountID,
+ StripeCustomerID: "fake-test-customer-normalized-trial",
+ PlanVersion: "cloud_starter",
+ SubscriptionState: "trial", // normalized stored form, NOT the raw "trialing"
+ }); err != nil {
+ t.Fatalf("CreateStripeAccount: %v", err)
+ }
+
+ tenant := &registry.Tenant{
+ ID: "t-SERVICE_NORMALIZED_TRIAL",
+ AccountID: accountID,
+ Email: "[email protected]",
+ State: registry.TenantStateActive,
+ PlanVersion: "cloud_starter",
+ }
+ if err := reg.Create(tenant); err != nil {
+ t.Fatalf("Create: %v", err)
+ }
+
+ now := time.Unix(1710000000, 0).UTC()
+ svc.SetNow(func() time.Time { return now })
+ if _, _, err := reg.StoreOrIssueHostedEntitlement(tenant.ID, "etr_normalized_trial", now); err != nil {
+ t.Fatalf("StoreOrIssueHostedEntitlement: %v", err)
+ }
+
+ result, err := svc.RefreshEntitlement("etr_normalized_trial", "t-service_normalized_trial.cloud.example.com")
+ if err != nil {
+ t.Fatalf("RefreshEntitlement returned %v for a normalized trial tenant; it should succeed", err)
+ }
+ if result == nil || result.EntitlementJWT == "" {
+ t.Fatalf("expected a signed entitlement lease, got %#v", result)
+ }
+ claims, err := pkglicensing.VerifyEntitlementLeaseToken(result.EntitlementJWT, pub, "t-service_normalized_trial.cloud.example.com", now)
+ if err != nil {
+ t.Fatalf("VerifyEntitlementLeaseToken: %v", err)
+ }
+ if claims.SubscriptionState != pkglicensing.SubStateActive {
+ t.Fatalf("claims.SubscriptionState=%q, want %q (trial with nil trial_ends_at must be treated as active)", claims.SubscriptionState, pkglicensing.SubStateActive)
+ }
+}
+
func TestRefreshPaidEntitlementCanonicalizesBaseURLForExpectedHost(t *testing.T) {
svc, pub, reg := newTestServiceWithBaseURL(t, "https://Cloud.Example.com:8443/admin")
diff --git a/pkg/licensing/stripe_subscription.go b/pkg/licensing/stripe_subscription.go
index 422045bf0e..d2ac0e4737 100644
--- a/pkg/licensing/stripe_subscription.go
+++ b/pkg/licensing/stripe_subscription.go
@@ -22,6 +22,33 @@ func MapStripeSubscriptionStatusToState(status string) SubscriptionState {
}
}
+// ParseStoredSubscriptionState converts the normalized state stored in
+// stripe_accounts.subscription_state back into a SubscriptionState. The stored
+// form ("active", "trial", "past_due", "canceled") is distinct from the raw
+// Stripe status form ("trialing", "incomplete", ...) accepted by
+// MapStripeSubscriptionStatusToState.
+func ParseStoredSubscriptionState(stored string) SubscriptionState {
+ switch strings.ToLower(strings.TrimSpace(stored)) {
+ case "active":
+ return SubStateActive
+ case "trial":
+ return SubStateTrial
+ case "past_due":
+ return SubStateGrace
+ case "canceled":
+ return SubStateCanceled
+ case "grace":
+ return SubStateGrace
+ case "suspended":
+ return SubStateSuspended
+ case "expired":
+ return SubStateExpired
+ default:
+ // Fail closed: unknown stored values must not grant paid capabilities.
+ return SubStateExpired
+ }
+}
+
func ShouldGrantPaidCapabilities(state SubscriptionState) bool {
switch state {
case SubStateActive, SubStateTrial, SubStateGrace:
diff --git a/pkg/licensing/stripe_subscription_test.go b/pkg/licensing/stripe_subscription_test.go
index 1c9c1594a0..f135ddcf79 100644
--- a/pkg/licensing/stripe_subscription_test.go
+++ b/pkg/licensing/stripe_subscription_test.go
@@ -30,6 +30,35 @@ func TestMapStripeSubscriptionStatusToState(t *testing.T) {
}
}
+func TestParseStoredSubscriptionState(t *testing.T) {
+ tests := []struct {
+ name string
+ stored string
+ want SubscriptionState
+ }{
+ {name: "active", stored: "active", want: SubStateActive},
+ {name: "trial (normalized form, the one that was misparsed)", stored: "trial", want: SubStateTrial},
+ {name: "past_due", stored: "past_due", want: SubStateGrace},
+ {name: "canceled", stored: "canceled", want: SubStateCanceled},
+ {name: "grace tolerated for migration", stored: "grace", want: SubStateGrace},
+ {name: "suspended tolerated for migration", stored: "suspended", want: SubStateSuspended},
+ {name: "expired tolerated for migration", stored: "expired", want: SubStateExpired},
+ {name: "raw stripe trialing must NOT be accepted here", stored: "trialing", want: SubStateExpired},
+ {name: "unknown fails closed", stored: "garbage", want: SubStateExpired},
+ {name: "blank fails closed", stored: "", want: SubStateExpired},
+ {name: "trim and case", stored: " TRIAL ", want: SubStateTrial},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := ParseStoredSubscriptionState(tt.stored)
+ if got != tt.want {
+ t.Fatalf("state=%q, want %q", got, tt.want)
+ }
+ })
+ }
+}
+
func TestShouldGrantPaidCapabilities(t *testing.T) {
tests := []struct {
name string