Skip to content

Commit

Permalink
Merge pull request #24 from ComradeProgrammer/opt
Browse files Browse the repository at this point in the history
feat: optimize ${OBJECT}&${NAMESPACE}&${RESOURCE}
  • Loading branch information
sagilio authored Sep 12, 2022
2 parents 1896204 + eef07c9 commit 284f244
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 29 deletions.
4 changes: 2 additions & 2 deletions example/allowed_repo/model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ spec:
e = !some(where (p.eft == deny))
[matchers]
m = r.obj.Request.Namespace == "default" && r.obj.Request.Resource.Resource =="deployments" && \
access(r.obj.Request.Object.Object.Spec.Template.Spec.Containers , 0, "Image") == p.obj
m = ${NAMESPACE} == "default" && ${RESOURCE} =="deployments" && \
access(${OBJECT}.Spec.Template.Spec.Containers , 0, "Image") == p.obj
4 changes: 2 additions & 2 deletions example/block_nodeport_service/model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ spec:
e = !some(where (p.eft == deny))
[matchers]
m = r.obj.Request.Namespace == "default" && r.obj.Request.Resource.Resource =="services" && \
m = ${NAMESPACE} == "default" && ${RESOURCE} =="services" && \
r.obj.Request.Operation != "DELETE" &&\
string(r.obj.Request.Object.Object.Spec.Type) == p.obj
string(${OBJECT}.Spec.Type) == p.obj
6 changes: 3 additions & 3 deletions example/container_resource_limit/model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ spec:
e = !some(where (p.eft == deny))
[matchers]
m = r.obj.Request.Namespace == "default" && r.obj.Request.Resource.Resource =="deployments" && \
parseFloat(access(r.obj.Request.Object.Object.Spec.Template.Spec.Containers , 0, "Resources","Limits","cpu","Value")) >= parseFloat(p.cpu) && \
parseFloat(access(r.obj.Request.Object.Object.Spec.Template.Spec.Containers , 0, "Resources","Limits","memory","Value")) >= parseFloat(p.memory)
m = ${NAMESPACE} == "default" && ${RESOURCE} =="deployments" && \
parseFloat(access(${OBJECT}.Spec.Template.Spec.Containers , 0, "Resources","Limits","cpu","Value")) >= parseFloat(p.cpu) && \
parseFloat(access(${OBJECT}.Spec.Template.Spec.Containers , 0, "Resources","Limits","memory","Value")) >= parseFloat(p.memory)
4 changes: 2 additions & 2 deletions example/disallowed_tag/model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ spec:
e = !some(where (p.eft == deny))
[matchers]
m = r.obj.Request.Namespace == "default" && r.obj.Request.Resource.Resource =="deployments" && \
contain(split(accessWithWildcard(r.obj.Request.Object.Object.Spec.Template.Spec.Containers , "*", "Image"),":",1) , p.obj)
m = ${NAMESPACE} == "default" && ${RESOURCE} =="deployments" && \
contain(split(accessWithWildcard(${OBJECT}.Spec.Template.Spec.Containers , "*", "Image"),":",1) , p.obj)
4 changes: 2 additions & 2 deletions example/external_ip/model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ spec:
e = !some(where (p.eft == deny))
[matchers]
m = r.obj.Request.Namespace == "default" && r.obj.Request.Resource.Resource =="services" && \
contain(accessWithWildcard(r.obj.Request.Object.Object.Spec.ExternalIPs , "*") , p.obj)
m = ${NAMESPACE} == "default" && ${RESOURCE} =="services" && \
contain(accessWithWildcard(${OBJECT}.Spec.ExternalIPs , "*") , p.obj)
8 changes: 4 additions & 4 deletions example/https_only/model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ spec:
e = some(where (p.eft == allow))
[matchers]
m = r.obj.Request.Namespace == "default" && r.obj.Request.Resource.Resource =="ingresses" && \
access(r.obj.Request.Object.Object , "Annotations", "kubernetes.io/ingress.allow-http") == "false" &&\
parseFloat(len(r.obj.Request.Object.Object.Spec.TLS)) > 0 || \
r.obj.Request.Resource.Resource !="ingresses"
m = ${NAMESPACE} == "default" && ${RESOURCE} =="ingresses" && \
access(${OBJECT} , "Annotations", "kubernetes.io/ingress.allow-http") == "false" &&\
parseFloat(len(${OBJECT}.Spec.TLS)) > 0 || \
${RESOURCE} !="ingresses"
6 changes: 3 additions & 3 deletions example/image_digest/model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ spec:
e = some(where (p.eft == allow))
[matchers]
m = r.obj.Request.Namespace == "default" && r.obj.Request.Resource.Resource =="deployments" && \
matchRegex(accessWithWildcard(r.obj.Request.Object.Object.Spec.Template.Spec.Containers , "*", "Image") , p.obj) || \
r.obj.Request.Resource.Resource !="deployments"
m = ${NAMESPACE} == "default" && ${RESOURCE} =="deployments" && \
matchRegex(accessWithWildcard(${OBJECT}.Spec.Template.Spec.Containers , "*", "Image") , p.obj) || \
${RESOURCE} !="deployments"
4 changes: 2 additions & 2 deletions example/replica_limits/model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ spec:
e = !some(where (p.eft == deny))
[matchers]
m = r.obj.Request.Namespace == "default" && r.obj.Request.Resource.Resource =="deployments" && \
parseFloat(access(r.obj.Request.Object.Object.Spec.Replicas)) <= parseFloat(p.obj)
m = ${NAMESPACE} == "default" && ${RESOURCE} =="deployments" && \
parseFloat(access(${OBJECT}.Spec.Replicas)) <= parseFloat(p.obj)
6 changes: 3 additions & 3 deletions example/required_annotations/model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ spec:
e = !some(where (p.eft == deny))
[matchers]
m = r.obj.Request.Namespace == "default" && r.obj.Request.Resource.Resource =="deployments" && \
access(r.obj.Request.Object.Object,"ObjectMeta","Annotations",p.key)!= p.value|| \
r.obj.Request.Resource.Resource !="deployments"
m = ${NAMESPACE} == "default" && ${RESOURCE} =="deployments" && \
access(${OBJECT},"ObjectMeta","Annotations",p.key)!= p.value|| \
${RESOURCE} !="deployments"
6 changes: 3 additions & 3 deletions example/required_labels/model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ spec:
e = !some(where (p.eft == deny))
[matchers]
m = r.obj.Request.Namespace == "default" && r.obj.Request.Resource.Resource =="deployments" && \
access(r.obj.Request.Object.Object,"ObjectMeta","Labels",p.key)!= p.value|| \
r.obj.Request.Resource.Resource !="deployments"
m = ${NAMESPACE} == "default" && ${RESOURCE} =="deployments" && \
access(${OBJECT},"ObjectMeta","Labels",p.key)!= p.value|| \
${RESOURCE} !="deployments"
4 changes: 2 additions & 2 deletions example/required_probes/model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ spec:
e = !some(where (p.eft == deny))
[matchers]
m = r.obj.Request.Namespace == "default" && r.obj.Request.Resource.Resource =="deployments"&& \
m = ${NAMESPACE} == "default" && ${RESOURCE} =="deployments"&& \
r.obj.Request.Operation != "DELETE" && \
isNil(access(r.obj.Request.Object.Object.Spec.Template.Spec.Containers,0,p.type))
isNil(access(${OBJECT}.Spec.Template.Spec.Containers,0,p.type))
12 changes: 11 additions & 1 deletion internal/model/model_adaptor_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package model
import (
"context"
"path/filepath"
"strings"

"github.com/casbin/casbin/v2/model"
"github.com/casbin/casbin/v2/persist"
Expand Down Expand Up @@ -60,7 +61,8 @@ func (m *ModelLoader) GetModelAndAdaptors() ([]ModelAdaptorPair, error) {
}
res := make([]ModelAdaptorPair, 0)
for _, crdModel := range list.Items {
casbinModel, err := model.NewModelFromString(crdModel.Spec.ModelText)
modelText := ModelMacroSubstitution(crdModel.Spec.ModelText)
casbinModel, err := model.NewModelFromString(modelText)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -104,3 +106,11 @@ func (m *ModelLoader) establishExternalClient() error {
m.clientset = clientset
return nil
}

func ModelMacroSubstitution(originalModelText string) string {
res := strings.ReplaceAll(originalModelText, "${OBJECT}", "r.obj.Request.Object.Object")
res = strings.ReplaceAll(res, "${NAMESPACE}", "r.obj.Request.Namespace")
res = strings.ReplaceAll(res, "${RESOURCE}", "r.obj.Request.Resource.Resource")

return res
}

0 comments on commit 284f244

Please sign in to comment.