@@ -893,3 +893,115 @@ func Test_CreateContainer_HugePageMount_LCOW(t *testing.T) {
893893 t .Fatalf ("output is supposed to contain pagesize=2M, output: %s" , output )
894894 }
895895}
896+
897+ func Test_RunContainer_ExecUser_LCOW (t * testing.T ) {
898+ requireFeatures (t , featureLCOW )
899+
900+ pullRequiredLcowImages (t , []string {imageLcowK8sPause , imageLcowCustomUser })
901+
902+ client := newTestRuntimeClient (t )
903+ ctx , cancel := context .WithCancel (context .Background ())
904+ defer cancel ()
905+
906+ sandboxRequest := getRunPodSandboxRequest (t , lcowRuntimeHandler , nil )
907+
908+ podID := runPodSandbox (t , client , ctx , sandboxRequest )
909+ defer removePodSandbox (t , client , ctx , podID )
910+ defer stopPodSandbox (t , client , ctx , podID )
911+
912+ cmd := []string {"sh" , "-c" , "while true; do sleep 1; done" }
913+ request := & runtime.CreateContainerRequest {
914+ PodSandboxId : podID ,
915+ Config : & runtime.ContainerConfig {
916+ Metadata : & runtime.ContainerMetadata {
917+ Name : t .Name () + "-Container" ,
918+ },
919+ Image : & runtime.ImageSpec {
920+ Image : imageLcowCustomUser ,
921+ },
922+ Command : cmd ,
923+ },
924+ SandboxConfig : sandboxRequest .Config ,
925+ }
926+
927+ containerID := createContainer (t , client , ctx , request )
928+ defer removeContainer (t , client , ctx , containerID )
929+ startContainer (t , client , ctx , containerID )
930+ defer stopContainer (t , client , ctx , containerID )
931+
932+ // The `imageLcowCustomUser` image has a user created in the image named test that is set to run the init process as. This tests that
933+ // any execed processes will honor the user set for the container also.
934+ cmd = []string {"whoami" }
935+ containerExecReq := & runtime.ExecSyncRequest {
936+ ContainerId : containerID ,
937+ Cmd : cmd ,
938+ Timeout : 20 ,
939+ }
940+ r := execSync (t , client , ctx , containerExecReq )
941+ if r .ExitCode != 0 {
942+ t .Fatalf ("failed with exit code %d: %s" , r .ExitCode , string (r .Stderr ))
943+ }
944+
945+ if ! strings .Contains (string (r .Stdout ), "test" ) {
946+ t .Fatalf ("expected user for exec to be 'test', got %q" , string (r .Stdout ))
947+ }
948+ }
949+
950+ func Test_RunContainer_ExecUser_Root_LCOW (t * testing.T ) {
951+ requireFeatures (t , featureLCOW )
952+
953+ pullRequiredLcowImages (t , []string {imageLcowK8sPause , imageLcowCustomUser })
954+
955+ client := newTestRuntimeClient (t )
956+ ctx , cancel := context .WithCancel (context .Background ())
957+ defer cancel ()
958+
959+ sandboxRequest := getRunPodSandboxRequest (t , lcowRuntimeHandler , nil )
960+
961+ podID := runPodSandbox (t , client , ctx , sandboxRequest )
962+ defer removePodSandbox (t , client , ctx , podID )
963+ defer stopPodSandbox (t , client , ctx , podID )
964+
965+ // Overide what user to run the container as and see if the exec also runs as root now.
966+ cmd := []string {"sh" , "-c" , "while true; do sleep 1; done" }
967+ request := & runtime.CreateContainerRequest {
968+ PodSandboxId : podID ,
969+ Config : & runtime.ContainerConfig {
970+ Metadata : & runtime.ContainerMetadata {
971+ Name : t .Name () + "-Container" ,
972+ },
973+ Image : & runtime.ImageSpec {
974+ Image : imageLcowCustomUser ,
975+ },
976+ Command : cmd ,
977+ Linux : & runtime.LinuxContainerConfig {
978+ SecurityContext : & runtime.LinuxContainerSecurityContext {
979+ RunAsUsername : "root" ,
980+ },
981+ },
982+ },
983+ SandboxConfig : sandboxRequest .Config ,
984+ }
985+
986+ containerID := createContainer (t , client , ctx , request )
987+ defer removeContainer (t , client , ctx , containerID )
988+ startContainer (t , client , ctx , containerID )
989+ defer stopContainer (t , client , ctx , containerID )
990+
991+ // The `imageLcowCustomUser` image has a user created in the image named test that is set to run the init process as. This tests that
992+ // any execed processes will honor the user set for the container also.
993+ cmd = []string {"whoami" }
994+ containerExecReq := & runtime.ExecSyncRequest {
995+ ContainerId : containerID ,
996+ Cmd : cmd ,
997+ Timeout : 20 ,
998+ }
999+ r := execSync (t , client , ctx , containerExecReq )
1000+ if r .ExitCode != 0 {
1001+ t .Fatalf ("failed with exit code %d: %s" , r .ExitCode , string (r .Stderr ))
1002+ }
1003+
1004+ if ! strings .Contains (string (r .Stdout ), "root" ) {
1005+ t .Fatalf ("expected user for exec to be 'root', got %q" , string (r .Stdout ))
1006+ }
1007+ }
0 commit comments