/
unused_secrets.go
180 lines (157 loc) 路 5.08 KB
/
unused_secrets.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Copyright 2022 DigitalOcean
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package basic
import (
"sync"
"github.com/digitalocean/clusterlint/checks"
"github.com/digitalocean/clusterlint/kube"
"golang.org/x/sync/errgroup"
corev1 "k8s.io/api/core/v1"
)
func init() {
checks.Register(&unusedSecretCheck{})
}
type unusedSecretCheck struct{}
type identifier struct {
Name string
Namespace string
}
// Name returns a unique name for this check.
func (s *unusedSecretCheck) Name() string {
return "unused-secret"
}
// Groups returns a list of group names this check should be part of.
func (s *unusedSecretCheck) Groups() []string {
return []string{"basic"}
}
// Description returns a detailed human-readable description of what this check
// does.
func (s *unusedSecretCheck) Description() string {
return "Checks if there are unused secrets in the cluster. Ignores service account tokens"
}
// Run runs this check on a set of Kubernetes objects. It can return warnings
// (low-priority problems) and errors (high-priority problems) as well as an
// error value indicating that the check failed to run.
func (s *unusedSecretCheck) Run(objects *kube.Objects) ([]checks.Diagnostic, error) {
var diagnostics []checks.Diagnostic
used, err := checkReferences(objects)
if err != nil {
return nil, err
}
for _, secret := range filter(objects.Secrets.Items) {
if _, ok := used[kube.Identifier{Name: secret.GetName(), Namespace: secret.GetNamespace()}]; !ok {
secret := secret
d := checks.Diagnostic{
Severity: checks.Warning,
Message: "Unused secret",
Kind: checks.Secret,
Object: &secret.ObjectMeta,
Owners: secret.ObjectMeta.GetOwnerReferences(),
}
diagnostics = append(diagnostics, d)
}
}
return diagnostics, nil
}
//checkReferences checks each pod for config map references in volumes and environment variables
func checkReferences(objects *kube.Objects) (map[kube.Identifier]struct{}, error) {
used := make(map[kube.Identifier]struct{})
var empty struct{}
var mu sync.Mutex
var g errgroup.Group
for _, pod := range objects.Pods.Items {
pod := pod
namespace := pod.GetNamespace()
g.Go(func() error {
for _, volume := range pod.Spec.Volumes {
s := volume.VolumeSource.Secret
if s != nil {
mu.Lock()
used[kube.Identifier{Name: s.SecretName, Namespace: namespace}] = empty
mu.Unlock()
}
if volume.VolumeSource.Projected != nil {
for _, source := range volume.VolumeSource.Projected.Sources {
s := source.Secret
if s != nil {
mu.Lock()
used[kube.Identifier{Name: s.LocalObjectReference.Name, Namespace: namespace}] = empty
mu.Unlock()
}
}
}
}
for _, imageSecret := range pod.Spec.ImagePullSecrets {
mu.Lock()
used[kube.Identifier{Name: imageSecret.Name, Namespace: namespace}] = empty
mu.Unlock()
}
identifiers := envVarsSecretRefs(pod.Spec.Containers, namespace)
identifiers = append(identifiers, envVarsSecretRefs(pod.Spec.InitContainers, namespace)...)
mu.Lock()
for _, i := range identifiers {
used[i] = empty
}
mu.Unlock()
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
for _, sa := range objects.ServiceAccounts.Items {
sa := sa
namespace := sa.Namespace
g.Go(func() error {
for _, imageSecret := range sa.ImagePullSecrets {
mu.Lock()
used[kube.Identifier{Name: imageSecret.Name, Namespace: namespace}] = empty
mu.Unlock()
}
for _, secret := range sa.Secrets {
mu.Lock()
used[kube.Identifier{Name: secret.Name, Namespace: namespace}] = empty
mu.Unlock()
}
return nil
})
}
return used, g.Wait()
}
// envVarsSecretRefs checks for secret references in container environment variables
func envVarsSecretRefs(containers []corev1.Container, namespace string) []kube.Identifier {
var refs []kube.Identifier
for _, container := range containers {
for _, env := range container.EnvFrom {
if env.SecretRef != nil {
refs = append(refs, kube.Identifier{Name: env.SecretRef.LocalObjectReference.Name, Namespace: namespace})
}
}
for _, env := range container.Env {
if env.ValueFrom != nil && env.ValueFrom.SecretKeyRef != nil {
refs = append(refs, kube.Identifier{Name: env.ValueFrom.SecretKeyRef.LocalObjectReference.Name, Namespace: namespace})
}
}
}
return refs
}
// filter returns Secrets that are not of type `kubernetes.io/service-account-token`
func filter(secrets []corev1.Secret) []corev1.Secret {
var filtered []corev1.Secret
for _, secret := range secrets {
if secret.Type != corev1.SecretTypeServiceAccountToken {
filtered = append(filtered, secret)
}
}
return filtered
}