forked from jamesboswell/go-junos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sd.go
1051 lines (893 loc) · 27.6 KB
/
sd.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 junos
import (
"encoding/xml"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/scottdware/go-rested"
)
// Addresses contains a list of address objects.
type Addresses struct {
Addresses []Address `xml:"address"`
}
// An Address contains information about each individual address object.
type Address struct {
ID int `xml:"id"`
Name string `xml:"name"`
AddressType string `xml:"address-type"`
Description string `xml:"description"`
IPAddress string `xml:"ip-address"`
Hostname string `xml:"host-name"`
}
// GroupMembers contains a list of all the members within a address or service
// group.
type GroupMembers struct {
Members []Member `xml:"members>member"`
}
// Member contains information about each individual group member.
type Member struct {
ID int `xml:"id"`
Name string `xml:"name"`
}
// Services contains a list of service objects.
type Services struct {
Services []Service `xml:"service"`
}
// A Service contains information about each individual service object.
type Service struct {
ID int `xml:"id"`
Name string `xml:"name"`
IsGroup bool `xml:"is-group"`
Description string `xml:"description"`
}
// A Policy contains information about each individual firewall policy.
type Policy struct {
ID int `xml:"id"`
Name string `xml:"name"`
Description string `xml:"description"`
}
// Policies contains a list of firewall policies.
type Policies struct {
Policies []Policy `xml:"firewall-policy"`
}
// SecurityDevices contains a list of security devices.
type SecurityDevices struct {
XMLName xml.Name `xml:"devices"`
Devices []SecurityDevice `xml:"device"`
}
// A SecurityDevice contains information about each individual security device.
type SecurityDevice struct {
ID int `xml:"id"`
Family string `xml:"device-family"`
Platform string `xml:"platform"`
IPAddress string `xml:"device-ip"`
Name string `xml:"name"`
}
// Variables contains a list of all polymorphic (variable) objects.
type Variables struct {
Variables []Variable `xml:"variable-definition"`
}
// A Variable contains information about each individual polymorphic (variable) object.
type Variable struct {
ID int `xml:"id"`
Name string `xml:"name"`
Description string `xml:"description"`
}
// VariableManagement contains our session state when updating a polymorphic (variable) object.
type VariableManagement struct {
Devices []SecurityDevice
Space *Space
}
// existingVariable contains all of our information in regards to said polymorphic (variable) object.
type existingVariable struct {
XMLName xml.Name `xml:"variable-definition"`
Name string `xml:"name"`
Description string `xml:"description"`
Type string `xml:"type"`
Version int `xml:"edit-version"`
DefaultName string `xml:"default-name"`
DefaultValue string `xml:"default-value-detail>default-value"`
VariableValuesList []variableValues `xml:"variable-values-list>variable-values"`
}
// variableValues contains the information for each device/object tied to the polymorphic (variable) object.
type variableValues struct {
XMLName xml.Name `xml:"variable-values"`
DeviceMOID string `xml:"device>moid"`
DeviceName string `xml:"device>name"`
VariableValue string `xml:"variable-value-detail>variable-value"`
VariableName string `xml:"variable-value-detail>name"`
}
// existingAddress contains information about an address object before modification.
type existingAddress struct {
Name string `xml:"name"`
EditVersion int `xml:"edit-version"`
Description string `xml:"description"`
}
// XML for creating an address object.
var addressesXML = `
<address>
<name>%s</name>
<address-type>%s</address-type>
<host-name/>
<edit-version/>
<members/>
<address-version>IPV4</address-version>
<definition-type>CUSTOM</definition-type>
<ip-address>%s</ip-address>
<description>%s</description>
</address>
`
// XML for creating a dns-host address object.
var dnsXML = `
<address>
<name>%s</name>
<address-type>%s</address-type>
<host-name>%s</host-name>
<edit-version/>
<members/>
<address-version>IPV4</address-version>
<definition-type>CUSTOM</definition-type>
<ip-address/>
<description>%s</description>
</address>
`
var modifyAddressXML = `
<address>
<name>%s</name>
<address-type>%s</address-type>
<host-name/>
<edit-version>%d</edit-version>
<members/>
<address-version>IPV4</address-version>
<definition-type>CUSTOM</definition-type>
<ip-address>%s</ip-address>
<description>%s</description>
</address>
`
var modifyDNSXML = `
<address>
<name>%s</name>
<address-type>%s</address-type>
<host-name>%s</host-name>
<edit-version>%d</edit-version>
<members/>
<address-version>IPV4</address-version>
<definition-type>CUSTOM</definition-type>
<ip-address/>
<description>%s</description>
</address>
`
// XML for creating a service object.
var serviceXML = `
<service>
<name>%s</name>
<description>%s</description>
<is-group>false</is-group>
<protocols>
<protocol>
<name>%s</name>
<dst-port>%s</dst-port>
<sunrpc-protocol-type>%s</sunrpc-protocol-type>
<msrpc-protocol-type>%s</msrpc-protocol-type>
<protocol-number>%d</protocol-number>
<protocol-type>%s</protocol-type>
<disable-timeout>%s</disable-timeout>
%s
</protocol>
</protocols>
</service>
`
// XML for adding an address group.
var addressGroupXML = `
<address>
<name>%s</name>
<address-type>GROUP</address-type>
<host-name/>
<edit-version/>
<address-version>IPV4</address-version>
<definition-type>CUSTOM</definition-type>
<description>%s</description>
</address>
`
// XML for adding a service group.
var serviceGroupXML = `
<service>
<name>%s</name>
<is-group>true</is-group>
<description>%s</description>
</service>
`
// XML for removing an address or service from a group.
var removeXML = `
<diff>
<remove sel="%s/members/member[name='%s']"/>
</diff>
`
// XML for adding addresses or services to a group.
var addGroupMemberXML = `
<diff>
<add sel="%s/members">
<member>
<name>%s</name>
</member>
</add>
</diff>
`
// XML for renaming an address or service object.
var renameXML = `
<diff>
<replace sel="%s/name">
<name>%s</name>
</replace>
</diff>
`
// XML for updating a security device.
var updateDeviceXML = `
<update-devices>
<sd-ids>
<id>%d</id>
</sd-ids>
<service-types>
<service-type>POLICY</service-type>
</service-types>
<update-options>
<enable-policy-rematch-srx-only>false</enable-policy-rematch-srx-only>
</update-options>
</update-devices>
`
// XML for publishing a changed policy.
var publishPolicyXML = `
<publish>
<policy-ids>
<policy-id>%d</policy-id>
</policy-ids>
</publish>
`
// XML for adding a new variable object.
var createVariableXML = `
<variable-definition>
<name>%s</name>
<type>%s</type>
<description>%s</description>
<context>DEVICE</context>
<default-name>%s</default-name>
<default-value-detail>
<default-value>%d</default-value>
</default-value-detail>
</variable-definition>
`
// XML for modifying variable objects.
var modifyVariableXML = `
<variable-definition>
<name>%s</name>
<type>%s</type>
<description>%s</description>
<edit-version>%d</edit-version>
<context>DEVICE</context>
<default-name>%s</default-name>
<default-value-detail>
<default-value>%s</default-value>
</default-value-detail>
<variable-values-list>
%s
</variable-values-list>
</variable-definition>
`
// getDeviceID returns the ID of a managed device.
func (s *Space) getSDDeviceID(device interface{}) (int, error) {
var err error
var deviceID int
ipRegex := regexp.MustCompile(`(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})`)
devices, err := s.SecurityDevices()
if err != nil {
return 0, err
}
switch device.(type) {
case int:
deviceID = device.(int)
case string:
if ipRegex.MatchString(device.(string)) {
for _, d := range devices.Devices {
if d.IPAddress == device.(string) {
deviceID = d.ID
}
}
}
for _, d := range devices.Devices {
if d.Name == device.(string) {
deviceID = d.ID
}
}
}
return deviceID, nil
}
// getObjectID returns the ID of the address or service object.
func (s *Space) getObjectID(object interface{}, otype string) (int, error) {
var err error
var objectID int
var services *Services
ipRegex := regexp.MustCompile(`(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d+)`)
if otype == "service" {
services, err = s.Services(object.(string))
}
objects, err := s.Addresses(object.(string))
if err != nil {
return 0, err
}
switch object.(type) {
case int:
objectID = object.(int)
case string:
if otype == "service" {
for _, o := range services.Services {
if o.Name == object {
objectID = o.ID
}
}
}
if ipRegex.MatchString(object.(string)) {
for _, o := range objects.Addresses {
if o.IPAddress == object {
objectID = o.ID
}
}
}
for _, o := range objects.Addresses {
if o.Name == object {
objectID = o.ID
}
}
}
return objectID, nil
}
// getPolicyID returns the ID of a firewall policy.
func (s *Space) getPolicyID(object string) (int, error) {
var err error
var objectID int
objects, err := s.Policies()
if err != nil {
return 0, err
}
for _, o := range objects.Policies {
if o.Name == object {
objectID = o.ID
}
}
return objectID, nil
}
// getVariableID returns the ID of a polymorphic (variable) object.
func (s *Space) getVariableID(variable string) (int, error) {
var err error
var variableID int
vars, err := s.Variables()
if err != nil {
return 0, err
}
for _, v := range vars.Variables {
if v.Name == variable {
variableID = v.ID
}
}
return variableID, nil
}
// getAddrTypeIP returns the address type and IP address of the given address object.
func (s *Space) getAddrTypeIP(address string) []string {
var addrType, ipaddr string
r := regexp.MustCompile(`(\d+\.\d+\.\d+\.\d+)(\/\d+)?`)
rDNS := regexp.MustCompile(`[-\w\.]*\.(com|net|org|us|gov)$`)
match := r.FindStringSubmatch(address)
if rDNS.MatchString(address) {
addrType = "DNS"
ipaddr = address
return []string{addrType, ipaddr}
}
switch match[2] {
case "", "/32":
addrType = "IPADDRESS"
ipaddr = match[1]
default:
addrType = "NETWORK"
ipaddr = address
}
return []string{addrType, ipaddr}
}
// modifyVariableContent creates the XML we use when modifying an existing polymorphic (variable) object.
func (s *Space) modifyVariableContent(data *existingVariable, moid, firewall, address string, vid int) string {
var varValuesList string
for _, d := range data.VariableValuesList {
varValuesList += fmt.Sprintf("<variable-values><device><moid>%s</moid><name>%s</name></device>", d.DeviceMOID, d.DeviceName)
varValuesList += fmt.Sprintf("<variable-value-detail><variable-value>%s</variable-value><name>%s</name></variable-value-detail></variable-values>", d.VariableValue, d.VariableName)
}
varValuesList += fmt.Sprintf("<variable-values><device><moid>%s</moid><name>%s</name></device>", moid, firewall)
varValuesList += fmt.Sprintf("<variable-value-detail><variable-value>net.juniper.jnap.sm.om.jpa.AddressEntity:%d</variable-value><name>%s</name></variable-value-detail></variable-values>", vid, address)
return varValuesList
}
// Addresses queries the Junos Space server and returns all of the information
// about each address that is managed by Space. Filter is optional, but if specified
// can help reduce the amount of objects returned.
func (s *Space) Addresses(filter ...string) (*Addresses, error) {
r := rested.NewRequest()
r.BasicAuth(s.User, s.Password)
var addresses Addresses
query := map[string]string{
"filter": "(global eq '')",
}
if len(filter) > 0 {
query["filter"] = fmt.Sprintf("(global eq '%s')", filter[0])
}
uri := fmt.Sprintf("https://%s/api/juniper/sd/address-management/addresses", s.Host)
resp := r.Send("get", uri, nil, nil, query)
if resp.Error != nil {
return nil, resp.Error
}
err := xml.Unmarshal(resp.Body, &addresses)
if err != nil {
return nil, err
}
return &addresses, nil
}
// AddAddress creates a new address object in Junos Space. Description is optional.
func (s *Space) AddAddress(name, ip string, description ...string) error {
r := rested.NewRequest()
r.BasicAuth(s.User, s.Password)
headers := map[string]string{}
desc := ""
re := regexp.MustCompile(`[-\w\.]*\.(com|net|org|us|gov)$`)
addrInfo := s.getAddrTypeIP(ip)
if len(description) > 0 {
desc = description[0]
}
address := fmt.Sprintf(addressesXML, name, addrInfo[0], addrInfo[1], desc)
if re.MatchString(ip) {
address = fmt.Sprintf(dnsXML, name, addrInfo[0], addrInfo[1], desc)
}
uri := fmt.Sprintf("https://%s/api/juniper/sd/address-management/addresses", s.Host)
headers["Content-Type"] = contentAddress
resp := r.Send("post", uri, []byte(address), headers, nil)
if resp.Error != nil {
return resp.Error
}
return nil
}
// EditAddress changes the IP/Network/FQDN of the given address object name.
func (s *Space) EditAddress(name, newip string) error {
r := rested.NewRequest()
r.BasicAuth(s.User, s.Password)
headers := map[string]string{}
var existing existingAddress
addrInfo := s.getAddrTypeIP(newip)
re := regexp.MustCompile(`[-\w\.]*\.(com|net|org|us|gov)$`)
objectID, err := s.getObjectID(name, "address")
if err != nil {
return err
}
uri := fmt.Sprintf("https://%s/api/juniper/sd/address-management/addresses/%d", s.Host, objectID)
headers["Content-Type"] = contentAddress
exResp := r.Send("get", uri, nil, headers, nil)
if exResp.Error != nil {
return exResp.Error
}
err = xml.Unmarshal(exResp.Body, &existing)
if err != nil {
return err
}
updateContent := fmt.Sprintf(modifyAddressXML, existing.Name, addrInfo[0], existing.EditVersion, addrInfo[1], existing.Description)
if re.MatchString(name) {
updateContent = fmt.Sprintf(modifyDNSXML, existing.Name, addrInfo[0], existing.EditVersion, addrInfo[1], existing.Description)
}
resp := r.Send("put", uri, []byte(updateContent), headers, nil)
if resp.Error != nil {
return resp.Error
}
return nil
}
// AddService creates a new service object to Junos Space. For a single port, just enter in
// the number. For a range of ports, enter the low-high range in quotes like so: "10000-10002".
func (s *Space) AddService(protocol, name string, ports interface{}, description string, timeout int) error {
r := rested.NewRequest()
r.BasicAuth(s.User, s.Password)
headers := map[string]string{}
var protoNumber int
var port, inactivity, secs string
ptype := fmt.Sprintf("PROTOCOL_%s", strings.ToUpper(protocol))
protocol = strings.ToUpper(protocol)
protoNumber = 6
if protocol == "UDP" {
protoNumber = 17
}
switch ports.(type) {
case int:
port = strconv.Itoa(ports.(int))
case string:
p := strings.Split(ports.(string), "-")
port = fmt.Sprintf("%s-%s", p[0], p[1])
}
inactivity = "false"
secs = fmt.Sprintf("<inactivity-timeout>%d</inactivity-timeout>", timeout)
if timeout == 0 {
inactivity = "true"
secs = "<inactivity-timeout/>"
}
service := fmt.Sprintf(serviceXML, name, description, name, port, protocol, protocol, protoNumber, ptype, inactivity, secs)
headers["Content-Type"] = contentService
uri := fmt.Sprintf("https://%s/api/juniper/sd/service-management/services", s.Host)
resp := r.Send("post", uri, []byte(service), headers, nil)
if resp.Error != nil {
return resp.Error
}
return nil
}
// AddGroup creates a new address or service group in Junos Space. Objecttype must be "address" or "service".
func (s *Space) AddGroup(grouptype, name string, description ...string) error {
r := rested.NewRequest()
r.BasicAuth(s.User, s.Password)
headers := map[string]string{}
desc := ""
uri := fmt.Sprintf("https://%s/api/juniper/sd/address-management/addresses", s.Host)
addGroupXML := addressGroupXML
content := contentAddress
if len(description) > 0 {
desc = description[0]
}
if grouptype == "service" {
uri = fmt.Sprintf("https://%s/api/juniper/sd/service-management/services", s.Host)
addGroupXML = serviceGroupXML
content = contentService
}
groupXML := fmt.Sprintf(addGroupXML, name, desc)
headers["Content-Type"] = content
resp := r.Send("post", uri, []byte(groupXML), headers, nil)
if resp.Error != nil {
return resp.Error
}
return nil
}
// EditGroup adds or removes objects to/from an existing address or service group. Grouptype must be
// "address" or "service." Action must be add or remove.
func (s *Space) EditGroup(grouptype, action, object, name string) error {
r := rested.NewRequest()
r.BasicAuth(s.User, s.Password)
headers := map[string]string{}
var err error
var uri, content, rel, xmlBody string
objectID, err := s.getObjectID(name, grouptype)
if err != nil {
return err
}
if objectID != 0 {
uri = fmt.Sprintf("https://%s/api/juniper/sd/address-management/addresses/%d", s.Host, objectID)
content = contentAddressPatch
rel = "address"
if grouptype == "service" {
uri = fmt.Sprintf("https://%s/api/juniper/sd/service-management/services/%d", s.Host, objectID)
content = contentServicePatch
rel = "service"
}
switch action {
case "add":
xmlBody = fmt.Sprintf(addGroupMemberXML, rel, object)
headers["Content-Type"] = content
case "remove":
xmlBody = fmt.Sprintf(removeXML, rel, object)
headers["Content-Type"] = content
}
resp := r.Send("patch", uri, []byte(xmlBody), headers, nil)
if resp.Error != nil {
return resp.Error
}
}
return nil
}
// RenameObject renames an address or service object to the given new name. Grouptype
// must be "address" or "service"
func (s *Space) RenameObject(grouptype, name, newname string) error {
r := rested.NewRequest()
r.BasicAuth(s.User, s.Password)
headers := map[string]string{}
var err error
var uri, content, rel, xmlBody string
objectID, err := s.getObjectID(name, grouptype)
if err != nil {
return err
}
if objectID != 0 {
uri = fmt.Sprintf("https://%s/api/juniper/sd/address-management/addresses/%d", s.Host, objectID)
content = contentAddressPatch
rel = "address"
if grouptype == "service" {
uri = fmt.Sprintf("https://%s/api/juniper/sd/service-management/services/%d", s.Host, objectID)
content = contentServicePatch
rel = "service"
}
xmlBody = fmt.Sprintf(renameXML, rel, newname)
headers["Content-Type"] = content
resp := r.Send("patch", uri, []byte(xmlBody), headers, nil)
if resp.Error != nil {
return resp.Error
}
}
return nil
}
// DeleteObject removes an address or service object from Junos Space. Grouptype
// must be "address" or "service"
func (s *Space) DeleteObject(grouptype, name string) error {
r := rested.NewRequest()
r.BasicAuth(s.User, s.Password)
var err error
var uri string
objectID, err := s.getObjectID(name, grouptype)
if err != nil {
return err
}
if objectID != 0 {
uri = fmt.Sprintf("https://%s/api/juniper/sd/address-management/addresses/%d", s.Host, objectID)
if grouptype == "service" {
uri = fmt.Sprintf("https://%s/api/juniper/sd/service-management/services/%d", s.Host, objectID)
}
resp := r.Send("delete", uri, nil, nil, nil)
if resp.Error != nil {
return resp.Error
}
}
return nil
}
// Services queries the Junos Space server and returns all of the information
// about each service that is managed by Space.
func (s *Space) Services(filter ...string) (*Services, error) {
r := rested.NewRequest()
r.BasicAuth(s.User, s.Password)
var services Services
query := map[string]string{
"filter": "(global eq '')",
}
if len(filter) > 0 {
query["filter"] = fmt.Sprintf("(global eq '%s')", filter[0])
}
uri := fmt.Sprintf("https://%s/api/juniper/sd/service-management/services", s.Host)
resp := r.Send("get", uri, nil, nil, query)
if resp.Error != nil {
return nil, resp.Error
}
err := xml.Unmarshal(resp.Body, &services)
if err != nil {
return nil, err
}
return &services, nil
}
// GroupMembers lists all of the address or service objects within the
// given group. Grouptype must be "address" or "service".
func (s *Space) GroupMembers(grouptype, name string) (*GroupMembers, error) {
r := rested.NewRequest()
r.BasicAuth(s.User, s.Password)
var members GroupMembers
objectID, err := s.getObjectID(name, grouptype)
uri := fmt.Sprintf("https://%s/api/juniper/sd/address-management/addresses/%d", s.Host, objectID)
if grouptype == "service" {
uri = fmt.Sprintf("https://%s/api/juniper/sd/service-management/services/%d", s.Host, objectID)
}
resp := r.Send("get", uri, nil, nil, nil)
if resp.Error != nil {
return nil, resp.Error
}
err = xml.Unmarshal(resp.Body, &members)
if err != nil {
return nil, err
}
return &members, nil
}
// SecurityDevices queries the Junos Space server and returns all of the information
// about each security device that is managed by Space.
func (s *Space) SecurityDevices() (*SecurityDevices, error) {
r := rested.NewRequest()
r.BasicAuth(s.User, s.Password)
var devices SecurityDevices
uri := fmt.Sprintf("https://%s/api/juniper/sd/device-management/devices", s.Host)
resp := r.Send("get", uri, nil, nil, nil)
if resp.Error != nil {
return nil, resp.Error
}
err := xml.Unmarshal(resp.Body, &devices)
if err != nil {
return nil, err
}
return &devices, nil
}
// Policies returns a list of all firewall policies managed by Junos Space.
func (s *Space) Policies() (*Policies, error) {
r := rested.NewRequest()
r.BasicAuth(s.User, s.Password)
var policies Policies
uri := fmt.Sprintf("https://%s/api/juniper/sd/fwpolicy-management/firewall-policies", s.Host)
resp := r.Send("get", uri, nil, nil, nil)
if resp.Error != nil {
return nil, resp.Error
}
err := xml.Unmarshal(resp.Body, &policies)
if err != nil {
return nil, err
}
return &policies, nil
}
// PublishPolicy publishes a changed firewall policy. If "true" is specified for
// update, then Junos Space will also update the device.
func (s *Space) PublishPolicy(policy interface{}, update bool) (int, error) {
r := rested.NewRequest()
r.BasicAuth(s.User, s.Password)
headers := map[string]string{
"Content-Type": contentPublish,
}
var err error
var job jobID
var id int
var uri = fmt.Sprintf("https://%s/api/juniper/sd/fwpolicy-management/publish", s.Host)
switch policy.(type) {
case int:
id = policy.(int)
case string:
id, err = s.getPolicyID(policy.(string))
if err != nil {
return 0, err
}
if id == 0 {
return 0, errors.New("no policy found")
}
}
publish := fmt.Sprintf(publishPolicyXML, id)
if update {
uri = fmt.Sprintf("https://%s/api/juniper/sd/fwpolicy-management/publish?update=true", s.Host)
}
resp := r.Send("post", uri, []byte(publish), headers, nil)
if resp.Error != nil {
return 0, resp.Error
}
err = xml.Unmarshal(resp.Body, &job)
if err != nil {
return 0, errors.New("no policy changes to publish")
}
return job.ID, nil
}
// UpdateDevice will update a changed security device, synchronizing it with
// Junos Space.
func (s *Space) UpdateDevice(device interface{}) (int, error) {
r := rested.NewRequest()
r.BasicAuth(s.User, s.Password)
headers := map[string]string{
"Content-Type": contentUpdateDevices,
}
uri := fmt.Sprintf("https://%s/api/juniper/sd/device-management/update-devices", s.Host)
var job jobID
deviceID, err := s.getDeviceID(device)
if err != nil {
return 0, err
}
update := fmt.Sprintf(updateDeviceXML, deviceID)
resp := r.Send("post", uri, []byte(update), headers, nil)
if resp.Error != nil {
return 0, resp.Error
}
err = xml.Unmarshal(resp.Body, &job)
if err != nil {
return 0, err
}
return job.ID, nil
}
// Variables returns a listing of all polymorphic (variable) objects.
func (s *Space) Variables() (*Variables, error) {
r := rested.NewRequest()
r.BasicAuth(s.User, s.Password)
var vars Variables
uri := fmt.Sprintf("https://%s/api/juniper/sd/variable-management/variable-definitions", s.Host)
resp := r.Send("get", uri, nil, nil, nil)
if resp.Error != nil {
return nil, resp.Error
}
err := xml.Unmarshal(resp.Body, &vars)
if err != nil {
return nil, err
}
return &vars, nil
}
// AddVariable creates a new polymorphic object (variable) on the Junos Space server.
// The address option is a default address object that will be used. This address object must
// already exist on the server.
func (s *Space) AddVariable(name, address string, description ...string) error {
r := rested.NewRequest()
r.BasicAuth(s.User, s.Password)
headers := map[string]string{
"Content-Type": contentVariable,
}
desc := ""
objectID, err := s.getObjectID(address, "address")
if err != nil {
return err
}
if len(description) > 0 {
desc = description[0]
}
varBody := fmt.Sprintf(createVariableXML, name, "ADDRESS", desc, address, objectID)
uri := fmt.Sprintf("https://%s/api/juniper/sd/variable-management/variable-definitions", s.Host)
resp := r.Send("post", uri, []byte(varBody), headers, nil)
if resp.Error != nil {
return resp.Error
}
return nil
}
// DeleteVariable removes the polymorphic (variable) object from Junos Space.
// If the variable object is in use by a policy, then it will not be deleted
// until you remove it from the policy.
func (s *Space) DeleteVariable(name string) error {
r := rested.NewRequest()
r.BasicAuth(s.User, s.Password)
headers := map[string]string{
"Content-Type": contentVariable,
}
varID, err := s.getVariableID(name)
if err != nil {
return err
}
uri := fmt.Sprintf("https://%s/api/juniper/sd/variable-management/variable-definitions/%d", s.Host, varID)
resp := r.Send("delete", uri, nil, headers, nil)
if resp.Error != nil {
return resp.Error
}
return nil
}
// EditVariable creates a new state when adding/removing addresses to
// a polymorphic (variable) object. We do this to only get the list of
// security devices (SecurityDevices()) once, instead of call the function
// each time we want to modify a variable.
func (s *Space) EditVariable() (*VariableManagement, error) {
devices, err := s.SecurityDevices()
if err != nil {
return nil, err
}
return &VariableManagement{
Devices: devices.Devices,
Space: s,
}, nil
}