-
Notifications
You must be signed in to change notification settings - Fork 0
/
zpa_application_segment_inspection.go
233 lines (213 loc) · 10.1 KB
/
zpa_application_segment_inspection.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
package applicationsegmentinspection
import (
"fmt"
"net/http"
"strings"
"github.com/SecurityGeekIO/zscaler-sdk-go/v2/zpa/services/common"
)
const (
mgmtConfig = "/mgmtconfig/v1/admin/customers/"
appSegmentInspectionEndpoint = "/application"
)
type AppSegmentInspection struct {
ID string `json:"id,omitempty"`
SegmentGroupID string `json:"segmentGroupId,omitempty"`
SegmentGroupName string `json:"segmentGroupName,omitempty"`
BypassType string `json:"bypassType,omitempty"`
BypassOnReauth bool `json:"bypassOnReauth,omitempty"`
ConfigSpace string `json:"configSpace,omitempty"`
DomainNames []string `json:"domainNames,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Enabled bool `json:"enabled"`
AppRecommendationId string `json:"appRecommendationId,omitempty"`
ICMPAccessType string `json:"icmpAccessType,omitempty"`
PassiveHealthEnabled bool `json:"passiveHealthEnabled,omitempty"`
FQDNDnsCheck bool `json:"fqdnDnsCheck"`
MatchStyle string `json:"matchStyle,omitempty"`
SelectConnectorCloseToApp bool `json:"selectConnectorCloseToApp"`
DoubleEncrypt bool `json:"doubleEncrypt"`
HealthCheckType string `json:"healthCheckType,omitempty"`
IsCnameEnabled bool `json:"isCnameEnabled"`
IPAnchored bool `json:"ipAnchored"`
HealthReporting string `json:"healthReporting,omitempty"`
CreationTime string `json:"creationTime,omitempty"`
ModifiedBy string `json:"modifiedBy,omitempty"`
ModifiedTime string `json:"modifiedTime,omitempty"`
TCPKeepAlive string `json:"tcpKeepAlive,omitempty"`
IsIncompleteDRConfig bool `json:"isIncompleteDRConfig"`
UseInDrMode bool `json:"useInDrMode"`
MicroTenantID string `json:"microtenantId,omitempty"`
MicroTenantName string `json:"microtenantName,omitempty"`
TCPPortRanges []string `json:"tcpPortRanges,omitempty"`
UDPPortRanges []string `json:"udpPortRanges,omitempty"`
TCPAppPortRange []common.NetworkPorts `json:"tcpPortRange,omitempty"`
UDPAppPortRange []common.NetworkPorts `json:"udpPortRange,omitempty"`
TCPProtocols []string `json:"tcpProtocols,omitempty"`
UDPProtocols []string `json:"udpProtocols,omitempty"`
InspectionAppDto []InspectionAppDto `json:"inspectionApps,omitempty"`
CommonAppsDto CommonAppsDto `json:"commonAppsDto,omitempty"`
AppServerGroups []AppServerGroups `json:"serverGroups,omitempty"`
SharedMicrotenantDetails SharedMicrotenantDetails `json:"sharedMicrotenantDetails,omitempty"`
}
type SharedMicrotenantDetails struct {
SharedFromMicrotenant SharedFromMicrotenant `json:"sharedFromMicrotenant,omitempty"`
SharedToMicrotenants []SharedToMicrotenant `json:"sharedToMicrotenants,omitempty"`
}
type SharedFromMicrotenant struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
type SharedToMicrotenant struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
type CommonAppsDto struct {
AppsConfig []AppsConfig `json:"appsConfig,omitempty"`
DeletedInspectApps []string `json:"deletedInspectApps,omitempty"`
}
type AppsConfig struct {
ID string `json:"id,omitempty"`
AppID string `json:"appId,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Enabled bool `json:"enabled"`
AllowOptions bool `json:"allowOptions"`
AppTypes []string `json:"appTypes,omitempty"`
ApplicationPort string `json:"applicationPort,omitempty"`
ApplicationProtocol string `json:"applicationProtocol,omitempty"`
InspectAppID string `json:"inspectAppId,omitempty"`
CertificateID string `json:"certificateId,omitempty"`
CertificateName string `json:"certificateName,omitempty"`
Cname string `json:"cname,omitempty"`
Domain string `json:"domain,omitempty"`
Hidden bool `json:"hidden"`
TrustUntrustedCert bool `json:"trustUntrustedCert"`
LocalDomain string `json:"localDomain,omitempty"`
Portal bool `json:"portal"`
}
type InspectionAppDto struct {
ID string `json:"id,omitempty"`
AppID string `json:"appId,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Enabled bool `json:"enabled"`
ApplicationPort string `json:"applicationPort,omitempty"`
ApplicationProtocol string `json:"applicationProtocol,omitempty"`
CertificateID string `json:"certificateId,omitempty"`
CertificateName string `json:"certificateName,omitempty"`
Domain string `json:"domain,omitempty"`
Protocols []string `json:"protocols,omitempty"`
TrustUntrustedCert bool `json:"trustUntrustedCert"`
MicroTenantID string `json:"microtenantId,omitempty"`
MicroTenantName string `json:"microtenantName,omitempty"`
}
type AppServerGroups struct {
ID string `json:"id"`
}
func (service *Service) Get(id string) (*AppSegmentInspection, *http.Response, error) {
v := new(AppSegmentInspection)
relativeURL := fmt.Sprintf("%s/%s", mgmtConfig+service.Client.Config.CustomerID+appSegmentInspectionEndpoint, id)
resp, err := service.Client.NewRequestDo("GET", relativeURL, common.Filter{MicroTenantID: service.microTenantID}, nil, v)
if err != nil {
return nil, nil, err
}
return v, resp, nil
}
func (service *Service) GetByName(appSegmentName string) (*AppSegmentInspection, *http.Response, error) {
relativeURL := mgmtConfig + service.Client.Config.CustomerID + appSegmentInspectionEndpoint
list, resp, err := common.GetAllPagesGenericWithCustomFilters[AppSegmentInspection](service.Client, relativeURL, common.Filter{Search: appSegmentName, MicroTenantID: service.microTenantID})
if err != nil {
return nil, nil, err
}
for _, app := range list {
if strings.EqualFold(app.Name, appSegmentName) {
return &app, resp, nil
}
}
return nil, resp, fmt.Errorf("no inspection application segment named '%s' was found", appSegmentName)
}
func (service *Service) Create(appSegmentInspection AppSegmentInspection) (*AppSegmentInspection, *http.Response, error) {
v := new(AppSegmentInspection)
resp, err := service.Client.NewRequestDo("POST", mgmtConfig+service.Client.Config.CustomerID+appSegmentInspectionEndpoint, common.Filter{MicroTenantID: service.microTenantID}, appSegmentInspection, &v)
if err != nil {
return nil, nil, err
}
return v, resp, nil
}
// return the new items that were added to slice1
func difference(slice1 []AppsConfig, slice2 []AppsConfig) []AppsConfig {
var diff []AppsConfig
for _, s1 := range slice1 {
found := false
for _, s2 := range slice2 {
if s1.Domain == s2.Domain || s1.Name == s2.Name {
found = true
break
}
}
if !found {
diff = append(diff, s1)
}
}
return diff
}
func mapInspectionApp(InspectionAppDto []InspectionAppDto) []AppsConfig {
result := []AppsConfig{}
for _, app := range InspectionAppDto {
result = append(result, AppsConfig{
Name: app.Name,
Domain: app.Domain,
ID: app.ID,
AppID: app.AppID,
})
}
return result
}
func appToListStringIDs(apps []AppsConfig) []string {
result := []string{}
for _, app := range apps {
result = append(result, app.ID)
}
return result
}
func (service *Service) Update(id string, appSegmentInspection *AppSegmentInspection) (*http.Response, error) {
existingResource, _, err := service.Get(id)
if err != nil {
return nil, err
}
existingApps := mapInspectionApp(existingResource.InspectionAppDto)
newApps := difference(appSegmentInspection.CommonAppsDto.AppsConfig, existingApps)
removedApps := difference(existingApps, appSegmentInspection.CommonAppsDto.AppsConfig)
appSegmentInspection.CommonAppsDto.AppsConfig = newApps
appSegmentInspection.CommonAppsDto.DeletedInspectApps = appToListStringIDs(removedApps)
path := fmt.Sprintf("%s/%s", mgmtConfig+service.Client.Config.CustomerID+appSegmentInspectionEndpoint, id)
resp, err := service.Client.NewRequestDo("PUT", path, common.Filter{MicroTenantID: service.microTenantID}, appSegmentInspection, nil)
if err != nil {
return nil, err
}
return resp, err
}
func (service *Service) Delete(id string) (*http.Response, error) {
path := fmt.Sprintf("%s/%s", mgmtConfig+service.Client.Config.CustomerID+appSegmentInspectionEndpoint, id)
resp, err := service.Client.NewRequestDo("DELETE", path, common.DeleteApplicationQueryParams{ForceDelete: true, MicroTenantID: service.microTenantID}, nil, nil)
if err != nil {
return nil, err
}
return resp, err
}
func (service *Service) GetAll() ([]AppSegmentInspection, *http.Response, error) {
relativeURL := mgmtConfig + service.Client.Config.CustomerID + appSegmentInspectionEndpoint
list, resp, err := common.GetAllPagesGenericWithCustomFilters[AppSegmentInspection](service.Client, relativeURL, common.Filter{MicroTenantID: service.microTenantID})
if err != nil {
return nil, nil, err
}
result := []AppSegmentInspection{}
// filter inspection apps
for _, item := range list {
if len(item.CommonAppsDto.AppsConfig) > 0 && common.InList(item.CommonAppsDto.AppsConfig[0].AppTypes, "INSPECT") {
result = append(result, item)
}
}
return result, resp, nil
}