Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package util
import (
"context"
"fmt"
"os"
"runtime"
"strings"
"time"

secv1 "github.com/openshift/api/security/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -19,11 +24,7 @@ import (
aaqv1alpha1 "kubevirt.io/application-aware-quota/staging/src/kubevirt.io/application-aware-quota-api/pkg/apis/core/v1alpha1"
sdkapi "kubevirt.io/controller-lifecycle-operator-sdk/api"
utils "kubevirt.io/controller-lifecycle-operator-sdk/pkg/sdk/resources"
"os"
"runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"strings"
"time"
)

const (
Expand Down Expand Up @@ -118,6 +119,7 @@ func CreateContainer(name, image, verbosity, pullPolicy string) corev1.Container
},
AllowPrivilegeEscalation: pointer.Bool(false),
RunAsNonRoot: pointer.Bool(true),
ReadOnlyRootFilesystem: pointer.Bool(true),
}
return *container
}
Expand Down Expand Up @@ -239,8 +241,8 @@ func SetRecommendedLabels(obj metav1.Object, installerLabels map[string]string,
}

func PrintVersion() {
klog.Infof(fmt.Sprintf("Go Version: %s", runtime.Version()))
klog.Infof(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH))
klog.Infof("Go Version: %s", runtime.Version())
klog.Infof("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)
}
func getNamespace(path string) string {
if data, err := os.ReadFile(path); err == nil {
Expand Down
48 changes: 48 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package util

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
)

func TestUtil(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Util Suite")
}

var _ = Describe("CreateContainer SecurityContext", func() {
newContainer := func() corev1.Container {
return CreateContainer("test", "img:latest", "1", "Always")
}

DescribeTable("should enforce restricted SecurityContext fields",
func(check func(corev1.Container)) {
check(newContainer())
},
Entry("ReadOnlyRootFilesystem=true", func(c corev1.Container) {
Expect(c.SecurityContext).NotTo(BeNil())
Expect(c.SecurityContext.ReadOnlyRootFilesystem).NotTo(BeNil())
Expect(*c.SecurityContext.ReadOnlyRootFilesystem).To(BeTrue())
}),
Entry("AllowPrivilegeEscalation=false", func(c corev1.Container) {
Expect(c.SecurityContext.AllowPrivilegeEscalation).NotTo(BeNil())
Expect(*c.SecurityContext.AllowPrivilegeEscalation).To(BeFalse())
}),
Entry("RunAsNonRoot=true", func(c corev1.Container) {
Expect(c.SecurityContext.RunAsNonRoot).NotTo(BeNil())
Expect(*c.SecurityContext.RunAsNonRoot).To(BeTrue())
}),
Entry("drops ALL capabilities", func(c corev1.Container) {
Expect(c.SecurityContext.Capabilities).NotTo(BeNil())
Expect(c.SecurityContext.Capabilities.Drop).To(ContainElement(corev1.Capability("ALL")))
}),
Entry("SeccompProfile=RuntimeDefault", func(c corev1.Container) {
Expect(c.SecurityContext.SeccompProfile).NotTo(BeNil())
Expect(c.SecurityContext.SeccompProfile.Type).To(Equal(corev1.SeccompProfileTypeRuntimeDefault))
}),
)
})
25 changes: 23 additions & 2 deletions tests/aaq_operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"
"encoding/json"
"fmt"
"reflect"
"time"

schedulev1 "k8s.io/api/scheduling/v1"
"kubevirt.io/application-aware-quota/pkg/aaq-operator/resources/cluster"
resourcesutils "kubevirt.io/application-aware-quota/pkg/util"
"kubevirt.io/application-aware-quota/tests/utils"
"reflect"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -690,6 +691,26 @@ var _ = Describe("ALL Operator tests", Serial, func() {
})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Consider also covering initContainers (if present) for the readOnlyRootFilesystem requirement

Since this test enforces ReadOnlyRootFilesystem=true on all main containers, it would be helpful to also iterate over deployment.Spec.Template.Spec.InitContainers and assert the same setting there, or assert that no initContainers exist. This would strengthen the test as a regression guard if initContainers are added later.

Suggested implementation:

			deployment, err := f.K8sClient.AppsV1().Deployments(f.AAQInstallNs).Get(context.TODO(), deploymentName, metav1.GetOptions{})
			Expect(err).ToNot(HaveOccurred(), "failed to get deployment %s", deploymentName)

			// ensure initContainers (if present) also comply with readOnlyRootFilesystem requirement
			Expect(len(deployment.Spec.Template.Spec.InitContainers)).To(
				Equal(0),
				"deployment %s must not define initContainers unless they also enforce ReadOnlyRootFilesystem=true",
				deploymentName,
			)

If you later decide to allow initContainers, replace the Equal(0) assertion with a loop over deployment.Spec.Template.Spec.InitContainers mirroring the checks done for Containers (non-nil SecurityContext, non-nil ReadOnlyRootFilesystem, and ReadOnlyRootFilesystem == true).

})

var _ = Describe("AAQ Security", func() {
f := framework.NewFramework("security-test")

It("All AAQ deployments should have readOnlyRootFilesystem set to true", func() {
for _, deploymentName := range []string{"aaq-operator", "aaq-server", "aaq-controller"} {
deployment, err := f.K8sClient.AppsV1().Deployments(f.AAQInstallNs).Get(context.TODO(), deploymentName, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred(), "failed to get deployment %s", deploymentName)

for _, container := range deployment.Spec.Template.Spec.Containers {
Expect(container.SecurityContext).ToNot(BeNil(),
"container %s in deployment %s has no SecurityContext", container.Name, deploymentName)
Expect(container.SecurityContext.ReadOnlyRootFilesystem).ToNot(BeNil(),
"container %s in deployment %s has no ReadOnlyRootFilesystem", container.Name, deploymentName)
Expect(*container.SecurityContext.ReadOnlyRootFilesystem).To(BeTrue(),
"container %s in deployment %s should have ReadOnlyRootFilesystem=true", container.Name, deploymentName)
}
}
})
})

func getAAQPods(f *framework.Framework) *corev1.PodList {
By("Getting AAQ pods")
labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{"app.kubernetes.io/component": "multi-tenant"}}
Expand Down