/
242429.go
65 lines (52 loc) · 1.93 KB
/
242429.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
// 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 = &Rule242429{}
type Rule242429 struct {
Client client.Client
Namespace string
DeploymentName string
ContainerName string
}
func (r *Rule242429) ID() string {
return ID242429
}
func (r *Rule242429) Name() string {
return "Kubernetes etcd must have the SSL Certificate Authority set (MEDIUM 242429)"
}
func (r *Rule242429) Run(ctx context.Context) (rule.RuleResult, error) {
const option = "etcd-cafile"
deploymentName := "kube-apiserver"
containerName := "kube-apiserver"
if r.DeploymentName != "" {
deploymentName = r.DeploymentName
}
if r.ContainerName != "" {
containerName = r.ContainerName
}
target := rule.NewTarget("name", deploymentName, "namespace", r.Namespace, "kind", "deployment")
optSlice, err := kubeutils.GetCommandOptionFromDeployment(ctx, r.Client, deploymentName, containerName, r.Namespace, option)
if err != nil {
return rule.SingleCheckResult(r, rule.ErroredCheckResult(err.Error(), target)), nil
}
// setting option is required
switch {
case len(optSlice) == 0:
return rule.SingleCheckResult(r, rule.FailedCheckResult(fmt.Sprintf("Option %s has not been set.", option), target)), nil
case len(optSlice) > 1:
return rule.SingleCheckResult(r, rule.WarningCheckResult(fmt.Sprintf("Option %s has been set more than once in container command.", option), target)), nil
case strings.TrimSpace(optSlice[0]) == "":
return rule.SingleCheckResult(r, rule.FailedCheckResult(fmt.Sprintf("Option %s is empty.", option), target)), nil
default:
return rule.SingleCheckResult(r, rule.PassedCheckResult(fmt.Sprintf("Option %s set.", option), target)), nil
}
}