-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvolume-secret-ref.yaml
63 lines (62 loc) · 1.5 KB
/
volume-secret-ref.yaml
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
apiVersion: kubevious.io/v1alpha1
kind: ClusterRule
metadata:
name: pod-spec-volume-secret-ref
spec:
summary: |
Validate PodSpec volume mount Secret reference.
categories:
- k8s
- pod-spec
- secret
- volume
- reference
target: |
Shortcut("PodSpec")
rule: |
for(const volume of config.spec?.volumes ?? [])
{
if (volume.secret)
{
checkSecretRef(volume.name, volume.secret.secretName, volume.secret);
}
else if (volume.projected)
{
for(const source of volume.projected.sources)
{
if (source.secret)
{
checkSecretRef(volume.name, source.secret.name, source.secret);
}
}
}
}
function checkSecretRef(volumeName, secretName, secretRef)
{
const secretItem = Shortcut("Secret", secretName)
.single();
if (!secretItem)
{
if (!secretRef.optional)
{
error(`Could not find Secret ${secretName} referenced in volume ${volumeName}.`);
}
}
else
{
if (secretRef.items)
{
const dataObj = secretItem.config.data ?? {};
for(const item of secretRef.items)
{
if (!dataObj[item.key])
{
if (!secretRef.optional)
{
error(`Could not find data key ${item.key} in Secret ${secretName} referenced in volume ${volumeName}.`);
}
}
}
}
}
}