Skip to content

Commit af1a090

Browse files
authored
Merge pull request containerd#5865 from dcantah/windows-pod-runasusername
Add RunAsUserName functionality for the Windows pod sandbox container
2 parents ebe8f8c + 25644b4 commit af1a090

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

pkg/cri/server/sandbox_run_windows.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ func (c *criService) sandboxContainerSpec(id string, config *runtime.PodSandboxC
5656

5757
specOpts = append(specOpts, customopts.WithWindowsDefaultSandboxShares)
5858

59+
// Start with the image config user and override below if RunAsUsername is not "".
60+
username := imageConfig.User
61+
62+
runAsUser := config.GetWindows().GetSecurityContext().GetRunAsUsername()
63+
if runAsUser != "" {
64+
username = runAsUser
65+
}
66+
67+
cs := config.GetWindows().GetSecurityContext().GetCredentialSpec()
68+
if cs != "" {
69+
specOpts = append(specOpts, customopts.WithWindowsCredentialSpec(cs))
70+
}
71+
72+
// There really isn't a good Windows way to verify that the username is available in the
73+
// image as early as here like there is for Linux. Later on in the stack hcsshim
74+
// will handle the behavior of erroring out if the user isn't available in the image
75+
// when trying to run the init process.
76+
specOpts = append(specOpts, oci.WithUser(username))
77+
5978
for pKey, pValue := range getPassthroughAnnotations(config.Annotations,
6079
runtimePodAnnotations) {
6180
specOpts = append(specOpts, customopts.WithAnnotation(pKey, pValue))

pkg/cri/server/sandbox_run_windows_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func getRunPodSandboxTestData() (*runtime.PodSandboxConfig, *imagespec.ImageConf
5353
Entrypoint: []string{"/pause"},
5454
Cmd: []string{"forever"},
5555
WorkingDir: "/workspace",
56+
User: "test-image-user",
5657
}
5758
specCheck := func(t *testing.T, id string, spec *runtimespec.Spec) {
5859
assert.Equal(t, "test-hostname", spec.Hostname)
@@ -62,6 +63,13 @@ func getRunPodSandboxTestData() (*runtime.PodSandboxConfig, *imagespec.ImageConf
6263
assert.Equal(t, "/workspace", spec.Process.Cwd)
6364
assert.EqualValues(t, *spec.Windows.Resources.CPU.Shares, opts.DefaultSandboxCPUshares)
6465

66+
// Also checks if override of the image configs user is behaving.
67+
t.Logf("Check username")
68+
assert.Contains(t, spec.Process.User.Username, "test-user")
69+
70+
t.Logf("Check credential spec")
71+
assert.Contains(t, spec.Windows.CredentialSpec, "{\"test\": \"spec\"}")
72+
6573
t.Logf("Check PodSandbox annotations")
6674
assert.Contains(t, spec.Annotations, annotations.SandboxID)
6775
assert.EqualValues(t, spec.Annotations[annotations.SandboxID], id)

0 commit comments

Comments
 (0)