diff --git a/marketplace/cmd/init.go b/marketplace/cmd/init.go index fce5d7a..3c57c0f 100644 --- a/marketplace/cmd/init.go +++ b/marketplace/cmd/init.go @@ -85,10 +85,11 @@ created. Idempotent: safe to run on every pod start and every upgrade.`, return fmt.Errorf("resolve workspace path %s: %w", workspacePath, err) } - result, err := bootstrap.Bootstrap(ctx, target, workspacePath, bootstrap.Options{ + opts := bootstrap.Options{ ServerUsers: serverUsers, ServerGroups: serverGroups, - }) + } + result, err := bootstrap.Bootstrap(ctx, target, workspacePath, opts) if err != nil { return err } @@ -108,7 +109,7 @@ created. Idempotent: safe to run on every pod start and every upgrade.`, if err != nil { return fmt.Errorf("resolve workspace path %s: %w", catalogPath, err) } - if err := bootstrap.BindCatalog(ctx, catalogCfg, result); err != nil { + if err := bootstrap.BindCatalog(ctx, catalogCfg, result, opts); err != nil { return fmt.Errorf("bind the Provider API into %s: %w", catalogPath, err) } if err := bootstrap.GrantPlatformAdmins(ctx, catalogCfg, adminUsers, adminGroups); err != nil { @@ -127,7 +128,7 @@ created. Idempotent: safe to run on every pod start and every upgrade.`, if err != nil { return fmt.Errorf("resolve workspace path %s: %w", producersPath, err) } - if err := bootstrap.BindProvidersParent(ctx, providersCfg, result); err != nil { + if err := bootstrap.BindProvidersParent(ctx, providersCfg, result, opts); err != nil { return fmt.Errorf("bind the provisioner claim into %s: %w", producersPath, err) } logger.Info("producers parent ready", "workspace", producersPath) diff --git a/marketplace/pkg/bootstrap/bootstrap.go b/marketplace/pkg/bootstrap/bootstrap.go index 691ee77..36843f1 100644 --- a/marketplace/pkg/bootstrap/bootstrap.go +++ b/marketplace/pkg/bootstrap/bootstrap.go @@ -170,10 +170,13 @@ func Bootstrap(ctx context.Context, cfg *rest.Config, exportsPath string, opts O // BindCatalog makes the workspace cfg points at the Producer registry: it // binds marketplace-catalog, then waits until Producers are servable. -func BindCatalog(ctx context.Context, cfg *rest.Config, result *Result) error { +func BindCatalog(ctx context.Context, cfg *rest.Config, result *Result, opts Options) error { if err := applyTier(ctx, cfg, result, apiexport.CatalogOrder); err != nil { return err } + if err := grantLocator(ctx, cfg, opts); err != nil { + return err + } dyn, err := dynamic.NewForConfig(cfg) if err != nil { @@ -198,8 +201,44 @@ func BindCatalog(ctx context.Context, cfg *rest.Config, result *Result) error { // parent: it binds marketplace-provisioner, so producer workspaces can be // created as its children. Nothing to wait for — the claim adds reach, not // a servable API. -func BindProvidersParent(ctx context.Context, cfg *rest.Config, result *Result) error { - return applyTier(ctx, cfg, result, apiexport.ProducersOrder) +func BindProvidersParent(ctx context.Context, cfg *rest.Config, result *Result, opts Options) error { + if err := applyTier(ctx, cfg, result, apiexport.ProducersOrder); err != nil { + return err + } + return grantLocator(ctx, cfg, opts) +} + +// grantLocator grants the server identities the least they need to find +// out which logical cluster the workspace cfg points at: kcp's workspace +// gate, and a read of the LogicalCluster that names it. The marketplace +// exports address clusters and not paths, so the operator cannot work +// with a hand-bound tier without it. +func grantLocator(ctx context.Context, cfg *rest.Config, opts Options) error { + if len(opts.ServerUsers)+len(opts.ServerGroups) == 0 { + return nil + } + kube, err := kubernetes.NewForConfig(cfg) + if err != nil { + return fmt.Errorf("build kubernetes client: %w", err) + } + role := &rbacv1.ClusterRole{ + ObjectMeta: metav1.ObjectMeta{Name: marketplacev1alpha1.GroupName + ":locator"}, + Rules: []rbacv1.PolicyRule{ + { + Verbs: []string{"access"}, + NonResourceURLs: []string{"/"}, + }, + { + APIGroups: []string{"core.kcp.io"}, + Resources: []string{"logicalclusters"}, + Verbs: []string{"get"}, + }, + }, + } + if err := applyRoleAndBinding(ctx, kube, role, opts.ServerUsers, opts.ServerGroups); err != nil { + return fmt.Errorf("grant the server identities the locator role: %w", err) + } + return nil } // applyTier applies one hand-bound tier's assets. These two tiers are the diff --git a/tenancy/cmd/init.go b/tenancy/cmd/init.go index ea25182..b7aa76c 100644 --- a/tenancy/cmd/init.go +++ b/tenancy/cmd/init.go @@ -133,7 +133,10 @@ root down. Idempotent: safe to run on every pod start and every upgrade.`, if err != nil { return fmt.Errorf("resolve workspace path %s: %w", tenantsPath, err) } - if err := bootstrap.BindTenantsParent(ctx, tenantsCfg, result); err != nil { + if err := bootstrap.BindTenantsParent(ctx, tenantsCfg, result, bootstrap.Options{ + ServerUsers: serverUsers, + ServerGroups: serverGroups, + }); err != nil { return fmt.Errorf("bind the provisioner claim into %s: %w", tenantsPath, err) } logger.Info("provisioning parent ready", "workspace", tenantsPath) diff --git a/tenancy/pkg/bootstrap/bootstrap.go b/tenancy/pkg/bootstrap/bootstrap.go index a9ed29c..dbb759f 100644 --- a/tenancy/pkg/bootstrap/bootstrap.go +++ b/tenancy/pkg/bootstrap/bootstrap.go @@ -184,7 +184,7 @@ func BindStore(ctx context.Context, cfg *rest.Config, result *Result, opts Optio if err != nil { return fmt.Errorf("build kubernetes client: %w", err) } - if err := applyStoreLocator(ctx, kube, opts.ServerUsers, opts.ServerGroups); err != nil { + if err := applyLocator(ctx, kube, tenancyv1alpha1.GroupName+":store-locator", opts.ServerUsers, opts.ServerGroups); err != nil { return fmt.Errorf("grant the server identities the store locator role: %w", err) } } @@ -214,8 +214,25 @@ func BindStore(ctx context.Context, cfg *rest.Config, result *Result, opts Optio // // There is nothing to wait for here — the claim adds no servable API, it // adds reach — so a readiness probe would have nothing to probe. -func BindTenantsParent(ctx context.Context, cfg *rest.Config, result *Result) error { - return applyTier(ctx, cfg, result, apiexport.TenantsOrder) +// +// The operator creates tenant workspaces under this one through the +// provisioner export, which addresses clusters and not paths, so the +// server identities get the same locator role as in the store. +func BindTenantsParent(ctx context.Context, cfg *rest.Config, result *Result, opts Options) error { + if err := applyTier(ctx, cfg, result, apiexport.TenantsOrder); err != nil { + return err + } + if len(opts.ServerUsers)+len(opts.ServerGroups) == 0 { + return nil + } + kube, err := kubernetes.NewForConfig(cfg) + if err != nil { + return fmt.Errorf("build kubernetes client: %w", err) + } + if err := applyLocator(ctx, kube, tenancyv1alpha1.GroupName+":tenants-locator", opts.ServerUsers, opts.ServerGroups); err != nil { + return fmt.Errorf("grant the server identities the tenants locator role: %w", err) + } + return nil } // applyTier applies one hand-bound tier's assets. @@ -392,13 +409,13 @@ func applyManifests(ctx context.Context, dyn dynamic.Interface, mapper *restmapp } } -// applyStoreLocator grants the least a server needs to find out which -// logical cluster the store workspace is: kcp's workspace gate, and a read -// of the LogicalCluster that names it. Deliberately not the Tenant API — -// tenants are written through the platform export, not through here. -func applyStoreLocator(ctx context.Context, kube kubernetes.Interface, users, groups []string) error { +// applyLocator grants the least a server needs to find out which logical +// cluster a workspace is: kcp's workspace gate, and a read of the +// LogicalCluster that names it. Deliberately not the Tenant API — tenants +// are written through the platform export, not through here. +func applyLocator(ctx context.Context, kube kubernetes.Interface, name string, users, groups []string) error { role := &rbacv1.ClusterRole{ - ObjectMeta: metav1.ObjectMeta{Name: tenancyv1alpha1.GroupName + ":store-locator"}, + ObjectMeta: metav1.ObjectMeta{Name: name}, Rules: []rbacv1.PolicyRule{ { Verbs: []string{"access"},