Skip to content

Commit

Permalink
Updated "Service Does Not Target Pod" for K8s
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaela-soares committed Apr 19, 2021
1 parent f317e45 commit 61f6ad9
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 73 deletions.
42 changes: 18 additions & 24 deletions assets/queries/k8s/service_does_not_target_pod/query.rego
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package Cx

import data.generic.k8s as k8sLib

CxPolicy[result] {
service := input.document[i]
service.kind == "Service"
metadata := service.metadata
ports := service.spec.ports
servicePorts := ports[j]
contains(service.spec.selector[_])
confirmPorts(servicePorts) == false
pod := match_label(service.spec.selector[_])
pod != null

not confirmPorts(servicePorts, pod)

result := {
"documentId": input.document[i].id,
Expand All @@ -22,7 +26,8 @@ CxPolicy[result] {
service := input.document[i]
service.kind == "Service"
metadata := service.metadata
contains(service.spec.selector[_]) == false
pod := match_label(service.spec.selector[_])
pod == null

result := {
"documentId": input.document[i].id,
Expand All @@ -33,30 +38,19 @@ CxPolicy[result] {
}
}

confirmPorts(servicePorts) {
pod := input.document[i]
pod.kind == "Pod"
confirmPorts(servicePorts, pod) {
specInfo := k8sLib.getSpecInfo(pod)
types := {"initContainers", "containers"}
containers := pod.spec[types[x]][j]
containers.ports[k].containerPort == servicePorts.targetPort
} else {
stateful_set := input.document[i]
stateful_set.kind == "StatefulSet"
types := {"initContainers", "containers"}
containers := stateful_set.spec.template.spec[types[x]][j]
containers.ports[k].containerPort == servicePorts.targetPort
} else = false {
true
containers := specInfo.spec[types[x]]
containers[_].ports[_].containerPort == servicePorts.targetPort
}

contains(string) {
pod := input.document[i]
pod.kind == "Pod"
match_label(string) = output {
pod := input.document[_]
listKinds := ["Pod", "Deployment", "DaemonSet", "StatefulSet", "ReplicaSet", "ReplicationController", "Job", "CronJob"]
pod.kind == listKinds[x]
pod.metadata.labels[_] == string
} else {
stateful_set := input.document[i]
stateful_set.kind == "StatefulSet"
stateful_set.metadata.labels[_] == string
} else = false {
output = pod
} else = null {
true
}
47 changes: 0 additions & 47 deletions assets/queries/k8s/service_does_not_target_pod/test/positive.yaml

This file was deleted.

27 changes: 27 additions & 0 deletions assets/queries/k8s/service_does_not_target_pod/test/positive1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: v1
kind: Service
metadata:
name: helloworld2
spec:
type: NodePort
selector:
app: helloworld2
ports:
- name: http
nodePort: 30475
port: 9377
protocol: TCP
targetPort: 9377
---
apiVersion: v1
kind: Pod
metadata:
name: nginx2
labels:
app: hellowwwworld
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 9377
36 changes: 36 additions & 0 deletions assets/queries/k8s/service_does_not_target_pod/test/positive2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
apiVersion: v1
kind: Service
metadata:
name: helloworld3
spec:
type: NodePort
selector:
app: helloworld3
ports:
- name: http
nodePort: 30475
port: 9377
protocol: TCP
targetPort: 9377
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: helloworld3
spec:
replicas: 3
selector:
matchLabels:
app: helloworld3
template:
metadata:
labels:
app: helloworld3
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
{
"queryName": "Service Does Not Target Pod",
"severity": "LOW",
"line": 14
"line": 7,
"fileName": "positive1.yaml"
},
{
"queryName": "Service Does Not Target Pod",
"severity": "LOW",
"line": 24
"line": 14,
"fileName": "positive2.yaml"
}
]

0 comments on commit 61f6ad9

Please sign in to comment.