File size: 8,486 Bytes
df0899b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
variables:
- name: containers
  expression: 'has(variables.anyObject.spec.containers) ? variables.anyObject.spec.containers : []'
- name: initContainers
  expression: 'has(variables.anyObject.spec.initContainers) ? variables.anyObject.spec.initContainers : []'
- name: ephemeralContainers
  expression: 'has(variables.anyObject.spec.ephemeralContainers) ? variables.anyObject.spec.ephemeralContainers : []'
- name: allowAllProfiles
  expression: |
    has(variables.params.allowedProfiles) && variables.params.allowedProfiles.exists(profile, profile == "*")
- name: exemptImagePrefixes
  expression: |
    !has(variables.params.exemptImages) ? [] :
      variables.params.exemptImages.filter(image, image.endsWith("*")).map(image, string(image).replace("*", ""))
- name: exemptImageExplicit
  expression: |
    !has(variables.params.exemptImages) ? [] : 
      variables.params.exemptImages.filter(image, !image.endsWith("*"))
- name: exemptImages
  expression: |
    (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container,
      container.image in variables.exemptImageExplicit ||
      variables.exemptImagePrefixes.exists(exemption, string(container.image).startsWith(exemption))).map(container, container.image)
- name: unverifiedContainers
  expression: |
    (variables.containers + variables.initContainers + variables.ephemeralContainers).filter(container,
      !variables.allowAllProfiles &&
      !(container.image in variables.exemptImages))
- name: inputAllowedProfiles
  expression: |
    !has(variables.params.allowedProfiles) ? [] : variables.params.allowedProfiles
- name: allowedLocalhostFiles
  expression: |
    has(variables.params.allowedLocalhostFiles) ? variables.params.allowedLocalhostFiles : []
- name: allowedProfilesTranslation
  expression: |
    (variables.inputAllowedProfiles.filter(profile,
    profile != "Localhost").map(profile, profile == "Unconfined" ? "unconfined" : profile)) + 
    (variables.inputAllowedProfiles.exists(profile, profile == "RuntimeDefault") ? ["runtime/default", "docker/default"] : [])
- name: allowSecurityContextLocalhost
  expression: |
    variables.inputAllowedProfiles.exists(profile, profile == "Localhost")
- name: derivedAllowedLocalhostFiles
  expression: |
    variables.allowSecurityContextLocalhost ? variables.params.allowedLocalhostFiles.map(file, "localhost/" + file) : []
- name: localhostWildcardAllowed
  expression: |
    variables.inputAllowedProfiles.exists(profile, profile == "localhost/*") || variables.derivedAllowedLocalhostFiles.exists(profile, profile == "localhost/*")
- name: allowedProfiles
  expression: |
    (variables.allowedProfilesTranslation + variables.derivedAllowedLocalhostFiles)
- name: hasPodSeccomp
  expression: |
    has(variables.anyObject.spec.securityContext) && has(variables.anyObject.spec.securityContext.seccompProfile)
- name: hasPodAnnotations
  expression: |
    has(variables.anyObject.metadata.annotations) && ("seccomp.security.alpha.kubernetes.io/pod" in variables.anyObject.metadata.annotations)
- name: podAnnotationsProfiles
  expression: |
    variables.unverifiedContainers.filter(container, 
      !(has(container.securityContext) && has(container.securityContext.seccompProfile)) && 
      !(has(variables.anyObject.metadata.annotations) && (("container.seccomp.security.alpha.kubernetes.io/" + container.name) in variables.anyObject.metadata.annotations)) && 
      !variables.hasPodSeccomp && 
      variables.hasPodAnnotations 
    ).map(container, {
      "container" : container.name,
      "profile" : variables.anyObject.metadata.annotations["seccomp.security.alpha.kubernetes.io/pod"],
      "file" : dyn(""),
      "location" : dyn("annotation seccomp.security.alpha.kubernetes.io/pod"),
    })
- name: containerAnnotationsProfiles
  expression: |
    variables.unverifiedContainers.filter(container, 
      !(has(container.securityContext) && has(container.securityContext.seccompProfile)) && 
      !variables.hasPodSeccomp && 
      has(variables.anyObject.metadata.annotations) && (("container.seccomp.security.alpha.kubernetes.io/" + container.name) in variables.anyObject.metadata.annotations)
    ).map(container, {
      "container" : container.name,
      "profile" : variables.anyObject.metadata.annotations["container.seccomp.security.alpha.kubernetes.io/" + container.name],
      "file" : dyn(""),
      "location" : dyn("annotation container.seccomp.security.alpha.kubernetes.io/" + container.name),
    })
