@@ -12,6 +12,7 @@ import (
1212 v1 "github.com/opencontainers/image-spec/specs-go/v1"
1313 rspec "github.com/opencontainers/runtime-spec/specs-go"
1414 validate "github.com/opencontainers/runtime-tools/validate/capabilities"
15+ "github.com/syndtr/gocapability/capability"
1516 types "k8s.io/cri-api/pkg/apis/runtime/v1"
1617 kubeletTypes "k8s.io/kubelet/pkg/types"
1718
@@ -23,6 +24,7 @@ import (
2324 "github.com/cri-o/cri-o/internal/storage"
2425 "github.com/cri-o/cri-o/internal/storage/references"
2526 "github.com/cri-o/cri-o/pkg/annotations"
27+ pkgConfig "github.com/cri-o/cri-o/pkg/config"
2628)
2729
2830var _ = t .Describe ("Container" , func () {
@@ -633,4 +635,213 @@ var _ = t.Describe("Container", func() {
633635 Expect (sut .Spec ().Config .Process .Capabilities .Inheritable ).To (HaveLen (1 ))
634636 })
635637 })
638+ t .Describe ("SpecSetPrivileges" , func () {
639+ It ("Non privileged container should get selected capabilities" , func () {
640+ // Given
641+ sc := & types.LinuxContainerSecurityContext {
642+ Capabilities : & types.Capability {
643+ AddCapabilities : []string {"CHOWN" },
644+ DropCapabilities : nil ,
645+ },
646+ }
647+ cfg := & pkgConfig.Config {}
648+
649+ // When
650+ Expect (sut .SpecSetPrivileges (context .Background (), sc , cfg )).To (Succeed ())
651+
652+ // Then
653+ Expect (sut .Spec ().Config .Process .Capabilities .Bounding ).To (HaveLen (len (cfg .DefaultCapabilities ) + 1 ))
654+ Expect (sut .Spec ().Config .Process .Capabilities .Effective ).To (HaveLen (len (cfg .DefaultCapabilities ) + 1 ))
655+ Expect (sut .Spec ().Config .Process .Capabilities .Permitted ).To (HaveLen (len (cfg .DefaultCapabilities ) + 1 ))
656+ Expect (sut .Spec ().Config .Process .Capabilities .Inheritable ).To (BeEmpty ())
657+ Expect (sut .Spec ().Config .Process .Capabilities .Ambient ).To (BeEmpty ())
658+ })
659+ It ("Privileged container gets all capabilities" , func () {
660+ // Given
661+ sc := & types.LinuxContainerSecurityContext {}
662+ cfg := & pkgConfig.Config {}
663+ config := & types.ContainerConfig {
664+ Metadata : & types.ContainerMetadata {Name : "name" },
665+ Linux : & types.LinuxContainerConfig {
666+ SecurityContext : & types.LinuxContainerSecurityContext {
667+ Privileged : true ,
668+ },
669+ },
670+ }
671+ sboxConfig := & types.PodSandboxConfig {
672+ Linux : & types.LinuxPodSandboxConfig {
673+ SecurityContext : & types.LinuxSandboxSecurityContext {
674+ Privileged : true ,
675+ },
676+ },
677+ }
678+ expectedSize := len (capability .List ())
679+
680+ // When
681+ Expect (sut .SetConfig (config , sboxConfig )).To (Succeed ())
682+ Expect (sut .SetPrivileged ()).To (Succeed ())
683+ Expect (sut .SpecSetPrivileges (context .Background (), sc , cfg )).To (Succeed ())
684+
685+ // Then
686+ Expect (sut .Spec ().Config .Process .Capabilities .Bounding ).To (HaveLen (expectedSize ))
687+ Expect (sut .Spec ().Config .Process .Capabilities .Effective ).To (HaveLen (expectedSize ))
688+ Expect (sut .Spec ().Config .Process .Capabilities .Permitted ).To (HaveLen (expectedSize ))
689+ Expect (sut .Spec ().Config .Process .Capabilities .Inheritable ).To (HaveLen (expectedSize ))
690+ Expect (sut .Spec ().Config .Process .Capabilities .Ambient ).To (HaveLen (expectedSize ))
691+ })
692+ It ("Should set NoNewPrivs flag if set" , func () {
693+ // Given
694+ sc := & types.LinuxContainerSecurityContext {
695+ NoNewPrivs : true ,
696+ }
697+ cfg := & pkgConfig.Config {}
698+
699+ // When
700+ Expect (sut .SpecSetPrivileges (context .Background (), sc , cfg )).To (Succeed ())
701+
702+ // Then
703+ Expect (sut .Spec ().Config .Process .NoNewPrivileges ).To (BeTrue ())
704+ })
705+ It ("Should add masked paths if set" , func () {
706+ // Given
707+ sc := & types.LinuxContainerSecurityContext {
708+ MaskedPaths : []string {"path1" , "path2" },
709+ }
710+ cfg := & pkgConfig.Config {}
711+
712+ // When
713+ Expect (sut .SpecSetPrivileges (context .Background (), sc , cfg )).To (Succeed ())
714+
715+ // Then
716+ Expect (sut .Spec ().Config .Linux .MaskedPaths ).To (HaveLen (2 ))
717+ })
718+ It ("Should add readonly paths if set" , func () {
719+ // Given
720+ sc := & types.LinuxContainerSecurityContext {
721+ ReadonlyPaths : []string {"path1" , "path2" },
722+ }
723+ cfg := & pkgConfig.Config {}
724+
725+ // When
726+ Expect (sut .SpecSetPrivileges (context .Background (), sc , cfg )).To (Succeed ())
727+
728+ // Then
729+ Expect (sut .Spec ().Config .Linux .ReadonlyPaths ).To (HaveLen (2 ))
730+ })
731+ })
732+ t .Describe ("SpecSetLinuxContainerResources" , func () {
733+ It ("Sets all fields to their expected values" , func () {
734+ // Given
735+ resources := & types.LinuxContainerResources {
736+ CpuPeriod : 1 ,
737+ CpuQuota : 2 ,
738+ CpuShares : 3 ,
739+ OomScoreAdj : 4 ,
740+ CpusetCpus : "5" ,
741+ CpusetMems : "6" ,
742+ }
743+
744+ // When
745+ Expect (sut .SpecSetLinuxContainerResources (resources , 0 )).To (Succeed ())
746+
747+ // Then
748+ Expect (* sut .Spec ().Config .Linux .Resources .CPU .Period ).To (Equal (uint64 (resources .CpuPeriod )))
749+ Expect (* sut .Spec ().Config .Linux .Resources .CPU .Quota ).To (Equal (resources .CpuQuota ))
750+ Expect (* sut .Spec ().Config .Linux .Resources .CPU .Shares ).To (Equal (uint64 (resources .CpuShares )))
751+ Expect (* sut .Spec ().Config .Process .OOMScoreAdj ).To (Equal (int (resources .OomScoreAdj )))
752+ Expect (sut .Spec ().Config .Linux .Resources .CPU .Cpus ).To (Equal (resources .CpusetCpus ))
753+ Expect (sut .Spec ().Config .Linux .Resources .CPU .Mems ).To (Equal (resources .CpusetMems ))
754+ })
755+ It ("Fails to set memory limit if invalid" , func () {
756+ // Given
757+ minMemory := int64 (2048 )
758+
759+ // When
760+ resources := & types.LinuxContainerResources {
761+ MemoryLimitInBytes : 1024 , // must be >= minMemory
762+ }
763+
764+ // Then
765+ Expect (sut .SpecSetLinuxContainerResources (resources , minMemory )).NotTo (Succeed ())
766+ })
767+ It ("Fails to set memory swap limit if invalid" , func () {
768+ // Given
769+ minMemory := int64 (2048 )
770+
771+ // When
772+ resources := & types.LinuxContainerResources {
773+ MemoryLimitInBytes : 2048 ,
774+ MemorySwapLimitInBytes : 1024 , // must be >= MemoryLimitInBytes
775+ }
776+
777+ // Then
778+ Expect (sut .SpecSetLinuxContainerResources (resources , minMemory )).NotTo (Succeed ())
779+ })
780+ It ("Set memory limit to both swap and RAM when only MemoryLimit is set" , func () {
781+ // Given
782+ resources := & types.LinuxContainerResources {
783+ MemoryLimitInBytes : 4096 ,
784+ }
785+
786+ // When
787+ Expect (sut .SpecSetLinuxContainerResources (resources , 2048 )).To (Succeed ())
788+
789+ // Then
790+ Expect (* sut .Spec ().Config .Linux .Resources .Memory .Limit ).To (Equal (resources .MemoryLimitInBytes ))
791+ Expect (* sut .Spec ().Config .Linux .Resources .Memory .Swap ).To (Equal (resources .MemoryLimitInBytes ))
792+ })
793+ It ("Set memory limits appropriately when Limit and SwapLimit are set" , func () {
794+ // Given
795+ resources := & types.LinuxContainerResources {
796+ MemoryLimitInBytes : 4096 ,
797+ MemorySwapLimitInBytes : 4096 ,
798+ }
799+
800+ // When
801+ Expect (sut .SpecSetLinuxContainerResources (resources , 0 )).To (Succeed ())
802+
803+ // Then
804+ Expect (* sut .Spec ().Config .Linux .Resources .Memory .Limit ).To (Equal (resources .MemoryLimitInBytes ))
805+ Expect (* sut .Spec ().Config .Linux .Resources .Memory .Swap ).To (Equal (resources .MemorySwapLimitInBytes ))
806+ })
807+ It ("Set hugepage limits" , func () {
808+ // Given
809+ hugepageLimits := []* types.HugepageLimit {
810+ {
811+ PageSize : "1KB" ,
812+ Limit : 1024 ,
813+ },
814+ {
815+ PageSize : "2KB" ,
816+ Limit : 2048 ,
817+ },
818+ }
819+ resources := & types.LinuxContainerResources {
820+ HugepageLimits : hugepageLimits ,
821+ }
822+
823+ // When
824+ Expect (sut .SpecSetLinuxContainerResources (resources , 0 )).To (Succeed ())
825+
826+ // Then
827+ for i , pageLimit := range sut .Spec ().Config .Linux .Resources .HugepageLimits {
828+ Expect (pageLimit .Pagesize ).To (Equal (hugepageLimits [i ].PageSize ))
829+ Expect (pageLimit .Limit ).To (Equal (hugepageLimits [i ].Limit ))
830+ }
831+ })
832+ It ("Set Cgroupv2 resources" , func () {
833+ // Given
834+ resources := & types.LinuxContainerResources {
835+ Unified : make (map [string ]string , 2 ),
836+ }
837+ resources .Unified ["memory.high" ] = "8000000"
838+ resources .Unified ["memory.low" ] = "100000"
839+
840+ // When
841+ Expect (sut .SpecSetLinuxContainerResources (resources , 2048 )).To (Succeed ())
842+
843+ // Then
844+ Expect (sut .Spec ().Config .Linux .Resources .Unified ).To (HaveLen (len (resources .Unified )))
845+ })
846+ })
636847})
0 commit comments