forked from giantswarm/kemp-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtualservices.go
387 lines (328 loc) · 10.5 KB
/
virtualservices.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
package kempclient
import (
"encoding/xml"
"fmt"
"net"
"strconv"
"strings"
"github.com/juju/errgo"
)
// The type of the virutalservice.
//
// Doc from webui:
// > Select the type of service that will be run over this Virtual Service.
// > The LoadMaster automatically tries to determine the type of the service
// > but the user can override this.
const (
VStypeHTTP = "http"
VStypeGeneric = "gen"
VStypeSTARTTLS = "tls"
VStypeRemoteTerminal = "ts"
VStypeLogInsight = "log"
)
// These are the options for the AddVia field.
// In the kemp webui it relates to the realserver option under Advanced Properties > Add HTTP Headers
//
// Doc from the webui
// > Select which headers are to be added to HTTP requests.
// > X-ClientSide and X-Forwarded-For are only added to Non-Transparent Connections.
const (
VSAddViaLegacyXClientSide = "0"
VSAddViaNone = "2"
VSAddViaXClientSideWithVia = "3"
VSAddViaXClientSideNoVia = "4"
VSAddViaXForwardedForWithVia = "1"
VSAddViaXForwardedForNoVia = "5"
VSAddViaViaOnly = "6"
)
type VirtualServiceListResponse struct {
Debug string `xml:",innerxml"`
XMLName xml.Name `xml:"Response"`
Data VirtualServiceList `xml:"Success>Data"`
}
type VirtualServiceList struct {
VS []VirtualService `xml:",any"`
}
type Header struct {
Key string
Value string
}
type VirtualServiceParams struct {
Name string
IPAddress string
Port string
Protocol string
CheckType string
CheckURL string
CheckPort string
SSLAcceleration bool
Transparent bool
AddVia string
VStype string
ExtraRequestHeaderKey string
ExtraRequestHeaderValue string
Headers map[string]string
ContentRequestRules []string
}
type VirtualServiceResponse struct {
Debug string `xml:",innerxml"`
XMLName xml.Name `xml:"Response"`
VS VirtualService `xml:"Success>Data"`
}
type VirtualService struct {
ID int `xml:"Index"`
Name string `xml:"NickName"`
IPAddress string `xml:"VSAddress"`
Port string `xml:"VSPort"`
Protocol string
Status string
Enable string
SSLReverse string
SSLReencrypt string
Intercept string
InterceptOpts []string `xml:"InterceptOpts>Opt"`
AlertThreshold string
Transactionlimit string
Transparent string
ServerInit string
StartTLSMode string
Idletime string
Cache string
Compress string
Verify string
UseforSnat string
ForceL7 string
ClientCert string
ErrorCode string
CertFile string
CheckURL string `xml:"CheckUrl"`
CheckUse11 string `xml:"CheckUse1.1"`
MatchLen string
CheckUseGet string
SSLRewrite string
VStype string
FollowVSID int
Schedule string
CheckType string
PersistTimeout string
SSLAcceleration string
CheckPort string
NRules string
NRequestRules string
NResponseRules string
NPreProcessRules string
EspEnabled string
InputAuthMode string
OutputAuthMode string
MasterVS string
MasterVSID int
AddVia string
TlsType string
NeedHostName string
OCSPVerify string
NumberOfRSs string
Rs []RealServer `xml:"Rs"`
ExtraHdrKey string
ExtraHdrValue string
}
func (c *Client) ListVirtualServices() ([]VirtualService, error) {
parameters := make(map[string]string)
data := VirtualServiceListResponse{}
err := c.Request("listvs", parameters, &data)
if err != nil {
return []VirtualService{}, errgo.NoteMask(err, "kemp could not list virtual services", errgo.Any)
}
if c.debug {
fmt.Println("DEBUG:", data.Debug)
}
return data.Data.VS, nil
}
func (c *Client) FindVirtualServiceByName(name string) (VirtualService, error) {
list, err := c.ListVirtualServices()
if err != nil {
return VirtualService{}, errgo.Mask(err)
}
for _, vs := range list {
if vs.Name == name {
return vs, nil
}
}
return VirtualService{}, nil
}
func (c *Client) ShowVirtualServiceByData(ip, port, protocol string) (VirtualService, error) {
parameters := make(map[string]string)
parameters["vs"] = ip
parameters["port"] = port
parameters["prot"] = protocol
return c.showVirtualService(parameters)
}
func (c *Client) ShowVirtualServiceByID(id int) (VirtualService, error) {
parameters := make(map[string]string)
parameters["vs"] = strconv.Itoa(id)
return c.showVirtualService(parameters)
}
func (c *Client) showVirtualService(parameters map[string]string) (VirtualService, error) {
data := VirtualServiceResponse{}
err := c.Request("showvs", parameters, &data)
if err != nil {
return VirtualService{}, errgo.NoteMask(err, fmt.Sprintf("kemp unable to show virtual service '%#v'", parameters), errgo.Any)
}
if c.debug {
fmt.Println("DEBUG:", data.Debug)
}
return data.VS, nil
}
func (c *Client) DeleteVirtualServiceByID(id int) error {
parameters := make(map[string]string)
parameters["vs"] = strconv.Itoa(id)
return c.deleteVirtualService(parameters)
}
func (c *Client) DeleteVirtualServiceByData(ip, port, protocol string) error {
parameters := make(map[string]string)
parameters["vs"] = ip
parameters["port"] = port
parameters["prot"] = protocol
return c.deleteVirtualService(parameters)
}
func (c *Client) deleteVirtualService(parameters map[string]string) error {
data := VirtualServiceResponse{}
err := c.Request("delvs", parameters, &data)
if err != nil {
return errgo.NoteMask(err, fmt.Sprintf("kemp unable to delete virtual service '%#v'", parameters), errgo.Any)
}
if c.debug {
fmt.Println("DEBUG:", data.Debug)
}
return nil
}
func (c *Client) UpdateVirtualService(id int, vs VirtualServiceParams) (VirtualService, error) {
parameters := make(map[string]string)
parameters["vs"] = strconv.Itoa(id)
if vs.IPAddress != "" {
parameters["vsaddress"] = vs.IPAddress
}
if vs.Port != "" {
parameters["vsport"] = vs.Port
}
if vs.Protocol != "" {
parameters["prot"] = vs.Protocol
}
c.mapVirtualServiceParamsToRequestParams(vs, parameters)
for key, value := range vs.Headers {
// Deleting the content rule http header as there isn't a truly update operation
if err := c.DeleteHeaderContentRule(strings.Replace(vs.Name+key, "-", "", -1)); err != nil {
fmt.Println(err)
}
if err := c.AddHeaderContentRule(strings.Replace(vs.Name+key, "-", "", -1), key, value); err != nil {
return VirtualService{}, err
}
}
data := VirtualServiceResponse{}
err := c.Request("modvs", parameters, &data)
if err != nil {
return VirtualService{}, errgo.NoteMask(err, fmt.Sprintf("kemp unable to update virtual service '%#v'", parameters), errgo.Any)
}
if c.debug {
fmt.Println("DEBUG:", data.Debug)
}
for key := range vs.Headers {
parameters["rule"] = strings.Replace(vs.Name+key, "-", "", -1)
err := c.Request("addrequestrule", parameters, &data)
if err != nil {
return VirtualService{}, errgo.NoteMask(err, fmt.Sprintf("kemp unable to add rule to the virtual service '%#v'", parameters), errgo.Any)
}
}
for _, rule := range vs.ContentRequestRules {
parameters["rule"] = rule
err := c.Request("addrequestrule", parameters, &data)
if err != nil {
return VirtualService{}, errgo.NoteMask(err, fmt.Sprintf("kemp unable to add rule to the virtual service '%#v'", parameters), errgo.Any)
}
}
return data.VS, nil
}
func (c *Client) AddVirtualService(vs VirtualServiceParams) (VirtualService, error) {
parameters := make(map[string]string)
if net.ParseIP(vs.IPAddress) == nil {
return VirtualService{}, errgo.Newf("%s is not a valid ip address", vs.IPAddress)
}
if vs.Port == "" {
return VirtualService{}, errgo.New("A virtual service needs a port")
}
if vs.Protocol != "tcp" && vs.Protocol != "udp" {
return VirtualService{}, errgo.New("The protocol of a virtual service is either tcp or udp")
}
parameters["vs"] = vs.IPAddress
parameters["port"] = vs.Port
parameters["prot"] = vs.Protocol
c.mapVirtualServiceParamsToRequestParams(vs, parameters)
if err := c.AddProtoPortHeaderRequestRules(); err != nil {
return VirtualService{}, errgo.New("An error occurred when trying to add X-Forwarded-Proto and X-Forwarded-Port delete headers content rules")
}
for key, value := range vs.Headers {
// Deleting the content rule http header as there isn't a truly update operation
if err := c.DeleteHeaderContentRule(strings.Replace(vs.Name+key, "-", "", -1)); err != nil {
fmt.Println(err)
}
if err := c.AddHeaderContentRule(strings.Replace(vs.Name+key, "-", "", -1), key, value); err != nil {
return VirtualService{}, err
}
}
data := VirtualServiceResponse{}
err := c.Request("addvs", parameters, &data)
if err != nil {
return VirtualService{}, errgo.NoteMask(err, fmt.Sprintf("kemp unable to add virtual service '%#v'", parameters), errgo.Any)
}
if c.debug {
fmt.Println("DEBUG:", data.Debug)
}
for key := range vs.Headers {
parameters["rule"] = strings.Replace(vs.Name+key, "-", "", -1)
err := c.Request("addrequestrule", parameters, &data)
if err != nil {
return VirtualService{}, errgo.NoteMask(err, fmt.Sprintf("kemp unable to add rule to the virtual service '%#v'", parameters), errgo.Any)
}
}
for _, rule := range vs.ContentRequestRules {
parameters["rule"] = rule
err := c.Request("addrequestrule", parameters, &data)
if err != nil {
return VirtualService{}, errgo.NoteMask(err, fmt.Sprintf("kemp unable to add rule to the virtual service '%#v'", parameters), errgo.Any)
}
}
return data.VS, nil
}
func (c *Client) mapVirtualServiceParamsToRequestParams(vs VirtualServiceParams, parameters map[string]string) {
if vs.Name != "" {
parameters["nickname"] = vs.Name
}
if vs.Transparent {
parameters["transparent"] = "Y"
} else {
parameters["transparent"] = "N"
}
if vs.CheckType != "" {
parameters["checktype"] = vs.CheckType
}
if vs.CheckURL != "" {
parameters["checkurl"] = vs.CheckURL
}
if vs.CheckPort != "" {
parameters["checkport"] = vs.CheckPort
}
if vs.SSLAcceleration {
parameters["sslacceleration"] = "Y"
} else {
parameters["sslacceleration"] = "N"
}
if vs.AddVia != "" {
parameters["addvia"] = vs.AddVia
}
if vs.ExtraRequestHeaderKey != "" && vs.ExtraRequestHeaderValue != "" {
parameters["extrahdrkey"] = vs.ExtraRequestHeaderKey
parameters["extrahdrvalue"] = vs.ExtraRequestHeaderValue
}
if vs.VStype != "" {
parameters["vstype"] = vs.VStype
}
}