- name: podLocalHostProfile
  expression: |
    variables.hasPodSeccomp && has(variables.anyObject.spec.securityContext.seccompProfile.localhostProfile) ? variables.anyObject.spec.securityContext.seccompProfile.localhostProfile : ""
- name: canonicalPodSecurityContextProfile
  expression: |
    has(variables.hasPodSeccomp) && has(variables.anyObject.spec.securityContext.seccompProfile.type) ? 
      (variables.anyObject.spec.securityContext.seccompProfile.type == "RuntimeDefault" ? (
        variables.allowedProfiles.exists(profile, profile == "runtime/default") ? "runtime/default" : variables.allowedProfiles.exists(profile, profile == "docker/default") ? "docker/default" : "runtime/default") : 
      variables.anyObject.spec.securityContext.seccompProfile.type == "Unconfined" ? "unconfined" : variables.anyObject.spec.securityContext.seccompProfile.type == "Localhost" ? "localhost/" + variables.podLocalHostProfile : "")
      : ""
- name: podSecurityContextProfiles
  expression: |
    variables.unverifiedContainers.filter(container, 
      !(has(container.securityContext) && has(container.securityContext.seccompProfile)) && 
      variables.hasPodSeccomp
    ).map(container, {
      "container" : container.name,
      "profile" : dyn(variables.canonicalPodSecurityContextProfile),
      "file" : variables.podLocalHostProfile,
      "location" : dyn("pod securityContext"),
    })
- name: containerSecurityContextProfiles
  expression: |
    variables.unverifiedContainers.filter(container, 
      has(container.securityContext) && has(container.securityContext.seccompProfile)
    ).map(container, {
      "container" : container.name,
      "profile" : dyn(has(container.securityContext.seccompProfile.type) ? (container.securityContext.seccompProfile.type == "RuntimeDefault" ? (
        variables.allowedProfiles.exists(profile, profile == "runtime/default") ? "runtime/default" : variables.allowedProfiles.exists(profile, profile == "docker/default") ? "docker/default" : "runtime/default") : 
      container.securityContext.seccompProfile.type == "Unconfined" ? "unconfined" : container.securityContext.seccompProfile.type == "Localhost" ? "localhost/" + container.securityContext.seccompProfile.localhostProfile : "")
      : ""),
      "file" : has(container.securityContext.seccompProfile.localhostProfile) ? container.securityContext.seccompProfile.localhostProfile : dyn(""),
      "location" : dyn("container securityContext"),
    })
- name: containerProfilesMissing
  expression: |
    variables.unverifiedContainers.filter(container, 
      !(has(container.securityContext) && has(container.securityContext.seccompProfile)) && 
      !(has(variables.anyObject.metadata.annotations) && (("container.seccomp.security.alpha.kubernetes.io/" + container.name) in variables.anyObject.metadata.annotations)) && 
      !variables.hasPodSeccomp && 
      !variables.hasPodAnnotations 
    ).map(container, {
      "container" : container.name,
      "profile" : dyn("not configured"),
      "file" : dyn(""),
      "location" : dyn("no explicit profile found"),
    })
- name: allContainerProfiles
  expression: |
    variables.podAnnotationsProfiles + variables.containerAnnotationsProfiles + variables.podSecurityContextProfiles + variables.containerSecurityContextProfiles + variables.containerProfilesMissing
- name: badContainerProfiles
  expression: |
    variables.allContainerProfiles.filter(badContainerProfile,
        !((badContainerProfile.profile in variables.allowedProfiles) || (badContainerProfile.profile.startsWith("localhost/") && variables.localhostWildcardAllowed))
    ).map(badProfile, "Seccomp profile '" + badProfile.profile + "' is not allowed for container '" + badProfile.container + "'. Found at: " + badProfile.location + ". Allowed profiles: " + variables.allowedProfiles.join(", "))
validations:
- expression: 'size(variables.badContainerProfiles) == 0'
  messageExpression: |
    variables.badContainerProfiles.join(", ")