-
Notifications
You must be signed in to change notification settings - Fork 6
/
242420.go
75 lines (60 loc) · 2.24 KB
/
242420.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package v1r11
import (
"context"
"fmt"
"strings"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
kubeutils "github.com/gardener/diki/pkg/kubernetes/utils"
"github.com/gardener/diki/pkg/rule"
)
var _ rule.Rule = &Rule242420{}
type Rule242420 struct {
Client client.Client
V1RESTClient rest.Interface
}
func (r *Rule242420) ID() string {
return ID242420
}
func (r *Rule242420) Name() string {
return "Kubernetes Kubelet must have the SSL Certificate Authority set (MEDIUM 242420)"
}
func (r *Rule242420) Run(ctx context.Context) (rule.RuleResult, error) {
checkResults := []rule.CheckResult{}
nodes, err := kubeutils.GetNodes(ctx, r.Client, 300)
if err != nil {
return rule.SingleCheckResult(r, rule.ErroredCheckResult(err.Error(), rule.NewTarget("kind", "nodeList"))), nil
}
if len(nodes) == 0 {
return rule.SingleCheckResult(r, rule.WarningCheckResult("No nodes found.", rule.NewTarget())), nil
}
const clientCAFileConfigOption = "authentication.x509.clientCAFile"
for _, node := range nodes {
target := rule.NewTarget("kind", "node", "name", node.Name)
if !kubeutils.NodeReadyStatus(node) {
checkResults = append(checkResults, rule.WarningCheckResult("Node is not in Ready state.", target))
continue
}
kubeletConfig, err := kubeutils.GetNodeConfigz(ctx, r.V1RESTClient, node.Name)
if err != nil {
checkResults = append(checkResults, rule.ErroredCheckResult(err.Error(), target))
continue
}
switch {
case kubeletConfig.Authentication.X509.ClientCAFile == nil:
checkResults = append(checkResults, rule.FailedCheckResult(fmt.Sprintf("Option %s not set.", clientCAFileConfigOption), target))
case strings.TrimSpace(*kubeletConfig.Authentication.X509.ClientCAFile) == "":
checkResults = append(checkResults, rule.FailedCheckResult(fmt.Sprintf("Option %s is empty.", clientCAFileConfigOption), target))
default:
checkResults = append(checkResults, rule.PassedCheckResult(fmt.Sprintf("Option %s set.", clientCAFileConfigOption), target))
}
}
return rule.RuleResult{
RuleID: r.ID(),
RuleName: r.Name(),
CheckResults: checkResults,
}, nil
}