Files
tenant-registry/internal/server/entitlements.go
T
Sharang ParnerkarandClaude Fable 5.1 62335a9be3
ci / shared (pull_request) Successful in 13s
ci / test (pull_request) Failing after 14m36s
ci / image (pull_request) Skipped
feat(m-mig-5): products table, entitlement check, tenant ladder, per-client authz
Registry becomes the entitlement authority (ERP/02, PRODUCT_INTEGRATION_SPEC 2.0):

- migrations 0002/0003: ladder statuses warning|restricted|halted on
  tenant_status; products table replaces the hard-coded catalog, seeded with
  pruefwerk (+ werkbank add-on), werkpilot, firmwerk; compliance disabled
  until the dsms migration (M-MIG-9)
- GET /v1/tenants (keyset pagination, status/kind filters) for product
  schedulers and reconcile jobs
- POST /v1/internal/entitlements/check → {allowed, read_only, status, limits,
  expires_at, reason}; add-on limits from tenant_products.config
- POST /v1/tenants/{id}/status — the single ladder transition endpoint;
  cancel now moves to halted; activate accepts products[] with limits and
  provisions them via the catalog's per-env provision_urls
- SyncTenantStatus: coarse tenant_status attribute on every Keycloak org
  member after every status change (attributes merged, roles untouched)
- AUTHZ_RULES: per-client (JWT azp) route groups; 403 FORBIDDEN_CLIENT
- INVITE_CLIENT_ID / INVITE_REDIRECT_URI replace the hard-coded dev-portal
  invite parameters
- catalog entries carry type, launch_url (per APP_ENV), skus, addons,
  trial_days; openapi.yaml + README updated

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
2026-09-11 16:29:44 +02:00

140 lines
3.5 KiB
Go

package server
import (
"context"
"errors"
"net/http"
"time"
"git.breakpilot.com/platform/tenant-registry/internal/store"
)
type entitlementCheckReq struct {
TenantID string `json:"tenant_id"`
Product string `json:"product"`
Addon string `json:"addon,omitempty"`
}
// EntitlementCheck is the answer products cache for ≤ 60 s
// (PRODUCT_INTEGRATION_SPEC 2.0 §4.4). ReadOnly mirrors the ladder: warning
// keeps full access, restricted/halted/frozen block writes.
type EntitlementCheck struct {
Allowed bool `json:"allowed"`
ReadOnly bool `json:"read_only"`
Status string `json:"status,omitempty"`
Limits map[string]interface{} `json:"limits,omitempty"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
Reason string `json:"reason,omitempty"`
}
// checkEntitlement is POST /v1/internal/entitlements/check.
func (s *Server) checkEntitlement(w http.ResponseWriter, r *http.Request) {
var in entitlementCheckReq
if !decodeJSON(w, r, &in) {
return
}
if in.TenantID == "" || in.Product == "" {
writeError(w, http.StatusBadRequest, "invalid_input", "tenant_id and product are required")
return
}
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
defer cancel()
res, err := s.evaluateEntitlement(ctx, in)
if err != nil {
writeError(w, http.StatusInternalServerError, "internal", err.Error())
return
}
writeJSON(w, http.StatusOK, res)
}
func (s *Server) evaluateEntitlement(ctx context.Context, in entitlementCheckReq) (EntitlementCheck, error) {
prod, err := s.product(ctx, in.Product)
if err != nil {
if errors.Is(err, store.ErrNotFound) {
return EntitlementCheck{Allowed: false, Reason: "unknown_product"}, nil
}
return EntitlementCheck{}, err
}
if in.Addon != "" && !prod.HasAddon(in.Addon) {
return EntitlementCheck{Allowed: false, Reason: "unknown_addon"}, nil
}
t, err := s.Store.GetTenant(ctx, in.TenantID)
if err != nil {
if errors.Is(err, store.ErrNotFound) {
return EntitlementCheck{Allowed: false, Reason: "tenant_not_found"}, nil
}
return EntitlementCheck{}, err
}
out := EntitlementCheck{Status: t.Status}
switch t.Status {
case store.StatusArchived:
out.Reason = "tenant_archived"
return out, nil
case store.StatusHalted:
out.Reason = "tenant_halted"
return out, nil
case store.StatusRestricted, store.StatusFrozen:
out.ReadOnly = true
}
tps, err := s.Store.ListTenantProducts(ctx, t.ID)
if err != nil {
return EntitlementCheck{}, err
}
now := time.Now().UTC()
for _, tp := range tps {
if tp.Product != prod.Key {
continue
}
if !tp.Enabled {
out.Reason = "entitlement_disabled"
return out, nil
}
if tp.ExpiresAt != nil && tp.ExpiresAt.Before(now) {
out.Reason = "entitlement_expired"
out.ExpiresAt = tp.ExpiresAt
return out, nil
}
if in.Addon != "" {
key := addonLimitKey(prod, in.Addon)
if v, ok := tp.Config[key]; !ok || toFloat(v) <= 0 {
out.Reason = "addon_not_purchased"
return out, nil
}
}
out.Allowed = true
out.Limits = tp.Config
out.ExpiresAt = tp.ExpiresAt
return out, nil
}
out.Reason = "not_entitled"
return out, nil
}
func addonLimitKey(p *store.Product, addon string) string {
for _, a := range p.Addons {
if a.ID == addon {
if a.LimitKey != "" {
return a.LimitKey
}
return addon
}
}
return addon
}
func toFloat(v interface{}) float64 {
switch n := v.(type) {
case float64:
return n
case int:
return float64(n)
case int64:
return float64(n)
case bool:
if n {
return 1
}
}
return 0
}