Skip to content

Commit 8a3cc99

Browse files
committed
webhook multi-err
1 parent 94bb8ec commit 8a3cc99

File tree

1 file changed

+10
-9
lines changed
  • content/en/blog/_posts/2024-04-01-validating-admission-policy-ga

1 file changed

+10
-9
lines changed

content/en/blog/_posts/2024-04-01-validating-admission-policy-ga/index.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,28 @@ enforce `runAsNonRoot`, `readOnlyRootFilesystem`, `allowPrivilegeEscalation`, an
2121

2222
```go
2323
func verifyDeployment(deploy *appsv1.Deployment) error {
24+
var errs []error
2425
for i, c := range deploy.Spec.Template.Spec.Containers {
2526
if c.Name == "" {
2627
return fmt.Errorf("container %d has no name", i)
2728
}
2829
if c.SecurityContext == nil {
29-
return fmt.Errorf("container %q does not have SecurityContext", c.Name)
30+
errs = append(errs, fmt.Errorf("container %q does not have SecurityContext", c.Name))
3031
}
3132
if c.SecurityContext.RunAsNonRoot == nil || !*c.SecurityContext.RunAsNonRoot {
32-
return fmt.Errorf("container %q must set RunAsNonRoot to true in its SecurityContext", c.Name)
33+
errs = append(errs, fmt.Errorf("container %q must set RunAsNonRoot to true in its SecurityContext", c.Name))
3334
}
3435
if c.SecurityContext.ReadOnlyRootFilesystem == nil || !*c.SecurityContext.ReadOnlyRootFilesystem {
35-
return fmt.Errorf("container %q must set ReadOnlyRootFilesystem to true in its SecurityContext", c.Name)
36+
errs = append(errs, fmt.Errorf("container %q must set ReadOnlyRootFilesystem to true in its SecurityContext", c.Name))
3637
}
3738
if c.SecurityContext.AllowPrivilegeEscalation != nil && *c.SecurityContext.AllowPrivilegeEscalation {
38-
return fmt.Errorf("container %q must NOT set AllowPrivilegeEscalation to true in its SecurityContext", c.Name)
39+
errs = append(errs, fmt.Errorf("container %q must NOT set AllowPrivilegeEscalation to true in its SecurityContext", c.Name))
3940
}
4041
if c.SecurityContext.Privileged != nil && *c.SecurityContext.Privileged {
41-
return fmt.Errorf("container %q must NOT set Privileged to true in its SecurityContext", c.Name)
42+
errs = append(errs, fmt.Errorf("container %q must NOT set Privileged to true in its SecurityContext", c.Name))
4243
}
4344
}
44-
return nil
45+
return errors.NewAggregate(errs)
4546
}
4647
```
4748

@@ -162,9 +163,9 @@ EOF
162163
```text
163164
Warning: Validation failed for ValidatingAdmissionPolicy 'pod-security.policy.example.com' with binding 'pod-security.policy-binding.example.com': all containers must set runAsNonRoot to true
164165
Warning: Validation failed for ValidatingAdmissionPolicy 'pod-security.policy.example.com' with binding 'pod-security.policy-binding.example.com': all containers must set readOnlyRootFilesystem to true
165-
Warning: Validation failed for ValidatingAdmissionPolicy 'pod-security.policy.example.com' with binding 'pod-security.policy-binding.example.com': all containers must NOT set allowPrivilegeEscalation to true
166-
Warning: Validation failed for ValidatingAdmissionPolicy 'pod-security.policy.example.com' with binding 'pod-security.policy-binding.example.com': all containers must NOT set privileged to true
167-
Error from server: error when creating "STDIN": admission webhook "webhook.example.com" denied the request: container "nginx" must set RunAsNonRoot to true in its SecurityContext
166+
Warning: Validation failed for ValidatingAdmissionPolicy 'pod-security.policy.example.com' with binding 'pod-security.policy-binding.example.com': all containers must set allowPrivilegeEscalation to false
167+
Warning: Validation failed for ValidatingAdmissionPolicy 'pod-security.policy.example.com' with binding 'pod-security.policy-binding.example.com': all containers must set privileged to false
168+
Error from server: error when creating "STDIN": admission webhook "cel-shim.example.com" denied the request: [container "nginx" must set RunAsNonRoot to true in its SecurityContext, container "nginx" must set ReadOnlyRootFilesystem to true in its SecurityContext, container "nginx" must NOT set AllowPrivilegeEscalation to true in its SecurityContext, container "nginx" must NOT set Privileged to true in its SecurityContext]
168169
```
169170
Not quite the exact same behavior but good enough. After a few other cases, when we are confident with our policy, maybe it is time for some refactoring.
170171
With Variable Composition introduced in beta, we can extract repeated sub-expressions into their own variables.

0 commit comments

Comments
 (0)