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

Support restricted PSA for worker pods #2410

Merged
merged 7 commits into from
Sep 14, 2022

Conversation

mhenriks
Copy link
Member

@mhenriks mhenriks commented Aug 25, 2022

Signed-off-by: Michael Henriksen mhenriks@redhat.com

What this PR does / why we need it:

Attempt to get in compliance with restricted PSA. See: https://kubernetes.io/docs/concepts/security/pod-security-admission/

  • Remove root pods
  • Remove selinux params
  • SecurityContextConstraint update
  • Run func test in restricted SCC

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

Special notes for your reviewer:

Release note:

Support Restricted PSA for worker pods

@kubevirt-bot kubevirt-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. labels Aug 25, 2022
@mhenriks
Copy link
Member Author

/hold

@kubevirt-bot kubevirt-bot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. size/S labels Aug 25, 2022
@awels
Copy link
Member

awels commented Aug 25, 2022

I suspect the nbdkit socket will be an issue here.

@mhenriks mhenriks changed the title Support restricted PSA Support restricted PSA for worker pods Aug 25, 2022
@mhenriks
Copy link
Member Author

I suspect the nbdkit socket will be an issue here.

please elaborate

@awels
Copy link
Member

awels commented Aug 26, 2022

Basically when I was working on #2331 I also tried removing root as part of that, and when I was running tests, they started failing because I was unable to access the nbdkit socket since I wasn't root. I didn't look into the exact reason much more though.

@awels
Copy link
Member

awels commented Aug 26, 2022

But it looks like the tests are passing, so maybe not an issue.

Copy link
Collaborator

@akalenyu akalenyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically when I was working on #2331 I also tried removing root as part of that, and when I was running tests, they started failing because I was unable to access the nbdkit socket since I wasn't root. I didn't look into the exact reason much more though.

Really interested about why last time around we saw imports failing on block without root.
Maybe related to us switching to less nbdkit usages?

Attempt to get in compliance with restricted PSA. See: https://kubernetes.io/docs/concepts/security/pod-security-admission/

Reading the k8s docs IIUC we need to explicitly set more things to comply with "restricted":

What am I missing?

  • SecurityContextConstraint update

If we end up changing our version of the SCC we should probably make the reconciliation of it more strict so it gets upgraded properly. Currently, it'll only reconcile if

if !sdk.ContainsStringValue(scc.Users, userName) {
scc.Users = append(scc.Users, userName)
return c.Update(context.TODO(), scc)
}

OFC this is a potential issue regardless of this PR so we can tackle elsewhere

