Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check that string probe port match container port #457

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/generated/checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,15 @@ strategyTypeRegex: ^(RollingUpdate|Rolling)$
**Remediation**: Ensure privileged ports [0, 1024] are not mapped within containers.

**Template**: [privileged-ports](templates.md#privileged-ports)
## probe-port

**Enabled by default**: Yes

**Description**: Alert on probe port that does not match a port defined in container ports

**Remediation**: Ensure probe port matches a port defined in container ports.

**Template**: [probe-port](templates.md#probe-port)
## read-secret-from-env-var

**Enabled by default**: No
Expand Down
9 changes: 9 additions & 0 deletions docs/generated/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,15 @@ KubeLinter supports the following templates:
**Supported Objects**: DeploymentLike


## Probe Port

**Key**: `probe-port`

**Description**: Flag unknown probe port

**Supported Objects**: DeploymentLike


## Read-only Root Filesystems

**Key**: `read-only-root-fs`
Expand Down
15 changes: 15 additions & 0 deletions e2etests/bats-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,21 @@ get_value_from() {
[[ "${count}" == "2" ]]
}

@test "probe-port" {
tmp="tests/checks/probe-port.yml"
cmd="${KUBE_LINTER_BIN} lint --include probe-port --do-not-auto-add-defaults --format json ${tmp}"
run ${cmd}

print_info "${status}" "${output}" "${cmd}" "${tmp}"
[ "$status" -eq 1 ]

message1=$(get_value_from "${lines[0]}" '.Reports[0].Object.K8sObject.GroupVersionKind.Kind + ": " + .Reports[0].Diagnostic.Message')
count=$(get_value_from "${lines[0]}" '.Reports | length')

[[ "${message1}" == "Deployment: probe port \"bar\" does not match a port in container \"myapp\"." ]]
[[ "${count}" == "1" ]]
}

@test "read-secret-from-env-var" {
tmp="tests/checks/read-secret-from-env-var.yml"
cmd="${KUBE_LINTER_BIN} lint --include read-secret-from-env-var --do-not-auto-add-defaults --format json ${tmp}"
Expand Down
1 change: 1 addition & 0 deletions internal/defaultchecks/default_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
"non-existent-service-account",
"privilege-escalation-container",
"privileged-container",
"probe-port",
"run-as-non-root",
"sensitive-host-mounts",
"ssh-port",
Expand Down
7 changes: 7 additions & 0 deletions pkg/builtinchecks/yamls/probeport.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: "probe-port"
description: "Alert on probe port that does not match a port defined in container ports"
remediation: "Ensure probe port matches a port defined in container ports."
scope:
objectKinds:
- DeploymentLike
template: "probe-port"
1 change: 1 addition & 0 deletions pkg/templates/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
_ "golang.stackrox.io/kube-linter/pkg/templates/privileged"
_ "golang.stackrox.io/kube-linter/pkg/templates/privilegedports"
_ "golang.stackrox.io/kube-linter/pkg/templates/privilegeescalation"
_ "golang.stackrox.io/kube-linter/pkg/templates/probeport"
_ "golang.stackrox.io/kube-linter/pkg/templates/readinessprobe"
_ "golang.stackrox.io/kube-linter/pkg/templates/readonlyrootfs"
_ "golang.stackrox.io/kube-linter/pkg/templates/readsecret"
Expand Down
52 changes: 52 additions & 0 deletions pkg/templates/probeport/internal/params/gen-params.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/templates/probeport/internal/params/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package params

// Params represents the params accepted by this template.
type Params struct {
}
59 changes: 59 additions & 0 deletions pkg/templates/probeport/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package probeport

import (
"fmt"

"golang.stackrox.io/kube-linter/internal/set"
"golang.stackrox.io/kube-linter/pkg/check"
"golang.stackrox.io/kube-linter/pkg/config"
"golang.stackrox.io/kube-linter/pkg/diagnostic"
"golang.stackrox.io/kube-linter/pkg/objectkinds"
"golang.stackrox.io/kube-linter/pkg/templates"
"golang.stackrox.io/kube-linter/pkg/templates/probeport/internal/params"
"golang.stackrox.io/kube-linter/pkg/templates/util"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)

func init() {
templates.Register(check.Template{
HumanName: "Probe Port",
Key: "probe-port",
Description: "Flag unknown probe port",
SupportedObjectKinds: config.ObjectKindsDesc{
ObjectKinds: []string{objectkinds.DeploymentLike},
},
Parameters: params.ParamDescs,
ParseAndValidateParams: params.ParseAndValidate,
Instantiate: params.WrapInstantiateFunc(func(_ params.Params) (check.Func, error) {
return util.PerNonInitContainerCheck(func(container *v1.Container) []diagnostic.Diagnostic {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be great if you extract this to a named function and add unit tests to check all code paths.

var portNames set.StringSet
for _, port := range container.Ports {
if name := port.Name; len(name) > 0 {
portNames.Add(name)
}
}
var results []diagnostic.Diagnostic
for _, probe := range []*v1.Probe{container.LivenessProbe, container.ReadinessProbe, container.StartupProbe} {
if probe == nil {
continue
}
var port intstr.IntOrString
if httpGet := probe.HTTPGet; httpGet != nil {
port = httpGet.Port
} else if tcpSocket := probe.TCPSocket; tcpSocket != nil {
port = tcpSocket.Port
} else {
continue
}
if port.Type == intstr.String && !portNames.Contains(port.StrVal) && port.IntValue() == 0 {
results = append(results, diagnostic.Diagnostic{
Message: fmt.Sprintf("probe port %q does not match a port in container %q.", port.StrVal, container.Name),
})
}
}
return results
}), nil
}),
})
}
122 changes: 122 additions & 0 deletions tests/checks/probe-port.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: no-probe
spec:
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myimage
resources:
limits:
memory: "128Mi"
cpu: "500m"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: livenessProbe-int
spec:
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myimage
resources:
limits:
memory: "128Mi"
cpu: "500m"
livenessProbe:
httpGet:
port: 1234
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: readinessProbe-int-str
spec:
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myimage
resources:
limits:
memory: "128Mi"
cpu: "500m"
readinessProbe:
tcpCheck:
port: "1234"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: startupProbe-str-container-port-ok
spec:
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myimage
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 1234
name: foo
startupProbe:
httpGet:
port: foo
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: startupProbe-str-container-port-ko
spec:
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myimage
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 1234
name: foo
startupProbe:
httpGet:
port: bar