-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
288 lines (249 loc) · 6.89 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
package onexgodataflowapi
import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"regexp"
"google.golang.org/grpc"
)
type grpcTransport struct {
clientConnection *grpc.ClientConn
location string
requestTimeout time.Duration
dialTimeout time.Duration
}
type GrpcTransport interface {
SetLocation(value string) GrpcTransport
Location() string
SetRequestTimeout(value time.Duration) GrpcTransport
RequestTimeout() time.Duration
SetDialTimeout(value time.Duration) GrpcTransport
DialTimeout() time.Duration
}
// 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
}
type httpTransport struct {
location string
verify bool
}
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 api struct {
grpc *grpcTransport
http *httpTransport
}
type Api interface {
NewGrpcTransport() GrpcTransport
hasGrpcTransport() bool
NewHttpTransport() HttpTransport
hasHttpTransport() bool
Close() error
}
// NewGrpcTransport sets the underlying transport of the Api as grpc
func (api *api) 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 *api) hasGrpcTransport() bool {
return api.grpc != nil
}
// NewHttpTransport sets the underlying transport of the Api as http
func (api *api) 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 *api) hasHttpTransport() bool {
return api.http != nil
}
// 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
var validation []string
func validationResult() error {
if len(validation) > 0 {
validation = append(validation, "validation errors")
errors := strings.Join(validation, "\n")
validation = nil
return fmt.Errorf(errors)
}
return nil
}
func 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 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 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 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 validateSlice(valSlice []string, sliceType string) error {
indices := []string{}
var err error
for i, val := range valSlice {
if sliceType == "mac" {
err = validateMac(val)
} else if sliceType == "ipv4" {
err = validateIpv4(val)
} else if sliceType == "ipv6" {
err = validateIpv6(val)
} else if sliceType == "hex" {
err = validateHex(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 validateMacSlice(mac []string) error {
return validateSlice(mac, "mac")
}
func validateIpv4Slice(ip []string) error {
return validateSlice(ip, "ipv4")
}
func validateIpv6Slice(ip []string) error {
return validateSlice(ip, "ipv6")
}
func validateHexSlice(hex []string) error {
return validateSlice(hex, "hex")
}