-
Notifications
You must be signed in to change notification settings - Fork 6
/
242417.go
134 lines (112 loc) · 4.59 KB
/
242417.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
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package v1r11
import (
"context"
"fmt"
"slices"
"strings"
corev1 "k8s.io/api/core/v1"
metav1validation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/gardener/diki/pkg/internal/utils"
"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/ruleset/disak8sstig/option"
)
var _ rule.Rule = &Rule242417{}
type Rule242417 struct {
Client client.Client
Options *Options242417
}
type Options242417 struct {
AcceptedPods []AcceptedPods242417 `json:"acceptedPods" yaml:"acceptedPods"`
}
var _ option.Option = (*Options242417)(nil)
type AcceptedPods242417 struct {
PodMatchLabels map[string]string `json:"podMatchLabels" yaml:"podMatchLabels"`
NamespaceNames []string `json:"namespaceNames" yaml:"namespaceNames"`
Justification string `json:"justification" yaml:"justification"`
Status string `json:"status" yaml:"status"`
}
func (o Options242417) Validate() field.ErrorList {
var (
allErrs field.ErrorList
rootPath = field.NewPath("acceptedPods")
)
for _, p := range o.AcceptedPods {
allErrs = append(allErrs, metav1validation.ValidateLabels(p.PodMatchLabels, rootPath.Child("podMatchLabels"))...)
for _, namespaceName := range p.NamespaceNames {
if !slices.Contains([]string{"kube-system", "kube-public", "kube-node-lease"}, namespaceName) {
allErrs = append(allErrs, field.Invalid(rootPath.Child("namespaceNames"), namespaceName, "must be one of 'kube-system', 'kube-public' or 'kube-node-lease'"))
}
}
if !slices.Contains([]string{"Passed", "Accepted"}, p.Status) && len(p.Status) > 0 {
allErrs = append(allErrs, field.Invalid(rootPath.Child("status"), p.Status, "must be one of 'Passed' or 'Accepted'"))
}
}
return allErrs
}
func (r *Rule242417) ID() string {
return ID242417
}
func (r *Rule242417) Name() string {
return "Kubernetes must separate user functionality (MEDIUM 242417)"
}
func (r *Rule242417) Run(ctx context.Context) (rule.RuleResult, error) {
checkResults := []rule.CheckResult{}
systemNamespaces := []string{"kube-system", "kube-public", "kube-node-lease"}
acceptedPods := []AcceptedPods242417{}
if r.Options != nil && r.Options.AcceptedPods != nil {
acceptedPods = r.Options.AcceptedPods
}
notDikiPodReq, err := labels.NewRequirement(pod.LabelComplianceRoleKey, selection.NotEquals, []string{pod.LabelComplianceRolePrivPod})
if err != nil {
return rule.SingleCheckResult(r, rule.ErroredCheckResult(err.Error(), rule.NewTarget())), nil
}
selector := labels.NewSelector().Add(*notDikiPodReq)
for _, namespace := range systemNamespaces {
podsPartialMetadata, err := kubeutils.GetObjectsMetadata(ctx, r.Client, corev1.SchemeGroupVersion.WithKind("PodList"), namespace, selector, 300)
if err != nil {
return rule.SingleCheckResult(r, rule.ErroredCheckResult(err.Error(), rule.NewTarget("namespace", namespace, "kind", "podList"))), nil
}
for _, podPartialMetadata := range podsPartialMetadata {
target := rule.NewTarget("name", podPartialMetadata.Name, "namespace", podPartialMetadata.Namespace, "kind", "pod")
acceptedPodIdx := slices.IndexFunc(acceptedPods, func(acceptedPod AcceptedPods242417) bool {
return slices.Contains(acceptedPod.NamespaceNames, namespace) &&
utils.MatchLabels(podPartialMetadata.Labels, acceptedPod.PodMatchLabels)
})
if acceptedPodIdx < 0 {
checkResults = append(checkResults, rule.FailedCheckResult("Found user pods in system namespaces.", target))
continue
}
acceptedPod := r.Options.AcceptedPods[acceptedPodIdx]
msg := strings.TrimSpace(acceptedPod.Justification)
status := strings.TrimSpace(acceptedPod.Status)
switch status {
case "Passed":
if len(msg) == 0 {
msg = "System pod in system namespaces."
}
checkResults = append(checkResults, rule.PassedCheckResult(msg, target))
case "Accepted", "":
if len(msg) == 0 {
msg = "Accepted user pod in system namespaces."
}
checkResults = append(checkResults, rule.AcceptedCheckResult(msg, target))
default:
checkResults = append(checkResults, rule.WarningCheckResult(fmt.Sprintf("unrecognized status: %s", status), target))
}
}
}
return rule.RuleResult{
RuleID: r.ID(),
RuleName: r.Name(),
CheckResults: checkResults,
}, nil
}