Skip to content

Commit

Permalink
add matchLabel parsing for cilium netpol
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <[email protected]>
  • Loading branch information
matthyx committed Jun 24, 2024
1 parent 3997fa5 commit abc34cd
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
3 changes: 2 additions & 1 deletion armometadata/k8sutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"hash/fnv"
"path"
"slices"

Check failure on line 9 in armometadata/k8sutils.go

View workflow job for this annotation

GitHub Actions / test / Create cross-platform build

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.19.13/x64/src/slices)
"strings"

"github.com/armosec/utils-k8s-go/wlid"
Expand Down Expand Up @@ -176,7 +177,7 @@ func ExtractMetadataFromJsonBytes(input []byte) (Metadata, error) {
m.OwnerReferences[unquote(key)] = unquote(value)
}

if subParent == "podSelector" {
if slices.Contains([]string{"podSelector", "endpointSelector"}, subParent) {
m.PodSelectorMatchLabels[unquote(key)] = unquote(value)
}

Expand Down
9 changes: 9 additions & 0 deletions armometadata/k8sutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ func TestExtractMetadataFromJsonBytes(t *testing.T) {
apiVersion: "spdx.softwarecomposition.kubescape.io/v1beta1",
podSelectorMatchLabels: map[string]string{},
},
{
name: "ciliumnetworkpolicy",
annotations: map[string]string{},
labels: map[string]string{},
ownerReferences: map[string]string{},
kind: "CiliumNetworkPolicy",
apiVersion: "cilium.io/v2",
podSelectorMatchLabels: map[string]string{"app": "frontend"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
61 changes: 61 additions & 0 deletions armometadata/testdata/ciliumnetworkpolicy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"apiVersion": "cilium.io/v2",
"kind": "CiliumNetworkPolicy",
"metadata": {
"name": "untitled-policy"
},
"spec": {
"endpointSelector": {
"matchLabels": {
"app": "frontend"
}
},
"egress": [
{
"toEndpoints": [
{
"matchLabels": {
"io.kubernetes.pod.namespace": "kube-system",
"k8s-app": "kube-dns"
}
}
],
"toPorts": [
{
"ports": [
{
"port": "53",
"protocol": "UDP"
}
],
"rules": {
"dns": [
{
"matchPattern": "*"
}
]
}
}
]
},
{
"toEndpoints": [
{
"matchLabels": {
"app": "backend"
}
}
],
"toPorts": [
{
"ports": [
{
"port": "443"
}
]
}
]
}
]
}
}

0 comments on commit abc34cd

Please sign in to comment.