-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
144 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package admission | ||
|
||
import ( | ||
kapi "k8s.io/kubernetes/pkg/api" | ||
) | ||
|
||
// ByRestrictions is a helper to sort SCCs based on priority. If priorities are equal | ||
// a string compare of the name is used. | ||
type ByPriority []*kapi.SecurityContextConstraints | ||
|
||
func (s ByPriority) Len() int { | ||
return len(s) | ||
} | ||
func (s ByPriority) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | ||
func (s ByPriority) Less(i, j int) bool { | ||
iSCC := s[i] | ||
jSCC := s[j] | ||
|
||
// a higher priority is considered "less" so that it moves to the front of the line | ||
if iSCC.Priority > jSCC.Priority { | ||
return true | ||
} | ||
|
||
if iSCC.Priority < jSCC.Priority { | ||
return false | ||
} | ||
|
||
// they are equal, let's try point values | ||
iRestrictionScore := pointValue(iSCC) | ||
jRestrictionScore := pointValue(jSCC) | ||
|
||
// a lower restriction score is considered "less" so that it moves to the front of the line | ||
// (the greater the score, the more lax the SCC is) | ||
if iRestrictionScore < jRestrictionScore { | ||
return true | ||
} | ||
|
||
if iRestrictionScore > jRestrictionScore { | ||
return false | ||
} | ||
|
||
// they are still equal, sort by name | ||
return iSCC.Name < jSCC.Name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package admission | ||
|
||
import ( | ||
"sort" | ||
"testing" | ||
|
||
kapi "k8s.io/kubernetes/pkg/api" | ||
) | ||
|
||
func TestByPriority(t *testing.T) { | ||
sccs := []*kapi.SecurityContextConstraints{testSCC("one", 1), testSCC("two", 2), testSCC("three", 3), testSCC("negative", -1), testSCC("super", 100)} | ||
expected := []string{"super", "three", "two", "one", "negative"} | ||
|
||
sort.Sort(ByPriority(sccs)) | ||
|
||
for i, scc := range sccs { | ||
if scc.Name != expected[i] { | ||
t.Errorf("sort by priority found %s at element %d but expected %s", scc.Name, i, expected[i]) | ||
} | ||
} | ||
} | ||
|
||
func TestByPrioritiesScore(t *testing.T) { | ||
privilegedSCC := testSCC("privileged", 1) | ||
privilegedSCC.AllowPrivilegedContainer = true | ||
|
||
nonPriviledSCC := testSCC("nonprivileged", 1) | ||
|
||
hostDirSCC := testSCC("hostdir", 1) | ||
hostDirSCC.AllowHostDirVolumePlugin = true | ||
|
||
sccs := []*kapi.SecurityContextConstraints{nonPriviledSCC, privilegedSCC, hostDirSCC} | ||
// with equal priorities expect that the SCCs will be sorted with hold behavior based on their score, | ||
// most restrictive first | ||
expected := []string{"nonprivileged", "hostdir", "privileged"} | ||
|
||
sort.Sort(ByPriority(sccs)) | ||
|
||
for i, scc := range sccs { | ||
if scc.Name != expected[i] { | ||
t.Errorf("sort by score found %s at element %d but expected %s", scc.Name, i, expected[i]) | ||
} | ||
} | ||
} | ||
|
||
func TestByPrioritiesName(t *testing.T) { | ||
sccs := []*kapi.SecurityContextConstraints{testSCC("e", 1), testSCC("d", 1), testSCC("a", 1), testSCC("c", 1), testSCC("b", 1)} | ||
// expect that with equal priorities AND an equal point value that SCCs are sorted by name | ||
expected := []string{"a", "b", "c", "d", "e"} | ||
|
||
sort.Sort(ByPriority(sccs)) | ||
|
||
for i, scc := range sccs { | ||
if scc.Name != expected[i] { | ||
t.Errorf("sort by priority found %s at element %d but expected %s", scc.Name, i, expected[i]) | ||
} | ||
} | ||
} | ||
|
||
func TestByPrioritiesMixedSCCs(t *testing.T) { | ||
privilegedSCC := testSCC("privileged", 1) | ||
privilegedSCC.AllowPrivilegedContainer = true | ||
|
||
nonPriviledSCC := testSCC("nonprivileged", 1) | ||
|
||
sccs := []*kapi.SecurityContextConstraints{testSCC("priorityB", 5), testSCC("priorityA", 5), testSCC("super", 100), privilegedSCC, nonPriviledSCC} | ||
// highest priority first, equal priority and equal score sorted by name, equal priority and non-equal score sorted most restrictive to least. | ||
expected := []string{"super", "priorityA", "priorityB", "nonprivileged", "privileged"} | ||
|
||
sort.Sort(ByPriority(sccs)) | ||
|
||
for i, scc := range sccs { | ||
if scc.Name != expected[i] { | ||
t.Errorf("sort by priority found %s at element %d but expected %s", scc.Name, i, expected[i]) | ||
} | ||
} | ||
} | ||
|
||
func testSCC(name string, priority int) *kapi.SecurityContextConstraints { | ||
return &kapi.SecurityContextConstraints{ | ||
ObjectMeta: kapi.ObjectMeta{ | ||
Name: name, | ||
}, | ||
Priority: priority, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters