-
Notifications
You must be signed in to change notification settings - Fork 337
/
common.go
218 lines (194 loc) · 11 KB
/
common.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
//Copyright 2017 Huawei Technologies Co., Ltd
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
package core
import (
"errors"
"github.com/ServiceComb/service-center/pkg/util"
"github.com/ServiceComb/service-center/pkg/validate"
pb "github.com/ServiceComb/service-center/server/core/proto"
"math"
"reflect"
"regexp"
)
var (
ServiceAPI pb.ServiceCtrlServer
InstanceAPI pb.SerivceInstanceCtrlServerEx
GovernServiceAPI pb.GovernServiceCtrlServerEx
VersionRegex *regexp.Regexp
MicroServiceValidator validate.Validator
MicroServiceInstanceValidator validate.Validator
ServiceRuleValidator validate.Validator
ServicePathValidator validate.Validator
HealthCheckInfoValidator validate.Validator
MicroServiceKeyValidator validate.Validator
DataCenterInfoValidator validate.Validator
GetMSExistsReqValidator validate.Validator
GetSchemaExistsReqValidator validate.Validator
GetServiceReqValidator validate.Validator
GetSchemaReqValidator validate.Validator
DependencyMSValidator validate.Validator
ProviderMsValidator validate.Validator
MSDependencyValidator validate.Validator
TagReqValidator validate.Validator
FindInstanceReqValidator validate.Validator
GetInstanceValidator validate.Validator
SchemasValidor validate.Validator
SchemaValidor validate.Validator
SchemaIdRule *validate.ValidateRule
SchemasRule *validate.ValidateRule
TagRule *validate.ValidateRule
)
func init() {
// 非map/slice的validator
nameRegex, _ := regexp.Compile(`^[a-zA-Z0-9]*$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$`)
serviceNameForFindRegex, _ := regexp.Compile(`^[a-zA-Z0-9]*$|^[a-zA-Z0-9][a-zA-Z0-9_\-.:]*[a-zA-Z0-9]$`)
//name模糊规则: name, *
nameFuzzyRegex, _ := regexp.Compile(`^[a-zA-Z0-9]*$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$|^\*$`)
VersionRegex, _ = regexp.Compile(`^[0-9]+(\.[0-9]+){0,2}$`)
// version模糊规则: 1.0, 1.0+, 1.0-2.0, latest
versionFuzzyRegex, _ := regexp.Compile(`^[0-9]*$|^[0-9]+(\.[0-9]+)*\+{0,1}$|^[0-9]+(\.[0-9]+)*-[0-9]+(\.[0-9]+)*$|^latest$`)
pathRegex, _ := regexp.Compile(`^[A-Za-z0-9.,?'\\/+&%$#=~_\-@{}]*$`)
descriptionRegex, _ := regexp.Compile(`^[\p{Han}\w\s。.:*,\-:”“]*$`)
levelRegex, _ := regexp.Compile(`^(FRONT|MIDDLE|BACK)$`)
statusRegex, _ := regexp.Compile(`^(UP|DOWN)*$`)
serviceIdRegex, _ := regexp.Compile(`^.*$`)
aliasRegex, _ := regexp.Compile(`^[a-zA-Z0-9_\-.:]*$`)
stageRegex, _ := regexp.Compile("^(development|testing|acceptance|production)*$")
// map/slice元素的validator
// 元素的格式和长度由正则控制
// map/slice的长度由validator中的min/max/length控制
schemaIdRegex, _ := regexp.Compile(`^[a-zA-Z0-9]{1,160}$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]{0,158}[a-zA-Z0-9]$`) //length:{1,160}
instStatusRegex, _ := regexp.Compile(`^(UP|DOWN|STARTING|OUTOFSERVICE)$`)
tagRegex, _ := regexp.Compile(`^[a-zA-Z][a-zA-Z0-9_\-.]{0,63}$`)
hbModeRegex, _ := regexp.Compile(`^(push|pull)$`)
numberAllowEmptyRegex, _ := regexp.Compile(`^[0-9]*$`)
numberRegex, _ := regexp.Compile(`^[0-9]+$`)
epRegex, _ := regexp.Compile(`^[A-Za-z0-9:/?=&%_.-]+$`)
simpleNameAllowEmptyRegex, _ := regexp.Compile(`^[A-Za-z0-9_.-]*$`)
simpleNameRegex, _ := regexp.Compile(`^[A-Za-z0-9_.-]+$`)
regionRegex, _ := regexp.Compile(`^[A-Za-z0-9_.-]+$`)
ruleRegex, _ := regexp.Compile(`^(WHITE|BLACK)$`)
ruleAttrRegex, _ := regexp.Compile(`((^tag_[a-zA-Z][a-zA-Z0-9_\-.]{0,63}$)|(^ServiceId$)|(^AppId$)|(^ServiceName$)|(^Version$)|(^Description$)|(^Level$)|(^Status$))`)
ServiceIdRule := &validate.ValidateRule{Min: 1, Length: 64, Regexp: serviceIdRegex}
InstanceStatusRule := &validate.ValidateRule{Regexp: instStatusRegex}
SchemaIdRule = &validate.ValidateRule{Regexp: schemaIdRegex}
stageRule := &validate.ValidateRule{Regexp: stageRegex}
nameRule := &validate.ValidateRule{Min: 1, Max: 128, Regexp: nameRegex}
versionFuzzyRule := &validate.ValidateRule{Min: 1, Max: 128, Regexp: versionFuzzyRegex}
TagRule = &validate.ValidateRule{Regexp: tagRegex}
MicroServiceKeyValidator.AddRule("AppId", &validate.ValidateRule{Min: 1, Max: 160, Regexp: nameRegex})
MicroServiceKeyValidator.AddRule("ServiceName", nameRule)
MicroServiceKeyValidator.AddRule("Version", &validate.ValidateRule{Min: 1, Max: 64, Regexp: VersionRegex})
ServicePathValidator.AddRule("Path", &validate.ValidateRule{Regexp: pathRegex})
MicroServiceValidator.AddRules(MicroServiceKeyValidator.GetRules())
MicroServiceValidator.AddRule("Description", &validate.ValidateRule{Length: 256, Regexp: descriptionRegex})
MicroServiceValidator.AddRule("Level", &validate.ValidateRule{Min: 1, Regexp: levelRegex})
MicroServiceValidator.AddRule("Status", &validate.ValidateRule{Min: 1, Regexp: statusRegex})
MicroServiceValidator.AddRule("Schemas", SchemaIdRule)
MicroServiceValidator.AddSub("Paths", &ServicePathValidator)
MicroServiceValidator.AddRule("Alias", &validate.ValidateRule{Length: 128, Regexp: aliasRegex})
GetMSExistsReqValidator.AddRule("AppId", MicroServiceKeyValidator.GetRule("AppId"))
GetMSExistsReqValidator.AddRule("ServiceName", nameRule)
GetMSExistsReqValidator.AddRule("Version", versionFuzzyRule)
GetSchemaExistsReqValidator.AddRule("ServiceId", ServiceIdRule)
GetSchemaExistsReqValidator.AddRule("SchemaId", SchemaIdRule)
SchemasValidor.AddRule("ServiceId", ServiceIdRule)
SchemasValidor.AddSub("Schemas", &SchemaValidor)
SchemaValidor.AddRule("SchemaId", SchemaIdRule)
SchemaSummaryRegex , _ := regexp.Compile(`(a-zA-Z0-9)*`)
SchemaValidor.AddRule("Summary", &validate.ValidateRule{Min: 1, Max: 512, Regexp: SchemaSummaryRegex})
SchemaValidor.AddRule("Schema", &validate.ValidateRule{Min: 1})
GetServiceReqValidator.AddRule("ServiceId", ServiceIdRule)
GetSchemaReqValidator.AddRule("ServiceId", ServiceIdRule)
GetSchemaReqValidator.AddRule("SchemaId", SchemaIdRule)
DependencyMSValidator.AddRules(MicroServiceKeyValidator.GetRules())
DependencyMSValidator.AddRule("Stage", stageRule)
ProviderMsValidator.AddRule("AppId", MicroServiceKeyValidator.GetRule("AppId"))
ProviderMsValidator.AddRule("ServiceName", &validate.ValidateRule{Min: 1, Max: 128, Regexp: nameFuzzyRegex})
ProviderMsValidator.AddRule("Version", versionFuzzyRule)
ProviderMsValidator.AddRule("Stage", stageRule)
MSDependencyValidator.AddSub("Consumer", &DependencyMSValidator)
MSDependencyValidator.AddSub("Providers", &ProviderMsValidator)
TagReqValidator.AddRule("ServiceId", ServiceIdRule)
TagReqValidator.AddRule("Tags", TagRule)
HealthCheckInfoValidator.AddRule("Mode", &validate.ValidateRule{Regexp: hbModeRegex})
HealthCheckInfoValidator.AddRule("Port", &validate.ValidateRule{Max: math.MaxInt16, Regexp: numberAllowEmptyRegex})
HealthCheckInfoValidator.AddRule("Times", &validate.ValidateRule{Max: math.MaxInt32, Regexp: numberRegex})
HealthCheckInfoValidator.AddRule("Interval", &validate.ValidateRule{Max: math.MaxInt32, Regexp: numberRegex})
HealthCheckInfoValidator.AddRule("Url", &validate.ValidateRule{Regexp: pathRegex})
MicroServiceInstanceValidator.AddRule("InstanceId", &validate.ValidateRule{Length: 64, Regexp: simpleNameAllowEmptyRegex})
MicroServiceInstanceValidator.AddRule("ServiceId", ServiceIdRule)
MicroServiceInstanceValidator.AddRule("Endpoints", &validate.ValidateRule{Regexp: epRegex})
MicroServiceInstanceValidator.AddRule("HostName", &validate.ValidateRule{Length: 64, Regexp: simpleNameRegex})
MicroServiceInstanceValidator.AddSub("HealthCheck", &HealthCheckInfoValidator)
MicroServiceInstanceValidator.AddRule("Status", InstanceStatusRule)
MicroServiceInstanceValidator.AddRule("Environment", &validate.ValidateRule{Min: 1, Regexp: stageRegex})
MicroServiceInstanceValidator.AddSub("DataCenterInfo", &DataCenterInfoValidator)
DataCenterInfoValidator.AddRule("Name", &validate.ValidateRule{Length: 128, Regexp: simpleNameRegex})
DataCenterInfoValidator.AddRule("Region", &validate.ValidateRule{Length: 128, Regexp: regionRegex})
DataCenterInfoValidator.AddRule("AvailableZone", &validate.ValidateRule{Length: 128, Regexp: regionRegex})
ServiceRuleValidator.AddRule("RuleType", &validate.ValidateRule{Regexp: ruleRegex})
ServiceRuleValidator.AddRule("Attribute", &validate.ValidateRule{Regexp: ruleAttrRegex})
ServiceRuleValidator.AddRule("Pattern", &validate.ValidateRule{Max: 64, Min: 1})
ServiceRuleValidator.AddRule("Description", MicroServiceValidator.GetRule("Description"))
FindInstanceReqValidator.AddRule("ConsumerServiceId", ServiceIdRule)
FindInstanceReqValidator.AddRule("AppId", MicroServiceKeyValidator.GetRule("AppId"))
FindInstanceReqValidator.AddRule("ServiceName", &validate.ValidateRule{Min: 1, Max: 128, Regexp: serviceNameForFindRegex})
FindInstanceReqValidator.AddRule("VersionRule", versionFuzzyRule)
FindInstanceReqValidator.AddRule("Tags", TagRule)
FindInstanceReqValidator.AddRule("Env", stageRule)
GetInstanceValidator.AddRule("ConsumerServiceId", ServiceIdRule)
GetInstanceValidator.AddRule("ProviderServiceId", ServiceIdRule)
GetInstanceValidator.AddRule("ProviderInstanceId", &validate.ValidateRule{Min: 1, Max: 64, Regexp: simpleNameAllowEmptyRegex})
GetInstanceValidator.AddRule("Tags", TagRule)
GetInstanceValidator.AddRule("Env", stageRule)
}
func Validate(v interface{}) error {
if v == nil {
util.Logger().Errorf(nil, "Data is nil!")
return errors.New("Data is nil!")
}
sv := reflect.ValueOf(v)
if sv.Kind() == reflect.Ptr && sv.IsNil() {
util.Logger().Errorf(nil, "Pointer is nil!")
return errors.New("Pointer is nil!")
}
switch t := v.(type) {
case (*pb.MicroService):
return MicroServiceValidator.Validate(v)
case *pb.MicroServiceInstance, *pb.UpdateInstanceStatusRequest:
return MicroServiceInstanceValidator.Validate(v)
case (*pb.AddOrUpdateServiceRule):
return ServiceRuleValidator.Validate(v)
case *pb.GetServiceRequest, *pb.UpdateServicePropsRequest,
*pb.DeleteServiceRequest, *pb.GetDependenciesRequest:
return GetServiceReqValidator.Validate(v)
case *pb.AddServiceTagsRequest, *pb.DeleteServiceTagsRequest,
*pb.UpdateServiceTagRequest, *pb.GetServiceTagsRequest:
return TagReqValidator.Validate(v)
case *pb.GetSchemaRequest, *pb.ModifySchemaRequest, *pb.DeleteSchemaRequest:
return GetSchemaReqValidator.Validate(v)
case *pb.ModifySchemasRequest:
return SchemasValidor.Validate(v)
case *pb.MicroServiceDependency:
return DependencyMSValidator.Validate(v)
case *pb.FindInstancesRequest:
return FindInstanceReqValidator.Validate(v)
case *pb.GetOneInstanceRequest, *pb.GetInstancesRequest:
return GetInstanceValidator.Validate(v)
default:
util.Logger().Errorf(nil, "No validator for %T.", t)
return nil
}
}