Skip to content

Commit

Permalink
fix(e2e): Add installation with helm,kustomize,olm securityContext re…
Browse files Browse the repository at this point in the history
…stricted validation
  • Loading branch information
gansheer committed Oct 4, 2023
1 parent 46001e8 commit 9a97c6f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
15 changes: 6 additions & 9 deletions e2e/install/cli/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,12 @@ func TestBasicInstallation(t *testing.T) {
Eventually(PlatformConditionStatus(ns, v1.IntegrationPlatformConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionTrue))

// Check if default security context has been applyed
Eventually(OperatorPodHas(ns, func(pod *corev1.Pod) bool {
if pod.Spec.Containers == nil || len(pod.Spec.Containers) == 0 {
return false
}
// exclude user for openshift
pod.Spec.Containers[0].SecurityContext.RunAsUser = nil
return reflect.DeepEqual(pod.Spec.Containers[0].SecurityContext, kubernetes.DefaultOperatorSecurityContext())
}), TestTimeoutShort).Should(BeTrue())
// Check if restricted security context has been applyed
operatorPod := OperatorPod(ns)()
Expect(operatorPod.Spec.Containers[0].SecurityContext.RunAsNonRoot).To(Equal(kubernetes.DefaultOperatorSecurityContext().RunAsNonRoot))
Expect(operatorPod.Spec.Containers[0].SecurityContext.Capabilities).To(Equal(kubernetes.DefaultOperatorSecurityContext().Capabilities))
Expect(operatorPod.Spec.Containers[0].SecurityContext.SeccompProfile).To(Equal(kubernetes.DefaultOperatorSecurityContext().SeccompProfile))
Expect(operatorPod.Spec.Containers[0].SecurityContext.AllowPrivilegeEscalation).To(Equal(kubernetes.DefaultOperatorSecurityContext().AllowPrivilegeEscalation))

t.Run("run yaml", func(t *testing.T) {
Expect(KamelRunWithID(operatorID, ns, "files/yaml.yaml").Execute()).To(Succeed())
Expand Down
8 changes: 8 additions & 0 deletions e2e/install/helm/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

. "github.com/apache/camel-k/v2/e2e/support"
"github.com/apache/camel-k/v2/pkg/util/defaults"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
. "github.com/onsi/gomega"
)

Expand Down Expand Up @@ -63,6 +64,13 @@ func TestHelmInstallRunUninstall(t *testing.T) {

Eventually(OperatorPod(ns)).ShouldNot(BeNil())

// Check if restricted security context has been applyed
operatorPod := OperatorPod(ns)()
Expect(operatorPod.Spec.Containers[0].SecurityContext.RunAsNonRoot).To(Equal(kubernetes.DefaultOperatorSecurityContext().RunAsNonRoot))
Expect(operatorPod.Spec.Containers[0].SecurityContext.Capabilities).To(Equal(kubernetes.DefaultOperatorSecurityContext().Capabilities))
Expect(operatorPod.Spec.Containers[0].SecurityContext.SeccompProfile).To(Equal(kubernetes.DefaultOperatorSecurityContext().SeccompProfile))
Expect(operatorPod.Spec.Containers[0].SecurityContext.AllowPrivilegeEscalation).To(Equal(kubernetes.DefaultOperatorSecurityContext().AllowPrivilegeEscalation))

//Test a simple route
t.Run("simple route", func(t *testing.T) {
name := "yaml"
Expand Down
9 changes: 9 additions & 0 deletions e2e/install/kustomize/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

. "github.com/apache/camel-k/v2/e2e/support"
testutil "github.com/apache/camel-k/v2/e2e/support/util"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"

. "github.com/onsi/gomega"
)
Expand Down Expand Up @@ -59,6 +60,14 @@ func TestOperatorBasic(t *testing.T) {

Eventually(OperatorPod(ns)).ShouldNot(BeNil())
Eventually(OperatorPodPhase(ns), TestTimeoutMedium).Should(Equal(corev1.PodRunning))

// Check if restricted security context has been applyed
operatorPod := OperatorPod(ns)()
Expect(operatorPod.Spec.Containers[0].SecurityContext.RunAsNonRoot).To(Equal(kubernetes.DefaultOperatorSecurityContext().RunAsNonRoot))
Expect(operatorPod.Spec.Containers[0].SecurityContext.Capabilities).To(Equal(kubernetes.DefaultOperatorSecurityContext().Capabilities))
Expect(operatorPod.Spec.Containers[0].SecurityContext.SeccompProfile).To(Equal(kubernetes.DefaultOperatorSecurityContext().SeccompProfile))
Expect(operatorPod.Spec.Containers[0].SecurityContext.AllowPrivilegeEscalation).To(Equal(kubernetes.DefaultOperatorSecurityContext().AllowPrivilegeEscalation))

Eventually(Platform(ns)).ShouldNot(BeNil())
})
}
Expand Down
8 changes: 8 additions & 0 deletions e2e/install/olm/olm_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
olm "github.com/operator-framework/api/pkg/operators/v1alpha1"

"github.com/apache/camel-k/v2/pkg/util/defaults"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
"github.com/apache/camel-k/v2/pkg/util/openshift"
)

Expand Down Expand Up @@ -96,6 +97,13 @@ func TestOLMInstallation(t *testing.T) {
// Check the IntegrationPlatform has been reconciled
Eventually(PlatformVersion(ns)).Should(ContainSubstring(ipVersionPrefix))

// Check if restricted security context has been applyed
operatorPod := OperatorPod(ns)()
Expect(operatorPod.Spec.Containers[0].SecurityContext.RunAsNonRoot).To(Equal(kubernetes.DefaultOperatorSecurityContext().RunAsNonRoot))
Expect(operatorPod.Spec.Containers[0].SecurityContext.Capabilities).To(Equal(kubernetes.DefaultOperatorSecurityContext().Capabilities))
Expect(operatorPod.Spec.Containers[0].SecurityContext.SeccompProfile).To(Equal(kubernetes.DefaultOperatorSecurityContext().SeccompProfile))
Expect(operatorPod.Spec.Containers[0].SecurityContext.AllowPrivilegeEscalation).To(Equal(kubernetes.DefaultOperatorSecurityContext().AllowPrivilegeEscalation))

// Clean up
Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
Expect(Kamel("uninstall", "-n", ns).Execute()).To(Succeed())
Expand Down
10 changes: 10 additions & 0 deletions e2e/support/test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,16 @@ func OperatorImage(ns string) func() string {
}
}

func OperatorPodSecurityContext(ns string) func() *corev1.SecurityContext {
return func() *corev1.SecurityContext {
pod := OperatorPod(ns)()
if pod == nil || pod.Spec.Containers == nil || len(pod.Spec.Containers) == 0 {
return nil
}
return pod.Spec.Containers[0].SecurityContext
}
}

func OperatorPodHas(ns string, predicate func(pod *corev1.Pod) bool) func() bool {
return func() bool {
pod := OperatorPod(ns)()
Expand Down

0 comments on commit 9a97c6f

Please sign in to comment.