forked from Shopify/kubeaudit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asat.go
108 lines (90 loc) · 3.77 KB
/
asat.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
package asat
import (
"github.com/elliotxx/kubeaudit"
"github.com/elliotxx/kubeaudit/pkg/k8s"
"github.com/elliotxx/kubeaudit/pkg/override"
)
const Name = "asat"
const (
// AutomountServiceAccountTokenDeprecated occurs when the deprecated serviceAccount field is non-empty
AutomountServiceAccountTokenDeprecated = "AutomountServiceAccountTokenDeprecated"
// AutomountServiceAccountTokenTrueAndDefaultSA occurs when automountServiceAccountToken is either not set
// (which defaults to true) or explicitly set to true, and serviceAccountName is either not set or set to "default"
AutomountServiceAccountTokenTrueAndDefaultSA = "AutomountServiceAccountTokenTrueAndDefaultSA"
)
const OverrideLabel = "allow-automount-service-account-token"
// AutomountServiceAccountToken implements Auditable
type AutomountServiceAccountToken struct{}
func New() *AutomountServiceAccountToken {
return &AutomountServiceAccountToken{}
}
// Audit checks that the deprecated serviceAccount field is not used and that the default service account is not
// being automatically mounted
func (a *AutomountServiceAccountToken) Audit(resource k8s.Resource, resources []k8s.Resource) ([]*kubeaudit.AuditResult, error) {
auditResult := auditResource(resource, resources)
auditResult = override.ApplyOverride(auditResult, Name, "", resource, OverrideLabel)
if auditResult != nil {
return []*kubeaudit.AuditResult{auditResult}, nil
}
return nil, nil
}
func auditResource(resource k8s.Resource, resources []k8s.Resource) *kubeaudit.AuditResult {
podSpec := k8s.GetPodSpec(resource)
if podSpec == nil {
return nil
}
if isDeprecatedServiceAccountName(podSpec) && !hasServiceAccountName(podSpec) {
return &kubeaudit.AuditResult{
Auditor: Name,
Rule: AutomountServiceAccountTokenDeprecated,
Severity: kubeaudit.Warn,
Message: "serviceAccount is a deprecated alias for serviceAccountName. serviceAccountName should be used instead.",
PendingFix: &fixDeprecatedServiceAccountName{
podSpec: podSpec,
},
Metadata: kubeaudit.Metadata{
"DeprecatedServiceAccount": podSpec.DeprecatedServiceAccount,
},
}
}
defaultServiceAccount := getDefaultServiceAccount(resources)
if usesDefaultServiceAccount(podSpec) && isAutomountTokenTrue(podSpec, defaultServiceAccount) {
return &kubeaudit.AuditResult{
Auditor: Name,
Rule: AutomountServiceAccountTokenTrueAndDefaultSA,
Severity: kubeaudit.Error,
Message: "Default service account with token mounted. automountServiceAccountToken should be set to 'false' on either the ServiceAccount or on the PodSpec or a non-default service account should be used.",
PendingFix: &fixDefaultServiceAccountWithAutomountToken{
podSpec: podSpec,
defaultServiceAccount: defaultServiceAccount,
},
}
}
return nil
}
func isDeprecatedServiceAccountName(podSpec *k8s.PodSpecV1) bool {
return podSpec.DeprecatedServiceAccount != ""
}
func hasServiceAccountName(podSpec *k8s.PodSpecV1) bool {
return podSpec.ServiceAccountName != ""
}
func isAutomountTokenTrue(podSpec *k8s.PodSpecV1, defaultServiceAccount *k8s.ServiceAccountV1) bool {
if podSpec.AutomountServiceAccountToken != nil {
return *podSpec.AutomountServiceAccountToken
}
return defaultServiceAccount == nil ||
defaultServiceAccount.AutomountServiceAccountToken == nil ||
*defaultServiceAccount.AutomountServiceAccountToken
}
func usesDefaultServiceAccount(podSpec *k8s.PodSpecV1) bool {
return podSpec.ServiceAccountName == "" || podSpec.ServiceAccountName == "default"
}
func getDefaultServiceAccount(resources []k8s.Resource) (serviceAccount *k8s.ServiceAccountV1) {
for _, resource := range resources {
serviceAccount, ok := resource.(*k8s.ServiceAccountV1)
if ok && (k8s.GetObjectMeta(serviceAccount).GetName() == "default") {
return serviceAccount
}
}
return
}