Skip to content

Commit 29853f2

Browse files
authored
Fix velero container to have explicit ReadOnlyRootFilesystem (#1755)
Current velero container does not use explicit ReadOnlyRootFilesystem flag set to true. The root filesystem is not writeable by the velero user, however it's not explicitly set as ReadOnlyRootFilesystem. This PR fixes it. Signed-off-by: Michal Pryc <[email protected]>
1 parent 577abe6 commit 29853f2

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

internal/controller/velero.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,18 @@ func (r *DataProtectionApplicationReconciler) customizeVeleroDeployment(veleroDe
272272
EmptyDir: &corev1.EmptyDirVolumeSource{},
273273
},
274274
},
275+
corev1.Volume{
276+
Name: "tmp",
277+
VolumeSource: corev1.VolumeSource{
278+
EmptyDir: &corev1.EmptyDirVolumeSource{},
279+
},
280+
},
281+
corev1.Volume{
282+
Name: "home",
283+
VolumeSource: corev1.VolumeSource{
284+
EmptyDir: &corev1.EmptyDirVolumeSource{},
285+
},
286+
},
275287
// used for short-lived credentials, inert if not used
276288
corev1.Volume{
277289
Name: "bound-sa-token",
@@ -573,7 +585,31 @@ func (r *DataProtectionApplicationReconciler) customizeVeleroContainer(veleroCon
573585
MountPath: "/var/run/secrets/openshift/serviceaccount",
574586
ReadOnly: true,
575587
},
588+
corev1.VolumeMount{
589+
Name: "tmp",
590+
MountPath: "/tmp",
591+
ReadOnly: false,
592+
},
593+
corev1.VolumeMount{
594+
Name: "home",
595+
MountPath: "/home/velero",
596+
ReadOnly: false,
597+
},
576598
)
599+
600+
// Ensure the /plugins and /target is ReadOnly
601+
for i, mount := range veleroContainer.VolumeMounts {
602+
if mount.MountPath == "/plugins" || mount.MountPath == "/target" {
603+
veleroContainer.VolumeMounts[i].ReadOnly = true
604+
}
605+
}
606+
607+
veleroContainer.SecurityContext = &corev1.SecurityContext{
608+
ReadOnlyRootFilesystem: ptr.To(true),
609+
Privileged: ptr.To(false),
610+
AllowPrivilegeEscalation: ptr.To(false),
611+
}
612+
577613
// append velero PodConfig envs to container
578614
if dpa.Spec.Configuration != nil && dpa.Spec.Configuration.Velero != nil && dpa.Spec.Configuration.Velero.PodConfig != nil && dpa.Spec.Configuration.Velero.PodConfig.Env != nil {
579615
veleroContainer.Env = common.AppendUniqueEnvVars(veleroContainer.Env, dpa.Spec.Configuration.Velero.PodConfig.Env)

internal/controller/velero_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,12 @@ var (
9494
}
9595

9696
baseVolumeMounts = []corev1.VolumeMount{
97-
{Name: "plugins", MountPath: "/plugins"},
97+
{Name: "plugins", MountPath: "/plugins", ReadOnly: true},
9898
{Name: "scratch", MountPath: "/scratch"},
9999
{Name: "certs", MountPath: "/etc/ssl/certs"},
100100
{Name: "bound-sa-token", MountPath: "/var/run/secrets/openshift/serviceaccount", ReadOnly: true},
101+
{Name: "tmp", MountPath: "/tmp"},
102+
{Name: "home", MountPath: "/home/velero"},
101103
}
102104

103105
baseVolumes = []corev1.Volume{
@@ -113,6 +115,14 @@ var (
113115
Name: "certs",
114116
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
115117
},
118+
{
119+
Name: "tmp",
120+
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
121+
},
122+
{
123+
Name: "home",
124+
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
125+
},
116126
{
117127
Name: "bound-sa-token",
118128
VolumeSource: corev1.VolumeSource{
@@ -417,6 +427,11 @@ func createTestBuiltVeleroDeployment(options TestBuiltVeleroDeploymentOptions) *
417427
ImagePullPolicy: corev1.PullAlways,
418428
TerminationMessagePath: "/dev/termination-log",
419429
TerminationMessagePolicy: corev1.TerminationMessageFallbackToLogsOnError,
430+
SecurityContext: &corev1.SecurityContext{
431+
ReadOnlyRootFilesystem: ptr.To(true),
432+
Privileged: ptr.To(false),
433+
AllowPrivilegeEscalation: ptr.To(false),
434+
},
420435
Ports: []corev1.ContainerPort{{
421436
Name: "metrics",
422437
ContainerPort: 8085,

0 commit comments

Comments
 (0)