-
Notifications
You must be signed in to change notification settings - Fork 6
/
242422.go
90 lines (74 loc) · 3.1 KB
/
242422.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
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package v1r11
import (
"context"
"fmt"
"strings"
"sigs.k8s.io/controller-runtime/pkg/client"
kubeutils "github.com/gardener/diki/pkg/kubernetes/utils"
"github.com/gardener/diki/pkg/rule"
)
var _ rule.Rule = &Rule242422{}
type Rule242422 struct {
Client client.Client
Namespace string
DeploymentName string
ContainerName string
}
func (r *Rule242422) ID() string {
return ID242422
}
func (r *Rule242422) Name() string {
return "Kubernetes API Server must have a certificate for communication (MEDIUM 242422)"
}
func (r *Rule242422) Run(ctx context.Context) (rule.RuleResult, error) {
const (
certOptName = "tls-cert-file"
keyOptName = "tls-private-key-file"
)
deploymentName := "kube-apiserver"
containerName := "kube-apiserver"
if r.DeploymentName != "" {
deploymentName = r.DeploymentName
}
if r.ContainerName != "" {
containerName = r.ContainerName
}
checkResults := []rule.CheckResult{}
target := rule.NewTarget("name", deploymentName, "namespace", r.Namespace, "kind", "deployment")
tlsCertFileOptionSlice, err := kubeutils.GetCommandOptionFromDeployment(ctx, r.Client, deploymentName, containerName, r.Namespace, certOptName)
if err != nil {
return rule.SingleCheckResult(r, rule.ErroredCheckResult(err.Error(), target)), nil
}
switch {
case len(tlsCertFileOptionSlice) == 0:
checkResults = append(checkResults, rule.FailedCheckResult(fmt.Sprintf("Option %s has not been set.", certOptName), target))
case len(tlsCertFileOptionSlice) > 1:
checkResults = append(checkResults, rule.WarningCheckResult(fmt.Sprintf("Option %s has been set more than once in container command.", certOptName), target))
case strings.TrimSpace(tlsCertFileOptionSlice[0]) == "":
checkResults = append(checkResults, rule.FailedCheckResult(fmt.Sprintf("Option %s is empty.", certOptName), target))
default:
checkResults = append(checkResults, rule.PassedCheckResult(fmt.Sprintf("Option %s set.", certOptName), target))
}
tlsPrivateKeyFileOptionSlice, err := kubeutils.GetCommandOptionFromDeployment(ctx, r.Client, deploymentName, containerName, r.Namespace, keyOptName)
if err != nil {
return rule.SingleCheckResult(r, rule.ErroredCheckResult(err.Error(), target)), nil
}
switch {
case len(tlsPrivateKeyFileOptionSlice) == 0:
checkResults = append(checkResults, rule.FailedCheckResult(fmt.Sprintf("Option %s has not been set.", keyOptName), target))
case len(tlsPrivateKeyFileOptionSlice) > 1:
checkResults = append(checkResults, rule.WarningCheckResult(fmt.Sprintf("Option %s has been set more than once in container command.", keyOptName), target))
case strings.TrimSpace(tlsPrivateKeyFileOptionSlice[0]) == "":
checkResults = append(checkResults, rule.FailedCheckResult(fmt.Sprintf("Option %s is empty.", keyOptName), target))
default:
checkResults = append(checkResults, rule.PassedCheckResult(fmt.Sprintf("Option %s set.", keyOptName), target))
}
return rule.RuleResult{
RuleID: r.ID(),
RuleName: r.Name(),
CheckResults: checkResults,
}, nil
}