-
Notifications
You must be signed in to change notification settings - Fork 11
/
validator.go
263 lines (216 loc) · 8.8 KB
/
validator.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
262
263
package util
import (
"reflect"
"sync"
"bitbucket.pearson.com/apseng/tensor/models"
"fmt"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/locales/en"
"github.com/go-playground/universal-translator"
"gopkg.in/go-playground/validator.v9"
en_translations "gopkg.in/go-playground/validator.v9/translations/en"
"io"
"net"
"regexp"
"strings"
)
const (
Become string = "^(sudo|su|pbrun|pfexec|runas|doas|dzdo)$"
CredentialKind string = "^(windows|ssh|net|scm|aws|rax|vmware|satellite6|cloudforms|gce|azure|openstack)$"
ScmType string = "^(manual|git|hg|svn)$"
JobType string = "^(run|check|scan)$"
DNSName string = `^([a-zA-Z0-9]{1}[a-zA-Z0-9_-]{1,62}){1}(\.[a-zA-Z0-9]{1}[a-zA-Z0-9_-]{1,62})*$`
IP string = `(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))`
URLSchema string = `((ftp|tcp|udp|wss?|https?):\/\/)`
URLUsername string = `(\S+(:\S*)?@)`
URLPath string = `((\/|\?|#)[^\s]*)`
URLPort string = `(:(\d{1,5}))`
URLIP string = `([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))`
URLSubdomain string = `((www\.)|([a-zA-Z0-9]([-\.][a-zA-Z0-9]+)*))`
URL string = `^` + URLSchema + `?` + URLUsername + `?` + `((` + URLIP + `|(\[` + IP + `\])|(([a-zA-Z0-9]([a-zA-Z0-9-]+)?[a-zA-Z0-9]([-\.][a-zA-Z0-9]+)*)|(` + URLSubdomain + `?))?(([a-zA-Z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-zA-Z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-zA-Z\x{00a1}-\x{ffff}]{1,}))?))` + URLPort + `?` + URLPath + `?$`
)
// use a single instance , it caches struct info
var Uni *ut.UniversalTranslator
var trans ut.Translator
var (
rxBecome = regexp.MustCompile(Become)
rxDNSName = regexp.MustCompile(DNSName)
rxURL = regexp.MustCompile(URL)
rxCredentialKind = regexp.MustCompile(CredentialKind)
rxScmType = regexp.MustCompile(ScmType)
rxJobType = regexp.MustCompile(JobType)
)
type SpaceValidator struct {
once sync.Once
validate *validator.Validate
}
var _ binding.StructValidator = &SpaceValidator{}
func (v *SpaceValidator) ValidateStruct(obj interface{}) error {
if kindOfData(obj) == reflect.Struct {
v.lazyinit()
if err := v.validate.Struct(obj); err != nil {
return error(err)
}
}
return nil
}
func (v *SpaceValidator) lazyinit() {
v.once.Do(func() {
v.validate = validator.New()
v.validate.SetTagName("binding")
// set translator
en := en.New()
Uni = ut.New(en, en)
trans, _ = Uni.GetTranslator("en")
en_translations.RegisterDefaultTranslations(v.validate, trans)
// Register custom validator functions
v.validate.RegisterValidation("become_method", isBecome)
v.validate.RegisterValidation("dnsname", IsDNSName)
v.validate.RegisterValidation("iphost", IsHost)
v.validate.RegisterValidation("credentialkind", IsCredentialKind)
v.validate.RegisterValidation("naproperty", NaProperty)
v.validate.RegisterValidation("scmtype", IsScmType)
v.validate.RegisterValidation("jobtype", IsJobType)
//translations
// credentialkind
v.validate.RegisterTranslation("credentialkind", trans, func(ut ut.Translator) error {
return ut.Add("credentialkind", "{0} must have either one of windows,ssh,net,scm,aws,rax,vmware,satellite6,cloudforms,gce,azure,openstack", true)
}, func(ut ut.Translator, fe validator.FieldError) string {
t, _ := ut.T("credentialkind", fe.Field())
return t
})
v.validate.RegisterTranslation("become_method", trans, func(ut ut.Translator) error {
return ut.Add("become_method", "{0} must have either one of sudo,su,pbrun,pfexec,runas,doas,dzdo", true)
}, func(ut ut.Translator, fe validator.FieldError) string {
t, _ := ut.T("become_method", fe.Field())
return t
})
v.validate.RegisterTranslation("naproperty", trans, func(ut ut.Translator) error {
return ut.Add("naproperty", "Invalid property, {0}", true)
}, func(ut ut.Translator, fe validator.FieldError) string {
t, _ := ut.T("naproperty", fe.Field())
return t
})
v.validate.RegisterTranslation("scmtype", trans, func(ut ut.Translator) error {
return ut.Add("scmtype", "{0} must have either one of manual,git,hg,svn", true)
}, func(ut ut.Translator, fe validator.FieldError) string {
t, _ := ut.T("scmtype", fe.Field())
return t
})
v.validate.RegisterTranslation("jobtype", trans, func(ut ut.Translator) error {
return ut.Add("jobtype", "{0} must have either one of run,check,scan", true)
}, func(ut ut.Translator, fe validator.FieldError) string {
t, _ := ut.T("jobtype", fe.Field())
return t
})
v.validate.RegisterTranslation("iphost", trans, func(ut ut.Translator) error {
return ut.Add("iphost", "{0} must have valid hostname or ip address", true)
}, func(ut ut.Translator, fe validator.FieldError) string {
t, _ := ut.T("iphost", fe.Field())
return t
})
//struct level validations
v.validate.RegisterStructValidation(CredentialStructLevelValidation, models.Credential{})
v.validate.RegisterStructValidation(ProjectStructLevelValidation, models.Project{})
})
}
func kindOfData(data interface{}) reflect.Kind {
value := reflect.ValueOf(data)
valueType := value.Kind()
if valueType == reflect.Ptr {
valueType = value.Elem().Kind()
}
return valueType
}
func isBecome(fl validator.FieldLevel) bool {
return rxBecome.MatchString(fl.Field().String())
}
// IsDNSName will validate the given string as a DNS name
func IsDNSName(fl validator.FieldLevel) bool {
str := fl.Field().String()
if str == "" || len(strings.Replace(str, ".", "", -1)) > 255 {
// constraints already violated
return false
}
return rxDNSName.MatchString(str)
}
// IsIP checks if a string is either IP version 4 or 6.
func IsIP(fl validator.FieldLevel) bool {
return net.ParseIP(fl.Field().String()) != nil
}
// IsHost checks if the string is a valid IP (both v4 and v6) or a valid DNS name
func IsHost(fl validator.FieldLevel) bool {
return IsIP(fl) || IsDNSName(fl)
}
func IsScmType(fl validator.FieldLevel) bool {
return rxScmType.MatchString(fl.Field().String())
}
func IsJobType(fl validator.FieldLevel) bool {
return rxJobType.MatchString(fl.Field().String())
}
func IsCredentialKind(fl validator.FieldLevel) bool {
return rxCredentialKind.MatchString(fl.Field().String())
}
// fail all
func NaProperty(fl validator.FieldLevel) bool {
if fl.Field().String() == "" {
// constraints not violated
return true
}
return false
}
func GetValidationErrors(err error) []string {
if err == io.EOF {
return []string{"Request body cannot be empty"}
}
if reflect.Ptr == reflect.TypeOf(err).Kind() {
return []string{"Invalid request body, " + err.Error()}
}
// translate all error at once
if errs, ok := err.(validator.ValidationErrors); ok {
for _, e := range errs {
// can translate each error one at a time.
fmt.Println(e.Translate(trans))
allerrs := []string{}
for _, v := range errs.Translate(trans) {
allerrs = append(allerrs, v)
}
return allerrs
}
}
return []string{}
}
// TODO: openstack,azure,gce,
func CredentialStructLevelValidation(sl validator.StructLevel) {
credentail := sl.Current().Interface().(models.Credential)
if credentail.Kind == "net" && len(credentail.Username) == 0 {
sl.ReportError(credentail.Username, "Username", "Username", "required", "")
}
if credentail.Kind == "aws" || credentail.Kind == "rax" {
if len(credentail.Username) == 0 {
sl.ReportError(credentail.Username, "Username", "Username", "required", "")
}
if len(credentail.Password) == 0 {
sl.ReportError(credentail.Username, "Password", "Password", "required", "")
}
}
if credentail.Kind == "vmware" || credentail.Kind == "satellite6" || credentail.Kind == "cloudforms" {
if len(credentail.Username) == 0 {
sl.ReportError(credentail.Username, "Username", "Username", "required", "")
}
if len(credentail.Password) == 0 {
sl.ReportError(credentail.Username, "Password", "Password", "required", "")
}
if len(credentail.Host) == 0 {
sl.ReportError(credentail.Host, "Host", "Password", "required", "")
}
}
}
func ProjectStructLevelValidation(sl validator.StructLevel) {
project := sl.Current().Interface().(models.Project)
if project.ScmType == "git" || project.ScmType == "svn" || project.ScmType == "hg" {
if len(project.ScmUrl) == 0 {
sl.ReportError(project.ScmUrl, "ScmUrl", "ScmUrl", "required", "")
}
}
}