Skip to content

Commit

Permalink
Do not run cronJobs as privileged containers
Browse files Browse the repository at this point in the history
When kolla is not required there's no need to run containers as
privileged (especially if we run commands that are supposed to
interact with DB).
This change removes the root user from the Manila cronJobs pod
and pass a SecurityContext that doesn't violate the k8s
requirements.

Signed-off-by: Francesco Pantano <[email protected]>
  • Loading branch information
fmount committed Oct 9, 2023
1 parent 252ecd8 commit 9f57c08
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
9 changes: 3 additions & 6 deletions pkg/manila/cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func CronJob(
labels map[string]string,
annotations map[string]string,
) *batchv1.CronJob {
runAsUser := int64(0)
var config0644AccessMode int32 = 0644
var DBPurgeCommand []string = DBPurgeCommandBase[:]
args := []string{"-c"}
Expand Down Expand Up @@ -103,11 +102,9 @@ func CronJob(
Command: []string{
"/bin/bash",
},
Args: args,
VolumeMounts: cronJobVolumeMounts,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
},
Args: args,
VolumeMounts: cronJobVolumeMounts,
SecurityContext: GetManilaSecurityContext(),
},
},
Volumes: cronJobVolume,
Expand Down
29 changes: 27 additions & 2 deletions pkg/manila/funcs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package manila

import "sigs.k8s.io/controller-runtime/pkg/client"
import (
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// GetOwningManilaName - Given a ManilaAPI, ManilaScheduler, ManilaBackup or ManilaVolume
// object, returning the parent Manila object that created it (if any)
Expand All @@ -10,6 +13,28 @@ func GetOwningManilaName(instance client.Object) string {
return ownerRef.Name
}
}

return ""
}

// GetManilaSecurityContext - Returns the right set of SecurityContext that
// does not violate the k8s requirements
func GetManilaSecurityContext() *corev1.SecurityContext {
falseVal := false
trueVal := true
runAsUser := int64(42429)
runAsGroup := int64(42429)
return &corev1.SecurityContext{
RunAsUser: &runAsUser,
RunAsGroup: &runAsGroup,
RunAsNonRoot: &trueVal,
AllowPrivilegeEscalation: &falseVal,
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{
"ALL",
},
},
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
}
}

0 comments on commit 9f57c08

Please sign in to comment.