/
util.go
182 lines (167 loc) · 5.31 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package util
import (
"fmt"
"k8s.io/apimachinery/pkg/util/sets"
api "k8s.io/kubernetes/pkg/apis/core"
securityv1 "github.com/openshift/api/security/v1"
securityapi "github.com/openshift/origin/pkg/security/apis/security"
)
func GetAllFSTypesExcept(exceptions ...string) sets.String {
fstypes := GetAllFSTypesAsSet()
for _, e := range exceptions {
fstypes.Delete(e)
}
return fstypes
}
func GetAllFSTypesAsSet() sets.String {
fstypes := sets.NewString()
fstypes.Insert(
string(securityapi.FSTypeHostPath),
string(securityapi.FSTypeAzureFile),
string(securityapi.FSTypeFlocker),
string(securityapi.FSTypeFlexVolume),
string(securityapi.FSTypeEmptyDir),
string(securityapi.FSTypeGCEPersistentDisk),
string(securityapi.FSTypeAWSElasticBlockStore),
string(securityapi.FSTypeGitRepo),
string(securityapi.FSTypeSecret),
string(securityapi.FSTypeNFS),
string(securityapi.FSTypeISCSI),
string(securityapi.FSTypeGlusterfs),
string(securityapi.FSTypePersistentVolumeClaim),
string(securityapi.FSTypeRBD),
string(securityapi.FSTypeCinder),
string(securityapi.FSTypeCephFS),
string(securityapi.FSTypeDownwardAPI),
string(securityapi.FSTypeFC),
string(securityapi.FSTypeConfigMap),
string(securityapi.FSTypeVsphereVolume),
string(securityapi.FSTypeQuobyte),
string(securityapi.FSTypeAzureDisk),
string(securityapi.FSTypePhotonPersistentDisk),
string(securityapi.FSProjected),
string(securityapi.FSPortworxVolume),
string(securityapi.FSScaleIO),
string(securityapi.FSStorageOS),
)
return fstypes
}
// getVolumeFSType gets the FSType for a volume.
func GetVolumeFSType(v api.Volume) (securityapi.FSType, error) {
switch {
case v.HostPath != nil:
return securityapi.FSTypeHostPath, nil
case v.EmptyDir != nil:
return securityapi.FSTypeEmptyDir, nil
case v.GCEPersistentDisk != nil:
return securityapi.FSTypeGCEPersistentDisk, nil
case v.AWSElasticBlockStore != nil:
return securityapi.FSTypeAWSElasticBlockStore, nil
case v.GitRepo != nil:
return securityapi.FSTypeGitRepo, nil
case v.Secret != nil:
return securityapi.FSTypeSecret, nil
case v.NFS != nil:
return securityapi.FSTypeNFS, nil
case v.ISCSI != nil:
return securityapi.FSTypeISCSI, nil
case v.Glusterfs != nil:
return securityapi.FSTypeGlusterfs, nil
case v.PersistentVolumeClaim != nil:
return securityapi.FSTypePersistentVolumeClaim, nil
case v.RBD != nil:
return securityapi.FSTypeRBD, nil
case v.FlexVolume != nil:
return securityapi.FSTypeFlexVolume, nil
case v.Cinder != nil:
return securityapi.FSTypeCinder, nil
case v.CephFS != nil:
return securityapi.FSTypeCephFS, nil
case v.Flocker != nil:
return securityapi.FSTypeFlocker, nil
case v.DownwardAPI != nil:
return securityapi.FSTypeDownwardAPI, nil
case v.FC != nil:
return securityapi.FSTypeFC, nil
case v.AzureFile != nil:
return securityapi.FSTypeAzureFile, nil
case v.ConfigMap != nil:
return securityapi.FSTypeConfigMap, nil
case v.VsphereVolume != nil:
return securityapi.FSTypeVsphereVolume, nil
case v.Quobyte != nil:
return securityapi.FSTypeQuobyte, nil
case v.AzureDisk != nil:
return securityapi.FSTypeAzureDisk, nil
case v.PhotonPersistentDisk != nil:
return securityapi.FSTypePhotonPersistentDisk, nil
case v.Projected != nil:
return securityapi.FSProjected, nil
case v.PortworxVolume != nil:
return securityapi.FSPortworxVolume, nil
case v.ScaleIO != nil:
return securityapi.FSScaleIO, nil
case v.StorageOS != nil:
return securityapi.FSStorageOS, nil
}
return "", fmt.Errorf("unknown volume type for volume: %#v", v)
}
// fsTypeToStringSet converts an FSType slice to a string set.
func FSTypeToStringSetInternal(fsTypes []securityapi.FSType) sets.String {
set := sets.NewString()
for _, v := range fsTypes {
set.Insert(string(v))
}
return set
}
// fsTypeToStringSet converts an FSType slice to a string set.
func FSTypeToStringSet(fsTypes []securityv1.FSType) sets.String {
set := sets.NewString()
for _, v := range fsTypes {
set.Insert(string(v))
}
return set
}
// SCCAllowsAllVolumes checks for FSTypeAll in the scc's allowed volumes.
func SCCAllowsAllVolumes(scc *securityapi.SecurityContextConstraints) bool {
return SCCAllowsFSTypeInternal(scc, securityapi.FSTypeAll)
}
// SCCAllowsFSTypeInternal is a utility for checking if an SCC allows a particular FSType.
// If all volumes are allowed then this will return true for any FSType passed.
func SCCAllowsFSTypeInternal(scc *securityapi.SecurityContextConstraints, fsType securityapi.FSType) bool {
if scc == nil {
return false
}
for _, v := range scc.Volumes {
if v == fsType || v == securityapi.FSTypeAll {
return true
}
}
return false
}
// SCCAllowsFSType is a utility for checking if an SCC allows a particular FSType.
// If all volumes are allowed then this will return true for any FSType passed.
func SCCAllowsFSType(scc *securityv1.SecurityContextConstraints, fsType securityv1.FSType) bool {
if scc == nil {
return false
}
for _, v := range scc.Volumes {
if v == fsType || v == securityv1.FSTypeAll {
return true
}
}
return false
}
// EqualStringSlices compares string slices for equality. Slices are equal when
// their sizes and elements on similar positions are equal.
func EqualStringSlices(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}