Skip to content

Commit 26dc5b9

Browse files
authored
Merge pull request #1505 from dcantah/windows-cred-spec
Add GMSA credential spec passing
2 parents ae8200b + 9620b2e commit 26dc5b9

3 files changed

Lines changed: 42 additions & 7 deletions

File tree

pkg/containerd/opts/spec_windows.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,15 @@ func WithWindowsDefaultSandboxShares(ctx context.Context, client oci.Client, c *
188188
s.Windows.Resources.CPU.Shares = &i
189189
return nil
190190
}
191+
192+
// WithWindowsCredentialSpec assigns `credentialSpec` to the
193+
// `runtime.Spec.Windows.CredentialSpec` field.
194+
func WithWindowsCredentialSpec(credentialSpec string) oci.SpecOpts {
195+
return func(ctx context.Context, client oci.Client, c *containers.Container, s *runtimespec.Spec) error {
196+
if s.Windows == nil {
197+
s.Windows = &runtimespec.Windows{}
198+
}
199+
s.Windows.CredentialSpec = credentialSpec
200+
return nil
201+
}
202+
}

pkg/server/container_create_windows.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,30 @@ func (c *criService) containerSpec(id string, sandboxID string, sandboxPid uint3
6868

6969
specOpts = append(specOpts, customopts.WithWindowsMounts(c.os, config, extraMounts))
7070

71-
specOpts = append(specOpts, customopts.WithWindowsResources(config.GetWindows().GetResources()))
72-
73-
username := config.GetWindows().GetSecurityContext().GetRunAsUsername()
74-
if username != "" {
75-
specOpts = append(specOpts, oci.WithUser(username))
71+
// Start with the image config user and override below if RunAsUsername is not "".
72+
username := imageConfig.User
73+
74+
windowsConfig := config.GetWindows()
75+
if windowsConfig != nil {
76+
specOpts = append(specOpts, customopts.WithWindowsResources(windowsConfig.GetResources()))
77+
securityCtx := windowsConfig.GetSecurityContext()
78+
if securityCtx != nil {
79+
runAsUser := securityCtx.GetRunAsUsername()
80+
if runAsUser != "" {
81+
username = runAsUser
82+
}
83+
cs := securityCtx.GetCredentialSpec()
84+
if cs != "" {
85+
specOpts = append(specOpts, customopts.WithWindowsCredentialSpec(cs))
86+
}
87+
}
7688
}
77-
// TODO(windows): Add CredentialSpec support.
89+
90+
// There really isn't a good Windows way to verify that the username is available in the
91+
// image as early as here like there is for Linux. Later on in the stack hcsshim
92+
// will handle the behavior of erroring out if the user isn't available in the image
93+
// when trying to run the init process.
94+
specOpts = append(specOpts, oci.WithUser(username))
7895

7996
for pKey, pValue := range getPassthroughAnnotations(sandboxConfig.Annotations,
8097
ociRuntime.PodAnnotations) {

pkg/server/container_create_windows_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ func getCreateContainerTestData() (*runtime.ContainerConfig, *runtime.PodSandbox
7272
MemoryLimitInBytes: 400,
7373
},
7474
SecurityContext: &runtime.WindowsContainerSecurityContext{
75-
RunAsUsername: "test-user",
75+
RunAsUsername: "test-user",
76+
CredentialSpec: "{\"test\": \"spec\"}",
7677
},
7778
},
7879
}
@@ -91,6 +92,7 @@ func getCreateContainerTestData() (*runtime.ContainerConfig, *runtime.PodSandbox
9192
Entrypoint: []string{"/entrypoint"},
9293
Cmd: []string{"cmd"},
9394
WorkingDir: "/workspace",
95+
User: "ContainerUser",
9496
}
9597
specCheck := func(t *testing.T, id string, sandboxID string, sandboxPid uint32, spec *runtimespec.Spec) {
9698
assert.Nil(t, spec.Root)
@@ -111,9 +113,13 @@ func getCreateContainerTestData() (*runtime.ContainerConfig, *runtime.PodSandbox
111113
assert.EqualValues(t, *spec.Windows.Resources.CPU.Maximum, 300)
112114
assert.EqualValues(t, *spec.Windows.Resources.Memory.Limit, 400)
113115

116+
// Also checks if override of the image configs user is behaving.
114117
t.Logf("Check username")
115118
assert.Contains(t, spec.Process.User.Username, "test-user")
116119

120+
t.Logf("Check credential spec")
121+
assert.Contains(t, spec.Windows.CredentialSpec, "{\"test\": \"spec\"}")
122+
117123
t.Logf("Check PodSandbox annotations")
118124
assert.Contains(t, spec.Annotations, annotations.SandboxID)
119125
assert.EqualValues(t, spec.Annotations[annotations.SandboxID], sandboxID)

0 commit comments

Comments
 (0)