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

Adding e2e test for running Windows hostprocess containers as members of a local usergroup #113119

Merged
merged 1 commit into from Oct 31, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
90 changes: 90 additions & 0 deletions test/e2e/windows/host_process.go
Expand Up @@ -770,6 +770,96 @@ var _ = SIGDescribe("[Feature:WindowsHostProcessContainers] [MinimumKubeletVersi
false,
"app logs should not contain 'status=failed")
})

ginkgo.It("should run as localgroup accounts", func() {
// This functionality is only supported on containerd v1.7+
ginkgo.By("Ensuring Windows nodes are running containerd v1.7+")
windowsNode, err := findWindowsNode(f)
framework.ExpectNoError(err, "error finding Windows node")
r, v, err := getNodeContainerRuntimeAndVersion(windowsNode)
framework.ExpectNoError(err, "error getting node container runtime and version")
framework.Logf("Got runtime: %s, version %v for node %s", r, v, windowsNode.Name)

if !strings.EqualFold(r, "containerd") {
e2eskipper.Skipf("container runtime is not containerd")
}

v1dot7 := semver.MustParse("1.7.0")
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: since 1.7 isn't officially a build yet, might add comment that it we build it with this version here for sig-windows: https://github.com/kubernetes-sigs/sig-windows-tools/releases/tag/windows-containerd-nightly

if v.LT(v1dot7) {
e2eskipper.Skipf("container runtime is < 1.7.0")
}

ginkgo.By("Scheduling a pod that creates a localgroup from an init container then starts a container using that group")
localGroupName := getRandomUserGrounName()
podName := "host-process-localgroup-pod"
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
},
Spec: v1.PodSpec{
SecurityContext: &v1.PodSecurityContext{
WindowsOptions: &v1.WindowsSecurityContextOptions{
HostProcess: &trueVar,
RunAsUserName: &User_NTAuthoritySystem,
},
},
HostNetwork: true,
InitContainers: []v1.Container{
{
Image: imageutils.GetE2EImage(imageutils.BusyBox),
Name: "setup",
Command: []string{"cmd", "/C", "net", "localgroup", localGroupName, "/add"},
},
},
Containers: []v1.Container{
{
Image: imageutils.GetE2EImage(imageutils.BusyBox),
Name: "localgroup-container",
Command: []string{"cmd", "/C", "whoami"},
SecurityContext: &v1.SecurityContext{
WindowsOptions: &v1.WindowsSecurityContextOptions{
RunAsUserName: &localGroupName,
},
},
},
},
RestartPolicy: v1.RestartPolicyNever,
NodeSelector: map[string]string{
"kubernetes.io/os": "windows",
},
},
}

e2epod.NewPodClient(f).Create(pod)

ginkgo.By("Waiting for pod to run")
e2epod.NewPodClient(f).WaitForFinish(podName, 3*time.Minute)

ginkgo.By("Then ensuring pod finished running successfully")
p, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(
context.TODO(),
podName,
metav1.GetOptions{})

framework.ExpectNoError(err, "error retrieving pod")
framework.ExpectEqual(p.Status.Phase, v1.PodSucceeded)

// whoami will output %COMPUTER_NAME%/{randomly generated username} here.
// It is sufficient to just check that the logs do not container `nt authority`
// because all of the 'built-in' accounts that can be used with HostProcess
// are prefixed with this.
ginkgo.By("Then ensuring pod was not running as a system account")
logs, err := e2epod.GetPodLogs(f.ClientSet, f.Namespace.Name, podName, "localgroup-container")
framework.ExpectNoError(err, "error retrieving container logs")
framework.Logf("Pod logs: %s", logs)
framework.ExpectEqual(
marosset marked this conversation as resolved.
Show resolved Hide resolved
strings.Contains(
strings.ToLower(logs),
"nt authority"),
false,
"Container runs 'whoami' and logs should not contain 'nt authority'")
})

})

func makeTestPodWithVolumeMounts(name string) *v1.Pod {
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/windows/utils.go
Expand Up @@ -18,6 +18,7 @@ package windows

import (
"fmt"
"math/rand"
"strings"
"time"

Expand Down Expand Up @@ -66,3 +67,14 @@ func getNodeContainerRuntimeAndVersion(n v1.Node) (string, semver.Version, error

return parts[0], v, nil
}

func getRandomUserGrounName() string {
var letters = []rune("abcdefghijklmnopqrstuvwxya")

s := make([]rune, 8)
for i := range s {
s[i] = letters[rand.Intn(len(letters))]
}

return "hpc-" + string(s)
}