Skip to content

Commit

Permalink
Merge pull request #452 from jaideepr97/add-podsecurity-fix-1.8
Browse files Browse the repository at this point in the history
fix: add security context for kam and cluster pods
  • Loading branch information
jaideepr97 authored Mar 1, 2023
2 parents 0415405 + e38031d commit a7bff86
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
15 changes: 15 additions & 0 deletions controllers/gitopsservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,9 @@ func (r *ReconcileGitopsService) reconcileBackend(gitopsserviceNamespacedName ty
{
deploymentObj := newBackendDeployment(gitopsserviceNamespacedName)

// Add SeccompProfile based on cluster version
util.AddSeccompProfileForOpenShift(r.Client, &deploymentObj.Spec.Template.Spec)

deploymentObj.Spec.Template.Spec.NodeSelector = argocommon.DefaultNodeSelector()
// Set GitopsService instance as the owner and controller
if err := controllerutil.SetControllerReference(instance, deploymentObj, r.Scheme); err != nil {
Expand Down Expand Up @@ -720,6 +723,18 @@ func newBackendDeployment(ns types.NamespacedName) *appsv1.Deployment {
corev1.ResourceCPU: resourcev1.MustParse("500m"),
},
},
SecurityContext: &corev1.SecurityContext{
AllowPrivilegeEscalation: util.BoolPtr(false),
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"ALL",
},
},
RunAsNonRoot: util.BoolPtr(true),
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
},
},
},
Volumes: []corev1.Volume{
Expand Down
65 changes: 65 additions & 0 deletions controllers/gitopsservice_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,71 @@ func TestReconcile_BackendResourceLimits(t *testing.T) {
assert.Equal(t, resources.Limits[corev1.ResourceMemory], resourcev1.MustParse("256Mi"))
}

func TestReconcile_BackendSecurityContext(t *testing.T) {
logf.SetLogger(argocd.ZapLogger(true))
s := scheme.Scheme
addKnownTypesToScheme(s)

fakeClient := fake.NewFakeClientWithScheme(s, util.NewClusterVersion("4.12.1"), newGitopsService())
reconciler := newReconcileGitOpsService(fakeClient, s)

_, err := reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
assertNoError(t, err)

deployment := appsv1.Deployment{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: serviceName, Namespace: serviceNamespace}, &deployment)
assertNoError(t, err)

securityContext := deployment.Spec.Template.Spec.Containers[0].SecurityContext
want := &corev1.SecurityContext{
AllowPrivilegeEscalation: util.BoolPtr(false),
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"ALL",
},
},
RunAsNonRoot: util.BoolPtr(true),
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
}
assert.DeepEqual(t, securityContext, want)
}

func TestReconcile_KamSecurityContext(t *testing.T) {
logf.SetLogger(argocd.ZapLogger(true))
s := scheme.Scheme
addKnownTypesToScheme(s)

util.SetConsoleAPIFound(true)
defer util.SetConsoleAPIFound(false)

fakeClient := fake.NewFakeClientWithScheme(s, util.NewClusterVersion("4.12.1"), newGitopsService())
reconciler := newReconcileGitOpsService(fakeClient, s)

_, err := reconciler.Reconcile(context.TODO(), newRequest("test", "test"))
assertNoError(t, err)

deployment := appsv1.Deployment{}
err = fakeClient.Get(context.TODO(), types.NamespacedName{Name: cliName, Namespace: serviceNamespace}, &deployment)
assertNoError(t, err)

securityContext := deployment.Spec.Template.Spec.Containers[0].SecurityContext
want := &corev1.SecurityContext{
AllowPrivilegeEscalation: util.BoolPtr(false),
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"ALL",
},
},
RunAsNonRoot: util.BoolPtr(true),
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
}
assert.DeepEqual(t, securityContext, want)
}

func TestReconcile_testArgoCDForOperatorUpgrade(t *testing.T) {
logf.SetLogger(argocd.ZapLogger(true))
s := scheme.Scheme
Expand Down
15 changes: 15 additions & 0 deletions controllers/kam.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ func newDeploymentForCLI() *appsv1.Deployment {
corev1.ResourceCPU: resourcev1.MustParse("500m"),
},
},
SecurityContext: &corev1.SecurityContext{
AllowPrivilegeEscalation: util.BoolPtr(false),
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"ALL",
},
},
RunAsNonRoot: util.BoolPtr(true),
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
},
},
},
}
Expand Down Expand Up @@ -184,6 +196,9 @@ func (r *ReconcileGitopsService) reconcileCLIServer(cr *pipelinesv1alpha1.Gitops

deploymentObj := newDeploymentForCLI()

// Add SeccompProfile based on cluster version
util.AddSeccompProfileForOpenShift(r.Client, &deploymentObj.Spec.Template.Spec)

deploymentObj.Spec.Template.Spec.NodeSelector = argocommon.DefaultNodeSelector()

if err := controllerutil.SetControllerReference(cr, deploymentObj, r.Scheme); err != nil {
Expand Down
23 changes: 23 additions & 0 deletions controllers/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ package util

import (
"context"
"fmt"
"os"
"strings"

"github.com/argoproj-labs/argocd-operator/controllers/argoutil"
configv1 "github.com/openshift/api/config/v1"
console "github.com/openshift/api/console/v1"
"golang.org/x/mod/semver"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -116,3 +118,24 @@ func caseInsensitiveGetenv(s string) (string, string) {
}
return "", ""
}

// BoolPtr returns a pointer to val
func BoolPtr(val bool) *bool {
return &val
}

func AddSeccompProfileForOpenShift(client client.Client, podspec *corev1.PodSpec) {

version, _ := GetClusterVersion(client)
if version == "" || semver.Compare(fmt.Sprintf("v%s", version), "v4.10.999") > 0 {
if podspec.SecurityContext == nil {
podspec.SecurityContext = &corev1.PodSecurityContext{}
}
if podspec.SecurityContext.SeccompProfile == nil {
podspec.SecurityContext.SeccompProfile = &corev1.SeccompProfile{}
}
if len(podspec.SecurityContext.SeccompProfile.Type) == 0 {
podspec.SecurityContext.SeccompProfile.Type = corev1.SeccompProfileTypeRuntimeDefault
}
}
}

0 comments on commit a7bff86

Please sign in to comment.