Skip to content

Commit

Permalink
Make SelfSubjectNamespaceAccessReview rancher project aware
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Jun 10, 2024
1 parent 18456c9 commit 8e7625c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
3 changes: 2 additions & 1 deletion apis/identity/v1alpha1/selfsubjectnamespaceaccessreview.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ type SelfSubjectNamespaceAccessReviewSpec struct {
}

type SubjectAccessNamespaceReviewStatus struct {
Namespaces []string `json:"namespaces,omitempty"`
Namespaces []string `json:"namespaces,omitempty"`
Projects map[string][]string `json:"projects,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
15 changes: 15 additions & 0 deletions apis/identity/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ spec:
items:
type: string
type: array
projects:
additionalProperties:
items:
type: string
type: array
type: object
type: object
required:
- spec
Expand Down
28 changes: 25 additions & 3 deletions pkg/registry/identity/selfsubjectnamespaceaccessreview/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package selfsubjectnamespaceaccessreview

import (
"context"
"sort"

identityapi "kubeops.dev/ui-server/apis/identity/v1alpha1"

Expand All @@ -30,6 +31,7 @@ import (
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/client-go/kubernetes"
clustermeta "kmodules.xyz/client-go/cluster"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -92,7 +94,7 @@ func (r *Storage) Create(ctx context.Context, obj runtime.Object, _ rest.Validat
return nil, err
}

allowedNs := make([]string, 0, len(list.Items))
allowedNs := make([]core.Namespace, 0, len(list.Items))
for _, ns := range list.Items {
allowed := true

Expand Down Expand Up @@ -145,10 +147,30 @@ func (r *Storage) Create(ctx context.Context, obj runtime.Object, _ rest.Validat
}

if allowed {
allowedNs = append(allowedNs, ns.Name)
allowedNs = append(allowedNs, ns)
}
}

in.Status.Namespaces = allowedNs
if clustermeta.IsRancherManaged(r.rtc.RESTMapper()) {
projects := map[string][]string{}
for _, ns := range allowedNs {
projectId := ns.Labels[clustermeta.LabelKeyRancherFieldProjectId]
projects[projectId] = append(projects[projectId], ns.Name)
}

for projectId, namespaces := range projects {
sort.Strings(namespaces)
projects[projectId] = namespaces
}
} else {
namespaces := make([]string, 0, len(allowedNs))
for _, ns := range allowedNs {
namespaces = append(namespaces, ns.Name)
}

sort.Strings(namespaces)
in.Status.Namespaces = namespaces
}

return in, nil
}

0 comments on commit 8e7625c

Please sign in to comment.