Skip to content

Commit 9a4ffeb

Browse files
authored
fix: add restricted SecurityContext to injected validation containers (#178)
Both buildValidationContainer (init/native sidecar) and buildLegacySidecarContainer (pre-1.28 fallback) now set RunAsNonRoot, ReadOnlyRootFilesystem, AllowPrivilegeEscalation=false, drop ALL capabilities, and SeccompProfile=RuntimeDefault — matching the Pod Security Standards Restricted profile. Signed-off-by: Sachin Sampras M <sampras343@gmail.com>
1 parent cd7d977 commit 9a4ffeb

2 files changed

Lines changed: 155 additions & 0 deletions

File tree

‎internal/webhooks/pod_webhook.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ func buildValidationContainer(
260260
container.Env = append(container.Env, envs...)
261261
}
262262

263+
container.SecurityContext = restrictedSecurityContext()
264+
263265
return container
264266
}
265267

@@ -333,9 +335,25 @@ func buildLegacySidecarContainer(
333335
}
334336
}
335337

338+
container.SecurityContext = restrictedSecurityContext()
339+
336340
return container
337341
}
338342

343+
func restrictedSecurityContext() *corev1.SecurityContext {
344+
return &corev1.SecurityContext{
345+
RunAsNonRoot: ptr.To(true),
346+
ReadOnlyRootFilesystem: ptr.To(true),
347+
AllowPrivilegeEscalation: ptr.To(false),
348+
SeccompProfile: &corev1.SeccompProfile{
349+
Type: corev1.SeccompProfileTypeRuntimeDefault,
350+
},
351+
Capabilities: &corev1.Capabilities{
352+
Drop: []corev1.Capability{"ALL"},
353+
},
354+
}
355+
}
356+
339357
func validationConfigToArgs(logger logr.Logger, cfg v1alpha1.ValidationConfig, model v1alpha1.Model) []string {
340358
logger.Info("construct args")
341359
res := []string{}

‎internal/webhooks/pod_webhook_test.go‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,5 +817,142 @@ var _ = Describe("Pod webhook", func() {
817817
By("Cleanup custom resources namespace")
818818
_ = k8sClient.Delete(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: customResTestNamespace}})
819819
})
820+
821+
It("Should set restricted SecurityContext on injected init container", func() {
822+
secCtxTestName := "secctx-test"
823+
secCtxTestNamespace := fmt.Sprintf("secctx-ns-%d", time.Now().UnixNano())
824+
825+
By("Creating the Namespace for SecurityContext test")
826+
secCtxNs := &corev1.Namespace{
827+
ObjectMeta: metav1.ObjectMeta{
828+
Name: secCtxTestNamespace,
829+
},
830+
}
831+
err := k8sClient.Create(ctx, secCtxNs)
832+
Expect(err).To(Not(HaveOccurred()))
833+
834+
By("Create ModelValidation resource")
835+
secCtxMv := testutil.CreateTestModelValidation(testutil.TestModelValidationOptions{
836+
Name: secCtxTestName,
837+
Namespace: secCtxTestNamespace,
838+
ConfigType: "sigstore",
839+
CertIdentity: "secctx@example.com",
840+
CertOidcIssuer: "https://accounts.google.com",
841+
})
842+
err = k8sClient.Create(ctx, secCtxMv)
843+
Expect(err).To(Not(HaveOccurred()))
844+
845+
statusTracker.AddModelValidation(ctx, secCtxMv)
846+
847+
By("create labeled pod")
848+
secCtxPod := testutil.CreateTestPod(testutil.TestPodOptions{
849+
Name: "secctx-pod",
850+
Namespace: secCtxTestNamespace,
851+
Labels: map[string]string{constants.ModelValidationLabel: secCtxTestName},
852+
})
853+
err = k8sClient.Create(ctx, secCtxPod)
854+
Expect(err).To(Not(HaveOccurred()))
855+
856+
By("Checking that init container has restricted SecurityContext")
857+
foundPod := &corev1.Pod{}
858+
Eventually(ctx, func(ctx context.Context) []corev1.Container {
859+
_ = k8sClient.Get(ctx, types.NamespacedName{
860+
Name: "secctx-pod",
861+
Namespace: secCtxTestNamespace,
862+
}, foundPod)
863+
return foundPod.Spec.InitContainers
864+
}, 5*time.Second).Should(HaveLen(1))
865+
866+
initContainer := foundPod.Spec.InitContainers[0]
867+
Expect(initContainer.SecurityContext).ToNot(BeNil(), "SecurityContext must be set")
868+
Expect(initContainer.SecurityContext.RunAsNonRoot).ToNot(BeNil())
869+
Expect(*initContainer.SecurityContext.RunAsNonRoot).To(BeTrue(), "RunAsNonRoot must be true")
870+
Expect(initContainer.SecurityContext.ReadOnlyRootFilesystem).ToNot(BeNil())
871+
Expect(*initContainer.SecurityContext.ReadOnlyRootFilesystem).To(BeTrue(), "ReadOnlyRootFilesystem must be true")
872+
Expect(initContainer.SecurityContext.AllowPrivilegeEscalation).ToNot(BeNil())
873+
Expect(*initContainer.SecurityContext.AllowPrivilegeEscalation).To(BeFalse(), "AllowPrivilegeEscalation must be false")
874+
Expect(initContainer.SecurityContext.Capabilities).ToNot(BeNil())
875+
Expect(initContainer.SecurityContext.Capabilities.Drop).To(ContainElement(corev1.Capability("ALL")), "Must drop ALL capabilities")
876+
Expect(initContainer.SecurityContext.SeccompProfile).ToNot(BeNil())
877+
Expect(initContainer.SecurityContext.SeccompProfile.Type).To(Equal(corev1.SeccompProfileTypeRuntimeDefault), "Seccomp must be RuntimeDefault")
878+
879+
By("Cleanup SecurityContext namespace")
880+
_ = k8sClient.Delete(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: secCtxTestNamespace}})
881+
})
882+
883+
It("Should set restricted SecurityContext on continuous validation init container", func() {
884+
contSecCtxTestName := "cont-secctx-test"
885+
contSecCtxTestNamespace := fmt.Sprintf("cont-secctx-ns-%d", time.Now().UnixNano())
886+
887+
By("Creating the Namespace for continuous SecurityContext test")
888+
contSecCtxNs := &corev1.Namespace{
889+
ObjectMeta: metav1.ObjectMeta{
890+
Name: contSecCtxTestNamespace,
891+
},
892+
}
893+
err := k8sClient.Create(ctx, contSecCtxNs)
894+
Expect(err).To(Not(HaveOccurred()))
895+
896+
By("Create ModelValidation with continuous validation enabled")
897+
contSecCtxMv := testutil.CreateTestModelValidation(testutil.TestModelValidationOptions{
898+
Name: contSecCtxTestName,
899+
Namespace: contSecCtxTestNamespace,
900+
ConfigType: "sigstore",
901+
CertIdentity: "cont-secctx@example.com",
902+
CertOidcIssuer: "https://accounts.google.com",
903+
})
904+
contSecCtxMv.Spec.ContinuousValidation = &v1alpha1.ContinuousValidation{
905+
Enabled: true,
906+
Interval: "10m",
907+
}
908+
err = k8sClient.Create(ctx, contSecCtxMv)
909+
Expect(err).To(Not(HaveOccurred()))
910+
911+
statusTracker.AddModelValidation(ctx, contSecCtxMv)
912+
913+
By("create labeled pod")
914+
contSecCtxPod := testutil.CreateTestPod(testutil.TestPodOptions{
915+
Name: "cont-secctx-pod",
916+
Namespace: contSecCtxTestNamespace,
917+
Labels: map[string]string{constants.ModelValidationLabel: contSecCtxTestName},
918+
})
919+
err = k8sClient.Create(ctx, contSecCtxPod)
920+
Expect(err).To(Not(HaveOccurred()))
921+
922+
By("Checking that continuous validation container has restricted SecurityContext")
923+
foundPod := &corev1.Pod{}
924+
Eventually(ctx, func(ctx context.Context) int {
925+
_ = k8sClient.Get(ctx, types.NamespacedName{
926+
Name: "cont-secctx-pod",
927+
Namespace: contSecCtxTestNamespace,
928+
}, foundPod)
929+
return len(foundPod.Spec.InitContainers) + len(foundPod.Spec.Containers)
930+
}, 5*time.Second).Should(BeNumerically(">", 1))
931+
932+
var validationContainer *corev1.Container
933+
for i := range foundPod.Spec.InitContainers {
934+
if foundPod.Spec.InitContainers[i].Name == constants.ModelValidationInitContainerName {
935+
validationContainer = &foundPod.Spec.InitContainers[i]
936+
break
937+
}
938+
}
939+
if validationContainer == nil {
940+
for i := range foundPod.Spec.Containers {
941+
if foundPod.Spec.Containers[i].Name == constants.ModelValidationSidecarContainerName {
942+
validationContainer = &foundPod.Spec.Containers[i]
943+
break
944+
}
945+
}
946+
}
947+
Expect(validationContainer).ToNot(BeNil(), "Validation container must exist")
948+
Expect(validationContainer.SecurityContext).ToNot(BeNil(), "SecurityContext must be set on continuous validation container")
949+
Expect(*validationContainer.SecurityContext.RunAsNonRoot).To(BeTrue())
950+
Expect(*validationContainer.SecurityContext.ReadOnlyRootFilesystem).To(BeTrue())
951+
Expect(*validationContainer.SecurityContext.AllowPrivilegeEscalation).To(BeFalse())
952+
Expect(validationContainer.SecurityContext.Capabilities.Drop).To(ContainElement(corev1.Capability("ALL")))
953+
954+
By("Cleanup continuous SecurityContext namespace")
955+
_ = k8sClient.Delete(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: contSecCtxTestNamespace}})
956+
})
820957
})
821958
})

0 commit comments

Comments
 (0)