-
Notifications
You must be signed in to change notification settings - Fork 5
/
workloads.go
1132 lines (989 loc) · 37.1 KB
/
workloads.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 illumioapi
import (
"encoding/json"
"errors"
"fmt"
"math"
"net"
"net/url"
"strconv"
"strings"
"time"
)
// An Agent is an Agent on a Workload
type Agent struct {
ActivePceFqdn string `json:"active_pce_fqdn,omitempty"`
Config *Config `json:"config,omitempty"`
Href string `json:"href,omitempty"`
SecureConnect *SecureConnect `json:"secure_connect,omitempty"`
Status *Status `json:"status,omitempty"`
TargetPceFqdn string `json:"target_pce_fqdn,omitempty"`
Hostname string `json:"hostname,omitempty"` // Added this for events
}
// AgentHealth represents the Agent Health of the Status of a Workload
type AgentHealth struct {
AuditEvent string `json:"audit_event,omitempty"`
Severity string `json:"severity,omitempty"`
Type string `json:"type,omitempty"`
}
// AgentHealthErrors represents the Agent Health Errors of the Status of a Workload
// This is depreciated - use AgentHealth
type AgentHealthErrors struct {
Errors []string `json:"errors,omitempty"`
Warnings []string `json:"warnings,omitempty"`
}
// Config represents the Configuration of an Agent on a Workload
type Config struct {
LogTraffic bool `json:"log_traffic"`
Mode string `json:"mode,omitempty"`
SecurityPolicyUpdateMode string `json:"security_policy_update_mode,omitempty"`
VisibilityLevel string `json:"visibility_level,omitempty"`
}
// DeletedBy represents the Deleted By property of an object
type DeletedBy struct {
Href string `json:"href,omitempty"`
}
// An Interface represent the Interfaces of a Workload
type Interface struct {
Address string `json:"address,omitempty"`
CidrBlock *int `json:"cidr_block,omitempty"`
DefaultGatewayAddress string `json:"default_gateway_address,omitempty"`
FriendlyName string `json:"friendly_name,omitempty"`
LinkState string `json:"link_state,omitempty"`
Name string `json:"name,omitempty"`
}
// OpenServicePorts represents open ports for a service running on a workload
type OpenServicePorts struct {
Address string `json:"address,omitempty"`
Package string `json:"package,omitempty"`
Port int `json:"port,omitempty"`
ProcessName string `json:"process_name,omitempty"`
Protocol int `json:"protocol,omitempty"`
User string `json:"user,omitempty"`
WinServiceName string `json:"win_service_name,omitempty"`
}
// A Workload represents a workload in the PCE
type Workload struct {
Agent *Agent `json:"agent,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
CreatedBy *CreatedBy `json:"created_by,omitempty"`
DataCenter string `json:"data_center,omitempty"`
DataCenterZone string `json:"data_center_zone,omitempty"`
DeleteType string `json:"delete_type,omitempty"`
Deleted *bool `json:"deleted,omitempty"`
DeletedAt string `json:"deleted_at,omitempty"`
DeletedBy *DeletedBy `json:"deleted_by,omitempty"`
Description string `json:"description,omitempty"`
DistinguishedName string `json:"distinguished_name,omitempty"`
EnforcementMode string `json:"enforcement_mode,omitempty"`
ExternalDataReference string `json:"external_data_reference,omitempty"`
ExternalDataSet string `json:"external_data_set,omitempty"`
Hostname string `json:"hostname,omitempty"`
Href string `json:"href,omitempty"`
IgnoredInterfaceNames *[]string `json:"ignored_interface_names,omitempty"`
Interfaces []*Interface `json:"interfaces,omitempty"`
Labels *[]*Label `json:"labels,omitempty"` // This breaks the removing all labels
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"` // Only used in Container Workloads
Online bool `json:"online,omitempty"`
OsDetail string `json:"os_detail,omitempty"`
OsID string `json:"os_id,omitempty"`
PublicIP string `json:"public_ip,omitempty"`
ServicePrincipalName string `json:"service_principal_name,omitempty"`
ServiceProvider string `json:"service_provider,omitempty"`
Services *Services `json:"services,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
UpdatedBy *UpdatedBy `json:"updated_by,omitempty"`
VEN *VEN `json:"ven,omitempty"`
VisibilityLevel string `json:"visibility_level,omitempty"`
VulnerabilitySummary *VulnerabilitySummary `json:"vulnerability_summary,omitempty"`
}
// SecureConnect represents SecureConnect for an Agent on a Workload
type SecureConnect struct {
MatchingIssuerName string `json:"matching_issuer_name,omitempty"`
}
// Services represent the Services running on a Workload
type Services struct {
CreatedAt string `json:"created_at,omitempty"`
OpenServicePorts []*OpenServicePorts `json:"open_service_ports,omitempty"`
UptimeSeconds int `json:"uptime_seconds,omitempty"`
}
// Status represents the Status of an Agent on a Workload
type Status struct {
AgentHealth []*AgentHealth `json:"agent_health,omitempty"`
AgentHealthErrors *AgentHealthErrors `json:"agent_health_errors,omitempty"`
AgentVersion string `json:"agent_version,omitempty"`
FirewallRuleCount int `json:"firewall_rule_count,omitempty"`
FwConfigCurrent bool `json:"fw_config_current,omitempty"`
InstanceID string `json:"instance_id,omitempty"`
LastHeartbeatOn string `json:"last_heartbeat_on,omitempty"`
ManagedSince string `json:"managed_since,omitempty"`
SecurityPolicyAppliedAt string `json:"security_policy_applied_at,omitempty"`
SecurityPolicyReceivedAt string `json:"security_policy_received_at,omitempty"`
SecurityPolicyRefreshAt string `json:"security_policy_refresh_at,omitempty"`
SecurityPolicySyncState string `json:"security_policy_sync_state,omitempty"`
Status string `json:"status,omitempty"`
UID string `json:"uid,omitempty"`
UptimeSeconds int `json:"uptime_seconds,omitempty"`
}
// Unpair is the payload for using the API to unpair workloads.
type Unpair struct {
Workloads []Workload `json:"workloads"`
IPTableRestore string `json:"ip_table_restore"`
}
// BulkResponse is the data structure for the bulk response API
type BulkResponse struct {
Href string `json:"href"`
Status string `json:"status"`
Token string `json:"token"`
Message string `json:"message"`
Errors []Error `json:"errors"`
}
type Error struct {
Token string `json:"token"`
Message string `json:"message"`
}
type IncraseTrafficUpdateReq struct {
Workloads []Workload `json:"workloads"`
}
type VulnerabilitySummary struct {
NumVulnerabilities int `json:"num_vulnerabilities,omitempty"`
MaxVulnerabilityScore int `json:"max_vulnerability_score,omitempty"`
VulnerabilityScore int `json:"vulnerability_score,omitempty"`
VulnerablePortExposure int `json:"vulnerable_port_exposure,omitempty"`
VulnerablePortWideExposure VulnerablePortWideExposure `json:"vulnerable_port_wide_exposure,omitempty"`
VulnerabilityExposureScore int `json:"vulnerability_exposure_score,omitempty"`
}
type VulnerablePortWideExposure struct {
Any bool `json:"any"`
IPList bool `json:"ip_list"`
}
// GetAllWorkloads returns an slice of workloads in the Illumio PCE.
// The first API call to the PCE does not use the async option.
// If the array length is >=500, it re-runs with async.
func (p *PCE) GetAllWorkloads() ([]Workload, APIResponse, error) {
wklds, a, err := p.GetAllWorkloadsQP(nil)
return wklds, a, err
}
// LoadWorkloadMap will populate the workload maps based on p.WorkloadSlice
func (p *PCE) LoadWorkloadMap() {
p.Workloads = make(map[string]Workload)
for _, w := range p.WorkloadsSlice {
p.Workloads[w.Href] = w
if w.Hostname != "" {
p.Workloads[w.Hostname] = w
}
if w.Name != "" {
p.Workloads[w.Name] = w
}
if w.ExternalDataSet != "" && w.ExternalDataReference != "" {
p.Workloads[w.ExternalDataSet+w.ExternalDataReference] = w
}
}
}
// GetAllWorkloadsQP returns a slice of workloads in the Illumio PCE.
// The first API call to the PCE does not use the async option.
// If the array length is >=500, it re-runs with async.
// QueryParameters can be passed as a map of [key]=vale
func (p *PCE) GetAllWorkloadsQP(queryParameters map[string]string) ([]Workload, APIResponse, error) {
var api APIResponse
// Build the API URL
apiURL, err := url.Parse("https://" + pceSanitization(p.FQDN) + ":" + strconv.Itoa(p.Port) + "/api/v2/orgs/" + strconv.Itoa(p.Org) + "/workloads")
if err != nil {
return nil, api, fmt.Errorf("get all workloads - %s", err)
}
// Set the query parameters
for key, value := range queryParameters {
q := apiURL.Query()
q.Set(key, value)
apiURL.RawQuery = q.Encode()
}
// Call the API
api, err = apicall("GET", apiURL.String(), *p, nil, false)
if err != nil {
return nil, api, fmt.Errorf("get all workloads - %s", err)
}
var workloads []Workload
json.Unmarshal([]byte(api.RespBody), &workloads)
// If length is 500, re-run with async
if len(workloads) >= 500 {
// Call async
api, err = apicall("GET", apiURL.String(), *p, nil, true)
if err != nil {
return nil, api, fmt.Errorf("get all workloads - %s", err)
}
// Unmarshal response to asyncWklds and return
var asyncWklds []Workload
json.Unmarshal([]byte(api.RespBody), &asyncWklds)
// Load the PCE with the returned workloads
p.WorkloadsSlice = asyncWklds
p.LoadWorkloadMap()
return asyncWklds, api, nil
}
// Load the PCE with the returned workloads
p.WorkloadsSlice = workloads
p.LoadWorkloadMap()
// Return if less than 500
return workloads, api, nil
}
// GetWkldByHref returns the workload with a specific href
func (p *PCE) GetWkldByHref(href string) (Workload, APIResponse, error) {
// Build the API URL
apiURL, err := url.Parse(fmt.Sprintf("https://%s:%d/api/v2%s", pceSanitization(p.FQDN), p.Port, href))
if err != nil {
return Workload{}, APIResponse{}, fmt.Errorf("get workload by href - %s", err)
}
// Call the API
api, err := apicall("GET", apiURL.String(), *p, nil, false)
if err != nil {
return Workload{}, api, fmt.Errorf("get workload by href - %s", err)
}
var wkld Workload
json.Unmarshal([]byte(api.RespBody), &wkld)
return wkld, api, nil
}
// GetWkldByHostname gets a workload based on the hostname
func (p *PCE) GetWkldByHostname(hostname string) (Workload, APIResponse, error) {
workloads, a, err := p.GetAllWorkloadsQP(map[string]string{"hostname": hostname})
if err != nil {
return Workload{}, a, fmt.Errorf("GetWkldByHref - %s", err)
}
for _, w := range workloads {
if w.Hostname == hostname {
return w, a, nil
}
}
return Workload{}, a, nil
}
// GetWkldHrefMap returns a map of all workloads with the Href as the key.
func (p *PCE) GetWkldHrefMap() (map[string]Workload, APIResponse, error) {
m := make(map[string]Workload)
wklds, a, err := p.GetAllWorkloads()
if err != nil {
return nil, a, err
}
for _, w := range wklds {
m[w.Href] = w
}
return m, a, nil
}
// GetWkldHostMap returns a map of all workloads with the hostname as the key.
func (p *PCE) GetWkldHostMap() (map[string]Workload, APIResponse, error) {
m := make(map[string]Workload)
wklds, a, err := p.GetAllWorkloads()
if err != nil {
return nil, a, err
}
for _, w := range wklds {
m[w.Hostname] = w
}
return m, a, nil
}
// CreateWorkload creates a new unmanaged workload in the Illumio PCE
func (p *PCE) CreateWorkload(workload Workload) (Workload, APIResponse, error) {
var newWL Workload
var api APIResponse
var err error
// Build the API URL
apiURL, err := url.Parse("https://" + pceSanitization(p.FQDN) + ":" + strconv.Itoa(p.Port) + "/api/v2/orgs/" + strconv.Itoa(p.Org) + "/workloads")
if err != nil {
return newWL, api, fmt.Errorf("create workload - %s", err)
}
// Call the API
workloadJSON, err := json.Marshal(workload)
if err != nil {
return newWL, api, fmt.Errorf("create workload - %s", err)
}
api.ReqBody = string(workloadJSON)
api, err = apicall("POST", apiURL.String(), *p, workloadJSON, false)
if err != nil {
return newWL, api, fmt.Errorf("create workload - %s", err)
}
// Marshal JSON
json.Unmarshal([]byte(api.RespBody), &newWL)
return newWL, api, nil
}
// IncreaseTrafficUpdateRate increases the VEN traffic update rate
func (p *PCE) IncreaseTrafficUpdateRate(wklds []Workload) (APIResponse, error) {
var api APIResponse
var err error
t := []Workload{}
for _, w := range wklds {
t = append(t, Workload{Href: w.Href})
}
wklds = t
// Build the API URL
apiURL, err := url.Parse("https://" + pceSanitization(p.FQDN) + ":" + strconv.Itoa(p.Port) + "/api/v2/orgs/" + strconv.Itoa(p.Org) + "/workloads/set_flow_reporting_frequency")
if err != nil {
return api, fmt.Errorf("IncreaseTrafficUpdateRate - %s", err)
}
// Call the API
wkldsJSON, err := json.Marshal(IncraseTrafficUpdateReq{Workloads: wklds})
if err != nil {
return api, fmt.Errorf("IncreaseTrafficUpdateRate - %s", err)
}
api, err = apicall("POST", apiURL.String(), *p, wkldsJSON, false)
api.ReqBody = string(wkldsJSON)
if err != nil {
return api, fmt.Errorf("IncreaseTrafficUpdateRate - %s", err)
}
return api, nil
}
// UpdateWorkload updates an existing workload in the Illumio PCE
// The provided workload struct must include an Href.
// Properties that cannot be included in the PUT method will be ignored.
func (p *PCE) UpdateWorkload(workload Workload) (APIResponse, error) {
var api APIResponse
var err error
// Build the API URL
apiURL, err := url.Parse("https://" + pceSanitization(p.FQDN) + ":" + strconv.Itoa(p.Port) + "/api/v2" + workload.Href)
if err != nil {
return api, fmt.Errorf("update workload - %s", err)
}
workload.SanitizePut()
// Call the API
workloadJSON, err := json.Marshal(workload)
if err != nil {
return api, fmt.Errorf("update workload - %s", err)
}
api.ReqBody = string(workloadJSON)
api, err = apicall("PUT", apiURL.String(), *p, workloadJSON, false)
if err != nil {
return api, fmt.Errorf("update workload - %s", err)
}
return api, nil
}
// ChangeLabel updates a workload struct with new label href.
// It does not call the Illumio API to update the workload in the PCE. Use pce.UpdateWorkload() or bulk update for that.
// The method returns the labelMapH in case it needs to create a new label.
func (w *Workload) ChangeLabel(pce PCE, targetKey, newValue string) (PCE, error) {
var updatedLabels []*Label
var newLabel Label
var err error
var ok bool
// Iterate through each of the workloads labels
for _, l := range *w.Labels {
// If they key isn't the target key, we add it to the updated labels
if pce.Labels[l.Href].Key != targetKey {
updatedLabels = append(updatedLabels, &Label{Href: l.Href})
}
}
// If our new label isn't blank, we need to get it's href and attach to array
if newValue == "" {
*w.Labels = updatedLabels
return pce, nil
}
// If our new label is not blank, we need to get it's href and add it to the array
if newLabel, ok = pce.Labels[targetKey+newValue]; !ok {
// If it doesn't exist, we create it and put it back into the label maps
newLabel, _, err = pce.CreateLabel(Label{Key: targetKey, Value: newValue})
if err != nil {
return pce, err
}
pce.Labels[newLabel.Href] = newLabel
pce.Labels[newLabel.Key+newLabel.Value] = newLabel
}
// Append the new label to our label slice
updatedLabels = append(updatedLabels, &Label{Href: newLabel.Href})
*w.Labels = updatedLabels
return pce, nil
}
// BulkWorkload takes a bulk action on an array of workloads.
// Method must be create, update, or delete
func (p *PCE) BulkWorkload(workloads []Workload, method string, stdoutLogs bool) ([]APIResponse, error) {
var apiResps []APIResponse
var err error
// Check on method
method = strings.ToLower(method)
if method != "create" && method != "update" && method != "delete" {
return apiResps, errors.New("bulk workload error - method must be create, update, or delete")
}
// Sanitize update
if method == "update" {
sanitizedWLs := []Workload{}
for _, workload := range workloads {
workload.SanitizeBulkUpdate()
sanitizedWLs = append(sanitizedWLs, workload)
}
workloads = sanitizedWLs
}
// Build the API URL
apiURL, err := url.Parse("https://" + pceSanitization(p.FQDN) + ":" + strconv.Itoa(p.Port) + "/api/v2/orgs/" + strconv.Itoa(p.Org) + "/workloads/bulk_" + method)
if err != nil {
return apiResps, fmt.Errorf("bulk workload error - %s", err)
}
// If the method is delete, we can only send Hrefs
if method == "delete" {
hrefWorkloads := []Workload{}
for _, workload := range workloads {
hrefWorkloads = append(hrefWorkloads, Workload{Href: workload.Href})
}
// Re-assign workloads to just the HREF
workloads = hrefWorkloads
}
// Figure out how many API calls we need to make
numAPICalls := int(math.Ceil(float64(len(workloads)) / 1000))
if stdoutLogs {
fmt.Printf("%s [INFO] - Bulk API actions happen in 1,000 workload chunks. %d %s calls will be required to process the %d workloads.\r\n", time.Now().Format("2006-01-02 15:04:05 "), numAPICalls, method, len(workloads))
}
// Build the array to be passed to the API
apiArrays := [][]Workload{}
for i := 0; i < numAPICalls; i++ {
// Get 1,000 elements if this is not the last array
if (i + 1) != numAPICalls {
apiArrays = append(apiArrays, workloads[i*1000:(1+i)*1000])
// Get the rest on the last array
} else {
apiArrays = append(apiArrays, workloads[i*1000:])
}
}
// Call the API for each array
for i, apiArray := range apiArrays {
workloadsJSON, err := json.Marshal(apiArray)
if err != nil {
return apiResps, fmt.Errorf("bulk workload error - %s", err)
}
api, err := apicall("PUT", apiURL.String(), *p, workloadsJSON, false)
if stdoutLogs {
fmt.Printf("%s [INFO] - API Call %d of %d - complete - status code %d.\r\n", time.Now().Format("2006-01-02 15:04:05 "), i+1, numAPICalls, api.StatusCode)
}
// Marshal JSON
var bulkResp []BulkResponse
json.Unmarshal([]byte(api.RespBody), &bulkResp)
for _, b := range bulkResp {
if method == "update" && b.Status != "updated" {
api.Warnings = append(api.Warnings, fmt.Sprintf("%s returned a status of %s with a message of %s and a token of %s", b.Href, b.Status, b.Message, b.Token))
}
if method == "delete" {
errorText := []string{}
for _, e := range b.Errors {
errorText = append(errorText, fmt.Sprintf("message: %s and token: %s", e.Message, e.Token))
}
api.Warnings = append(api.Warnings, fmt.Sprintf("%s returned errors: %s", b.Href, strings.Join(errorText, ";")))
}
if method == "create" && b.Status != "created" {
api.Warnings = append(api.Warnings, fmt.Sprintf("workload creation attempt returned a %s status and a %s message", b.Status, b.Message))
}
}
api.ReqBody = string(workloadsJSON)
apiResps = append(apiResps, api)
if err != nil {
return apiResps, fmt.Errorf("bulk workload error - %s", err)
}
}
return apiResps, nil
}
// SanitizeBulkUpdate removes the properites necessary for a bulk update
func (w *Workload) SanitizeBulkUpdate() {
// All Workloads
w.CreatedAt = ""
w.CreatedBy = nil
w.DeleteType = ""
w.Deleted = nil
w.DeletedAt = ""
w.DeletedBy = nil
w.UpdatedAt = ""
w.UpdatedBy = nil
w.Services = nil
w.VulnerabilitySummary = nil
// Managed workloads
if w.GetMode() != "unmanaged" {
w.Hostname = ""
w.Interfaces = nil
w.Online = false
w.OsDetail = ""
w.OsID = ""
w.PublicIP = ""
w.Services = nil
w.Online = false
w.Agent.Status = nil
w.Agent.SecureConnect = nil
w.Agent.ActivePceFqdn = "" // For supercluster-paired workloads
w.Agent.TargetPceFqdn = "" // For supercluster-paired workloads
w.Agent.Config.SecurityPolicyUpdateMode = ""
w.VEN = nil // The VEN is not updateable.
}
if w.EnforcementMode != "" {
w.Agent = nil
}
// Replace Labels with Hrefs
newLabels := []*Label{}
for _, l := range *w.Labels {
newLabel := Label{Href: l.Href}
newLabels = append(newLabels, &newLabel)
}
*w.Labels = newLabels
// Change distinguised name
w.DistinguishedName = ""
}
// SanitizePut removes the necessary properties to update an unmanaged and managed workload
func (w *Workload) SanitizePut() {
w.SanitizeBulkUpdate()
w.Href = ""
}
// GetRole takes a map of labels with the href string as the key and returns the role label for a workload.
// To get the LabelMap call GetLabelMapH.
func (w *Workload) GetRole(labelMap map[string]Label) Label {
if w.Labels == nil {
return Label{}
}
for _, l := range *w.Labels {
if labelMap[l.Href].Key == "role" {
return labelMap[l.Href]
}
}
return Label{}
}
// GetApp takes a map of labels with the href string as the key and returns the app label for a workload.
// To get the LabelMap call GetLabelMapH.
func (w *Workload) GetApp(labelMap map[string]Label) Label {
if w.Labels == nil {
return Label{}
}
for _, l := range *w.Labels {
if labelMap[l.Href].Key == "app" {
return labelMap[l.Href]
}
}
return Label{}
}
// GetEnv takes a map of labels with the href string as the key and returns the env label for a workload.
// To get the LabelMap call GetLabelMapH.
func (w *Workload) GetEnv(labelMap map[string]Label) Label {
if w.Labels == nil {
return Label{}
}
for _, l := range *w.Labels {
if labelMap[l.Href].Key == "env" {
return labelMap[l.Href]
}
}
return Label{}
}
// GetLoc takes a map of labels with the href string as the key and returns the loc label for a workload.
// To get the LabelMap call GetLabelMapH.
func (w *Workload) GetLoc(labelMap map[string]Label) Label {
if w.Labels == nil {
return Label{}
}
for _, l := range *w.Labels {
if labelMap[l.Href].Key == "loc" {
return labelMap[l.Href]
}
}
return Label{}
}
// LabelsMatch checks if the workload matches the provided labels.
// Blank values ("") for role, app, env, or loc mean no label assigned for that key.
// A single asterisk (*) can be used to represent any in a particular key.
// For example, using "*" for role will return true as long as the app, env, and loc match.
func (w *Workload) LabelsMatch(role, app, env, loc string, labelMap map[string]Label) bool {
if (role == "*" || w.GetRole(labelMap).Value == role) &&
(app == "*" || w.GetApp(labelMap).Value == app) &&
(env == "*" || w.GetEnv(labelMap).Value == env) &&
(loc == "*" || w.GetLoc(labelMap).Value == loc) {
return true
}
return false
}
// GetAppGroup returns the app group string of a workload in the format of App | Env.
// If the workload does not have an app or env label, "NO APP GROUP" is returned.
// Use GetAppGroupL to include the loc label in the app group.
func (w *Workload) GetAppGroup(labelMap map[string]Label) string {
if w.GetApp(labelMap).Href == "" || w.GetEnv(labelMap).Href == "" {
return "NO APP GROUP"
}
return fmt.Sprintf("%s | %s", w.GetApp(labelMap).Value, w.GetEnv(labelMap).Value)
}
// GetAppGroupL returns the app group string of a workload in the format of App | Env | Loc.
// If the workload does not have an app, env, or loc label, "NO APP GROUP" is returned.
// Use GetAppGroup to only use app and env in App Group.
func (w *Workload) GetAppGroupL(labelMap map[string]Label) string {
if w.GetApp(labelMap).Href == "" || w.GetEnv(labelMap).Href == "" || w.GetLoc(labelMap).Href == "" {
return "NO APP GROUP"
}
return fmt.Sprintf("%s | %s | %s", w.GetApp(labelMap).Value, w.GetEnv(labelMap).Value, w.GetLoc(labelMap).Value)
}
// GetMode returns the mode of the workloads.
// The returned value in 20.2 and newer PCEs will be unmanaged, idle, visibility_only, full, or selective.
// For visibility levels, use the w.GetVisibilityLevel() method.
//
// The returned value in 20.1 and lower PCEs will be unmanaged, idle, build, test, enforced-no, enforced-low, enforced-high.
// The enforced options represent no logging, low details, and high detail.
func (w *Workload) GetMode() string {
// Covers 20.2+ with the new API structure for VEN and enforcement_mode
if w.EnforcementMode != "" {
if (w.VEN == nil || w.VEN.Href == "") && w.ServicePrincipalName == "" {
return "unmanaged"
}
return w.EnforcementMode
}
// Covers prior to 20.2 when the API switched to enforcement_mode
if (w.Agent == nil || w.Agent.Href == "") && w.ServicePrincipalName == "" {
return "unmanaged"
}
if w.Agent.Config.Mode == "illuminated" && !w.Agent.Config.LogTraffic {
return "build"
}
if w.Agent.Config.Mode == "illuminated" && w.Agent.Config.LogTraffic {
return "test"
}
if w.Agent.Config.Mode == "enforced" && w.Agent.Config.VisibilityLevel == "flow_summary" {
return "enforced-high"
}
if w.Agent.Config.Mode == "enforced" && w.Agent.Config.VisibilityLevel == "flow_drops" {
return "enforced-low"
}
if w.Agent.Config.Mode == "enforced" && w.Agent.Config.VisibilityLevel == "flow_off" {
return "enforced-no"
}
if w.Agent.Config.Mode == "idle" {
return "idle"
}
return "unk"
}
// SetMode adjusts the workload to reflect the assigned mode.
// Nothing is changed in the PCE. To reflect the change in the PCE use SetMode method followed by PCE.UpdateWorkload() method.
//
// Valid options in 20.2 and newer PCEs are idle, visibility_only, full, and selective.
// For visibility levels, use the w.SetVisibilityLevel() method.
//
// Valid options in 20.1 and lower PCEs are idle, build, test, enforced-no, enforced-low, enforced-high.
// The enforced options represent no logging, low details, and high detail.
func (w *Workload) SetMode(m string) error {
m = strings.ToLower(m)
// If the VEN href is populated, use the new method and properties
if w.VEN != nil && w.VEN.Href != "" && (m == "visibility_only" || m == "full" || m == "selective" || m == "idle") {
w.EnforcementMode = m
return nil
}
// Old VEN status
switch m {
case "idle":
if w.VEN != nil && w.VEN.Href != "" {
w.EnforcementMode = "idle"
} else {
w.Agent.Config.Mode = "idle"
}
case "build":
if w.VEN != nil && w.VEN.Href != "" {
w.EnforcementMode = "visibility_only"
if err := w.SetVisibilityLevel("flow_summary"); err != nil {
return err
}
} else {
w.Agent.Config.Mode = "illuminated"
w.Agent.Config.LogTraffic = false
}
case "test":
if w.VEN != nil && w.VEN.Href != "" {
w.EnforcementMode = "visibility_only"
if err := w.SetVisibilityLevel("flow_summary"); err != nil {
return err
}
} else {
w.Agent.Config.Mode = "illuminated"
w.Agent.Config.LogTraffic = true
}
case "enforced-no":
if w.VEN != nil && w.VEN.Href != "" {
w.EnforcementMode = "full"
if err := w.SetVisibilityLevel("flow_off"); err != nil {
return err
}
} else {
w.Agent.Config.Mode = "enforced"
w.Agent.Config.VisibilityLevel = "flow_off"
w.Agent.Config.LogTraffic = false
}
case "enforced-low":
if w.VEN != nil && w.VEN.Href != "" {
w.EnforcementMode = "full"
if err := w.SetVisibilityLevel("flow_drops"); err != nil {
return err
}
} else {
w.Agent.Config.Mode = "enforced"
w.Agent.Config.VisibilityLevel = "flow_drops"
w.Agent.Config.LogTraffic = true
}
case "enforced-high":
if w.VEN != nil && w.VEN.Href != "" {
w.EnforcementMode = "full"
if err := w.SetVisibilityLevel("flow_summary"); err != nil {
return err
}
} else {
w.Agent.Config.Mode = "enforced"
w.Agent.Config.VisibilityLevel = "flow_summary"
w.Agent.Config.LogTraffic = true
}
default:
return fmt.Errorf("%s is not a valid mode. See SetMode documentation for valid modes", m)
}
return nil
}
// SetVisibilityLevel adjusts the workload to reflect the assigned visibility level.
// Nothing is changed in the PCE. To reflect the change in the PCE use SetVisibilityLevel method followed by PCE.UpdateWorkload() method.
//
// Valid options in 20.2 and newer PCEs are flow_summary (blocked_allowed), flow_drops (blocked), flow_off (off), or enhanced_data_collection. The options in paranthesis are the UI values. Both are acceptable.
//
// 20.1 PCEs and lower do not use this method.
func (w *Workload) SetVisibilityLevel(v string) error {
v = strings.ToLower(v)
if v == "blocked_allowed" {
v = "flow_summary"
}
if v == "blocked" {
v = "flow_drops"
}
if v == "off" {
v = "flow_off"
}
if v != "flow_summary" && v != "flow_drops" && v != "flow_off" && v != "enhanced_data_collection" {
return fmt.Errorf("%s is not a valid visibility_level. See SetVisibilityLevel documentation for valid levels", v)
}
w.VisibilityLevel = v
return nil
}
// GetVisibilityLevel returns unmanaged, blocked_allowed, blocked, or off.
func (w *Workload) GetVisibilityLevel() string {
if w.GetMode() == "unmanaged" {
return "unmanaged"
}
switch w.VisibilityLevel {
case "flow_summary":
return "blocked_allowed"
case "flow_drops":
return "blocked"
case "flow_off":
return "off"
default:
return w.VisibilityLevel
}
}
//GetID returns the ID from the Href of an Agent
func (a *Agent) GetID() string {
x := strings.Split(a.Href, "/")
return x[len(x)-1]
}
// WorkloadUpgrade upgrades the VEN version on the workload
func (p *PCE) WorkloadUpgrade(wkldHref, targetVersion string) (APIResponse, error) {
// Build the API URL
apiURL, err := url.Parse("https://" + pceSanitization(p.FQDN) + ":" + strconv.Itoa(p.Port) + "/api/v2" + wkldHref + "/upgrade")
if err != nil {
return APIResponse{}, fmt.Errorf("upgrade workload - %s", err)
}
// Call the API
api, err := apicall("POST", apiURL.String(), *p, json.RawMessage(fmt.Sprintf("{\"release\": \"%s\"}", targetVersion)), false)
if err != nil {
return api, fmt.Errorf("upgrade workload - %s", err)
}
return api, nil
}
// WorkloadsUnpair unpairs workloads. There is no limit to the length of []Workloads. The method
// chunks the API calls into groups of 1,000 to conform to the Illumio API.
func (p *PCE) WorkloadsUnpair(wklds []Workload, ipTablesRestore string) ([]APIResponse, error) {
// Build the payload
var targetWklds []Workload
for _, w := range wklds {
targetWklds = append(targetWklds, Workload{Href: w.Href})
}
// Build the API URL
apiURL, err := url.Parse("https://" + pceSanitization(p.FQDN) + ":" + strconv.Itoa(p.Port) + "/api/v2/orgs/" + strconv.Itoa(p.Org) + "/workloads/unpair")
if err != nil {
return nil, fmt.Errorf("unpair error - %s", err)
}
// Figure out how many API calls we need to make
numAPICalls := int(math.Ceil(float64(len(targetWklds)) / 1000))
// Build the array to be passed to the API
apiArrays := [][]Workload{}
for i := 0; i < numAPICalls; i++ {
// Get 1,000 elements if this is not the last array
if (i + 1) != numAPICalls {
apiArrays = append(apiArrays, targetWklds[i*1000:(1+i)*1000])
// Get the rest on the last array
} else {
apiArrays = append(apiArrays, targetWklds[i*1000:])
}
}
// Call the API for each array
var apiResps []APIResponse
for _, apiArray := range apiArrays {
// Marshal the payload
unpair := Unpair{IPTableRestore: ipTablesRestore, Workloads: apiArray}
payload, err := json.Marshal(unpair)
if err != nil {
return nil, fmt.Errorf("unpair error - %s", err)
}
// Make the API call and append the response to the results
api, err := apicall("PUT", apiURL.String(), *p, payload, false)
api.ReqBody = string(payload)
apiResps = append(apiResps, api)
if err != nil {
return apiResps, fmt.Errorf("unpair error - %s", err)
}
}
return apiResps, nil
}
// GetDefaultGW returns the default gateway for a workload.
// If the workload does not have a default gateway (many unmanaged workloads) it will return "NA"
func (w *Workload) GetDefaultGW() string {
for _, i := range w.Interfaces {
if i.DefaultGatewayAddress != "" {
return i.DefaultGatewayAddress
}
}
return "NA"
}
// GetIPWithDefaultGW returns the IP address of the interface that has the default gateway
// If the workload does not have a default gateway (many unmanaged workloads), it will return "NA"
func (w *Workload) GetIPWithDefaultGW() string {
for _, i := range w.Interfaces {
if i.DefaultGatewayAddress != "" {
return i.Address
}
}
return "NA"
}
// GetNetMaskWithDefaultGW returns the netmask of the ip address that has the default gateway
// If the workload does not have a default gateway (many unmanaged workloads), it will return "NA"
func (w *Workload) GetNetMaskWithDefaultGW() string {
for _, i := range w.Interfaces {
if i.DefaultGatewayAddress != "" {
return w.GetNetMask(i.Address)
}
}
return "NA"
}
// GetNetworkWithDefaultGateway returns the CIDR notation of the network of the interface with the default gateway.
// If the workload does not have a default gateway (many unmanaged workloads), it will return "NA"
func (w *Workload) GetNetworkWithDefaultGateway() string {
for _, i := range w.Interfaces {
if i.DefaultGatewayAddress != "" && i.CidrBlock != nil {
_, net, err := net.ParseCIDR(fmt.Sprintf("%s/%d", i.Address, *i.CidrBlock))
if err != nil {
return "NA"
}
return net.String()