-
Notifications
You must be signed in to change notification settings - Fork 6
/
242393.go
141 lines (119 loc) · 4.71 KB
/
242393.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package v1r11
import (
"cmp"
"context"
"fmt"
"slices"
"strings"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/component-base/version"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/gardener/diki/imagevector"
"github.com/gardener/diki/pkg/kubernetes/pod"
kubeutils "github.com/gardener/diki/pkg/kubernetes/utils"
"github.com/gardener/diki/pkg/rule"
"github.com/gardener/diki/pkg/shared/images"
"github.com/gardener/diki/pkg/shared/provider"
"github.com/gardener/diki/pkg/shared/ruleset/disak8sstig/option"
)
var _ rule.Rule = &Rule242393{}
type Rule242393 struct {
InstanceID string
Client client.Client
PodContext pod.PodContext
Options *Options242393
Logger provider.Logger
}
type Options242393 struct {
NodeGroupByLabels []string `json:"nodeGroupByLabels" yaml:"nodeGroupByLabels"`
}
var _ option.Option = (*Options242393)(nil)
func (o Options242393) Validate() field.ErrorList {
return option.ValidateLabelNames(o.NodeGroupByLabels, field.NewPath("nodeGroupByLabels"))
}
func (r *Rule242393) ID() string {
return ID242393
}
func (r *Rule242393) Name() string {
return "Kubernetes Worker Nodes must not have sshd service running (MEDIUM 242393)"
}
func (r *Rule242393) Run(ctx context.Context) (rule.RuleResult, error) {
checkResults := []rule.CheckResult{}
nodeLabels := []string{}
if r.Options != nil && r.Options.NodeGroupByLabels != nil {
nodeLabels = slices.Clone(r.Options.NodeGroupByLabels)
}
pods, err := kubeutils.GetPods(ctx, r.Client, "", labels.NewSelector(), 300)
if err != nil {
return rule.SingleCheckResult(r, rule.ErroredCheckResult(err.Error(), rule.NewTarget("kind", "podList"))), nil
}
nodes, err := kubeutils.GetNodes(ctx, r.Client, 300)
if err != nil {
return rule.SingleCheckResult(r, rule.ErroredCheckResult(err.Error(), rule.NewTarget("kind", "nodeList"))), nil
}
nodesAllocatablePods := kubeutils.GetNodesAllocatablePodsNum(pods, nodes)
selectedNodes, checks := kubeutils.SelectNodes(nodes, nodesAllocatablePods, nodeLabels)
checkResults = append(checkResults, checks...)
if len(selectedNodes) == 0 {
return rule.SingleCheckResult(r, rule.ErroredCheckResult("no allocatable nodes could be selected", rule.NewTarget())), nil
}
image, err := imagevector.ImageVector().FindImage(images.DikiOpsImageName)
if err != nil {
return rule.RuleResult{}, fmt.Errorf("failed to find image version for %s: %w", images.DikiOpsImageName, err)
}
image.WithOptionalTag(version.Get().GitVersion)
slices.SortFunc(selectedNodes, func(n1, n2 corev1.Node) int {
return cmp.Compare(n1.Name, n2.Name)
})
for _, node := range selectedNodes {
podName := fmt.Sprintf("diki-%s-%s", r.ID(), Generator.Generate(10))
nodeTarget := rule.NewTarget("kind", "node", "name", node.Name)
execPodTarget := rule.NewTarget("name", podName, "namespace", "kube-system", "kind", "pod")
defer func() {
if err := r.PodContext.Delete(ctx, podName, "kube-system"); err != nil {
r.Logger.Error(err.Error())
}
}()
additionalLabels := map[string]string{
pod.LabelInstanceID: r.InstanceID,
}
podExecutor, err := r.PodContext.Create(ctx, pod.NewPrivilegedPod(podName, "kube-system", image.String(), node.Name, additionalLabels))
if err != nil {
checkResults = append(checkResults, rule.ErroredCheckResult(err.Error(), execPodTarget))
continue
}
commandResult, err := podExecutor.Execute(ctx, "/bin/sh", `ss -tulpn | grep "LISTEN" | grep -E ":22(\s|$)" || true`)
if err != nil {
checkResults = append(checkResults, rule.ErroredCheckResult(err.Error(), execPodTarget))
continue
}
if strings.TrimSpace(commandResult) != "" {
checkResults = append(checkResults, rule.FailedCheckResult("SSH daemon started on port 22", nodeTarget))
continue
}
commandResult, err = podExecutor.Execute(ctx, "/bin/sh", `systemctl is-active sshd || true`)
if err != nil {
checkResults = append(checkResults, rule.ErroredCheckResult(err.Error(), execPodTarget))
continue
}
if strings.TrimSpace(strings.ToLower(commandResult)) == "inactive" {
checkResults = append(checkResults, rule.PassedCheckResult("SSH daemon service not installed", nodeTarget))
continue
}
if strings.TrimSpace(strings.ToLower(commandResult)) == "active" {
checkResults = append(checkResults, rule.FailedCheckResult("SSH daemon active", nodeTarget))
continue
}
checkResults = append(checkResults, rule.PassedCheckResult("SSH daemon inactive (or could not be probed)", nodeTarget))
}
return rule.RuleResult{
RuleID: r.ID(),
RuleName: r.Name(),
CheckResults: checkResults,
}, nil
}