Skip to content

Commit

Permalink
UPSTREAM: 93475: Strip unnecessary security contexts on Windows
Browse files Browse the repository at this point in the history
As of now, the kubelet is passing the security context to container runtime even
if the security context has invalid options for a particular OS. As a result,
the pod fails to come up on the node. This error is particularly pronounced on
the Windows nodes where kubelet is allowing Linux specific options like SELinux,
RunAsUser etc where as in [documentation](https://kubernetes.io/docs/setup/production-environment/windows/intro-windows-in-kubernetes/#v1-container),
we clearly state they are not supported. This PR ensures that the kubelet strips
the security contexts of the pod, if they don't make sense on the Windows OS.
  • Loading branch information
ravisantoshgudimetla committed Jul 30, 2020
1 parent 53f1b9d commit 84b703d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
25 changes: 23 additions & 2 deletions pkg/kubelet/kuberuntime/kuberuntime_container_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ limitations under the License.
package kuberuntime

import (
"fmt"
"runtime"

"k8s.io/api/core/v1"
Expand Down Expand Up @@ -125,9 +124,31 @@ func (m *kubeGenericRuntimeManager) generateWindowsContainerConfig(container *v1

// setup security context
effectiveSc := securitycontext.DetermineEffectiveSecurityContext(pod, container)

// Strip down all the unnecessary options on the Windows
if effectiveSc.SELinuxOptions != nil {
effectiveSc.SELinuxOptions = nil
}

if effectiveSc.AllowPrivilegeEscalation != nil {
effectiveSc.AllowPrivilegeEscalation = nil
}

if effectiveSc.Capabilities != nil {
effectiveSc.Capabilities = nil
}

if effectiveSc.Privileged != nil {
effectiveSc.Privileged = nil
}

if effectiveSc.ProcMount != nil {
effectiveSc.ProcMount = nil
}

// RunAsUser only supports int64 from Kubernetes API, but Windows containers only support username.
if effectiveSc.RunAsUser != nil {
return nil, fmt.Errorf("run as uid (%d) is not supported on Windows", *effectiveSc.RunAsUser)
effectiveSc.RunAsUser = nil
}
if username != "" {
wc.SecurityContext.RunAsUsername = username
Expand Down
12 changes: 8 additions & 4 deletions pkg/kubelet/kuberuntime/kuberuntime_sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"net"
"net/url"
"runtime"
"sort"

v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -143,6 +144,9 @@ func (m *kubeGenericRuntimeManager) generatePodSandboxConfig(pod *v1.Pod, attemp
}

// generatePodSandboxLinuxConfig generates LinuxPodSandboxConfig from v1.Pod.
// We've to call PodSandboxLinuxConfig always irrespective of the underlying OS as securityContext is not part of
// podSandboxConfig. It is currently part of LinuxPodSandboxConfig. In future, if we have securityContext pulled out
// in podSandboxConfig we should be able to use it.
func (m *kubeGenericRuntimeManager) generatePodSandboxLinuxConfig(pod *v1.Pod) (*runtimeapi.LinuxPodSandboxConfig, error) {
cgroupParent := m.runtimeHelper.GetPodCgroupParent(pod)
lc := &runtimeapi.LinuxPodSandboxConfig{
Expand All @@ -169,15 +173,15 @@ func (m *kubeGenericRuntimeManager) generatePodSandboxLinuxConfig(pod *v1.Pod) (

if pod.Spec.SecurityContext != nil {
sc := pod.Spec.SecurityContext
if sc.RunAsUser != nil {
if sc.RunAsUser != nil && runtime.GOOS != "windows" {
lc.SecurityContext.RunAsUser = &runtimeapi.Int64Value{Value: int64(*sc.RunAsUser)}
}
if sc.RunAsGroup != nil {
if sc.RunAsGroup != nil && runtime.GOOS != "windows" {
lc.SecurityContext.RunAsGroup = &runtimeapi.Int64Value{Value: int64(*sc.RunAsGroup)}
}
lc.SecurityContext.NamespaceOptions = namespacesForPod(pod)

if sc.FSGroup != nil {
if sc.FSGroup != nil && runtime.GOOS != "windows" {
lc.SecurityContext.SupplementalGroups = append(lc.SecurityContext.SupplementalGroups, int64(*sc.FSGroup))
}
if groups := m.runtimeHelper.GetExtraSupplementalGroupsForPod(pod); len(groups) > 0 {
Expand All @@ -188,7 +192,7 @@ func (m *kubeGenericRuntimeManager) generatePodSandboxLinuxConfig(pod *v1.Pod) (
lc.SecurityContext.SupplementalGroups = append(lc.SecurityContext.SupplementalGroups, int64(sg))
}
}
if sc.SELinuxOptions != nil {
if sc.SELinuxOptions != nil && runtime.GOOS != "windows" {
lc.SecurityContext.SelinuxOptions = &runtimeapi.SELinuxOption{
User: sc.SELinuxOptions.User,
Role: sc.SELinuxOptions.Role,
Expand Down
21 changes: 20 additions & 1 deletion test/e2e/windows/security_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

const runAsUserNameContainerName = "run-as-username-container"

var _ = SIGDescribe("[Feature:Windows] SecurityContext RunAsUserName", func() {
var _ = SIGDescribe("[Feature:Windows] SecurityContext", func() {
f := framework.NewDefaultFramework("windows-run-as-username")

ginkgo.It("should be able create pods and run containers with a given username", func() {
Expand Down Expand Up @@ -71,6 +71,24 @@ var _ = SIGDescribe("[Feature:Windows] SecurityContext RunAsUserName", func() {
f.TestContainerOutput("check overridden username", pod, 0, []string{"ContainerUser"})
f.TestContainerOutput("check pod SecurityContext username", pod, 1, []string{"ContainerAdministrator"})
})
ginkgo.It("should ignore Linux Specific SecurityContext if set", func() {
ginkgo.By("Creating a pod with SELinux options")

windowsPodWithSELinux := createTestPod(f, "mcr.microsoft.com/powershell:lts-nanoserver-1809", windowsOS)
windowsPodWithSELinux.Spec.Containers[0].Args = []string{"test-webserver-with-selinux"}
windowsPodWithSELinux.Spec.SecurityContext = &v1.PodSecurityContext{}
containerUserName := "ContainerAdministrator"
windowsPodWithSELinux.Spec.SecurityContext.SELinuxOptions = &v1.SELinuxOptions{Level: "s0:c24,c9"}
windowsPodWithSELinux.Spec.Containers[0].SecurityContext = &v1.SecurityContext{
WindowsOptions: &v1.WindowsSecurityContextOptions{RunAsUserName: &containerUserName}}
windowsPodWithSELinux.Spec.Tolerations = []v1.Toleration{{Key: "os", Value: "Windows"}}
windowsPodWithSELinux, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.TODO(),
windowsPodWithSELinux, metav1.CreateOptions{})
framework.ExpectNoError(err)
framework.Logf("Created pod %v", windowsPodWithSELinux)
framework.ExpectNoError(e2epod.WaitForPodNameRunningInNamespace(f.ClientSet, windowsPodWithSELinux.Name,
f.Namespace.Name), "failed to wait for pod %s to be running", windowsPodWithSELinux.Name)
})
})

func runAsUserNamePod(username *string) *v1.Pod {
Expand All @@ -80,6 +98,7 @@ func runAsUserNamePod(username *string) *v1.Pod {
Name: podName,
},
Spec: v1.PodSpec{
NodeSelector: map[string]string{"kubernetes.io/os": "windows"},
Containers: []v1.Container{
{
Name: runAsUserNameContainerName,
Expand Down

0 comments on commit 84b703d

Please sign in to comment.