Skip to content

Commit

Permalink
Perform all namespace access check
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Sep 11, 2024
1 parent 2c8ce06 commit 279e44e
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ require (
kmodules.xyz/custom-resources v0.30.0
kmodules.xyz/go-containerregistry v0.0.12
kmodules.xyz/monitoring-agent-api v0.29.0
kmodules.xyz/resource-metadata v0.18.13-0.20240908185540-fa8ef486973e
kmodules.xyz/resource-metadata v0.18.13-0.20240911040707-0451a5c4bbeb
kmodules.xyz/resource-metrics v0.30.4
kmodules.xyz/resource-metrics/utils v0.30.4
kmodules.xyz/sets v0.29.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -922,8 +922,8 @@ kmodules.xyz/monitoring-agent-api v0.29.0 h1:gpFl6OZrlMLb/ySMHdREI9EwGtnJ91oZBn9
kmodules.xyz/monitoring-agent-api v0.29.0/go.mod h1:iNbvaMTgVFOI5q2LJtGK91j4Dmjv4ZRiRdasGmWLKQI=
kmodules.xyz/offshoot-api v0.30.0 h1:dq9F93pu4Q8rL9oTcCk+vGGy8vpS7RNt0GSwx7Bvhec=
kmodules.xyz/offshoot-api v0.30.0/go.mod h1:o9VoA3ImZMDBp3lpLb8+kc2d/KBxioRwCpaKDfLIyDw=
kmodules.xyz/resource-metadata v0.18.13-0.20240908185540-fa8ef486973e h1:PcKUBcb60nuswPKqb7SsVZ04Qk2S8sTyq200eX88dZ8=
kmodules.xyz/resource-metadata v0.18.13-0.20240908185540-fa8ef486973e/go.mod h1:t1tu60j9i2EduufyYBjz7h3vPymfHb9mqv5a+EvSjzo=
kmodules.xyz/resource-metadata v0.18.13-0.20240911040707-0451a5c4bbeb h1:7744OQAOxB96MYUa5op80/zC8KYXmtrN8AmbUwGqEmc=
kmodules.xyz/resource-metadata v0.18.13-0.20240911040707-0451a5c4bbeb/go.mod h1:t1tu60j9i2EduufyYBjz7h3vPymfHb9mqv5a+EvSjzo=
kmodules.xyz/resource-metrics v0.30.4 h1:8HBPtYmo9ETY91gsc55JE8Z986+3ZuRq57M0wZ9npqI=
kmodules.xyz/resource-metrics v0.30.4/go.mod h1:w9+rz7/s/kGP1GWzYSuRdCn+l7EwpesmESSEHkLBnIQ=
kmodules.xyz/resource-metrics/utils v0.30.4 h1:bJS/x0Qr7N1FFdxugFbzZ/Es6HVs4ptsFlhkmgj3jac=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,46 @@ func (r *Storage) Create(ctx context.Context, obj runtime.Object, _ rest.Validat
extra[k] = v
}

// check for all namespaces
{
allowed := true
var err error
for _, attr := range in.Spec.ResourceAttributes {
attr.Namespace = ""
review := &authorization.SelfSubjectAccessReview{
Spec: authorization.SelfSubjectAccessReviewSpec{
ResourceAttributes: &attr,
NonResourceAttributes: nil,
},
}
review, err = r.kc.AuthorizationV1().SelfSubjectAccessReviews().Create(ctx, review, metav1.CreateOptions{})
if err != nil {
return nil, err
}
if !review.Status.Allowed {
allowed = false
break
}
}
for _, attr := range in.Spec.NonResourceAttributes {
review := &authorization.SelfSubjectAccessReview{
Spec: authorization.SelfSubjectAccessReviewSpec{
NonResourceAttributes: &attr,
},
}
review, err = r.kc.AuthorizationV1().SelfSubjectAccessReviews().Create(ctx, review, metav1.CreateOptions{})
if err != nil {
return nil, err
}
if !review.Status.Allowed {
allowed = false
break
}
}

in.Status.AllNamespaces = allowed
}

var list core.NamespaceList
err := r.rtc.List(ctx, &list)
if err != nil {
Expand Down Expand Up @@ -146,14 +186,18 @@ func (r *Storage) Create(ctx context.Context, obj runtime.Object, _ rest.Validat
}

if allowed {
// in.Status.Namespaces
allowedNs = append(allowedNs, ns)
}
}

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

Expand Down

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 @@ -64,8 +64,9 @@ type SelfSubjectNamespaceAccessReviewSpec struct {
}

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

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ spec:
description: Status is filled in by the server and indicates whether the
request is allowed or not
properties:
allNamespaces:
type: boolean
namespaces:
items:
type: string
Expand All @@ -106,6 +108,8 @@ spec:
type: string
type: array
type: object
required:
- allNamespaces
type: object
required:
- spec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ spec:
name: clickhousebindings
scope: Namespaced
version: v1alpha1
ui:
editor:
name: catalogappscodecom-clickhousebinding-editor
sourceRef:
apiGroup: source.toolkit.fluxcd.io
kind: HelmRepository
name: appscode-charts-oci
version: v0.5.0
enforceQuota: false
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2011,7 +2011,7 @@ kmodules.xyz/monitoring-agent-api/client
## explicit; go 1.22.0
kmodules.xyz/offshoot-api/api/v1
kmodules.xyz/offshoot-api/api/v2
# kmodules.xyz/resource-metadata v0.18.13-0.20240908185540-fa8ef486973e
# kmodules.xyz/resource-metadata v0.18.13-0.20240911040707-0451a5c4bbeb
## explicit; go 1.22.1
kmodules.xyz/resource-metadata/apis/core/install
kmodules.xyz/resource-metadata/apis/core/v1alpha1
Expand Down

0 comments on commit 279e44e

Please sign in to comment.