Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cherry-pick-1.15] Add SecurityContext to restore-helper #8495

Merged
merged 1 commit into from
Dec 9, 2024
Merged
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
1 change: 1 addition & 0 deletions changelogs/unreleased/8495-reasonerjt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add SecurityContext to restore-helper
32 changes: 29 additions & 3 deletions pkg/restore/actions/pod_volume_restore_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"fmt"
"strings"

"github.com/vmware-tanzu/velero/pkg/util/boolptr"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
Expand All @@ -44,6 +46,7 @@
defaultCPURequestLimit = "100m"
defaultMemRequestLimit = "128Mi"
defaultCommand = "/velero-restore-helper"
restoreHelperUID = 1000
)

type PodVolumeRestoreAction struct {
Expand Down Expand Up @@ -143,9 +146,15 @@

runAsUser, runAsGroup, allowPrivilegeEscalation, secCtx := getSecurityContext(log, config)

securityContext, err := kube.ParseSecurityContext(runAsUser, runAsGroup, allowPrivilegeEscalation, secCtx)
if err != nil {
log.Errorf("Using default securityContext values, couldn't parse securityContext requirements: %s.", err)
var securityContext corev1.SecurityContext
if runAsUser == "" && runAsGroup == "" && allowPrivilegeEscalation == "" && secCtx == "" {
securityContext = defaultSecurityCtx()
} else {
securityContext, err = kube.ParseSecurityContext(runAsUser, runAsGroup, allowPrivilegeEscalation, secCtx)
if err != nil {
log.Errorf("Using default securityContext values, couldn't parse securityContext requirements: %s.", err)
securityContext = defaultSecurityCtx()
}

Check warning on line 157 in pkg/restore/actions/pod_volume_restore_action.go

View check run for this annotation

Codecov / codecov/patch

pkg/restore/actions/pod_volume_restore_action.go#L153-L157

Added lines #L153 - L157 were not covered by tests
}

initContainerBuilder := newRestoreInitContainerBuilder(image, string(input.Restore.UID))
Expand Down Expand Up @@ -282,3 +291,20 @@
},
}...)
}

// defaultSecurityCtx returns a default security context for the init container, which has the level "restricted" per
// Pod Security Standards.
func defaultSecurityCtx() corev1.SecurityContext {
uid := int64(restoreHelperUID)
return corev1.SecurityContext{
AllowPrivilegeEscalation: boolptr.False(),
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
},
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
RunAsUser: &uid,
RunAsNonRoot: boolptr.True(),
}
}
16 changes: 14 additions & 2 deletions pkg/restore/actions/pod_volume_restore_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"sort"
"testing"

"github.com/vmware-tanzu/velero/pkg/util/boolptr"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -113,8 +115,18 @@ func TestPodVolumeRestoreActionExecute(t *testing.T) {
defaultCPURequestLimit, defaultMemRequestLimit, // requests
defaultCPURequestLimit, defaultMemRequestLimit, // limits
)

securityContext, _ := kube.ParseSecurityContext("", "", "", "")
id := int64(1000)
securityContext := corev1api.SecurityContext{
AllowPrivilegeEscalation: boolptr.False(),
Capabilities: &corev1api.Capabilities{
Drop: []corev1api.Capability{"ALL"},
},
SeccompProfile: &corev1api.SeccompProfile{
Type: corev1api.SeccompProfileTypeRuntimeDefault,
},
RunAsUser: &id,
RunAsNonRoot: boolptr.True(),
}

var (
restoreName = "my-restore"
Expand Down
Loading