-
Notifications
You must be signed in to change notification settings - Fork 1
/
policysetcontroller.go
312 lines (279 loc) · 12.4 KB
/
policysetcontroller.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
package policysetcontroller
import (
"fmt"
"io"
"log"
"net/http"
"sort"
"strconv"
"strings"
"github.com/zscaler/zscaler-sdk-go/v2/zpa/services/common"
)
const (
mgmtConfig = "/mgmtconfig/v1/admin/customers/"
)
type PolicySet struct {
CreationTime string `json:"creationTime,omitempty"`
Description string `json:"description,omitempty"`
Enabled bool `json:"enabled"`
ID string `json:"id,omitempty"`
ModifiedBy string `json:"modifiedBy,omitempty"`
ModifiedTime string `json:"modifiedTime,omitempty"`
Name string `json:"name,omitempty"`
Sorted bool `json:"sorted"`
PolicyType string `json:"policyType,omitempty"`
MicroTenantID string `json:"microtenantId,omitempty"`
MicroTenantName string `json:"microtenantName,omitempty"`
Rules []PolicyRule `json:"rules"`
}
type PolicyRule struct {
Action string `json:"action,omitempty"`
ActionID string `json:"actionId,omitempty"`
BypassDefaultRule bool `json:"bypassDefaultRule"`
CreationTime string `json:"creationTime,omitempty"`
CustomMsg string `json:"customMsg,omitempty"`
DefaultRule bool `json:"defaultRule,omitempty"`
DefaultRuleName string `json:"defaultRuleName,omitempty"`
Description string `json:"description,omitempty"`
ID string `json:"id,omitempty"`
IsolationDefaultRule bool `json:"isolationDefaultRule"`
ModifiedBy string `json:"modifiedBy,omitempty"`
ModifiedTime string `json:"modifiedTime,omitempty"`
Name string `json:"name,omitempty"`
Operator string `json:"operator,omitempty"`
PolicySetID string `json:"policySetId"`
PolicyType string `json:"policyType,omitempty"`
Priority string `json:"priority,omitempty"`
ReauthDefaultRule bool `json:"reauthDefaultRule"`
ReauthIdleTimeout string `json:"reauthIdleTimeout,omitempty"`
ReauthTimeout string `json:"reauthTimeout,omitempty"`
RuleOrder string `json:"ruleOrder"`
LSSDefaultRule bool `json:"lssDefaultRule"`
ZpnCbiProfileID string `json:"zpnCbiProfileId,omitempty"`
ZpnIsolationProfileID string `json:"zpnIsolationProfileId,omitempty"`
ZpnInspectionProfileID string `json:"zpnInspectionProfileId,omitempty"`
ZpnInspectionProfileName string `json:"zpnInspectionProfileName,omitempty"`
MicroTenantID string `json:"microtenantId,omitempty"`
MicroTenantName string `json:"microtenantName,omitempty"`
Conditions []Conditions `json:"conditions"`
AppServerGroups []AppServerGroups `json:"appServerGroups"`
AppConnectorGroups []AppConnectorGroups `json:"appConnectorGroups"`
ServiceEdgeGroups []ServiceEdgeGroups `json:"serviceEdgeGroups"`
Credential *Credential `json:"credential,omitempty"`
PrivilegedCapabilities PrivilegedCapabilities `json:"privilegedCapabilities,omitempty"`
}
type Conditions struct {
CreationTime string `json:"creationTime,omitempty"`
ID string `json:"id,omitempty"`
ModifiedBy string `json:"modifiedBy,omitempty"`
ModifiedTime string `json:"modifiedTime,omitempty"`
Negated bool `json:"negated"`
Operands []Operands `json:"operands"`
Operator string `json:"operator,omitempty"`
MicroTenantID string `json:"microtenantId,omitempty"`
}
type Operands struct {
CreationTime string `json:"creationTime,omitempty"`
ID string `json:"id,omitempty"`
IdpID string `json:"idpId,omitempty"`
LHS string `json:"lhs,omitempty"`
ModifiedBy string `json:"modifiedBy,omitempty"`
ModifiedTime string `json:"modifiedTime,omitempty"`
Name string `json:"name,omitempty"`
ObjectType string `json:"objectType,omitempty"`
RHS string `json:"rhs,omitempty"`
MicroTenantID string `json:"microtenantId,omitempty"`
}
type AppServerGroups struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
type AppConnectorGroups struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
type ServiceEdgeGroups struct {
ID string `json:"id,omitempty"`
}
type Credential struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
}
type PrivilegedCapabilities struct {
ID string `json:"id"`
CreationTime string `json:"creationTime,omitempty"`
ModifiedBy string `json:"modifiedBy,omitempty"`
ModifiedTime string `json:"modifiedTime,omitempty"`
MicroTenantID string `json:"microtenantId,omitempty"`
Capabilities []string `json:"capabilities,omitempty"`
}
type Count struct {
Count string `json:"count"`
}
func (service *Service) GetByPolicyType(policyType string) (*PolicySet, *http.Response, error) {
v := new(PolicySet)
relativeURL := fmt.Sprintf(mgmtConfig + service.Client.Config.CustomerID + "/policySet/policyType/" + policyType)
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
}
// GET --> mgmtconfig/v1/admin/customers/{customerId}/policySet/{policySetId}/rule/{ruleId}
func (service *Service) GetPolicyRule(policySetID, ruleId string) (*PolicyRule, *http.Response, error) {
v := new(PolicyRule)
url := fmt.Sprintf(mgmtConfig+service.Client.Config.CustomerID+"/policySet/%s/rule/%s", policySetID, ruleId)
resp, err := service.Client.NewRequestDo("GET", url, common.Filter{MicroTenantID: service.microTenantID}, nil, &v)
if err != nil {
return nil, nil, err
}
return v, resp, nil
}
// POST --> mgmtconfig/v1/admin/customers/{customerId}/policySet/{policySetId}/rule
func (service *Service) CreateRule(rule *PolicyRule) (*PolicyRule, *http.Response, error) {
v := new(PolicyRule)
path := fmt.Sprintf(mgmtConfig+service.Client.Config.CustomerID+"/policySet/%s/rule", rule.PolicySetID)
resp, err := service.Client.NewRequestDo("POST", path, common.Filter{MicroTenantID: service.microTenantID}, &rule, v)
if err != nil {
return nil, nil, err
}
return v, resp, nil
}
// PUT --> mgmtconfig/v1/admin/customers/{customerId}/policySet/{policySetId}/rule/{ruleId}
func (service *Service) UpdateRule(policySetID, ruleId string, policySetRule *PolicyRule) (*http.Response, error) {
if policySetRule != nil && len(policySetRule.Conditions) == 0 {
policySetRule.Conditions = []Conditions{}
} else {
for i, condtion := range policySetRule.Conditions {
if len(condtion.Operands) == 0 {
policySetRule.Conditions[i].Operands = []Operands{}
} else {
for i, operand := range condtion.Operands {
if operand.Name != "" {
condtion.Operands[i].Name = ""
}
}
}
}
}
path := fmt.Sprintf(mgmtConfig+service.Client.Config.CustomerID+"/policySet/%s/rule/%s", policySetID, ruleId)
resp, err := service.Client.NewRequestDo("PUT", path, common.Filter{MicroTenantID: service.microTenantID}, policySetRule, nil)
if err != nil {
return nil, err
}
return resp, err
}
// DELETE --> mgmtconfig/v1/admin/customers/{customerId}/policySet/{policySetId}/rule/{ruleId}
func (service *Service) Delete(policySetID, ruleId string) (*http.Response, error) {
path := fmt.Sprintf(mgmtConfig+service.Client.Config.CustomerID+"/policySet/%s/rule/%s", policySetID, ruleId)
resp, err := service.Client.NewRequestDo("DELETE", path, common.Filter{MicroTenantID: service.microTenantID}, nil, nil)
if err != nil {
return nil, err
}
return resp, err
}
func (service *Service) GetByNameAndType(policyType, ruleName string) (*PolicyRule, *http.Response, error) {
relativeURL := fmt.Sprintf(mgmtConfig+service.Client.Config.CustomerID+"/policySet/rules/policyType/%s", policyType)
list, resp, err := common.GetAllPagesGenericWithCustomFilters[PolicyRule](service.Client, relativeURL, common.Filter{Search: ruleName, MicroTenantID: service.microTenantID})
if err != nil {
return nil, nil, err
}
for _, p := range list {
if strings.EqualFold(ruleName, p.Name) {
return &p, resp, nil
}
}
return nil, resp, fmt.Errorf("no policy rule named :%s found", ruleName)
}
func (service *Service) GetByNameAndTypes(policyTypes []string, ruleName string) (p *PolicyRule, resp *http.Response, err error) {
for _, policyType := range policyTypes {
p, resp, err = service.GetByNameAndType(policyType, ruleName)
if err != nil {
continue
} else {
return
}
}
return
}
// PUT --> /mgmtconfig/v1/admin/customers/{customerId}/policySet/{policySetId}/rule/{ruleId}/reorder/{newOrder}
func (service *Service) Reorder(policySetID, ruleId string, order int) (*http.Response, error) {
path := fmt.Sprintf(mgmtConfig+service.Client.Config.CustomerID+"/policySet/%s/rule/%s/reorder/%d", policySetID, ruleId, order)
resp, err := service.Client.NewRequestDo("PUT", path, common.Filter{MicroTenantID: service.microTenantID}, nil, nil)
if err != nil {
return nil, err
}
return resp, err
}
// PUT --> /mgmtconfig/v1/admin/customers/{customerId}/policySet/{policySet}/reorder
// ruleIdOrders is a map[ruleID]Order
func (service *Service) BulkReorder(policySetType string, ruleIdToOrder map[string]int) (*http.Response, error) {
policySet, resp, err := service.GetByPolicyType(policySetType)
if err != nil {
return resp, err
}
all, resp, err := service.GetAllByType(policySetType)
if err != nil {
return resp, err
}
sort.SliceStable(all, func(i, j int) bool {
ruleIDi := all[i].ID
ruleIDj := all[j].ID
// Check if ruleIDi and ruleIDj exist in the ruleIdToOrder map
orderi, existsi := ruleIdToOrder[ruleIDi]
orderj, existsj := ruleIdToOrder[ruleIDj]
// If both rules exist in the map, compare their orders
if existsi && existsj {
return orderi <= orderj
}
// If only one of the rules exists in the map, prioritize it
if existsi {
return true
} else if existsj {
return false
}
// If neither rule exists in the map, maintain their relative order
return i <= j
})
// Construct the URL path
path := fmt.Sprintf(mgmtConfig+service.Client.Config.CustomerID+"/policySet/%s/reorder", policySet.ID)
ruleIdsOrdered := []string{}
for _, r := range all {
ruleIdsOrdered = append(ruleIdsOrdered, r.ID)
}
// Create a new PUT request
resp, err = service.Client.NewRequestDo("PUT", path, common.Filter{MicroTenantID: service.microTenantID}, ruleIdsOrdered, nil)
if err != nil {
return nil, err
}
// Check for non-2xx status code and log response body for debugging
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
defer resp.Body.Close() // Ensure the body is always closed
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
// Handle the error of reading the body (optional)
log.Printf("Error reading response body: %s\n", err.Error())
}
log.Printf("Error response from API: %s\n", string(bodyBytes))
return resp, fmt.Errorf("API request failed with status code %d: %s", resp.StatusCode, string(bodyBytes))
}
return resp, nil
}
func (service *Service) RulesCount() (int, *http.Response, error) {
v := new(Count)
relativeURL := fmt.Sprintf(mgmtConfig+service.Client.Config.CustomerID+"/policySet/rules/policyType/GLOBAL_POLICY/count", service.Client.Config.CustomerID)
resp, err := service.Client.NewRequestDo("GET", relativeURL, common.Filter{MicroTenantID: service.microTenantID}, nil, &v)
if err != nil {
return 0, nil, err
}
count, err := strconv.Atoi(v.Count)
return count, resp, err
}
func (service *Service) GetAllByType(policyType string) ([]PolicyRule, *http.Response, error) {
relativeURL := fmt.Sprintf(mgmtConfig+service.Client.Config.CustomerID+"/policySet/rules/policyType/%s", policyType)
list, resp, err := common.GetAllPagesGenericWithCustomFilters[PolicyRule](service.Client, relativeURL, common.Filter{MicroTenantID: service.microTenantID})
if err != nil {
return nil, nil, err
}
return list, resp, nil
}