From 953afc300bf70a47d9b17818c295453bb476b181 Mon Sep 17 00:00:00 2001 From: Scot Wells Date: Fri, 27 Nov 2026 16:37:13 -0600 Subject: [PATCH] fix(registration): Identify the registering caller by username Registration admission for MachineCapacity refused any caller without a UID. Some credentials, such as client certificates, carry a username but no UID, so a publisher signed in with one could not register capacity. A caller is now identified by username, as Kubernetes RBAC identifies it. The UID stays optional: the SubjectAccessReviews carry it when the credential has one, and the authorizer decides. A test against a real API server binds RBAC to a username alone and registers as that user with no UID. Co-Authored-By: Claude Opus 5.5 --- .../inventory-capacity-registration.md | 8 ++++-- internal/registration/admission.go | 2 +- internal/registration/admission_test.go | 28 ++++++++++++++++++- internal/registration/envtest_test.go | 25 ++++++++++++++++- internal/registration/runtime.go | 2 +- 5 files changed, 58 insertions(+), 7 deletions(-) diff --git a/docs/development/inventory-capacity-registration.md b/docs/development/inventory-capacity-registration.md index 76853e3e..e0fea69d 100644 --- a/docs/development/inventory-capacity-registration.md +++ b/docs/development/inventory-capacity-registration.md @@ -85,12 +85,14 @@ stage-1 preparation endpoint. inventory-backed capacity it: 1. requires Milo's authenticated `iam.miloapis.com/parent-*` extras naming a - single Project in the configured provider scope, and an actor UID; + single Project in the configured provider scope, and an actor username. As + in Kubernetes RBAC, the username identifies the actor. Its UID is optional, + because some credentials, such as client certificates, carry none; 2. requires the `default` namespace, the exact shape above, and a name equal to `device-`; 3. resolves the current Project lifetime through the root Milo client; -4. runs SubjectAccessReviews with the caller's username, UID, groups and - extras for `machinecapacities.create` and named `devices.get` in +4. runs SubjectAccessReviews with the caller's username, UID when present, + groups and extras for `machinecapacities.create` and named `devices.get` in `default`. Denials, errors and incomplete evaluations fail closed. This happens before the Device is read, so an unauthorized caller cannot learn whether a Device exists; diff --git a/internal/registration/admission.go b/internal/registration/admission.go index 3accfd7b..32637d58 100644 --- a/internal/registration/admission.go +++ b/internal/registration/admission.go @@ -112,7 +112,7 @@ func projectName(actor authenticationv1.UserInfo) (string, error) { } } values := actor.Extra[iam.ParentNameExtraKey] - if len(values) != 1 || values[0] == "" || actor.Username == "" || actor.UID == "" { + if len(values) != 1 || values[0] == "" || actor.Username == "" { return "", errors.New("authenticated actor and project identity are required") } return values[0], nil diff --git a/internal/registration/admission_test.go b/internal/registration/admission_test.go index d39bb5e0..5e17430b 100644 --- a/internal/registration/admission_test.go +++ b/internal/registration/admission_test.go @@ -85,6 +85,32 @@ func TestRegistrationAllowsLiveEligibleDeviceWithoutEffects(t *testing.T) { } } +// A caller is identified by username, as Kubernetes RBAC identifies it. Some +// credentials, such as client certificates, carry no UID. +func TestRegistrationIdentifiesTheCallerByUsername(t *testing.T) { + handler, device := fixture(t) + var reviewed []authenticationv1.UserInfo + handler.Authorize = func(_ context.Context, _ Project, actor authenticationv1.UserInfo, _, _, _ string) error { + reviewed = append(reviewed, actor) + return nil + } + request := registrationRequest(t, device) + request.UserInfo.UID = "" + dryRun := true + request.DryRun = &dryRun + if response := handler.Handle(context.Background(), request); !response.Allowed { + t.Fatalf("a caller without a UID was denied: %s", response.Result.Message) + } + if len(reviewed) == 0 { + t.Fatal("the caller's permission was not reviewed") + } + for _, actor := range reviewed { + if actor.Username != "operator" || actor.UID != "" { + t.Fatalf("reviewed %+v, want the caller's username and no UID", actor) + } + } +} + func TestRegistrationNegatives(t *testing.T) { tests := map[string]func(*Handler, *inventory.Device, *admission.Request){ "stale UID": func(_ *Handler, _ *inventory.Device, request *admission.Request) { @@ -126,7 +152,7 @@ func TestRegistrationNegatives(t *testing.T) { request.Resource.Resource = "devices" }, "missing project": func(_ *Handler, _ *inventory.Device, request *admission.Request) { request.UserInfo.Extra = nil }, - "missing actor": func(_ *Handler, _ *inventory.Device, request *admission.Request) { request.UserInfo.UID = "" }, + "missing actor": func(_ *Handler, _ *inventory.Device, request *admission.Request) { request.UserInfo.Username = "" }, "organization context": func(_ *Handler, _ *inventory.Device, request *admission.Request) { request.UserInfo.Extra[iam.ParentKindExtraKey] = authenticationv1.ExtraValue{"Organization"} }, diff --git a/internal/registration/envtest_test.go b/internal/registration/envtest_test.go index 75cc4668..a67d290b 100644 --- a/internal/registration/envtest_test.go +++ b/internal/registration/envtest_test.go @@ -78,9 +78,16 @@ func (api *liveAPI) actor(name string, groups ...string) client.Client { // actorIn impersonates a caller routed through the named project. func (api *liveAPI) actorIn(project, name string, groups ...string) client.Client { + api.t.Helper() + return api.actorWithUID(project, name, name+"-uid", groups...) +} + +// actorWithUID impersonates a caller with the given UID, empty for a +// credential that carries none. +func (api *liveAPI) actorWithUID(project, name, uid string, groups ...string) client.Client { api.t.Helper() config := rest.CopyConfig(api.config) - config.Impersonate = rest.ImpersonationConfig{UserName: name, UID: name + "-uid", Groups: groups, Extra: map[string][]string{iam.ParentKindExtraKey: {"Project"}, iam.ParentAPIGroupExtraKey: {"resourcemanager.miloapis.com"}, iam.ParentNameExtraKey: {project}}} + config.Impersonate = rest.ImpersonationConfig{UserName: name, UID: uid, Groups: groups, Extra: map[string][]string{iam.ParentKindExtraKey: {"Project"}, iam.ParentAPIGroupExtraKey: {"resourcemanager.miloapis.com"}, iam.ParentNameExtraKey: {project}}} actor, err := client.New(config, client.Options{Scheme: api.scheme}) if err != nil { api.t.Fatal(err) @@ -255,6 +262,22 @@ func TestRegistrationAgainstLiveDevices(t *testing.T) { missing := &metal.MachineCapacity{ObjectMeta: metav1.ObjectMeta{Name: CapacityName("9d0c3c2e-1f5b-4f37-9e43-2f5c6a1b7d80"), Namespace: "default", Annotations: map[string]string{DeviceUIDAnnotation: "9d0c3c2e-1f5b-4f37-9e43-2f5c6a1b7d80", DeviceVersionAnnotation: "1", OperationAnnotation: "missing"}}, Spec: metal.MachineCapacitySpec{DeviceRef: metal.LocalReference{Name: "never-recorded"}, AllocationPolicy: "Disabled", ReclaimPolicy: "Retain"}} requireDenied(t, operator.Create(ctx, missing), "does not exist") }) + t.Run("a caller without a UID is authorized by username", func(t *testing.T) { + target := api.device("username-target", "default", "username-serial", "77777777-7777-7777-7777-777777777771") + // RBAC binds the username alone; the caller's credential carries no UID. + if err := api.admin.Create(ctx, &rbacv1.Role{ObjectMeta: metav1.ObjectMeta{Name: "username-registrar", Namespace: "default"}, Rules: []rbacv1.PolicyRule{{APIGroups: []string{"metal.datumapis.com"}, Resources: []string{"machinecapacities"}, Verbs: []string{"create"}}, {APIGroups: []string{inventory.GroupVersion.Group}, Resources: []string{"devices"}, Verbs: []string{"get"}}}}); err != nil { + t.Fatal(err) + } + if err := api.admin.Create(ctx, &rbacv1.RoleBinding{ObjectMeta: metav1.ObjectMeta{Name: "username-registrar", Namespace: "default"}, RoleRef: rbacv1.RoleRef{APIGroup: "rbac.authorization.k8s.io", Kind: "Role", Name: "username-registrar"}, Subjects: []rbacv1.Subject{{APIGroup: "rbac.authorization.k8s.io", Kind: "User", Name: "service-example"}}}); err != nil { + t.Fatal(err) + } + if err := api.actorWithUID("datum-cloud", "unbound-example", "").Create(ctx, api.registration(target)); !apierrors.IsForbidden(err) { + t.Fatalf("an unbound caller without a UID registered: %v", err) + } + if err := api.actorWithUID("datum-cloud", "service-example", "").Create(ctx, api.registration(target)); err != nil { + t.Fatalf("a bound caller without a UID was denied: %v", err) + } + }) t.Run("unauthorized caller is denied", func(t *testing.T) { limited := api.actor("limited") requireDenied(t, limited.Create(ctx, api.registration(limitedTarget)), "permission on devices is required") diff --git a/internal/registration/runtime.go b/internal/registration/runtime.go index f92434bf..dc3f4527 100644 --- a/internal/registration/runtime.go +++ b/internal/registration/runtime.go @@ -33,7 +33,7 @@ func NewHandler(authority string, projects []string, root client.Client, templat } // Authorize evaluates the caller's own permission with a SubjectAccessReview -// carrying the caller's username, UID, groups and extras. The service's +// carrying the caller's username, UID when present, groups and extras. The service's // credentials evaluate the review; they never stand in for the caller. func Authorize(ctx context.Context, project Project, actor authenticationv1.UserInfo, group, resource, name string) error { verb := "get"