-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathobjects.go
2506 lines (1967 loc) · 80.7 KB
/
objects.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package panos
import (
"encoding/xml"
"errors"
"fmt"
"strings"
"time"
easycsv "github.com/scottdware/go-easycsv"
)
// URLCategory contains a slice of all custom URL category objects.
type URLCategory struct {
XMLName xml.Name `xml:"response"`
Status string `xml:"status,attr"`
Code string `xml:"code,attr"`
URLs []CustomURL `xml:"result>custom-url-category>entry"`
}
// CustomURL contains information about each individual custom URL category object.
type CustomURL struct {
Name string `xml:"name,attr"`
Description string `xml:"description,omitempty"`
Members []string `xml:"list>member,omitempty"`
}
// Recurrance contains the information for external dynamic lists when it comes to how often they are downloaded. Method
// must be one of five-minute, hourly, daily, weekly, monthly. DayOfWeek is the name of the day, such as "tuesday." DayOfMonth
// is specified as a number, ranging from 1-31. Hour must be in 23-hour format, such as "03" for 3 am. The hourly and five-minute
// methods do not require any additional fields. DayOfWeek and DayOfMonth both require the Hour field as well.
type Recurrance struct {
Method string
DayOfWeek string
DayOfMonth int
Hour string
}
// Tags contains information about all tags on the system.
type Tags struct {
Tags []Tag
}
// Tag contains information about each individual tag.
type Tag struct {
Name string
Color string
Comments string
}
// xmlTags is used for parsing all tags on the system.
type xmlTags struct {
XMLName xml.Name `xml:"response"`
Status string `xml:"status,attr"`
Code string `xml:"code,attr"`
Tags []xmlTag `xml:"result>tag>entry"`
}
// xmlTag is used for parsing each individual tag.
type xmlTag struct {
Name string `xml:"name,attr"`
Color string `xml:"color,omitempty"`
Comments string `xml:"comments,omitempty"`
}
// SecurityProfiles contains a list of security profiles to apply to a rule. If you have a security group
// then you can just specify that and omit the individual ones.
type SecurityProfiles struct {
URLFiltering string
FileBlocking string
AntiVirus string
AntiSpyware string
Vulnerability string
Wildfire string
Group string
}
// LogForwarding contains a list of all log forwarding profiles on the device.
type LogForwarding struct {
XMLName xml.Name `xml:"response"`
Status string `xml:"status,attr"`
Code string `xml:"code,attr"`
Profiles []LogForwardingProfile `xml:"result>profiles>entry"`
}
// LogForwardingProfile contains information about each individual log forwarding profile.
type LogForwardingProfile struct {
Name string `xml:"name,attr"`
MatchList []LogForwardingMatchList `xml:"match-list>entry"`
}
// LogForwardingMatchList contains all of the match criteria in a log forwarding profile.
type LogForwardingMatchList struct {
Name string `xml:"name,attr"`
SendToPanorama string `xml:"send-to-panorama"`
LogType string `xml:"log-type"`
Filter string `xml:"filter"`
}
// SecurityGroups contains a list of all security profile groups on the device.
type SecurityGroups struct {
XMLName xml.Name `xml:"response"`
Status string `xml:"status,attr"`
Code string `xml:"code,attr"`
Profiles []SecurityProfileGroup `xml:"result>profile-group>entry"`
}
// SecurityProfileGroup contains information about each individual security profile group.
type SecurityProfileGroup struct {
Name string `xml:"name,attr"`
URLFiltering string `xml:"url-filtering>member"`
FileBlocking string `xml:"file-blocking>member"`
AntiVirus string `xml:"virus>member"`
AntiSpyware string `xml:"spyware>member"`
Vulnerability string `xml:"vulnerability>member"`
DataFiltering string `xml:"data-filtering>member"`
Wildfire string `xml:"wildfire-analysis>member"`
}
var (
tagColors = map[string]string{
"Red": "color1",
"Green": "color2",
"Blue": "color3",
"Yellow": "color4",
"Copper": "color5",
"Orange": "color6",
"Purple": "color7",
"Gray": "color8",
"Light Green": "color9",
"Cyan": "color10",
"Light Gray": "color11",
"Blue Gray": "color12",
"Lime": "color13",
"Black": "color14",
"Gold": "color15",
"Brown": "color16",
}
)
// URLCategory returns a list of all custom URL category 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.
func (p *PaloAlto) URLCategory(devicegroup ...string) (*URLCategory, error) {
var urls URLCategory
xpath := "/config/devices/entry//custom-url-category"
if p.DeviceType != "panorama" && len(devicegroup) > 0 {
return nil, errors.New("you must be connected to a Panorama device when specifying a device-group")
}
if p.DeviceType == "panos" && p.Panorama == true {
xpath = "/config/panorama//custom-url-category"
}
if p.DeviceType == "panos" && p.Panorama == false {
xpath = "/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/profiles/custom-url-category"
}
if p.DeviceType == "panorama" && len(devicegroup) > 0 {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s']/profiles/custom-url-category", devicegroup[0])
}
_, urlData, 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(urlData), &urls); err != nil {
return nil, err
}
if urls.Status != "success" {
return nil, fmt.Errorf("error code %s: %s", urls.Code, errorCodes[urls.Code])
}
return &urls, nil
}
// CreateURLCategory creates a custom URL category to be used in a policy. When specifying multiple URL's, use a
// []string variable for the url parameter (e.g. members := []string{"www.*.com", "*.somesite.net"}). If creating a
// URL category on a Panorama device, specify the device-group as the last parameter.
func (p *PaloAlto) CreateURLCategory(name string, urls []string, description string, devicegroup ...string) error {
var xpath string
var reqError requestError
xmlBody := "<list>"
for _, m := range urls {
xmlBody += fmt.Sprintf("<member>%s</member>", strings.TrimSpace(m))
}
xmlBody += "</list>"
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']/profiles/custom-url-category/entry[@name='%s']", name)
}
if p.DeviceType == "panorama" && p.Shared == true {
xpath = fmt.Sprintf("/config/shared/profiles/custom-url-category/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']/profiles/custom-url-category/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 creating a URL category 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
}
// EditURLCategory adds or removes URL's from the given custom URL category. Action must be add or remove If editing
// a URL category on a Panorama device, specify the device-group as the last parameter.
func (p *PaloAlto) EditURLCategory(action, url, name string, devicegroup ...string) error {
var xpath string
var xmlBody string
var reqError requestError
query := fmt.Sprintf("type=config&key=%s", p.Key)
if p.DeviceType == "panos" {
if action == "add" {
xmlBody += fmt.Sprintf("<member>%s</member>", url)
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/profiles/custom-url-category/entry[@name='%s']/list", name)
query += fmt.Sprintf("&action=set&xpath=%s&element=%s", xpath, xmlBody)
}
if action == "remove" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/profiles/custom-url-category/entry[@name='%s']/list/member[text()='%s']", name, url)
query += fmt.Sprintf("&action=delete&xpath=%s", xpath)
}
}
if p.DeviceType == "panorama" && p.Shared == true {
if action == "add" {
xmlBody = fmt.Sprintf("<member>%s</member>", url)
xpath = fmt.Sprintf("/config/shared/profiles/custom-url-category/entry[@name='%s']/list", name)
query += fmt.Sprintf("&action=set&xpath=%s&element=%s", xpath, xmlBody)
}
if action == "remove" {
xpath = fmt.Sprintf("/config/shared/profiles/custom-url-category/entry[@name='%s']/list/member[text()='%s']", name, url)
query += fmt.Sprintf("&action=delete&xpath=%s", xpath)
}
}
if p.DeviceType == "panorama" && p.Shared == false && len(devicegroup) > 0 {
if action == "add" {
xmlBody = fmt.Sprintf("<member>%s</member>", url)
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s']/profiles/custom-url-category/entry[@name='%s']/list", devicegroup[0], name)
query += fmt.Sprintf("&action=set&xpath=%s&element=%s", xpath, xmlBody)
}
if action == "remove" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s']/profiles/custom-url-category/entry[@name='%s']/list/member[text()='%s']", devicegroup[0], name, url)
query += fmt.Sprintf("&action=delete&xpath=%s", xpath)
}
}
if p.DeviceType == "panorama" && p.Shared == false && len(devicegroup) <= 0 {
return errors.New("you must specify a device-group when editing a URL category on a Panorama device")
}
_, resp, errs := r.Post(p.URI).Query(query).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
}
// DeleteURLCategory removes a custom URL category from the device. If deleting a URL category on a
// Panorama device, specify the device-group as the last parameter.
func (p *PaloAlto) DeleteURLCategory(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']/profiles/custom-url-category/entry[@name='%s']", name)
}
if p.DeviceType == "panorama" && p.Shared == true {
xpath = fmt.Sprintf("/config/shared/profiles/custom-url-category/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']/profiles/custom-url-category/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 a URL category 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
}
// EditGroup will add or remove objects from the specified group type (e.g., "address" or "service"). Action must be
// add or remove. If editing a group on a Panorama device, specify the device-group as the last parameter.
func (p *PaloAlto) EditGroup(objecttype, action, object, group string, devicegroup ...string) error {
var xmlBody string
var xpath string
var reqError requestError
query := fmt.Sprintf("type=config&key=%s", p.Key)
if p.DeviceType == "panos" {
if action == "add" {
xmlBody = fmt.Sprintf("<member>%s</member>", object)
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/address-group/entry[@name='%s']/static", group)
if objecttype == "service" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/service-group/entry[@name='%s']/members", group)
}
query += fmt.Sprintf("&action=set&xpath=%s&element=%s", xpath, xmlBody)
}
if action == "remove" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/address-group/entry[@name='%s']/static/member[text()='%s']", group, object)
if objecttype == "service" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/service-group/entry[@name='%s']/members/member[text()='%s']", group, object)
}
query += fmt.Sprintf("&action=delete&xpath=%s", xpath)
}
}
if p.DeviceType == "panorama" && p.Shared == true {
if action == "add" {
xmlBody = fmt.Sprintf("<member>%s</member>", object)
xpath = fmt.Sprintf("/config/shared/address-group/entry[@name='%s']/static", group)
if objecttype == "service" {
xpath = fmt.Sprintf("/config/shared/service-group/entry[@name='%s']/members", group)
}
query += fmt.Sprintf("&action=set&xpath=%s&element=%s", xpath, xmlBody)
}
if action == "remove" {
xpath = fmt.Sprintf("/config/shared/address-group/entry[@name='%s']/static/member[text()='%s']", group, object)
if objecttype == "service" {
xpath = fmt.Sprintf("/config/shared/service-group/entry[@name='%s']/members/member[text()='%s']", group, object)
}
query += fmt.Sprintf("&action=delete&xpath=%s", xpath)
}
}
if p.DeviceType == "panorama" && p.Shared == false && len(devicegroup) > 0 {
if action == "add" {
xmlBody = fmt.Sprintf("<member>%s</member>", object)
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s']/address-group/entry[@name='%s']/static", devicegroup[0], group)
if objecttype == "service" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s']/service-group/entry[@name='%s']/members", devicegroup[0], group)
}
query += fmt.Sprintf("&action=set&xpath=%s&element=%s", xpath, xmlBody)
}
if action == "remove" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s']/address-group/entry[@name='%s']/static/member[text()='%s']", devicegroup[0], group, object)
if objecttype == "service" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s']/service-group/entry[@name='%s']/members/member[text()='%s']", devicegroup[0], group, object)
}
query += fmt.Sprintf("&action=delete&xpath=%s", xpath)
}
}
if p.DeviceType == "panorama" && p.Shared == false && len(devicegroup) <= 0 {
return errors.New("you must specify a device-group when editing a shared group on a Panorama device")
}
_, resp, errs := r.Post(p.URI).Query(query).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
}
// RenameObject will rename the given object from oldname to the newname. You can rename the following
// object types:
//
// address, address-groups, service, service-groups, tags.
//
// If renaming objects on a Panorama device, specify the device-group as the last parameter.
func (p *PaloAlto) RenameObject(oldname, newname string, devicegroup ...string) error {
var xpath string
var reqError requestError
adObj, _ := p.Addresses()
agObj, _ := p.AddressGroups()
sObj, _ := p.Services()
sgObj, _ := p.ServiceGroups()
tags, _ := p.Tags()
for _, a := range adObj.Addresses {
if oldname == a.Name {
if p.DeviceType == "panos" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/address/entry[@name='%s']", oldname)
_, resp, errs := r.Post(p.URI).Query(fmt.Sprintf("type=config&action=rename&xpath=%s&newname=%s&key=%s", xpath, newname, 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
}
if p.DeviceType == "panorama" {
if p.Shared == true {
xpath = fmt.Sprintf("/config/shared/address/entry[@name='%s']", oldname)
}
if len(devicegroup) > 0 && devicegroup[0] == "shared" {
xpath = fmt.Sprintf("/config/shared/address/entry[@name='%s']", oldname)
}
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], oldname)
}
if p.Shared == false && len(devicegroup) <= 0 {
return errors.New("you must specify a device-group when renaming objects on a Panorama device")
}
_, resp, errs := r.Post(p.URI).Query(fmt.Sprintf("type=config&action=rename&xpath=%s&newname=%s&key=%s", xpath, newname, 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
}
}
}
for _, ag := range agObj.Groups {
if oldname == ag.Name {
if p.DeviceType == "panos" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/address-group/entry[@name='%s']", oldname)
_, resp, errs := r.Post(p.URI).Query(fmt.Sprintf("type=config&action=rename&xpath=%s&newname=%s&key=%s", xpath, newname, 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
}
if p.DeviceType == "panorama" {
if p.Shared == true {
xpath = fmt.Sprintf("/config/shared/address-group/entry[@name='%s']", oldname)
}
if len(devicegroup) > 0 && devicegroup[0] == "shared" {
xpath = fmt.Sprintf("/config/shared/address-group/entry[@name='%s']", oldname)
}
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], oldname)
}
if p.Shared == false && len(devicegroup) <= 0 {
return errors.New("you must specify a device-group when renaming objects on a Panorama device")
}
_, resp, errs := r.Post(p.URI).Query(fmt.Sprintf("type=config&action=rename&xpath=%s&newname=%s&key=%s", xpath, newname, 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
}
}
}
for _, s := range sObj.Services {
if oldname == s.Name {
if p.DeviceType == "panos" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/service/entry[@name='%s']", oldname)
_, resp, errs := r.Post(p.URI).Query(fmt.Sprintf("type=config&action=rename&xpath=%s&newname=%s&key=%s", xpath, newname, 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
}
if p.DeviceType == "panorama" {
if p.Shared == true {
xpath = fmt.Sprintf("/config/shared/service/entry[@name='%s']", oldname)
}
if len(devicegroup) > 0 && devicegroup[0] == "shared" {
xpath = fmt.Sprintf("/config/shared/service/entry[@name='%s']", oldname)
}
if p.Shared == false && len(devicegroup) > 0 && devicegroup[0] != "shared" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s']/service/entry[@name='%s']", devicegroup[0], oldname)
}
if p.Shared == false && len(devicegroup) <= 0 {
return errors.New("you must specify a device-group when renaming objects on a Panorama device")
}
_, resp, errs := r.Post(p.URI).Query(fmt.Sprintf("type=config&action=rename&xpath=%s&newname=%s&key=%s", xpath, newname, 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
}
}
}
for _, sg := range sgObj.Groups {
if oldname == sg.Name {
if p.DeviceType == "panos" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/service-group/entry[@name='%s']", oldname)
_, resp, errs := r.Post(p.URI).Query(fmt.Sprintf("type=config&action=rename&xpath=%s&newname=%s&key=%s", xpath, newname, 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
}
if p.DeviceType == "panorama" {
if p.Shared == true {
xpath = fmt.Sprintf("/config/shared/service-group/entry[@name='%s']", oldname)
}
if len(devicegroup) > 0 && devicegroup[0] == "shared" {
xpath = fmt.Sprintf("/config/shared/service-group/entry[@name='%s']", oldname)
}
if p.Shared == false && len(devicegroup) > 0 && devicegroup[0] != "shared" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s']/service-group/entry[@name='%s']", devicegroup[0], oldname)
}
if p.Shared == false && len(devicegroup) <= 0 {
return errors.New("you must specify a device-group when renaming objects on a Panorama device")
}
_, resp, errs := r.Post(p.URI).Query(fmt.Sprintf("type=config&action=rename&xpath=%s&newname=%s&key=%s", xpath, newname, 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
}
}
}
for _, t := range tags.Tags {
if oldname == t.Name {
if p.DeviceType == "panos" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/tag/entry[@name='%s']", oldname)
_, resp, errs := r.Post(p.URI).Query(fmt.Sprintf("type=config&action=rename&xpath=%s&newname=%s&key=%s", xpath, newname, 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
}
if p.DeviceType == "panorama" {
if p.Shared == true {
xpath = fmt.Sprintf("/config/shared/tag/entry[@name='%s']", oldname)
}
if len(devicegroup) > 0 && devicegroup[0] == "shared" {
xpath = fmt.Sprintf("/config/shared/tag/entry[@name='%s']", oldname)
}
if p.Shared == false && len(devicegroup) > 0 && devicegroup[0] != "shared" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/device-group/entry[@name='%s']/tag/entry[@name='%s']", devicegroup[0], oldname)
}
if p.Shared == false && len(devicegroup) <= 0 {
return errors.New("you must specify a device-group when renaming objects on a Panorama device")
}
_, resp, errs := r.Post(p.URI).Query(fmt.Sprintf("type=config&action=rename&xpath=%s&newname=%s&key=%s", xpath, newname, 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
}
}
}
return nil
}
// CreateExternalDynamicList will create an external dynamic list on the device. Listtype must be one of:
//
// ip, domain, or url
//
// Configuring the recurrance requires you to use the `Recurrance` struct when passing the configuration for this
// parameter - please see the documentation for that struct. If creating an EDL on a Panorama device, specify
// the device-group as the last parameter.
func (p *PaloAlto) CreateExternalDynamicList(listtype string, name string, url string, recurrance *Recurrance, devicegroup ...string) error {
var xpath string
var reqError requestError
var xmlBody string
var recurring string
ver := splitSWVersion(p.SoftwareVersion)
switch recurrance.Method {
case "hourly":
recurring = "<hourly/>"
case "five-minute":
recurring = "<five-minute/>"
case "daily":
recurring = fmt.Sprintf("<daily><at>%s</at></daily>", recurrance.Hour)
case "weekly":
recurring = fmt.Sprintf("<weekly><day-of-week>%s</day-of-week><at>%s</at></weekly>", recurrance.DayOfWeek, recurrance.Hour)
case "monthly":
recurring = fmt.Sprintf("<monthly><day-of-month>%d</day-of-month><at>%s</at></monthly>", recurrance.DayOfMonth, recurrance.Hour)
}
if ver[0] >= 8 {
xmlBody = fmt.Sprintf("<type><%s><recurring>%s</recurring><url>%s</url></%s></type>", listtype, recurring, url, listtype)
}
if ver[0] <= 7 {
xmlBody = fmt.Sprintf("<recurring>%s</recurring><url>%s</url><type>%s</type>", recurring, url, listtype)
}
if p.DeviceType == "panos" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/external-list/entry[@name='%s']", name)
}
if p.DeviceType == "panorama" && p.Shared == true {
xpath = fmt.Sprintf("/config/shared/external-list/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']/external-list/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 creating an external dynamic list 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
}
// DeleteExternalDynamicList removes an external dynamic list from the device. If deleting an EDL on a
// Panorama device, specify the device-group as the last parameter.
func (p *PaloAlto) DeleteExternalDynamicList(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']/external-list/entry[@name='%s']", name)
}
if p.DeviceType == "panorama" && p.Shared == true {
xpath = fmt.Sprintf("/config/shared/external-list/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']/external-list/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 a external dynamic list 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
}
// Tags returns information about all tags on the system. You can (optionally) specify a device-group
// when ran against a Panorama device. If no device-group is specified, then all tags are returned, including
// shared objects if run against a Panorama device.
func (p *PaloAlto) Tags(devicegroup ...string) (*Tags, error) {
var parsedTags xmlTags
var tags Tags
var tcolor string
xpath := "/config//tag"
if p.DeviceType == "panos" {
if p.Panorama == true {
xpath = "/config//tag"
}
if p.Panorama == false {
xpath = "/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/tag"
}
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']/tag", devicegroup[0])
}
}
_, tData, 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(tData), &parsedTags); err != nil {
return nil, err
}
if parsedTags.Status != "success" {
return nil, fmt.Errorf("error code %s: %s", parsedTags.Code, errorCodes[parsedTags.Code])
}
for _, t := range parsedTags.Tags {
tname := t.Name
for k, v := range tagColors {
if t.Color == v {
tcolor = k
}
}
tcomments := t.Comments
tags.Tags = append(tags.Tags, Tag{Name: tname, Color: tcolor, Comments: tcomments})
}
return &tags, nil
}
// CreateTag will add a new tag to the device. You can use the following colors:
//
// Red, Green, Blue, Yellow, Copper, Orange, Purple, Gray, Light Green, Cyan, Light Gray,
// Blue Gray, Lime, Black, Gold, Brown.
//
// If creating a tag on a Panorama device, specify the device-group as the last parameter.
func (p *PaloAlto) CreateTag(name, color, comments string, devicegroup ...string) error {
var xmlBody string
var xpath string
var reqError requestError
xmlBody = fmt.Sprintf("<color>%s</color>", tagColors[color])
if comments != "" {
xmlBody += fmt.Sprintf("<comments>%s</comments>", comments)
}
if p.DeviceType == "panos" {
xpath = fmt.Sprintf("/config/devices/entry[@name='localhost.localdomain']/vsys/entry[@name='vsys1']/tag/entry[@name='%s']", name)
}
if p.DeviceType == "panos" && len(devicegroup) > 0 {
return errors.New("you do not need to specify a device-group on a non-Panorama device")
}
if p.DeviceType == "panorama" && p.Shared == true {
xpath = fmt.Sprintf("/config/shared/tag/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']/tag/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 creating a tag 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
}
// DeleteTag will remove a tag from the device. If deleting a tag on a Panorama device, specify the
// device-group as the last parameter.
func (p *PaloAlto) DeleteTag(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']/tag/entry[@name='%s']", name)
}
if p.DeviceType == "panorama" && p.Shared == true {
xpath = fmt.Sprintf("/config/shared/tag/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']/tag/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 a tag 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
}
// TagObject will apply the given tag to the specified address or service object(s). To apply multiple tags,
// separate them by a comma e.g.: "tag1, tag2". If you have address/service objects with the same name,
// then the tag(s) will be applied to all that match. If tagging objects on a Panorama device,
// specify the device-group as the last parameter.
func (p *PaloAlto) TagObject(tag, object string, devicegroup ...string) error {
var xpath, xmlBody string
var reqError requestError
tags := stringToSlice(tag)
adObj, _ := p.Addresses()
agObj, _ := p.AddressGroups()
sObj, _ := p.Services()
sgObj, _ := p.ServiceGroups()
for _, t := range tags {
xmlBody += fmt.Sprintf("<member>%s</member>", strings.TrimSpace(t))
}