Skip to content

tenancy: move to tenancy.platform.kcp.io, add self-service requests and memberships - #14

Merged
mjudeikis merged 1 commit into
kcp-dev:mainfrom
olamilekan000:tenancy-changes
Nov 2, 2026
Merged

mjudeikis merged 1 commit into
kcp-dev:mainfrom
olamilekan000:tenancy-changes

Conversation

@olamilekan000

Copy link
Copy Markdown
Contributor
  • Move the tenancy APIs to the tenancy.platform.kcp.io group.
  • Let users request tenants and projects themselves (SelfTenantRequest, SelfProjectRequest).
  • Serve memberships through the virtual workspace, and add platform admins to bootstrap.
  • CLI: add user, membership and seed commands, and keep the current tenant and project in a local context.

@kcp-ci-bot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@mjudeikis

Copy link
Copy Markdown
Contributor

@mjudeikis-bot review this please

@mjudeikis-bot mjudeikis-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of head 12e38e1. Findings are below; no Critical issues verified.

Findings: 3 Warning, 2 Note (inline).

  • Warning: membership Create can demote the last tenant admin (guard exists only in Delete).
  • Warning: lossy, kind-less Membership names let different subjects overwrite each other.
  • Warning: seedFirstAdmin re-grants a revoked first admin on every reconcile.
  • Notes: tenant-name enumeration via SelfTenantRequest; seed ignores a conflicting pre-existing grant.

Not anchored / not traced: I did not trace directory.ReviewFor role resolution for project-scoped admins, the CLI (tenancy/pkg/cli, clicontext), the bootstrap/APIExport YAML, or the generated OpenAPI/deepcopy for currency; I also did not run make codegen to check for drift. Concurrent Delete of two admins is a TOCTOU on the last-admin check against a cached directory, and I noted but did not separately report it.

Checked

  • Read beyond the diff: selftenantrequest/storage.go, selfprojectrequest/storage.go, membership/storage.go, server/projects.go (project/membership/tenant writers), naming/naming.go, operator/tenant.go (reconcile + seedFirstAdmin), server/options.go wiring grep for self-service flags.
  • Correctness: found the three Warnings above; the create/idempotency paths otherwise handle AlreadyExists and nil create.
  • Concurrency: last-admin check is non-atomic against the directory cache (TOCTOU), not reported separately; no new goroutines traced.
  • kcp specifics: writers address tenant workspaces by cluster derived from the caller's own claim, not request input; confirmed in projectWriter and membershipWriter.
  • API/storage: Group rename to tenancy.platform.kcp.io replaces the old schema YAMLs; I did not verify migration/deprecation of objects stored under the old group.
  • Security: self-service tenant gating is off by default and groups-restricted; FirstAdmin is forced to the caller (good). Enumeration noted.
  • Tests: new unit tests exist for membership, selfproject/tenant request; none cover demoting the last admin, slug collisions, or re-seeding after revoke.
  • Commands: go build ./... OK; go test ./tenancy/pkg/... all packages OK; go vet ./tenancy/... printed no diagnostics (I only saw module download output). Linter not run.
  • E2E: not run (needs a kcp environment).

Spec: m.Spec,
}

created, err := r.opts.Create(ctx, claim.Cluster, grant)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning: Create can demote the last tenant admin, bypassing the guard Delete enforces
Create upserts in place (membershipWriter.Create overwrites existing.Spec, server/projects.go:115-119). An admin who POSTs {subject: <self or the only other admin>, role: view, tenant: T} rewrites the sole tenant-wide admin grant to view. lastTenantAdmin is only checked in Delete (line ~239), so the "cannot be undone through this API" state the Delete comment tries to prevent is reachable through Create.

if existing := r.findGrant(claim.Cluster, grant.Name); existing != nil &&
	existing.Role == string(tenancyv1alpha1.RoleAdmin) && existing.Project == "" &&
	m.Spec.Role != tenancyv1alpha1.RoleAdmin &&
	lastTenantAdmin(r.directory.MembershipsIn(claim.Cluster), m.Spec.Tenant) {
	return nil, apierrors.NewForbidden(tenancyv1alpha1.Resource("memberships"), grant.Name,
		fmt.Errorf("this is the last admin of tenant %q", m.Spec.Tenant))
}


grant := &tenancyv1alpha1.Membership{
ObjectMeta: metav1.ObjectMeta{
Name: naming.MembershipName(m.Spec.Subject.Name, m.Spec.Tenant, m.Spec.Project),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning: grant name drops the subject kind and is lossy, so distinct subjects overwrite each other
naming.MembershipName slugifies the subject, so alice@x.com (user), ALICE@X.COM and a group named alice.x.com all map to alice-x-com-<tenant>. Because membershipWriter.Create updates an existing object in place, granting view to the group silently replaces an admin grant for the user (and Delete of one name revokes the other). Include the kind and disambiguate with a hash of the raw subject when the slug is lossy.

func MembershipName(kind, subject, tenant, project string) string {
	sum := sha256.Sum256([]byte(kind + "\x00" + subject))
	name := Slugify(subject) + "-" + hex.EncodeToString(sum[:])[:6] + "-" + Slugify(tenant)
	...
}

Comment thread tenancy/pkg/operator/tenant.go Outdated
Tenant: tenant.Name,
},
}
if err := c.Create(ctx, membership); err != nil && !apierrors.IsAlreadyExists(err) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning: first admin is re-granted on every reconcile, contradicting "once" and "it must not come back"
seedFirstAdmin runs on every successful reconcile of a ready Tenant and recreates the Membership whenever Create succeeds, i.e. whenever it is absent. There is no status marker. If a tenant admin revokes spec.firstAdmin (e.g. a departed user) via the new membership Delete, the next Tenant event, operator restart or resync grants admin back. Record that seeding happened (e.g. a Seeded condition or status field set after a successful create) and skip when it is set.

if tenant.Status.FirstAdminSeeded {
	return nil
}
// ... create ...
tenant.Status.FirstAdminSeeded = true // persisted via updateTenantStatus

}
// Someone else may hold it. Saying so is better than implying it is
// theirs, and the review will show whether they can reach it.
return fmt.Sprintf("tenant %q already exists", tenant)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: "already exists" lets any authenticated caller enumerate tenant names
The project path deliberately avoids revealing whether a tenant exists (selfprojectrequest/storage.go Forbidden comment), but here the status Created=false / "already exists" message confirms a name to any caller (any authenticated user when --self-service-tenant-groups is empty). Names can also be squatted first-come. Consider returning a generic Conflict/Forbidden for names the caller does not administer, or documenting the trade-off.

Comment thread tenancy/pkg/operator/tenant.go Outdated
Tenant: tenant.Name,
},
}
if err := c.Create(ctx, membership); err != nil && !apierrors.IsAlreadyExists(err) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: a pre-existing Membership with the seed name hides a missing first admin
IsAlreadyExists is ignored without checking the existing object's role. If a view grant already has the slug name (e.g. from the CLI's MembershipName), the first admin is never granted admin, and the tenant may end up with no admin while the status shows Ready. Compare the existing spec or log/surface it in status.

…nd memberships

Signed-off-by: olamilekan000 <odukoyaonline@gmail.com>
@mjudeikis
mjudeikis merged commit f716a3e into kcp-dev:main Nov 2, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants