Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions docs/development/inventory-capacity-registration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-<annotated Device UID>`;
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;
Expand Down
2 changes: 1 addition & 1 deletion internal/registration/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 27 additions & 1 deletion internal/registration/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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"}
},
Expand Down
25 changes: 24 additions & 1 deletion internal/registration/envtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion internal/registration/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading