-
Notifications
You must be signed in to change notification settings - Fork 7
/
common.go
481 lines (417 loc) · 12 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package gosnappi
import (
"context"
"fmt"
"net"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"github.com/Masterminds/semver/v3"
"google.golang.org/grpc"
)
type grpcTransport struct {
clientConnection *grpc.ClientConn
location string
requestTimeout time.Duration
dialTimeout time.Duration
}
type GrpcTransport interface {
// SetLocation set client connection to the given grpc target
SetLocation(value string) GrpcTransport
// Location get grpc target
Location() string
// SetRequestTimeout set timeout in grpc request
SetRequestTimeout(value time.Duration) GrpcTransport
// RequestTimeout get timeout in grpc request
RequestTimeout() time.Duration
// SetDialTimeout set timeout in grpc dial
SetDialTimeout(value time.Duration) GrpcTransport
// DialTimeout get timeout in grpc dial
DialTimeout() time.Duration
// SetClientConnection set grpc DialContext
// SetClientConnection and (SetLocation, SetDialTimeout) are mutually exclusive
SetClientConnection(con *grpc.ClientConn) GrpcTransport
// ClientConnection get grpc DialContext
ClientConnection() *grpc.ClientConn
}
// Location
func (obj *grpcTransport) Location() string {
return obj.location
}
// SetLocation
func (obj *grpcTransport) SetLocation(value string) GrpcTransport {
obj.location = value
return obj
}
// RequestTimeout returns the grpc request timeout in seconds
func (obj *grpcTransport) RequestTimeout() time.Duration {
return obj.requestTimeout
}
// SetRequestTimeout contains the timeout value in seconds for a grpc request
func (obj *grpcTransport) SetRequestTimeout(value time.Duration) GrpcTransport {
obj.requestTimeout = value
return obj
}
func (obj *grpcTransport) DialTimeout() time.Duration {
return obj.dialTimeout
}
func (obj *grpcTransport) SetDialTimeout(value time.Duration) GrpcTransport {
obj.dialTimeout = value
return obj
}
func (obj *grpcTransport) ClientConnection() *grpc.ClientConn {
return obj.clientConnection
}
func (obj *grpcTransport) SetClientConnection(con *grpc.ClientConn) GrpcTransport {
obj.clientConnection = con
return obj
}
type httpTransport struct {
location string
verify bool
conn net.Conn
}
type HttpTransport interface {
SetLocation(value string) HttpTransport
Location() string
SetVerify(value bool) HttpTransport
Verify() bool
}
// Location
func (obj *httpTransport) Location() string {
return obj.location
}
// SetLocation
func (obj *httpTransport) SetLocation(value string) HttpTransport {
obj.location = value
return obj
}
// Verify returns whether or not TLS certificates will be verified by the server
func (obj *httpTransport) Verify() bool {
return obj.verify
}
// SetVerify determines whether or not TLS certificates will be verified by the server
func (obj *httpTransport) SetVerify(value bool) HttpTransport {
obj.verify = value
return obj
}
type apiSt struct {
grpc *grpcTransport
http *httpTransport
warnings string
}
type api interface {
NewGrpcTransport() GrpcTransport
hasGrpcTransport() bool
NewHttpTransport() HttpTransport
hasHttpTransport() bool
Close() error
// Warnings Api is only for testing purpose
// and not intended to use in production
getWarnings() string
deprecated(message string)
under_review(message string)
addWarnings(message string)
}
// NewGrpcTransport sets the underlying transport of the Api as grpc
func (api *apiSt) NewGrpcTransport() GrpcTransport {
api.grpc = &grpcTransport{
location: "localhost:5050",
requestTimeout: 10 * time.Second,
dialTimeout: 10 * time.Second,
}
api.http = nil
return api.grpc
}
// HasGrpcTransport will return True for gRPC transport
func (api *apiSt) hasGrpcTransport() bool {
return api.grpc != nil
}
// NewHttpTransport sets the underlying transport of the Api as http
func (api *apiSt) NewHttpTransport() HttpTransport {
api.http = &httpTransport{
location: "https://localhost:443",
verify: false,
}
if api.grpc != nil {
if api.grpc.clientConnection != nil {
api.grpc.clientConnection.Close()
}
}
api.grpc = nil
return api.http
}
func (api *apiSt) hasHttpTransport() bool {
return api.http != nil
}
func (api *apiSt) getWarnings() string {
return api.warnings
}
func (api *apiSt) addWarnings(message string) {
fmt.Printf("[WARNING]: %s\n", message)
api.warnings = message
}
func (api *apiSt) deprecated(message string) {
api.warnings = message
fmt.Printf("warning: %s\n", message)
}
func (api *apiSt) under_review(message string) {
api.warnings = message
fmt.Printf("warning: %s\n", message)
}
// HttpRequestDoer will return True for HTTP transport
type httpRequestDoer interface {
Do(req *http.Request) (*http.Response, error)
}
type httpClient struct {
client httpRequestDoer
ctx context.Context
}
// All methods that perform validation will add errors here
// All api rpcs MUST call Validate
type Constraints interface {
ValueOf(name string) interface{}
}
type validation struct {
validationErrors []string
warnings []string
constraints map[string]map[string]Constraints
}
type Validation interface {
validationResult() error
deprecated(message string)
under_review(message string)
Warnings() []string
addWarnings(message string)
}
func (obj *validation) validationResult() error {
obj.constraints = make(map[string]map[string]Constraints)
if len(obj.validationErrors) > 0 {
errors := strings.Join(obj.validationErrors, "\n")
obj.validationErrors = nil
return fmt.Errorf(errors)
}
return nil
}
func (obj *validation) Warnings() []string {
if len(obj.warnings) > 0 {
warns := obj.warnings
obj.warnings = nil
return warns
}
return obj.warnings
}
func (obj *validation) addWarnings(message string) {
fmt.Printf("[WARNING]: %s\n", message)
obj.warnings = append(obj.warnings, message)
}
func (obj *validation) deprecated(message string) {
fmt.Printf("warning: %s\n", message)
obj.warnings = append(obj.warnings, message)
}
func (obj *validation) under_review(message string) {
fmt.Printf("warning: %s\n", message)
obj.warnings = append(obj.warnings, message)
}
func (obj *validation) validateMac(mac string) error {
macSlice := strings.Split(mac, ":")
if len(macSlice) != 6 {
return fmt.Errorf(fmt.Sprintf("Invalid Mac address %s", mac))
}
octInd := []string{"0th", "1st", "2nd", "3rd", "4th", "5th"}
for ind, val := range macSlice {
num, err := strconv.ParseUint(val, 16, 32)
if err != nil || num > 255 {
return fmt.Errorf(fmt.Sprintf("Invalid Mac address at %s octet in %s mac", octInd[ind], mac))
}
}
return nil
}
func (obj *validation) validateIpv4(ip string) error {
ipSlice := strings.Split(ip, ".")
if len(ipSlice) != 4 {
return fmt.Errorf(fmt.Sprintf("Invalid Ipv4 address %s", ip))
}
octInd := []string{"1st", "2nd", "3rd", "4th"}
for ind, val := range ipSlice {
num, err := strconv.ParseUint(val, 10, 32)
if err != nil || num > 255 {
return fmt.Errorf(fmt.Sprintf("Invalid Ipv4 address at %s octet in %s ipv4", octInd[ind], ip))
}
}
return nil
}
func (obj *validation) validateIpv6(ip string) error {
ip = strings.Trim(ip, " \t")
if strings.Count(ip, " ") > 0 || strings.Count(ip, ":") > 7 ||
strings.Count(ip, "::") > 1 || strings.Count(ip, ":::") > 0 ||
strings.Count(ip, ":") == 0 {
return fmt.Errorf(fmt.Sprintf("Invalid ipv6 address %s", ip))
}
if (string(ip[0]) == ":" && string(ip[:2]) != "::") || (string(ip[len(ip)-1]) == ":" && string(ip[len(ip)-2:]) != "::") {
return fmt.Errorf(fmt.Sprintf("Invalid ipv6 address %s", ip))
}
if strings.Count(ip, "::") == 0 && strings.Count(ip, ":") != 7 {
return fmt.Errorf(fmt.Sprintf("Invalid ipv6 address %s", ip))
}
if ip == "::" {
return nil
}
if ip[:2] == "::" {
r := strings.NewReplacer("::", "0:")
ip = r.Replace(ip)
} else if ip[len(ip)-2:] == "::" {
r := strings.NewReplacer("::", ":0")
ip = r.Replace(ip)
} else {
r := strings.NewReplacer("::", ":0:")
ip = r.Replace(ip)
}
octInd := []string{"1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th"}
ipSlice := strings.Split(ip, ":")
for ind, val := range ipSlice {
num, err := strconv.ParseUint(val, 16, 64)
if err != nil || num > 65535 {
return fmt.Errorf(fmt.Sprintf("Invalid Ipv6 address at %s octet in %s ipv6", octInd[ind], ip))
}
}
return nil
}
func (obj *validation) validateHex(hex string) error {
matched, err := regexp.MatchString(`^[0-9a-fA-F]+$|^0[x|X][0-9a-fA-F]+$`, hex)
if err != nil || !matched {
return fmt.Errorf(fmt.Sprintf("Invalid hex value %s", hex))
}
return nil
}
func (obj *validation) validateOid(oid string) error {
segments := strings.Split(oid, ".")
if len(segments) < 2 {
return fmt.Errorf(fmt.Sprintf("Invalid oid value %s", oid))
}
for _, segment := range segments {
_, err := strconv.ParseUint(segment, 10, 32)
if err != nil {
return fmt.Errorf(fmt.Sprintf("Invalid oid value %s", oid))
}
}
return nil
}
func (obj *validation) validateSlice(valSlice []string, sliceType string) error {
indices := []string{}
var err error
for i, val := range valSlice {
if sliceType == "mac" {
err = obj.validateMac(val)
} else if sliceType == "ipv4" {
err = obj.validateIpv4(val)
} else if sliceType == "ipv6" {
err = obj.validateIpv6(val)
} else if sliceType == "hex" {
err = obj.validateHex(val)
} else if sliceType == "oid" {
err = obj.validateOid(val)
} else {
return fmt.Errorf(fmt.Sprintf("Invalid slice type received <%s>", sliceType))
}
if err != nil {
indices = append(indices, fmt.Sprintf("%d", i))
}
}
if len(indices) > 0 {
return fmt.Errorf(
fmt.Sprintf("Invalid %s addresses at indices %s", sliceType, strings.Join(indices, ",")),
)
}
return nil
}
func (obj *validation) validateMacSlice(mac []string) error {
return obj.validateSlice(mac, "mac")
}
func (obj *validation) validateIpv4Slice(ip []string) error {
return obj.validateSlice(ip, "ipv4")
}
func (obj *validation) validateIpv6Slice(ip []string) error {
return obj.validateSlice(ip, "ipv6")
}
func (obj *validation) validateHexSlice(hex []string) error {
return obj.validateSlice(hex, "hex")
}
func (obj *validation) validateOidSlice(oid []string) error {
return obj.validateSlice(oid, "oid")
}
// TODO: restore behavior
// func (obj *validation) createMap(objName string) {
// if obj.constraints == nil {
// obj.constraints = make(map[string]map[string]Constraints)
// }
// _, ok := obj.constraints[objName]
// if !ok {
// obj.constraints[objName] = make(map[string]Constraints)
// }
// }
// TODO: restore behavior
// func (obj *validation) isUnique(objectName, value string, object Constraints) bool {
// if value == "" {
// return true
// }
// obj.createMap("globals")
// _, ok := obj.constraints["globals"][value]
// unique := false
// if !ok {
// obj.constraints["globals"][value] = object
// obj.createMap(objectName)
// obj.constraints[objectName][value] = object
// unique = true
// }
// return unique
// }
// TODO: restore behavior
// func (obj *validation) validateConstraint(objectName []string, value string) bool {
// if value == "" {
// return false
// }
// found := false
// for _, object := range objectName {
// obj_ := strings.Split(object, ".")
// strukt, ok := obj.constraints[obj_[0]]
// if !ok {
// continue
// }
// for _, object := range strukt {
// intf := object.ValueOf(obj_[1])
// if intf == nil {
// continue
// }
// if value == fmt.Sprintf("%v", intf) {
// found = true
// break
// }
// }
// if found {
// break
// }
// }
// return found
// }
func checkClientServerVersionCompatibility(clientVer string, serverVer string, componentName string) error {
c, err := semver.NewVersion(clientVer)
if err != nil {
return fmt.Errorf("client %s version '%s' is not a valid semver", componentName, clientVer)
}
s, err := semver.NewConstraint(serverVer)
if err != nil {
return fmt.Errorf("server %s version '%s' is not a valid semver constraint", componentName, serverVer)
}
err = fmt.Errorf("client %s version '%s' is not semver compatible with server %s version constraint '%s'", componentName, clientVer, componentName, serverVer)
valid, errs := s.Validate(c)
if len(errs) != 0 {
return fmt.Errorf("%v: %v", err, errs)
}
if !valid {
return err
}
return nil
}