k8s.io/kubernetes@v1.29.3/pkg/kubelet/kuberuntime/security_context_windows.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  /*
     5  Copyright 2020 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package kuberuntime
    21  
    22  import (
    23  	"fmt"
    24  	"k8s.io/api/core/v1"
    25  	"k8s.io/klog/v2"
    26  	"k8s.io/kubernetes/pkg/kubelet/util/format"
    27  	"k8s.io/kubernetes/pkg/securitycontext"
    28  	"strings"
    29  )
    30  
    31  var (
    32  	windowsRootUserName = "ContainerAdministrator"
    33  )
    34  
    35  // verifyRunAsNonRoot verifies RunAsNonRoot on windows.
    36  // https://github.com/kubernetes/enhancements/tree/master/keps/sig-windows/116-windows-node-support#v1container
    37  // Windows does not have a root user, we cannot judge the root identity of the windows container by the way to judge the root(uid=0) of the linux container.
    38  // According to the discussion of sig-windows, at present, we assume that ContainerAdministrator is the windows container root user,
    39  // and then optimize this logic according to the best time.
    40  // https://docs.google.com/document/d/1Tjxzjjuy4SQsFSUVXZbvqVb64hjNAG5CQX8bK7Yda9w
    41  // note: usernames on Windows are NOT case sensitive!
    42  func verifyRunAsNonRoot(pod *v1.Pod, container *v1.Container, uid *int64, username string) error {
    43  	effectiveSc := securitycontext.DetermineEffectiveSecurityContext(pod, container)
    44  	// If the option is not set, or if running as root is allowed, return nil.
    45  	if effectiveSc == nil || effectiveSc.RunAsNonRoot == nil || !*effectiveSc.RunAsNonRoot {
    46  		return nil
    47  	}
    48  	if effectiveSc.RunAsUser != nil {
    49  		klog.InfoS("Windows container does not support SecurityContext.RunAsUser, please use SecurityContext.WindowsOptions",
    50  			"pod", klog.KObj(pod), "containerName", container.Name)
    51  	}
    52  	if effectiveSc.SELinuxOptions != nil {
    53  		klog.InfoS("Windows container does not support SecurityContext.SELinuxOptions, please use SecurityContext.WindowsOptions",
    54  			"pod", klog.KObj(pod), "containerName", container.Name)
    55  	}
    56  	if effectiveSc.RunAsGroup != nil {
    57  		klog.InfoS("Windows container does not support SecurityContext.RunAsGroup", "pod", klog.KObj(pod), "containerName", container.Name)
    58  	}
    59  	// Verify that if runAsUserName is set for the pod and/or container that it is not set to 'ContainerAdministrator'
    60  	if effectiveSc.WindowsOptions != nil {
    61  		if effectiveSc.WindowsOptions.RunAsUserName != nil {
    62  			if strings.EqualFold(*effectiveSc.WindowsOptions.RunAsUserName, windowsRootUserName) {
    63  				return fmt.Errorf("container's runAsUserName (%s) which will be regarded as root identity and will break non-root policy (pod: %q, container: %s)", *effectiveSc.WindowsOptions.RunAsUserName, format.Pod(pod), container.Name)
    64  			}
    65  			return nil
    66  		}
    67  	}
    68  	// Verify that if runAsUserName is NOT set for the pod and/or container that the default user for the container image is not set to 'ContainerAdministrator'
    69  	if len(username) > 0 && strings.EqualFold(username, windowsRootUserName) {
    70  		return fmt.Errorf("container's runAsUser (%s) which will be regarded as root identity and will break non-root policy (pod: %q, container: %s)", username, format.Pod(pod), container.Name)
    71  	}
    72  	return nil
    73  }