forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
200 lines (178 loc) · 6.99 KB
/
validation.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package validation
import (
"fmt"
"strings"
kvalidation "github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
"github.com/openshift/origin/pkg/user/api"
)
func ValidateUserName(name string, _ bool) (bool, string) {
if strings.Contains(name, "%") {
return false, `may not contain "%"`
}
if strings.Contains(name, "/") {
return false, `may not contain "/"`
}
if strings.Contains(name, ":") {
return false, `may not contain ":"`
}
if name == ".." {
return false, `may not equal ".."`
}
if name == "." {
return false, `may not equal "."`
}
if name == "~" {
return false, `may not equal "~"`
}
return true, ""
}
func ValidateIdentityName(name string, _ bool) (bool, string) {
if strings.Contains(name, "%") {
return false, `may not contain "%"`
}
if strings.Contains(name, "/") {
return false, `may not contain "/"`
}
if name == ".." {
return false, `may not equal ".."`
}
if name == "." {
return false, `may not equal "."`
}
parts := strings.Split(name, ":")
if len(parts) != 2 {
return false, `must be in the format <providerName>:<providerUserName>`
}
if len(parts[0]) == 0 {
return false, `must be in the format <providerName>:<providerUserName> with a non-empty providerName`
}
if len(parts[1]) == 0 {
return false, `must be in the format <providerName>:<providerUserName> with a non-empty providerUserName`
}
return true, ""
}
func ValidateGroupName(name string, _ bool) (bool, string) {
if strings.Contains(name, "%") {
return false, `may not contain "%"`
}
if strings.Contains(name, "/") {
return false, `may not contain "/"`
}
if strings.Contains(name, ":") {
return false, `may not contain ":"`
}
if name == ".." {
return false, `may not equal ".."`
}
if name == "." {
return false, `may not equal "."`
}
if name == "~" {
return false, `may not equal "~"`
}
return true, ""
}
func ValidateIdentityProviderName(name string) (bool, string) {
if strings.Contains(name, "%") {
return false, `may not contain "%"`
}
if strings.Contains(name, "/") {
return false, `may not contain "/"`
}
if strings.Contains(name, ":") {
return false, `may not contain ":"`
}
return true, ""
}
func ValidateIdentityProviderUserName(name string) (bool, string) {
// Any provider user name must be a valid user name
return ValidateUserName(name, false)
}
func ValidateUser(user *api.User) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
allErrs = append(allErrs, kvalidation.ValidateObjectMeta(&user.ObjectMeta, false, ValidateUserName).Prefix("metadata")...)
for index, identity := range user.Identities {
if ok, msg := ValidateIdentityName(identity, false); !ok {
allErrs = append(allErrs, fielderrors.NewFieldInvalid(fmt.Sprintf("identities[%d]", index), identity, msg))
}
}
for index, group := range user.Groups {
if len(group) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldInvalid(fmt.Sprintf("groups[%d]", index), group, "may not be empty"))
continue
}
if ok, msg := ValidateGroupName(group, false); !ok {
allErrs = append(allErrs, fielderrors.NewFieldInvalid(fmt.Sprintf("groups[%d]", index), group, msg))
}
}
return allErrs
}
func ValidateUserUpdate(user *api.User, old *api.User) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
allErrs = append(allErrs, kvalidation.ValidateObjectMetaUpdate(&old.ObjectMeta, &user.ObjectMeta).Prefix("metadata")...)
allErrs = append(allErrs, ValidateUser(user)...)
return allErrs
}
func ValidateIdentity(identity *api.Identity) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
allErrs = append(allErrs, kvalidation.ValidateObjectMeta(&identity.ObjectMeta, false, ValidateIdentityName).Prefix("metadata")...)
if len(identity.ProviderName) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("providerName"))
} else if ok, msg := ValidateIdentityProviderName(identity.ProviderName); !ok {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("providerName", identity.ProviderName, msg))
}
if len(identity.ProviderUserName) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("providerUserName"))
} else if ok, msg := ValidateIdentityProviderName(identity.ProviderUserName); !ok {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("providerUserName", identity.ProviderUserName, msg))
}
if len(identity.ProviderName) > 0 && len(identity.ProviderUserName) > 0 {
expectedIdentityName := identity.ProviderName + ":" + identity.ProviderUserName
if identity.Name != expectedIdentityName {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("user.name", identity.User.Name, fmt.Sprintf("must be %s", expectedIdentityName)))
}
}
if ok, msg := ValidateUserName(identity.User.Name, false); !ok {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("user.name", identity.User.Name, msg))
}
if len(identity.User.Name) == 0 && len(identity.User.UID) != 0 {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("user.uid", identity.User.UID, "may not be set if user.name is empty"))
}
if len(identity.User.Name) != 0 && len(identity.User.UID) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("user.uid"))
}
return allErrs
}
func ValidateIdentityUpdate(identity *api.Identity, old *api.Identity) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
allErrs = append(allErrs, kvalidation.ValidateObjectMetaUpdate(&old.ObjectMeta, &identity.ObjectMeta).Prefix("metadata")...)
allErrs = append(allErrs, ValidateIdentity(identity)...)
if identity.ProviderName != old.ProviderName {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("providerName", identity.ProviderName, "may not change providerName"))
}
if identity.ProviderUserName != old.ProviderUserName {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("providerUserName", identity.ProviderUserName, "may not change providerUserName"))
}
return allErrs
}
func ValidateUserIdentityMapping(mapping *api.UserIdentityMapping) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
allErrs = append(allErrs, kvalidation.ValidateObjectMeta(&mapping.ObjectMeta, false, ValidateIdentityName).Prefix("metadata")...)
if len(mapping.Identity.Name) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("identity.name"))
}
if mapping.Identity.Name != mapping.Name {
allErrs = append(allErrs, fielderrors.NewFieldInvalid("identity.name", mapping.Identity.Name, "must match metadata.name"))
}
if len(mapping.User.Name) == 0 {
allErrs = append(allErrs, fielderrors.NewFieldRequired("user.name"))
}
return allErrs
}
func ValidateUserIdentityMappingUpdate(mapping *api.UserIdentityMapping, old *api.UserIdentityMapping) fielderrors.ValidationErrorList {
allErrs := fielderrors.ValidationErrorList{}
allErrs = append(allErrs, kvalidation.ValidateObjectMetaUpdate(&old.ObjectMeta, &mapping.ObjectMeta).Prefix("metadata")...)
allErrs = append(allErrs, ValidateUserIdentityMapping(mapping)...)
return allErrs
}