forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
261 lines (215 loc) · 9.06 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package validation
import (
"fmt"
"net/url"
"strings"
"k8s.io/kubernetes/pkg/api/validation"
"k8s.io/kubernetes/pkg/serviceaccount"
"k8s.io/kubernetes/pkg/util/validation/field"
oapi "github.com/openshift/origin/pkg/api"
authorizerscopes "github.com/openshift/origin/pkg/authorization/authorizer/scope"
"github.com/openshift/origin/pkg/oauth/api"
uservalidation "github.com/openshift/origin/pkg/user/api/validation"
)
const MinTokenLength = 32
func ValidateTokenName(name string, prefix bool) (bool, string) {
if ok, reason := oapi.MinimalNameRequirements(name, prefix); !ok {
return ok, reason
}
if len(name) < MinTokenLength {
return false, fmt.Sprintf("must be at least %d characters long", MinTokenLength)
}
return true, ""
}
func ValidateRedirectURI(redirect string) (bool, string) {
if len(redirect) == 0 {
return true, ""
}
u, err := url.Parse(redirect)
if err != nil {
return false, err.Error()
}
if len(u.Fragment) != 0 {
return false, "may not contain a fragment"
}
for _, s := range strings.Split(u.Path, "/") {
if s == "." {
return false, "may not contain a path segment of ."
}
if s == ".." {
return false, "may not contain a path segment of .."
}
}
return true, ""
}
func ValidateAccessToken(accessToken *api.OAuthAccessToken) field.ErrorList {
allErrs := validation.ValidateObjectMeta(&accessToken.ObjectMeta, false, ValidateTokenName, field.NewPath("metadata"))
allErrs = append(allErrs, ValidateClientNameField(accessToken.ClientName, field.NewPath("clientName"))...)
allErrs = append(allErrs, ValidateUserNameField(accessToken.UserName, field.NewPath("userName"))...)
allErrs = append(allErrs, ValidateScopes(accessToken.Scopes, field.NewPath("scopes"))...)
if len(accessToken.UserUID) == 0 {
allErrs = append(allErrs, field.Required(field.NewPath("userUID"), ""))
}
if ok, msg := ValidateRedirectURI(accessToken.RedirectURI); !ok {
allErrs = append(allErrs, field.Invalid(field.NewPath("redirectURI"), accessToken.RedirectURI, msg))
}
return allErrs
}
func ValidateAuthorizeToken(authorizeToken *api.OAuthAuthorizeToken) field.ErrorList {
allErrs := validation.ValidateObjectMeta(&authorizeToken.ObjectMeta, false, ValidateTokenName, field.NewPath("metadata"))
allErrs = append(allErrs, ValidateClientNameField(authorizeToken.ClientName, field.NewPath("clientName"))...)
allErrs = append(allErrs, ValidateUserNameField(authorizeToken.UserName, field.NewPath("userName"))...)
allErrs = append(allErrs, ValidateScopes(authorizeToken.Scopes, field.NewPath("scopes"))...)
if len(authorizeToken.UserUID) == 0 {
allErrs = append(allErrs, field.Required(field.NewPath("userUID"), ""))
}
if ok, msg := ValidateRedirectURI(authorizeToken.RedirectURI); !ok {
allErrs = append(allErrs, field.Invalid(field.NewPath("redirectURI"), authorizeToken.RedirectURI, msg))
}
return allErrs
}
func ValidateClient(client *api.OAuthClient) field.ErrorList {
allErrs := validation.ValidateObjectMeta(&client.ObjectMeta, false, validation.NameIsDNSSubdomain, field.NewPath("metadata"))
for i, redirect := range client.RedirectURIs {
if ok, msg := ValidateRedirectURI(redirect); !ok {
allErrs = append(allErrs, field.Invalid(field.NewPath("redirectURIs").Index(i), redirect, msg))
}
}
for i, restriction := range client.ScopeRestrictions {
allErrs = append(allErrs, ValidateScopeRestriction(restriction, field.NewPath("scopeRestrictions").Index(i))...)
}
return allErrs
}
func ValidateScopeRestriction(restriction api.ScopeRestriction, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
specifiers := 0
if len(restriction.ExactValues) > 0 {
specifiers = specifiers + 1
}
if restriction.ClusterRole != nil {
specifiers = specifiers + 1
}
if specifiers != 1 {
allErrs = append(allErrs, field.Invalid(fldPath, restriction, "exactly one of literals, clusterRole is required"))
return allErrs
}
switch {
case len(restriction.ExactValues) > 0:
for i, literal := range restriction.ExactValues {
if len(literal) == 0 {
allErrs = append(allErrs, field.Invalid(fldPath.Child("literals").Index(i), literal, "may not be empty"))
}
}
case restriction.ClusterRole != nil:
if len(restriction.ClusterRole.RoleNames) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("clusterRole", "roleNames"), "won't match anything"))
}
if len(restriction.ClusterRole.Namespaces) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("clusterRole", "namespaces"), "won't match anything"))
}
}
return allErrs
}
func ValidateClientUpdate(client *api.OAuthClient, oldClient *api.OAuthClient) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, ValidateClient(client)...)
allErrs = append(allErrs, validation.ValidateObjectMetaUpdate(&client.ObjectMeta, &oldClient.ObjectMeta, field.NewPath("metadata"))...)
return allErrs
}
func ValidateClientAuthorizationName(name string, prefix bool) (bool, string) {
if ok, reason := oapi.MinimalNameRequirements(name, prefix); !ok {
return ok, reason
}
lastColon := strings.Index(name, ":")
if lastColon <= 0 || lastColon >= len(name)-1 {
return false, "must be in the format <userName>:<clientName>"
}
return true, ""
}
func ValidateClientAuthorization(clientAuthorization *api.OAuthClientAuthorization) field.ErrorList {
allErrs := field.ErrorList{}
expectedName := fmt.Sprintf("%s:%s", clientAuthorization.UserName, clientAuthorization.ClientName)
metadataErrs := validation.ValidateObjectMeta(&clientAuthorization.ObjectMeta, false, ValidateClientAuthorizationName, field.NewPath("metadata"))
if len(metadataErrs) > 0 {
allErrs = append(allErrs, metadataErrs...)
} else if clientAuthorization.Name != expectedName {
allErrs = append(allErrs, field.Invalid(field.NewPath("metadata", "name"), clientAuthorization.Name, "must be in the format <userName>:<clientName>"))
}
allErrs = append(allErrs, ValidateClientNameField(clientAuthorization.ClientName, field.NewPath("clientName"))...)
allErrs = append(allErrs, ValidateUserNameField(clientAuthorization.UserName, field.NewPath("userName"))...)
allErrs = append(allErrs, ValidateScopes(clientAuthorization.Scopes, field.NewPath("scopes"))...)
if len(clientAuthorization.UserUID) == 0 {
allErrs = append(allErrs, field.Required(field.NewPath("useruid"), ""))
}
return allErrs
}
func ValidateClientAuthorizationUpdate(newAuth *api.OAuthClientAuthorization, oldAuth *api.OAuthClientAuthorization) field.ErrorList {
allErrs := ValidateClientAuthorization(newAuth)
allErrs = append(allErrs, validation.ValidateObjectMetaUpdate(&newAuth.ObjectMeta, &oldAuth.ObjectMeta, field.NewPath("metadata"))...)
if oldAuth.ClientName != newAuth.ClientName {
allErrs = append(allErrs, field.Invalid(field.NewPath("clientName"), newAuth.ClientName, "clientName is not a mutable field"))
}
if oldAuth.UserName != newAuth.UserName {
allErrs = append(allErrs, field.Invalid(field.NewPath("userName"), newAuth.UserName, "userName is not a mutable field"))
}
if oldAuth.UserUID != newAuth.UserUID {
allErrs = append(allErrs, field.Invalid(field.NewPath("userUID"), newAuth.UserUID, "userUID is not a mutable field"))
}
return allErrs
}
func ValidateClientNameField(value string, fldPath *field.Path) field.ErrorList {
if len(value) == 0 {
return field.ErrorList{field.Required(fldPath, "")}
} else if _, saName, err := serviceaccount.SplitUsername(value); err == nil {
if ok, errString := validation.ValidateServiceAccountName(saName, false); !ok {
return field.ErrorList{field.Invalid(fldPath, value, errString)}
}
} else if ok, msg := validation.NameIsDNSSubdomain(value, false); !ok {
return field.ErrorList{field.Invalid(fldPath, value, msg)}
}
return field.ErrorList{}
}
func ValidateUserNameField(value string, fldPath *field.Path) field.ErrorList {
if len(value) == 0 {
return field.ErrorList{field.Required(fldPath, "")}
} else if ok, msg := uservalidation.ValidateUserName(value, false); !ok {
return field.ErrorList{field.Invalid(fldPath, value, msg)}
}
return field.ErrorList{}
}
func ValidateScopes(scopes []string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
for i, scope := range scopes {
illegalCharacter := false
// https://tools.ietf.org/html/rfc6749#section-3.3 (full list of allowed chars is %x21 / %x23-5B / %x5D-7E)
// for those without an ascii table, that's `!`, `#-[`, `]-~` inclusive.
for _, ch := range scope {
switch {
case ch == rune("!"[0]):
case ch >= rune("#"[0]) && ch <= rune("]"[0]):
case ch >= rune("]"[0]) && ch <= rune("~"[0]):
default:
allErrs = append(allErrs, field.Invalid(fldPath.Index(i), scope, fmt.Sprintf("%v not allowed", ch)))
illegalCharacter = true
}
}
if illegalCharacter {
continue
}
found := false
for _, evaluator := range authorizerscopes.ScopeEvaluators {
if !evaluator.Handles(scope) {
continue
}
found = true
if err := evaluator.Validate(scope); err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Index(i), scope, err.Error()))
break
}
}
if !found {
allErrs = append(allErrs, field.Invalid(fldPath.Index(i), scope, "no scope handler found"))
}
}
return allErrs
}