Skip to content

Commit

Permalink
add a test case for protocol:all handling
Browse files Browse the repository at this point in the history
Signed-off-by: rksharma95 <[email protected]>
  • Loading branch information
rksharma95 committed Dec 24, 2024
1 parent 22b8338 commit e4ece04
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 12 deletions.
12 changes: 4 additions & 8 deletions KubeArmor/enforcer/appArmorProfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,7 @@ func (ae *AppArmorEnforcer) SetNetworkMatchProtocols(proto tp.NetworkProtocolTyp
rule.Deny = deny
rule.Allow = !deny
if len(proto.FromSource) == 0 {
if proto.Protocol != "all" {
addRuletoMap(rule, proto.Protocol, prof.NetworkRules)
}
addRuletoMap(rule, proto.Protocol, prof.NetworkRules)
return
}

Expand All @@ -267,9 +265,7 @@ func (ae *AppArmorEnforcer) SetNetworkMatchProtocols(proto tp.NetworkProtocolTyp
prof.FromSource[source] = val
}
}
if proto.Protocol != "all" {
addRuletoMap(rule, proto.Protocol, prof.FromSource[source].NetworkRules)
}
addRuletoMap(rule, proto.Protocol, prof.FromSource[source].NetworkRules)
}
}

Expand Down Expand Up @@ -391,9 +387,9 @@ func (ae *AppArmorEnforcer) GenerateProfileBody(securityPolicies []tp.SecurityPo
if len(secPolicy.Spec.Network.MatchProtocols) > 0 {
for _, proto := range secPolicy.Spec.Network.MatchProtocols {
if proto.Action == "Allow" {
ae.SetNetworkMatchProtocols(proto, &profile, false, defaultPosture.NetworkAction != "block" || proto.Protocol == "all")
ae.SetNetworkMatchProtocols(proto, &profile, false, defaultPosture.NetworkAction != "block")
} else if proto.Action == "Block" {
ae.SetNetworkMatchProtocols(proto, &profile, true, true && proto.Protocol != "all")
ae.SetNetworkMatchProtocols(proto, &profile, true, true)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions KubeArmor/enforcer/appArmorTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,11 @@ profile {{$v := $.Name | split "."}}{{$v._0}}_{{ regexReplaceAllLiteral "[^a-z A
## == Network START == ##
{{- range $value, $data := .NetworkRules}}
{{- if $data.Deny}}
{{- if eq $value "all" }}
deny network,
{{- else }}
deny network {{$value}},
{{- end}}
{{- end}}
{{- if $data.Allow}}
network {{$value}},
Expand Down
Binary file modified KubeArmor/enforcer/bpflsm/enforcer_bpfeb.o
Binary file not shown.
Binary file modified KubeArmor/enforcer/bpflsm/enforcer_bpfel.o
Binary file not shown.
Binary file modified KubeArmor/enforcer/bpflsm/enforcer_path_bpfeb.o
Binary file not shown.
Binary file modified KubeArmor/enforcer/bpflsm/enforcer_path_bpfel.o
Binary file not shown.
1 change: 0 additions & 1 deletion KubeArmor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ require (
github.com/emicklei/go-restful/v3 v3.11.2 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand Down
74 changes: 71 additions & 3 deletions pkg/KubeArmorController/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,74 @@ kind: ClusterRole
metadata:
name: manager-role
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "create","delete","update","list", "watch"]
- apiGroups:
- ""
resources:
- pods
verbs:
- create
- delete
- get
- list
- update
- watch
- apiGroups:
- security.kubearmor.com
resources:
- kubearmorclusterpolicies
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- security.kubearmor.com
resources:
- kubearmorclusterpolicies/status
verbs:
- get
- patch
- update
- apiGroups:
- security.kubearmor.com
resources:
- kubearmorhostpolicies
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- security.kubearmor.com
resources:
- kubearmorhostpolicies/status
verbs:
- get
- patch
- update
- apiGroups:
- security.kubearmor.com
resources:
- kubearmorpolicies
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- security.kubearmor.com
resources:
- kubearmorpolicies/status
verbs:
- get
- patch
- update
26 changes: 26 additions & 0 deletions tests/k8s_env/ksp/ksp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,32 @@ var _ = Describe("Ksp", func() {

})

It("it can block all network traffic", func() {

// Apply Policy
err := K8sApplyFile("multiubuntu/ksp-ubuntu-1-block-net-all.yaml")
Expect(err).To(BeNil())

// Start KubeArmor Logs
err = KarmorLogStart("policy", "multiubuntu", "Network", ub1)
Expect(err).To(BeNil())
AssertCommand(ub1, "multiubuntu", []string{"bash", "-c", "arping -c 1 127.0.0.1"},
MatchRegexp("arping.*Permission denied"), true,
)

expect := protobuf.Alert{
PolicyName: "ksp-ubuntu-1-block-net-all",
Severity: "8",
Action: "Block",
Result: "Permission denied",
}

res, err := KarmorGetTargetAlert(5*time.Second, &expect)
Expect(err).To(BeNil())
Expect(res.Found).To(BeTrue())

})

})

Describe("Apply Capabilities Policy", func() {
Expand Down
15 changes: 15 additions & 0 deletions tests/k8s_env/ksp/multiubuntu/ksp-ubuntu-1-block-net-all.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: security.kubearmor.com/v1
kind: KubeArmorPolicy
metadata:
name: ksp-ubuntu-1-block-net-all
namespace: multiubuntu
spec:
severity: 8
selector:
matchLabels:
container: ubuntu-1
network:
matchProtocols:
- protocol: all
action:
Block

0 comments on commit e4ece04

Please sign in to comment.