@@ -956,11 +956,12 @@ func (g *Generator) SetupPrivileged(privileged bool) {
956956 }
957957 g .initSpecLinux ()
958958 g .initSpecProcessCapabilities ()
959- g .spec .Process .Capabilities .Bounding = finalCapList
960- g .spec .Process .Capabilities .Effective = finalCapList
961- g .spec .Process .Capabilities .Inheritable = finalCapList
962- g .spec .Process .Capabilities .Permitted = finalCapList
963- g .spec .Process .Capabilities .Ambient = finalCapList
959+ g .ClearProcessCapabilities ()
960+ g .spec .Process .Capabilities .Bounding = append (g .spec .Process .Capabilities .Bounding , finalCapList ... )
961+ g .spec .Process .Capabilities .Effective = append (g .spec .Process .Capabilities .Effective , finalCapList ... )
962+ g .spec .Process .Capabilities .Inheritable = append (g .spec .Process .Capabilities .Inheritable , finalCapList ... )
963+ g .spec .Process .Capabilities .Permitted = append (g .spec .Process .Capabilities .Permitted , finalCapList ... )
964+ g .spec .Process .Capabilities .Ambient = append (g .spec .Process .Capabilities .Ambient , finalCapList ... )
964965 g .spec .Process .SelinuxLabel = ""
965966 g .spec .Process .ApparmorProfile = ""
966967 g .spec .Linux .Seccomp = nil
@@ -988,40 +989,60 @@ func (g *Generator) AddProcessCapability(c string) error {
988989
989990 g .initSpecProcessCapabilities ()
990991
992+ var foundBounding bool
991993 for _ , cap := range g .spec .Process .Capabilities .Bounding {
992994 if strings .ToUpper (cap ) == cp {
993- return nil
995+ foundBounding = true
996+ break
994997 }
995998 }
996- g .spec .Process .Capabilities .Bounding = append (g .spec .Process .Capabilities .Bounding , cp )
999+ if ! foundBounding {
1000+ g .spec .Process .Capabilities .Bounding = append (g .spec .Process .Capabilities .Bounding , cp )
1001+ }
9971002
1003+ var foundEffective bool
9981004 for _ , cap := range g .spec .Process .Capabilities .Effective {
9991005 if strings .ToUpper (cap ) == cp {
1000- return nil
1006+ foundEffective = true
1007+ break
10011008 }
10021009 }
1003- g .spec .Process .Capabilities .Effective = append (g .spec .Process .Capabilities .Effective , cp )
1010+ if ! foundEffective {
1011+ g .spec .Process .Capabilities .Effective = append (g .spec .Process .Capabilities .Effective , cp )
1012+ }
10041013
1014+ var foundInheritable bool
10051015 for _ , cap := range g .spec .Process .Capabilities .Inheritable {
10061016 if strings .ToUpper (cap ) == cp {
1007- return nil
1017+ foundInheritable = true
1018+ break
10081019 }
10091020 }
1010- g .spec .Process .Capabilities .Inheritable = append (g .spec .Process .Capabilities .Inheritable , cp )
1021+ if ! foundInheritable {
1022+ g .spec .Process .Capabilities .Inheritable = append (g .spec .Process .Capabilities .Inheritable , cp )
1023+ }
10111024
1025+ var foundPermitted bool
10121026 for _ , cap := range g .spec .Process .Capabilities .Permitted {
10131027 if strings .ToUpper (cap ) == cp {
1014- return nil
1028+ foundPermitted = true
1029+ break
10151030 }
10161031 }
1017- g .spec .Process .Capabilities .Permitted = append (g .spec .Process .Capabilities .Permitted , cp )
1032+ if ! foundPermitted {
1033+ g .spec .Process .Capabilities .Permitted = append (g .spec .Process .Capabilities .Permitted , cp )
1034+ }
10181035
1036+ var foundAmbient bool
10191037 for _ , cap := range g .spec .Process .Capabilities .Ambient {
10201038 if strings .ToUpper (cap ) == cp {
1021- return nil
1039+ foundAmbient = true
1040+ break
10221041 }
10231042 }
1024- g .spec .Process .Capabilities .Ambient = append (g .spec .Process .Capabilities .Ambient , cp )
1043+ if ! foundAmbient {
1044+ g .spec .Process .Capabilities .Ambient = append (g .spec .Process .Capabilities .Ambient , cp )
1045+ }
10251046
10261047 return nil
10271048}
@@ -1035,33 +1056,39 @@ func (g *Generator) DropProcessCapability(c string) error {
10351056
10361057 g .initSpecProcessCapabilities ()
10371058
1059+ // we don't care about order...and this is way faster...
1060+ removeFunc := func (s []string , i int ) []string {
1061+ s [i ] = s [len (s )- 1 ]
1062+ return s [:len (s )- 1 ]
1063+ }
1064+
10381065 for i , cap := range g .spec .Process .Capabilities .Bounding {
10391066 if strings .ToUpper (cap ) == cp {
1040- g .spec .Process .Capabilities .Bounding = append (g .spec .Process .Capabilities .Bounding [: i ], g . spec . Process . Capabilities . Bounding [ i + 1 :] ... )
1067+ g .spec .Process .Capabilities .Bounding = removeFunc (g .spec .Process .Capabilities .Bounding , i )
10411068 }
10421069 }
10431070
10441071 for i , cap := range g .spec .Process .Capabilities .Effective {
10451072 if strings .ToUpper (cap ) == cp {
1046- g .spec .Process .Capabilities .Effective = append (g .spec .Process .Capabilities .Effective [: i ], g . spec . Process . Capabilities . Effective [ i + 1 :] ... )
1073+ g .spec .Process .Capabilities .Effective = removeFunc (g .spec .Process .Capabilities .Effective , i )
10471074 }
10481075 }
10491076
10501077 for i , cap := range g .spec .Process .Capabilities .Inheritable {
10511078 if strings .ToUpper (cap ) == cp {
1052- g .spec .Process .Capabilities .Inheritable = append (g .spec .Process .Capabilities .Inheritable [: i ], g . spec . Process . Capabilities . Inheritable [ i + 1 :] ... )
1079+ g .spec .Process .Capabilities .Inheritable = removeFunc (g .spec .Process .Capabilities .Inheritable , i )
10531080 }
10541081 }
10551082
10561083 for i , cap := range g .spec .Process .Capabilities .Permitted {
10571084 if strings .ToUpper (cap ) == cp {
1058- g .spec .Process .Capabilities .Permitted = append (g .spec .Process .Capabilities .Permitted [: i ], g . spec . Process . Capabilities . Permitted [ i + 1 :] ... )
1085+ g .spec .Process .Capabilities .Permitted = removeFunc (g .spec .Process .Capabilities .Permitted , i )
10591086 }
10601087 }
10611088
10621089 for i , cap := range g .spec .Process .Capabilities .Ambient {
10631090 if strings .ToUpper (cap ) == cp {
1064- g .spec .Process .Capabilities .Ambient = append (g .spec .Process .Capabilities .Ambient [: i ], g . spec . Process . Capabilities . Ambient [ i + 1 :] ... )
1091+ g .spec .Process .Capabilities .Ambient = removeFunc (g .spec .Process .Capabilities .Ambient , i )
10651092 }
10661093 }
10671094
0 commit comments