@@ -1326,6 +1326,90 @@ func TestGenerateUserString(t *testing.T) {
13261326 }
13271327}
13281328
1329+ func TestProcessUser (t * testing.T ) {
1330+ testID := "test-id"
1331+ testSandboxID := "sandbox-id"
1332+ testContainerName := "container-name"
1333+ testPid := uint32 (1234 )
1334+ ociRuntime := config.Runtime {}
1335+ c := newTestCRIService ()
1336+ testContainer := & containers.Container {ID : "64ddfe361f0099f8d59075398feeb3dcb3863b6851df7b946744755066c03e9d" }
1337+ ctx := context .Background ()
1338+
1339+ etcPasswd := `
1340+ root:x:0:0:root:/root:/bin/sh
1341+ alice:x:1000:1000:alice:/home/alice:/bin/sh
1342+ ` // #nosec G101
1343+ etcGroup := `
1344+ root:x:0
1345+ alice:x:1000:
1346+ additional-group-for-alice:x:11111:alice
1347+ additional-group-for-root:x:22222:root
1348+ `
1349+ tempRootDir , err := os .MkdirTemp ("" , "TestContainerUser-" )
1350+ require .NoError (t , err )
1351+ if tempRootDir != "" {
1352+ defer os .RemoveAll (tempRootDir )
1353+ }
1354+ require .NoError (t ,
1355+ os .MkdirAll (filepath .Join (tempRootDir , "etc" ), 0755 ),
1356+ )
1357+ require .NoError (t ,
1358+ os .WriteFile (filepath .Join (tempRootDir , "etc" , "passwd" ), []byte (etcPasswd ), 0644 ),
1359+ )
1360+ require .NoError (t ,
1361+ os .WriteFile (filepath .Join (tempRootDir , "etc" , "group" ), []byte (etcGroup ), 0644 ),
1362+ )
1363+
1364+ for desc , test := range map [string ]struct {
1365+ imageConfigUser string
1366+ securityContext * runtime.LinuxContainerSecurityContext
1367+ expected runtimespec.User
1368+ }{
1369+ "Only SecurityContext was set, SecurityContext defines User" : {
1370+ securityContext : & runtime.LinuxContainerSecurityContext {
1371+ RunAsUser : & runtime.Int64Value {Value : 1000 },
1372+ RunAsGroup : & runtime.Int64Value {Value : 2000 },
1373+ SupplementalGroups : []int64 {3333 },
1374+ },
1375+ expected : runtimespec.User {UID : 1000 , GID : 2000 , AdditionalGids : []uint32 {2000 , 3333 , 11111 }},
1376+ },
1377+ "Only imageConfig.User was set, imageConfig.User defines User" : {
1378+ imageConfigUser : "1000" ,
1379+ securityContext : nil ,
1380+ expected : runtimespec.User {UID : 1000 , GID : 1000 , AdditionalGids : []uint32 {1000 , 11111 }},
1381+ },
1382+ "Both SecurityContext and ImageConfig.User was set, SecurityContext defines User" : {
1383+ imageConfigUser : "0" ,
1384+ securityContext : & runtime.LinuxContainerSecurityContext {
1385+ RunAsUser : & runtime.Int64Value {Value : 1000 },
1386+ RunAsGroup : & runtime.Int64Value {Value : 2000 },
1387+ SupplementalGroups : []int64 {3333 },
1388+ },
1389+ expected : runtimespec.User {UID : 1000 , GID : 2000 , AdditionalGids : []uint32 {2000 , 3333 , 11111 }},
1390+ },
1391+ "No SecurityContext nor ImageConfig.User were set, runtime default defines User" : {
1392+ expected : runtimespec.User {UID : 0 , GID : 0 , AdditionalGids : []uint32 {0 , 22222 }},
1393+ },
1394+ } {
1395+ t .Run (desc , func (t * testing.T ) {
1396+ containerConfig , sandboxConfig , imageConfig , _ := getCreateContainerTestData ()
1397+ containerConfig .Linux .SecurityContext = test .securityContext
1398+ imageConfig .User = test .imageConfigUser
1399+
1400+ spec , err := c .buildContainerSpec (currentPlatform , testID , testSandboxID , testPid , "" , testContainerName , testImageName , containerConfig , sandboxConfig , imageConfig , nil , ociRuntime )
1401+ require .NoError (t , err )
1402+
1403+ spec .Root .Path = tempRootDir // simulating /etc/{passwd, group}
1404+ opts , err := c .containerSpecOpts (containerConfig , imageConfig )
1405+ require .NoError (t , err )
1406+ oci .ApplyOpts (ctx , nil , testContainer , spec , opts ... )
1407+
1408+ require .Equal (t , test .expected , spec .Process .User )
1409+ })
1410+ }
1411+ }
1412+
13291413func TestNonRootUserAndDevices (t * testing.T ) {
13301414 testPid := uint32 (1234 )
13311415 c := newTestCRIService ()
0 commit comments