diff --git a/internal/keycloak/adapter.go b/internal/keycloak/adapter.go index ea56ffe..ef3fb7e 100644 --- a/internal/keycloak/adapter.go +++ b/internal/keycloak/adapter.go @@ -91,4 +91,9 @@ type Adapter interface { // Health pings the admin endpoint. Used by readyz and the cluster cold- // start sequence (INFRASTRUCTURE.md §10 scenario F). Health(ctx context.Context) error + + // ServiceToken returns a client_credentials access token of the + // registry's own Keycloak client — the bearer for product lifecycle + // calls (PRODUCT_INTEGRATION_SPEC 2.0 §4.1; products verify aud + azp). + ServiceToken(ctx context.Context) (string, error) } diff --git a/internal/keycloak/client.go b/internal/keycloak/client.go index 2ec6529..64289fd 100644 --- a/internal/keycloak/client.go +++ b/internal/keycloak/client.go @@ -58,6 +58,9 @@ func NewHTTPAdapter(cfg HTTPConfig) *HTTPAdapter { // ─── auth ──────────────────────────────────────────────────────────────── +// ServiceToken exposes the cached client_credentials token (see Adapter). +func (a *HTTPAdapter) ServiceToken(ctx context.Context) (string, error) { return a.token(ctx) } + func (a *HTTPAdapter) token(ctx context.Context) (string, error) { a.mu.Lock() defer a.mu.Unlock() diff --git a/internal/keycloak/mock.go b/internal/keycloak/mock.go index 3cb6072..f9ba3d6 100644 --- a/internal/keycloak/mock.go +++ b/internal/keycloak/mock.go @@ -41,6 +41,9 @@ func (m *Mock) SyncTenantStatus(_ context.Context, slug, status string) error { func (m *Mock) Health(_ context.Context) error { return nil } +// ServiceToken returns a fixed token so provisioning requests carry a bearer. +func (m *Mock) ServiceToken(_ context.Context) (string, error) { return "mock-service-token", nil } + func (m *Mock) CreateOrgAndInvite(_ context.Context, in InviteInput) (*InviteResult, error) { m.mu.Lock() defer m.mu.Unlock() diff --git a/internal/server/tenants.go b/internal/server/tenants.go index 400b830..cb1e438 100644 --- a/internal/server/tenants.go +++ b/internal/server/tenants.go @@ -370,7 +370,14 @@ func (s *Server) provisionProduct(ctx context.Context, r *http.Request, t *store "id": t.ID, "name": t.Name, "slug": t.Slug, "plan": t.Plan, "product": p.Key, "config": tp.Config, "expires_at": tp.ExpiresAt, "contract_version": "2.0", } - if err := product.ProvisionAt(ctx, url, payload, nil, 10*time.Second); err != nil { + auth := func(ctx context.Context) (string, error) { + tok, err := s.Keycloak.ServiceToken(ctx) + if err != nil || tok == "" { + return "", err + } + return "Bearer " + tok, nil + } + if err := product.ProvisionAt(ctx, url, payload, auth, 10*time.Second); err != nil { s.emitAudit(ctx, r, store.AuditEvent{ TenantID: t.ID, Action: "product.provision_failed", TargetID: p.Key, TargetType: "product", Metadata: map[string]interface{}{"url": url, "err": err.Error()}, diff --git a/migrations/0005_provision_urls.down.sql b/migrations/0005_provision_urls.down.sql new file mode 100644 index 0000000..5c3d84e --- /dev/null +++ b/migrations/0005_provision_urls.down.sql @@ -0,0 +1 @@ +UPDATE products SET provision_urls = '{}'::jsonb WHERE key IN ('pruefwerk', 'firmwerk'); diff --git a/migrations/0005_provision_urls.up.sql b/migrations/0005_provision_urls.up.sql new file mode 100644 index 0000000..92da989 --- /dev/null +++ b/migrations/0005_provision_urls.up.sql @@ -0,0 +1,4 @@ +-- M-MIG-8: the registry provisions tenants at the products' platform endpoints +-- (PRODUCT_INTEGRATION_SPEC 2.0 §4.1) with its own service token. +UPDATE products SET provision_urls = '{"stage": "https://pruefwerk-stage.breakpilot.com/api/v1/platform/tenants/{id}/provision", "prod": "https://pruefwerk.breakpilot.com/api/v1/platform/tenants/{id}/provision"}'::jsonb WHERE key = 'pruefwerk'; +UPDATE products SET provision_urls = '{"stage": "https://firmwerk-stage.breakpilot.com/api/v1/platform/tenants/{id}/provision", "prod": "https://firmwerk.breakpilot.com/api/v1/platform/tenants/{id}/provision"}'::jsonb WHERE key = 'firmwerk';