diff --git a/README.md b/README.md index 593403d..fc6d47f 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,7 @@ State-changing endpoints emit audit events automatically. The OpenAPI contract t - No ERP: the outbox takes nothing, the ERP webhook is off, and `ERP_BASE_URL` / `ERP_WEBHOOK_SECRET` must be empty. - No trial: a new workspace starts `active`, and the ladder never escalates by time (the licence is the contract). - No sign-up and no sales requests: the trial-offer, trial-terms, catalog request, trial request and admin trial routes answer 404 `not_available`. -- Products and URLs only from `PRODUCTS` and `PRODUCT_*_URLS`. The seeded `stage`/`prod` URLs are never used, so nothing is sent to a Breakpilot host. Each listed product needs a launch URL. +- Products and URLs only from `PRODUCTS` and `PRODUCT_*_URLS`. The seeded `stage`/`prod` URLs are never used, so provisioning never reaches a Breakpilot host (the test install in `platform/onprem` checks all outbound traffic). Each listed product needs a launch URL; an unknown key stops the start. The catalog offers no trials. - No cloud defaults: the registry refuses to start without `AUTH_ENABLED=true`, `AUTHZ_RULES`, `LICENCE_SIGNING_KEY`, `DATABASE_URL`, the Keycloak admin settings, `PORTAL_URL`, `INVITE_CLIENT_ID`, `INVITE_REDIRECT_URI`, `MAIL_FROM` and `PRODUCTS`, and without `LITELLM_BASE_URL` when `LITELLM_MASTER_KEY` is set. Contract documents stay off. ## ERP link (M-MIG-7) diff --git a/cmd/server/main.go b/cmd/server/main.go index 165e21c..bf54457 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -84,9 +84,19 @@ func main() { defer s.Close() kind := storeKind(s) // PRODUCTS / PRODUCT_*_URLS (on-prem: the only source of product URLs). + if err := store.CheckProductKeys(bootCtx, s, cfg.Products, cfg.LaunchURLs, cfg.ProvisionURLs); err != nil { + slog.Error("product overrides invalid", "err", err) + os.Exit(1) + } s = store.WithProductOverrides(s, cfg.CatalogEnv(), cfg.Products, cfg.LaunchURLs, cfg.ProvisionURLs) if cfg.OnPrem() { - slog.Info("on-prem mode: no ERP, no trial ladder, no sign-up", "products", cfg.Products) + var noProvision []string + for _, k := range cfg.Products { + if cfg.ProvisionURLs[k] == "" { + noProvision = append(noProvision, k) + } + } + slog.Info("on-prem mode: no ERP, no trial ladder, no sign-up", "products", cfg.Products, "without_provisioning", noProvision) } var kc keycloak.Adapter diff --git a/internal/server/catalog.go b/internal/server/catalog.go index ddb09eb..096bc7b 100644 --- a/internal/server/catalog.go +++ b/internal/server/catalog.go @@ -24,7 +24,11 @@ func (s *Server) getCatalog(w http.ResponseWriter, r *http.Request) { } items := make([]store.CatalogEntry, 0, len(products)) for _, p := range products { - items = append(items, p.Catalog(s.env())) + e := p.Catalog(s.env()) + if s.onPrem() { + e.SupportsTrial = false // no trials on a customer's node: the portal hides the button + } + items = append(items, e) } writeJSON(w, http.StatusOK, map[string]any{"items": items}) } diff --git a/internal/server/onprem_test.go b/internal/server/onprem_test.go index 49fc5ef..5741834 100644 --- a/internal/server/onprem_test.go +++ b/internal/server/onprem_test.go @@ -79,12 +79,23 @@ func TestOnPrem_cloudOnlyRoutesAnswer404(t *testing.T) { func TestOnPrem_newWorkspaceIsActiveAndQueuesNothingForTheERP(t *testing.T) { eachStore(t, func(t *testing.T, h *testHarness) { - onPremHarness(h, nil) ctx := context.Background() + // Control: in the cloud a new workspace queues its ERP customer. before, err := h.store.PendingOutbox(ctx) if err != nil { t.Fatal(err) } + if resp, body := h.do("POST", "/v1/tenants", map[string]any{"slug": "cloud-ws", "name": "Cloud GmbH"}); resp.StatusCode != http.StatusCreated { + t.Fatalf("cloud create = %d %s", resp.StatusCode, body) + } + if n, _ := h.store.PendingOutbox(ctx); n != before+1 { + t.Fatalf("cloud outbox %d → %d, want +1", before, n) + } + onPremHarness(h, nil) + before, err = h.store.PendingOutbox(ctx) + if err != nil { + t.Fatal(err) + } resp, body := h.do("POST", "/v1/tenants", map[string]any{"slug": "werk", "name": "Werk GmbH"}) if resp.StatusCode != http.StatusCreated { t.Fatalf("create = %d %s", resp.StatusCode, body) @@ -190,3 +201,92 @@ func newTrial(t *testing.T, h *testHarness) *store.Tenant { } return tr } + +// cloudURLStore serves Prüfwerk with seeded stage/prod provision URLs, as the +// migrations do on a real database. +type cloudURLStore struct { + store.Store + url string +} + +func (c cloudURLStore) seed(p *store.Product) { + if p.Key == "pruefwerk" { + p.ProvisionURLs = map[string]string{"stage": c.url + "/{id}/stage", "prod": c.url + "/{id}/prod"} + } +} + +func (c cloudURLStore) GetProduct(ctx context.Context, key string) (*store.Product, error) { + p, err := c.Store.GetProduct(ctx, key) + if err == nil { + c.seed(p) + } + return p, err +} + +func (c cloudURLStore) ListProducts(ctx context.Context, all bool) ([]store.Product, error) { + ps, err := c.Store.ListProducts(ctx, all) + for i := range ps { + c.seed(&ps[i]) + } + return ps, err +} + +// Blocker #1: the seeded cloud URLs are never called on-prem, even when the +// values file gives a product no provision URL of its own. +func TestOnPrem_seededCloudProvisionURLsAreNeverCalled(t *testing.T) { + eachStore(t, func(t *testing.T, h *testHarness) { + var mu sync.Mutex + calls := 0 + cloud := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + calls++ + mu.Unlock() + w.WriteHeader(http.StatusOK) + })) + defer cloud.Close() + seeded := cloudURLStore{Store: h.store, url: cloud.URL} + activate := func() { + t.Helper() + if resp, body := h.do("POST", "/v1/tenants/"+h.tenant.ID+"/activate", map[string]any{ + "products": []map[string]any{{"key": "pruefwerk"}}, + }); resp.StatusCode != http.StatusOK { + t.Fatalf("activate = %d %s", resp.StatusCode, body) + } + } + + // Control: in the cloud (dev reads stage) the seeded URL is called. + h.app.Store = seeded + activate() + mu.Lock() + if calls != 1 { + t.Fatalf("cloud: %d calls to the seeded URL, want 1", calls) + } + calls = 0 + mu.Unlock() + + h.app.Cfg = &config.Config{Env: config.EnvOnPrem, LicenceTTLDays: 30} + h.app.Store = store.WithProductOverrides(seeded, h.app.Cfg.CatalogEnv(), []string{"pruefwerk"}, + map[string]string{"pruefwerk": "https://pruefwerk.onprem.test"}, nil) + activate() + mu.Lock() + defer mu.Unlock() + if calls != 0 { + t.Errorf("on-prem: %d calls to a seeded cloud URL", calls) + } + }) +} + +func TestOnPrem_catalogOffersNoTrials(t *testing.T) { + eachStore(t, func(t *testing.T, h *testHarness) { + onPremHarness(h, nil) + _, body := h.do("GET", "/v1/catalog", nil) + out := decode[struct { + Items []store.CatalogEntry `json:"items"` + }](t, body) + for _, it := range out.Items { + if it.SupportsTrial { + t.Errorf("%s offers a trial on-prem", it.Key) + } + } + }) +} diff --git a/internal/store/product_overrides.go b/internal/store/product_overrides.go index ee5474c..c29acaa 100644 --- a/internal/store/product_overrides.go +++ b/internal/store/product_overrides.go @@ -1,6 +1,11 @@ package store -import "context" +import ( + "context" + "fmt" + "sort" + "strings" +) // ProductOverrides decorates a Store's catalog reads with the deployment's // own product list and URLs (PRODUCTS, PRODUCT_LAUNCH_URLS, @@ -77,3 +82,37 @@ func (o *ProductOverrides) ListProducts(ctx context.Context, includeDisabled boo } return out, nil } + +// CheckProductKeys fails when PRODUCTS or a URL override names a product the +// catalog does not know: a typo would otherwise ship a product without a +// tile or skip its provisioning without a trace. +func CheckProductKeys(ctx context.Context, s Store, only []string, launch, provision map[string]string) error { + all, err := s.ListProducts(ctx, true) + if err != nil { + return err + } + known := map[string]bool{} + for _, p := range all { + known[p.Key] = true + } + var unknown []string + add := func(src, k string) { + if !known[k] { + unknown = append(unknown, src+": "+k) + } + } + for _, k := range only { + add("PRODUCTS", k) + } + for k := range launch { + add("PRODUCT_LAUNCH_URLS", k) + } + for k := range provision { + add("PRODUCT_PROVISION_URLS", k) + } + if len(unknown) > 0 { + sort.Strings(unknown) + return fmt.Errorf("unknown products (%s)", strings.Join(unknown, ", ")) + } + return nil +} diff --git a/internal/store/product_overrides_test.go b/internal/store/product_overrides_test.go new file mode 100644 index 0000000..916f573 --- /dev/null +++ b/internal/store/product_overrides_test.go @@ -0,0 +1,38 @@ +package store + +import ( + "context" + "strings" + "testing" +) + +func TestCheckProductKeys(t *testing.T) { + m := NewMemory() + ctx := context.Background() + if err := CheckProductKeys(ctx, m, []string{"pruefwerk", "werkmeister"}, + map[string]string{"pruefwerk": "https://pruefwerk.onprem.test"}, + map[string]string{"pruefwerk": "https://pruefwerk.onprem.test/{id}"}); err != nil { + t.Errorf("known keys refused: %v", err) + } + err := CheckProductKeys(ctx, m, []string{"pruefwerck"}, map[string]string{"firmwrk": "https://x.onprem.test"}, nil) + if err == nil || !strings.Contains(err.Error(), "PRODUCTS: pruefwerck") || !strings.Contains(err.Error(), "PRODUCT_LAUNCH_URLS: firmwrk") { + t.Errorf("err = %v", err) + } +} + +func TestProductOverrides_doNotShareTheStoreMaps(t *testing.T) { + m := NewMemory() + ctx := context.Background() + o := WithProductOverrides(m, "onprem", nil, map[string]string{"pruefwerk": "https://pruefwerk.onprem.test"}, nil) + p, err := o.GetProduct(ctx, "pruefwerk") + if err != nil || p.LaunchURLs["onprem"] != "https://pruefwerk.onprem.test" { + t.Fatalf("override missing: %v %v", p, err) + } + raw, _ := m.GetProduct(ctx, "pruefwerk") + if _, leaked := raw.LaunchURLs["onprem"]; leaked { + t.Error("the override was written into the store's own map") + } + if WithProductOverrides(m, "prod", nil, nil, nil) != Store(m) { + t.Error("no overrides should return the store unchanged") + } +}