/
validator.go
177 lines (153 loc) · 3.88 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
//
// Last.Backend LLC CONFIDENTIAL
// __________________
//
// [2014] - [2018] Last.Backend LLC
// All Rights Reserved.
//
// NOTICE: All information contained herein is, and remains
// the property of Last.Backend LLC and its suppliers,
// if any. The intellectual and technical concepts contained
// herein are proprietary to Last.Backend LLC
// and its suppliers and may be covered by Russian Federation and Foreign Patents,
// patents in process, and are protected by trade secret or copyright law.
// Dissemination of this information or reproduction of this material
// is strictly forbidden unless prior written permission is obtained
// from Last.Backend LLC.
//
package validator
import (
"encoding/base64"
"fmt"
"github.com/asaskevich/govalidator"
"reflect"
"regexp"
"strconv"
"strings"
)
func IsNil(a interface{}) bool {
defer func() { recover() }()
return a == nil || reflect.ValueOf(a).IsNil()
}
func IsBool(s string) bool {
s = strings.ToLower(s)
if s == "true" || s == "1" || s == "t" || s == "false" || s == "0" || s == "f" {
return true
}
return false
}
func IsEmail(s string) bool {
return govalidator.IsEmail(s)
}
func IsNamespaceName(s string) bool {
reg, _ := regexp.Compile("^[A-Za-z0-9][A-Za-z0-9\\.]+(?:[_-][A-Za-z0-9\\.]+)*")
str := reg.FindStringSubmatch(s)
if len(str) == 1 && str[0] == s && len(s) >= 4 && len(s) <= 64 {
return true
}
return false
}
func IsServiceName(s string) bool {
reg, _ := regexp.Compile("[A-Za-z0-9]+(?:[_-][A-Za-z0-9]+)*")
str := reg.FindStringSubmatch(s)
if len(str) == 1 && str[0] == s && len(s) >= 4 && len(s) <= 64 {
return true
}
return false
}
func IsIP(ip string) bool {
return govalidator.IsIP(ip)
}
func IsMac(mac string) bool {
return govalidator.IsMAC(mac)
}
func IsUUID(uuid string) bool {
return govalidator.IsUUIDv4(uuid)
}
func IsRole(role string) bool {
switch role {
case "member":
return true
case "admin":
return true
}
return false
}
func IsPort(port int) bool {
return govalidator.IsPort(strconv.Itoa(port))
}
func IsDomain(domain string) bool { // TODO domain Validator
return true
}
func IsProtocol(protocol string) bool {
correctProtocols := []string{"tcp", "udp"}
for _, correctProtocol := range correctProtocols {
if strings.EqualFold(correctProtocol, protocol) {
return true
}
}
return false
}
func IsPublicKey(key string) bool {
var splited = strings.SplitN(key, " ", 3)
if len(splited) < 2 {
return false
}
var alg = strings.TrimSpace(splited[0])
var cb64 = strings.TrimSpace(splited[1])
_, err := base64.StdEncoding.DecodeString(cb64)
if err != nil {
return false
}
switch alg {
case "ssh-rsa":
return true
case "ssh-dss":
return true
case "ecdsa-sha2-nistp256", "ecdsa-sha2-nistp384", "ecdsa-sha2-nistp521":
return true
}
return false
}
// Check incoming string on git valid utl
// Ex:
// * https://github.com/lastbackend/enterprise.git
// * git@github.com:lastbackend/enterprise.git
func IsGitUrl(url string) bool {
res, err := regexp.MatchString(`^(?:ssh|git|http(?:s)?)(?:@|:\/\/(?:.+@)?)((\w+)\.\w+)(?:\/|:)(.+)(?:\/)(.+)(?:\..+)$`, url)
if err != nil {
return false
}
return res
}
func validateDockerRepoName(repoName string) error {
var (
owner string
name string
)
nameParts := strings.SplitN(repoName, "/", 2)
if len(nameParts) < 2 {
owner = ""
name = nameParts[0]
} else {
owner = nameParts[0]
name = nameParts[1]
}
validApp := regexp.MustCompile(`^([a-z0-9_]{4,30})$`)
if !validApp.MatchString(owner) {
return fmt.Errorf("invalid owner name (%s), only [a-z0-9_] are allowed, size between 4 and 30", owner)
}
validRepo := regexp.MustCompile(`^([a-zA-Z0-9-_.]+)$`)
if !validRepo.MatchString(name) {
return fmt.Errorf("invalid repo name (%s), only [a-zA-Z0-9-_.] are allowed", name)
}
return nil
}
func IsValueInList(value string, list []string) bool {
for _, v := range list {
if v == value {
return true
}
}
return false
}