|
| 1 | +package pod |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + corev1 "k8s.io/api/core/v1" |
| 7 | +) |
| 8 | + |
| 9 | +func TestRestrictiveSecurityContext(t *testing.T) { |
| 10 | + var uid int64 = 42457 |
| 11 | + sc := RestrictiveSecurityContext(uid) |
| 12 | + |
| 13 | + if sc.RunAsUser == nil || *sc.RunAsUser != uid { |
| 14 | + t.Errorf("expected RunAsUser %d, got %v", uid, sc.RunAsUser) |
| 15 | + } |
| 16 | + if sc.RunAsGroup == nil || *sc.RunAsGroup != uid { |
| 17 | + t.Errorf("expected RunAsGroup %d, got %v", uid, sc.RunAsGroup) |
| 18 | + } |
| 19 | + if sc.RunAsNonRoot == nil || !*sc.RunAsNonRoot { |
| 20 | + t.Error("expected RunAsNonRoot true") |
| 21 | + } |
| 22 | + if sc.AllowPrivilegeEscalation == nil || *sc.AllowPrivilegeEscalation { |
| 23 | + t.Error("expected AllowPrivilegeEscalation false") |
| 24 | + } |
| 25 | + if sc.Capabilities == nil || len(sc.Capabilities.Drop) != 1 || sc.Capabilities.Drop[0] != "ALL" { |
| 26 | + t.Errorf("expected Capabilities.Drop [ALL], got %v", sc.Capabilities) |
| 27 | + } |
| 28 | + if sc.SeccompProfile == nil || sc.SeccompProfile.Type != corev1.SeccompProfileTypeRuntimeDefault { |
| 29 | + t.Errorf("expected SeccompProfile RuntimeDefault, got %v", sc.SeccompProfile) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestRestrictiveSecurityContextWithGID(t *testing.T) { |
| 34 | + var uid int64 = 42415 |
| 35 | + var gid int64 = 42416 |
| 36 | + sc := RestrictiveSecurityContextWithGID(uid, gid) |
| 37 | + |
| 38 | + if sc.RunAsUser == nil || *sc.RunAsUser != uid { |
| 39 | + t.Errorf("expected RunAsUser %d, got %v", uid, sc.RunAsUser) |
| 40 | + } |
| 41 | + if sc.RunAsGroup == nil || *sc.RunAsGroup != gid { |
| 42 | + t.Errorf("expected RunAsGroup %d, got %v", gid, sc.RunAsGroup) |
| 43 | + } |
| 44 | + if sc.RunAsNonRoot == nil || !*sc.RunAsNonRoot { |
| 45 | + t.Error("expected RunAsNonRoot true") |
| 46 | + } |
| 47 | + if sc.AllowPrivilegeEscalation == nil || *sc.AllowPrivilegeEscalation { |
| 48 | + t.Error("expected AllowPrivilegeEscalation false") |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestRestrictiveSecurityContextNoAddCaps(t *testing.T) { |
| 53 | + sc := RestrictiveSecurityContext(42457) |
| 54 | + if sc.Capabilities.Add != nil { |
| 55 | + t.Errorf("expected no Add capabilities, got %v", sc.Capabilities.Add) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestRestrictiveSecurityContextWithAddCaps(t *testing.T) { |
| 60 | + sc := RestrictiveSecurityContext(42457, "NET_BIND_SERVICE", "CHOWN") |
| 61 | + |
| 62 | + if len(sc.Capabilities.Drop) != 1 || sc.Capabilities.Drop[0] != "ALL" { |
| 63 | + t.Errorf("expected Drop [ALL], got %v", sc.Capabilities.Drop) |
| 64 | + } |
| 65 | + if len(sc.Capabilities.Add) != 2 { |
| 66 | + t.Fatalf("expected 2 Add capabilities, got %d", len(sc.Capabilities.Add)) |
| 67 | + } |
| 68 | + if sc.Capabilities.Add[0] != "NET_BIND_SERVICE" || sc.Capabilities.Add[1] != "CHOWN" { |
| 69 | + t.Errorf("expected Add [NET_BIND_SERVICE CHOWN], got %v", sc.Capabilities.Add) |
| 70 | + } |
| 71 | +} |
0 commit comments