@@ -733,6 +723,7 @@ func MakeCloneSourcePodSpec(sourceVolumeMode corev1.PersistentVolumeMode, image,
{
Name: DataVolName,
MountPath: common.ClonerMountPath,
ReadOnly: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, is there a real impact for this? IIUC the other ReadOnly is just about enforcing this one

@@ -592,15 +592,6 @@ func MakeCloneSourcePodSpec(sourceVolumeMode corev1.PersistentVolumeMode, image,
},
},
Spec: corev1.PodSpec{
SecurityContext: &corev1.PodSecurityContext{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! this will fix an issue someone was seeing on OpenShift 4.12 with kubevirt tekton tasks https://github.com/kubevirt/tekton-tasks-operator/blob/main/data/tekton-pipelines/okd/windows10-customize.yaml#L126
I can try to find the chats if needed

@mhenriks
Copy link
Member Author

/hold

@mhenriks
Copy link
Member Author

mhenriks commented Sep 6, 2022

/hold cancel

@kubevirt-bot kubevirt-bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Sep 6, 2022
@mhenriks mhenriks force-pushed the restricted-psa branch 2 times, most recently from 74f1da8 to 1f1c0fc Compare September 6, 2022 23:56
@mhenriks
Copy link
Member Author

mhenriks commented Sep 9, 2022

/retest-required

@mhenriks
Copy link
Member Author

mhenriks commented Sep 9, 2022

/test pull-cdi-linter

@mhenriks
Copy link
Member Author

mhenriks commented Sep 9, 2022

/retest

Copy link
Collaborator

@akalenyu akalenyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome
We probably won't hit the same issues as in the PR description here, right? kubevirt/kubevirt#8436

@@ -46,8 +46,8 @@ import (

const (
tempFile = "tmpimage"
nbdkitPid = "/var/run/nbdkit.pid"
nbdkitSocket = "/var/run/nbdkit.sock"
nbdkitPid = "/tmp/nbdkit.pid"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we concerned about some OS variants cleaning up /tmp after a certain period of time?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never heard of any tmp cleanup happening in the absence of a reboot.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://serverfault.com/a/377349
For example (RHEL)
But I don't this applies/matters much to our ephemeral workloads

@@ -165,7 +165,7 @@ func getImageFileName(dir string) (string, error) {
}

func createCertificateDir(registryCertDir string) (string, error) {
allCerts := "/var/local/all_certs"
allCerts := "/tmp/all_certs"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, I am missing why /var/local wasn't good enough?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non root cannot write to /var/local

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotcha

@@ -37,6 +37,34 @@ import (

const sccName = "containerized-data-importer"

func setSCC(scc *secv1.SecurityContextConstraints) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really important but I recently discovered https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/controller/controllerutil#CreateOrUpdate which could've worked in ensureSCCExists()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, maybe next time

}
options := fmt.Sprintf("-%s%s", tarOptions, "xvC")
untar := exec.Command("/usr/bin/tar", "--no-same-owner", options, destDir, strings.Join(args, ""))
untar := exec.Command("/usr/bin/tar", "--no-same-owner", "-xvC", destDir)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about printing the whole cmd before executing it with verbosity lvl 3?
always nice when debugging, we do the same for qemu-img

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw you took up the suggestion in the clone source pod but not here. Intended?

args = append(args, "--files-from", "/dev/null")
}

cmd := exec.Command("/usr/bin/tar", args...)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, if you're up for printing the args of course

@mhenriks
Copy link
Member Author

Awesome We probably won't hit the same issues as in the PR description here, right? kubevirt/kubevirt#8436

What issues, specifically? They are attacking the issue in a totally different way for now

Signed-off-by: Michael Henriksen <mhenriks@redhat.com>
Signed-off-by: Michael Henriksen <mhenriks@redhat.com>
Signed-off-by: Michael Henriksen <mhenriks@redhat.com>
Signed-off-by: Michael Henriksen <mhenriks@redhat.com>
Signed-off-by: Michael Henriksen <mhenriks@redhat.com>
refactor scc management

Signed-off-by: Michael Henriksen <mhenriks@redhat.com>
// pod-security.kubernetes.io/<MODE>: <LEVEL>
labels["pod-security.kubernetes.io/enforce"] = "restricted"
if utils.IsOpenshift(f.K8sClient) {
labels["security.openshift.io/scc.podSecurityLabelSync"] = "false"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice OpenShift awareness!

@akalenyu
Copy link
Collaborator

Awesome We probably won't hit the same issues as in the PR description here, right? kubevirt/kubevirt#8436

What issues, specifically? They are attacking the issue in a totally different way for now

Looks good just let me know if you want to tackle the few small replies, otherwise I think we are good

Copy link

@jean-edouard jean-edouard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one minor comment in-line

fi
fi

set +e
_kubectl patch cdi ${CR_NAME} --type=json -p '[{ "op": "remove", "path": "/metadata/finalizers" }]' | echo "this is fine"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DId you mean || echo "this is fine"? Piping the output of the kubectl command to echo does nothing...
Either way however, the error code of the kubectl command will be ignored, so set +e/set -e should not be necessary here.
But the code works the way it is, so no big deal.

@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label Sep 13, 2022
Signed-off-by: Michael Henriksen <mhenriks@redhat.com>
@kubevirt-bot kubevirt-bot removed the lgtm Indicates that a PR is ready to be merged. label Sep 14, 2022
@mhenriks
Copy link
Member Author

/test pull-containerized-data-importer-e2e-destructive

@akalenyu
Copy link
Collaborator

/lgtm

@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label Sep 14, 2022
@mhenriks
Copy link
Member Author

/approve

@kubevirt-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mhenriks

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubevirt-bot kubevirt-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 14, 2022
@kubevirt-bot kubevirt-bot merged commit 46c6aa9 into kubevirt:main Sep 14, 2022
mhenriks added a commit to mhenriks/containerized-data-importer that referenced this pull request Apr 13, 2023
Signed-off-by: Michael Henriksen <mhenriks@redhat.com>
kubevirt-bot pushed a commit that referenced this pull request Apr 17, 2023
Signed-off-by: Michael Henriksen <mhenriks@redhat.com>
mhenriks added a commit to mhenriks/containerized-data-importer that referenced this pull request Apr 26, 2023
Signed-off-by: Michael Henriksen <mhenriks@redhat.com>
kubevirt-bot pushed a commit that referenced this pull request Apr 26, 2023
Signed-off-by: Michael Henriksen <mhenriks@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants