-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathaddress.go
370 lines (299 loc) · 12.4 KB
/
address.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
package panos
import (
"encoding/xml"
"errors"
"fmt"
"strings"
)
// AddressObjects contains a slice of all address objects.
type AddressObjects struct {
XMLName xml.Name `xml:"response"`
Status string `xml:"status,attr"`
Code string `xml:"code,attr"`
Addresses []Address `xml:"result>address>entry"`
}
// Address contains information about each individual address object.
type Address struct {
Name string `xml:"name,attr"`
IPAddress string `xml:"ip-netmask,omitempty"`
IPRange string `xml:"ip-range,omitempty"`
FQDN string `xml:"fqdn,omitempty"`
Description string `xml:"description,omitempty"`
Tag []string `xml:"tag>member,omitempty"`
}
// AddressGroups contains a slice of all address groups.
type AddressGroups struct {
Groups []AddressGroup
}
// AddressGroup contains information about each individual address group.
type AddressGroup struct {
Name string
Type string
Members []string
DynamicFilter string
Description string
Tag []string
}
// xmlAddressGroups is used for parsing of all address groups.
type xmlAddressGroups struct {
XMLName xml.Name `xml:"response"`
Status string `xml:"status,attr"`
Code string `xml:"code,attr"`
Groups []xmlAddressGroup `xml:"result>address-group>entry"`
}
// xmlAddressGroup is used for parsing each individual address group.
type xmlAddressGroup struct {
Name string `xml:"name,attr"`
Members []string `xml:"static>member,omitempty"`
DynamicFilter string `xml:"dynamic>filter,omitempty"`
Description string `xml:"description,omitempty"`
Tag []string `xml:"tag>member,omitempty"`
}
// Addresses returns information about all of the address objects. You can (optionally) specify a device-group
// when ran against a Panorama device. If no device-group is specified, then all objects are returned, including
// shared objects if run against a Panorama device.
func (p *PaloAlto) Addresses(devicegroup ...string) (*AddressObjects, error) {
var addrs AddressObjects
xpath := "/config//address"
if p.DeviceType == "panos" {
if p.Panorama == true {
xpath = "/config//address"
}
if p.Panorama == false {
xpath = "/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/address"
}
if len(devicegroup) > 0 && len(devicegroup[0]) > 0 {
return nil, errors.New("you must be connected to a Panorama device when specifying a device-group")
}
}
if p.DeviceType == "panorama" {
if len(devicegroup) > 0 && len(devicegroup[0]) > 0 {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s']/address", devicegroup[0])
}
}
_, addrData, errs := r.Get(p.URI).Query(fmt.Sprintf("type=config&action=get&xpath=%s&key=%s", xpath, p.Key)).End()
if errs != nil {
return nil, errs[0]
}
if err := xml.Unmarshal([]byte(addrData), &addrs); err != nil {
return nil, err
}
if addrs.Status != "success" {
return nil, fmt.Errorf("error code %s: %s", addrs.Code, errorCodes[addrs.Code])
}
return &addrs, nil
}
// AddressGroups returns information about all of the address groups. You can (optionally) specify a device-group
// when ran against a Panorama device. If no device-group is specified, then all address groups are returned, including
// shared objects if run against a Panorama device.
func (p *PaloAlto) AddressGroups(devicegroup ...string) (*AddressGroups, error) {
var parsedGroups xmlAddressGroups
var groups AddressGroups
xpath := "/config//address-group"
if p.DeviceType == "panos" {
if p.Panorama == true {
xpath = "/config//address-group"
}
if p.Panorama == false {
xpath = "/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/address-group"
}
if len(devicegroup) > 0 && len(devicegroup[0]) > 0 {
return nil, errors.New("you must be connected to a Panorama device when specifying a device-group")
}
}
if p.DeviceType == "panorama" {
if len(devicegroup) > 0 && len(devicegroup[0]) > 0 {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s']/address-group", devicegroup[0])
}
}
_, groupData, errs := r.Get(p.URI).Query(fmt.Sprintf("type=config&action=get&xpath=%s&key=%s", xpath, p.Key)).End()
if errs != nil {
return nil, errs[0]
}
if err := xml.Unmarshal([]byte(groupData), &parsedGroups); err != nil {
return nil, err
}
if parsedGroups.Status != "success" {
return nil, fmt.Errorf("error code %s: %s", parsedGroups.Code, errorCodes[parsedGroups.Code])
}
for _, g := range parsedGroups.Groups {
gname := g.Name
gtype := "Static"
gmembers := g.Members
gfilter := strings.TrimSpace(g.DynamicFilter)
gdesc := g.Description
gtag := g.Tag
if g.DynamicFilter != "" {
gtype = "Dynamic"
}
groups.Groups = append(groups.Groups, AddressGroup{Name: gname, Type: gtype, Members: gmembers, DynamicFilter: gfilter, Description: gdesc, Tag: gtag})
}
return &groups, nil
}
// CreateAddress will add a new address object to the device. Addrtype should be one of ip, range, or fqdn. If creating an address
// object on a Panorama device, specify the device-group as the last parameter.
func (p *PaloAlto) CreateAddress(name, addrtype, address, description string, devicegroup ...string) error {
var xmlBody string
var xpath string
var reqError requestError
switch addrtype {
case "ip":
xmlBody = fmt.Sprintf("<ip-netmask>%s</ip-netmask>", strings.TrimSpace(address))
case "range":
xmlBody = fmt.Sprintf("<ip-range>%s</ip-range>", strings.TrimSpace(address))
case "fqdn":
xmlBody = fmt.Sprintf("<fqdn>%s</fqdn>", strings.TrimSpace(address))
}
if description != "" {
xmlBody += fmt.Sprintf("<description>%s</description>", description)
}
if p.DeviceType == "panos" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/address/entry[@name='%s']", name)
}
if p.DeviceType == "panorama" {
if p.Shared == true {
xpath = fmt.Sprintf("/config/shared/address/entry[@name='%s']", name)
}
if len(devicegroup) > 0 && devicegroup[0] == "shared" {
xpath = fmt.Sprintf("/config/shared/address/entry[@name='%s']", name)
}
if p.Shared == false && len(devicegroup) > 0 && devicegroup[0] != "shared" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s']/address/entry[@name='%s']", devicegroup[0], name)
}
if p.Shared == false && len(devicegroup) <= 0 {
return errors.New("you must specify a device-group when creating address objects on a Panorama device")
}
}
_, resp, errs := r.Post(p.URI).Query(fmt.Sprintf("type=config&action=set&xpath=%s&element=%s&key=%s", xpath, xmlBody, p.Key)).End()
if errs != nil {
return errs[0]
}
if err := xml.Unmarshal([]byte(resp), &reqError); err != nil {
return err
}
if reqError.Status != "success" {
return fmt.Errorf("error code %s: %s", reqError.Code, errorCodes[reqError.Code])
}
return nil
}
// CreateAddressGroup will create a new static or dynamic address group on the device, as specified by the grouptype
// parameter. If you are creating a static address group, you must add pre-existing members to the group by specifying them using a
// []string type, for the members parameter. You can specify this as a variable like so:
//
// hosts := []string{"web-server", "db-server", "mail-server"}
//
// When creating a dynamic address group, the match criteria (tags) must be a string type, specified for the members parameter like so:
//
// match := "'web-servers' and 'dmz-servers'"
//
// If you do not want to include a description, just leave the parameter blank using double-quotes (""). If creating an address group on
// a Panorama device, specify the device-group as the last parameter.
func (p *PaloAlto) CreateAddressGroup(name, grouptype string, members interface{}, description string, devicegroup ...string) error {
var xmlBody string
var xpath string
var reqError requestError
switch grouptype {
case "static":
staticMembers := members.([]string)
if len(staticMembers) <= 0 {
return errors.New("you cannot create a static address group without any members")
}
xmlBody = "<static>"
for _, member := range staticMembers {
xmlBody += fmt.Sprintf("<member>%s</member>", strings.TrimSpace(member))
}
xmlBody += "</static>"
case "dynamic":
criteria := members.(string)
xmlBody = fmt.Sprintf("<dynamic><filter>%s</filter></dynamic>", criteria)
}
if description != "" {
xmlBody += fmt.Sprintf("<description>%s</description>", description)
}
if p.DeviceType == "panos" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/address-group/entry[@name='%s']", name)
}
if p.DeviceType == "panorama" {
if p.Shared == true {
xpath = fmt.Sprintf("/config/shared/address-group/entry[@name='%s']", name)
}
if len(devicegroup) > 0 && devicegroup[0] == "shared" {
xpath = fmt.Sprintf("/config/shared/address-group/entry[@name='%s']", name)
}
if p.Shared == false && len(devicegroup) > 0 && devicegroup[0] != "shared" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s']/address-group/entry[@name='%s']", devicegroup[0], name)
}
if p.Shared == false && len(devicegroup) <= 0 {
return errors.New("you must specify a device-group when creating address groups on a Panorama device")
}
}
_, resp, errs := r.Post(p.URI).Query(fmt.Sprintf("type=config&action=set&xpath=%s&element=%s&key=%s", xpath, xmlBody, p.Key)).End()
if errs != nil {
return errs[0]
}
if err := xml.Unmarshal([]byte(resp), &reqError); err != nil {
return err
}
if reqError.Status != "success" {
return fmt.Errorf("error code %s: %s", reqError.Code, errorCodes[reqError.Code])
}
return nil
}
// DeleteAddress will remove an address object from the device. If deleting an address object on a
// Panorama device, specify the device-group as the last parameter.
func (p *PaloAlto) DeleteAddress(name string, devicegroup ...string) error {
var xpath string
var reqError requestError
if p.DeviceType == "panos" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/address/entry[@name='%s']", name)
}
if p.DeviceType == "panorama" && p.Shared == true {
xpath = fmt.Sprintf("/config/shared/address/entry[@name='%s']", name)
}
if p.DeviceType == "panorama" && p.Shared == false && len(devicegroup) > 0 {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s']/address/entry[@name='%s']", devicegroup[0], name)
}
if p.DeviceType == "panorama" && p.Shared == false && len(devicegroup) <= 0 {
return errors.New("you must specify a device-group when deleting address objects on a Panorama device")
}
_, resp, errs := r.Get(p.URI).Query(fmt.Sprintf("type=config&action=delete&xpath=%s&key=%s", xpath, p.Key)).End()
if errs != nil {
return errs[0]
}
if err := xml.Unmarshal([]byte(resp), &reqError); err != nil {
return err
}
if reqError.Status != "success" {
return fmt.Errorf("error code %s: %s", reqError.Code, errorCodes[reqError.Code])
}
return nil
}
// DeleteAddressGroup will remove an address group from the device. If deleting an address group on a
// Panorama device, specify the device-group as the last parameter.
func (p *PaloAlto) DeleteAddressGroup(name string, devicegroup ...string) error {
var xpath string
var reqError requestError
if p.DeviceType == "panos" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/address-group/entry[@name='%s']", name)
}
if p.DeviceType == "panorama" && p.Shared == true {
xpath = fmt.Sprintf("/config/shared/address-group/entry[@name='%s']", name)
}
if p.DeviceType == "panorama" && p.Shared == false && len(devicegroup) > 0 {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s']/address-group/entry[@name='%s']", devicegroup[0], name)
}
if p.DeviceType == "panorama" && p.Shared == false && len(devicegroup) <= 0 {
return errors.New("you must specify a device-group when deleting address groups on a Panorama device")
}
_, resp, errs := r.Get(p.URI).Query(fmt.Sprintf("type=config&action=delete&xpath=%s&key=%s", xpath, p.Key)).End()
if errs != nil {
return errs[0]
}
if err := xml.Unmarshal([]byte(resp), &reqError); err != nil {
return err
}
if reqError.Status != "success" {
return fmt.Errorf("error code %s: %s", reqError.Code, errorCodes[reqError.Code])
}
return nil
}