-
Notifications
You must be signed in to change notification settings - Fork 5
/
services.go
214 lines (196 loc) · 7.25 KB
/
services.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
package illumioapi
import (
"fmt"
"strings"
)
// Service represent a service in the PCE
type Service struct {
Href string `json:"href,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
ProcessName string `json:"process_name,omitempty"`
ServicePorts *[]ServicePort `json:"service_ports,omitempty"`
WindowsServices *[]WindowsService `json:"windows_services,omitempty"`
ExternalDataReference *string `json:"external_data_reference,omitempty"`
ExternalDataSet *string `json:"external_data_set,omitempty"`
UpdateType string `json:"update_type,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
CreatedBy *Href `json:"created_by,omitempty"`
DeletedAt string `json:"deleted_at,omitempty"`
DeletedBy *Href `json:"deleted_by,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
UpdatedBy *Href `json:"updated_by,omitempty"`
RiskDetails *RiskDetail `json:"risk_details,omitempty"`
}
// ServicePort represent port and protocol information for a non-Windows service
type ServicePort struct {
IcmpCode int `json:"icmp_code,omitempty"`
IcmpType int `json:"icmp_type,omitempty"`
ID int `json:"id,omitempty"`
Port *int `json:"port,omitempty"` // Pointer for 0 value
Protocol int `json:"proto,omitempty"`
ToPort int `json:"to_port,omitempty"`
}
// WindowsService represents port, protocol, and process information for a Windows service
type WindowsService struct {
IcmpCode int `json:"icmp_code,omitempty"`
IcmpType int `json:"icmp_type,omitempty"`
Port *int `json:"port,omitempty"` // Pointer for 0 value
ProcessName string `json:"process_name,omitempty"`
Protocol int `json:"proto,omitempty"`
ServiceName string `json:"service_name,omitempty"`
ToPort int `json:"to_port,omitempty"`
}
type RiskDetail struct {
Ransomware *Ransomware `json:"ransomware,omitempty"`
}
type Ransomware struct {
Category string `json:"category,omitempty"`
Severity string `json:"severity,omitempty"`
OsPlatforms []string `json:"os_platforms,omitempty"`
}
// GetServices returns a slice of IP lists from the PCE. pStatus must be "draft" or "active".
// queryParameters can be used for filtering in the form of ["parameter"]="value".
// The first API call to the PCE does not use the async option.
// If the slice length is >=500, it re-runs with async.
func (p *PCE) GetServices(queryParameters map[string]string, pStatus string) (api APIResponse, err error) {
// Validate pStatus
pStatus = strings.ToLower(pStatus)
if pStatus != "active" && pStatus != "draft" {
return api, fmt.Errorf("invalid pStatus")
}
api, err = p.GetCollection("sec_policy/"+pStatus+"/services", false, queryParameters, &p.ServicesSlice)
if len(p.ServicesSlice) >= 500 {
p.ServicesSlice = nil
api, err = p.GetCollection("sec_policy/"+pStatus+"/services", true, queryParameters, &p.ServicesSlice)
}
p.Services = make(map[string]Service)
for _, s := range p.ServicesSlice {
p.Services[s.Href] = s
p.Services[s.Name] = s
}
return api, err
}
// CreateService creates a new service in the PCE.
func (p *PCE) CreateService(service Service) (createdService Service, api APIResponse, err error) {
api, err = p.Post("sec_policy/draft/services", &service, &createdService)
return createdService, api, err
}
// UpdateService updates an existing service object in the Illumio PCE
func (p *PCE) UpdateService(service Service) (APIResponse, error) {
service.CreatedAt = ""
service.CreatedBy = nil
service.UpdateType = ""
service.UpdatedAt = ""
service.UpdatedBy = nil
api, err := p.Put(&service)
return api, err
}
// ParseService returns a slice of WindowsServices and ServicePorts from an Illumio service object
func (s *Service) ParseService() (windowsServices, servicePorts []string) {
// Create a string for Windows Services
for _, ws := range PtrToVal(s.WindowsServices) {
var svcSlice []string
if PtrToVal(ws.Port) != 0 && ws.Protocol != 0 {
if ws.ToPort != 0 {
svcSlice = append(svcSlice, fmt.Sprintf("%d-%d %s", PtrToVal(ws.Port), ws.ToPort, ProtocolList()[ws.Protocol]))
} else {
svcSlice = append(svcSlice, fmt.Sprintf("%d %s", PtrToVal(ws.Port), ProtocolList()[ws.Protocol]))
}
}
if ws.IcmpCode != 0 && ws.IcmpType != 0 {
svcSlice = append(svcSlice, fmt.Sprintf("%d/%d %s", ws.IcmpType, ws.IcmpCode, ProtocolList()[ws.Protocol]))
}
if ws.ProcessName != "" {
svcSlice = append(svcSlice, ws.ProcessName)
}
if ws.ServiceName != "" {
svcSlice = append(svcSlice, ws.ServiceName)
}
windowsServices = append(windowsServices, strings.Join(svcSlice, " "))
}
// Process Service Ports
for _, sp := range PtrToVal(s.ServicePorts) {
var svcSlice []string
if PtrToVal(sp.Port) != 0 && sp.Protocol != 0 {
if sp.ToPort != 0 {
svcSlice = append(svcSlice, fmt.Sprintf("%d-%d %s", PtrToVal(sp.Port), sp.ToPort, ProtocolList()[sp.Protocol]))
} else {
svcSlice = append(svcSlice, fmt.Sprintf("%d %s", PtrToVal(sp.Port), ProtocolList()[sp.Protocol]))
}
}
if sp.IcmpCode != 0 && sp.IcmpType != 0 {
svcSlice = append(svcSlice, fmt.Sprintf("%d/%d %s", sp.IcmpType, sp.IcmpCode, ProtocolList()[sp.Protocol]))
} else if PtrToVal(sp.Port) == 0 && sp.Protocol != 0 {
svcSlice = append(svcSlice, ProtocolList()[sp.Protocol])
}
servicePorts = append(servicePorts, strings.Join(svcSlice, " "))
}
return windowsServices, servicePorts
}
// ToExplorer takes a service and returns an explorer query include and exclude
func (s *Service) ToExplorer() ([]IncludeOrExclude, []IncludeOrExclude) {
includes := []IncludeOrExclude{}
excludes := []IncludeOrExclude{}
// Process WindowsServices
for _, ws := range PtrToVal(s.WindowsServices) {
include := IncludeOrExclude{}
exclude := IncludeOrExclude{}
check := false
if PtrToVal(ws.Port) != 0 {
include.Port = PtrToVal(ws.Port)
exclude.Port = PtrToVal(ws.Port)
check = true
}
if ws.ToPort != 0 {
include.ToPort = ws.ToPort
exclude.ToPort = ws.ToPort
check = true
}
if ws.Protocol != 0 {
include.Proto = ws.Protocol
exclude.Proto = ws.Protocol
check = true
}
if ws.ProcessName != "" {
include.Process = ws.ProcessName
exclude.Process = ws.ProcessName
check = true
}
if ws.ServiceName != "" {
include.WindowsService = ws.ServiceName
exclude.WindowsService = ws.ServiceName
check = true
}
if check {
includes = append(includes, include)
excludes = append(excludes, exclude)
}
}
// Service Ports
for _, s := range PtrToVal(s.ServicePorts) {
include := IncludeOrExclude{}
exclude := IncludeOrExclude{}
check := false
if PtrToVal(s.Port) != 0 {
include.Port = PtrToVal(s.Port)
exclude.Port = PtrToVal(s.Port)
check = true
}
if s.ToPort != 0 {
include.ToPort = s.ToPort
exclude.ToPort = s.ToPort
check = true
}
if s.Protocol != 0 && s.Protocol != -1 {
include.Proto = s.Protocol
exclude.Proto = s.Protocol
check = true
}
if check {
includes = append(includes, include)
excludes = append(excludes, exclude)
}
}
return includes, excludes
}