forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteams_rules.go
359 lines (290 loc) · 11.3 KB
/
teams_rules.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
package cloudflare
import (
"context"
"fmt"
"net/http"
"time"
"github.com/goccy/go-json"
)
type TeamsRuleSettings struct {
// list of ipv4 or ipv6 ips to override with, when action is set to dns override
OverrideIPs []string `json:"override_ips"`
// show this string at block page caused by this rule
BlockReason string `json:"block_reason"`
// host name to override with when action is set to dns override. Can not be used with OverrideIPs
OverrideHost string `json:"override_host"`
// settings for browser isolation actions
BISOAdminControls *TeamsBISOAdminControlSettings `json:"biso_admin_controls"`
// settings for l4(network) level overrides
L4Override *TeamsL4OverrideSettings `json:"l4override"`
// settings for adding headers to http requests
AddHeaders http.Header `json:"add_headers"`
// settings for session check in allow action
CheckSession *TeamsCheckSessionSettings `json:"check_session"`
// Enable block page on rules with action block
BlockPageEnabled bool `json:"block_page_enabled"`
// whether to disable dnssec validation for allow action
InsecureDisableDNSSECValidation bool `json:"insecure_disable_dnssec_validation"`
// settings for rules with egress action
EgressSettings *EgressSettings `json:"egress"`
// DLP payload logging configuration
PayloadLog *TeamsDlpPayloadLogSettings `json:"payload_log"`
//AuditSsh Settings
AuditSSH *AuditSSHRuleSettings `json:"audit_ssh"`
// Turns on ip category based filter on dns if the rule contains dns category checks
IPCategories bool `json:"ip_categories"`
// Turns on for explicitly ignoring cname domain category matches
IgnoreCNAMECategoryMatches *bool `json:"ignore_cname_category_matches"`
// Allow parent MSP accounts to enable bypass their children's rules. Do not set them for non MSP accounts.
AllowChildBypass *bool `json:"allow_child_bypass,omitempty"`
// Allow child MSP accounts to bypass their parent's rules. Do not set them for non MSP accounts.
BypassParentRule *bool `json:"bypass_parent_rule,omitempty"`
// Action taken when an untrusted origin certificate error occurs in a http allow rule
UntrustedCertSettings *UntrustedCertSettings `json:"untrusted_cert"`
// Specifies that a resolver policy should use Cloudflare's DNS Resolver.
ResolveDnsThroughCloudflare *bool `json:"resolve_dns_through_cloudflare,omitempty"`
// Resolver policy settings.
DnsResolverSettings *TeamsDnsResolverSettings `json:"dns_resolvers,omitempty"`
NotificationSettings *TeamsNotificationSettings `json:"notification_settings"`
}
type TeamsGatewayUntrustedCertAction string
const (
UntrustedCertPassthrough TeamsGatewayUntrustedCertAction = "pass_through"
UntrustedCertBlock TeamsGatewayUntrustedCertAction = "block"
UntrustedCertError TeamsGatewayUntrustedCertAction = "error"
)
type UntrustedCertSettings struct {
Action TeamsGatewayUntrustedCertAction `json:"action"`
}
type TeamsNotificationSettings struct {
Enabled *bool `json:"enabled,omitempty"`
Message string `json:"msg"`
SupportURL string `json:"support_url"`
}
type AuditSSHRuleSettings struct {
CommandLogging bool `json:"command_logging"`
}
type EgressSettings struct {
Ipv6Range string `json:"ipv6"`
Ipv4 string `json:"ipv4"`
Ipv4Fallback string `json:"ipv4_fallback"`
}
// TeamsL4OverrideSettings used in l4 filter type rule with action set to override.
type TeamsL4OverrideSettings struct {
IP string `json:"ip,omitempty"`
Port int `json:"port,omitempty"`
}
type TeamsBISOAdminControlSettings struct {
DisablePrinting bool `json:"dp"`
DisableCopyPaste bool `json:"dcp"`
DisableDownload bool `json:"dd"`
DisableUpload bool `json:"du"`
DisableKeyboard bool `json:"dk"`
DisableClipboardRedirection bool `json:"dcr"`
}
type TeamsCheckSessionSettings struct {
Enforce bool `json:"enforce"`
Duration Duration `json:"duration"`
}
type (
TeamsDnsResolverSettings struct {
V4Resolvers []TeamsDnsResolverAddressV4 `json:"ipv4,omitempty"`
V6Resolvers []TeamsDnsResolverAddressV6 `json:"ipv6,omitempty"`
}
TeamsDnsResolverAddressV4 struct {
TeamsDnsResolverAddress
}
TeamsDnsResolverAddressV6 struct {
TeamsDnsResolverAddress
}
TeamsDnsResolverAddress struct {
IP string `json:"ip"`
Port *int `json:"port,omitempty"`
VnetID string `json:"vnet_id,omitempty"`
RouteThroughPrivateNetwork *bool `json:"route_through_private_network,omitempty"`
}
)
type TeamsDlpPayloadLogSettings struct {
Enabled bool `json:"enabled"`
}
type TeamsFilterType string
type TeamsGatewayAction string
const (
HttpFilter TeamsFilterType = "http"
DnsFilter TeamsFilterType = "dns"
L4Filter TeamsFilterType = "l4"
EgressFilter TeamsFilterType = "egress"
DnsResolverFilter TeamsFilterType = "dns_resolver"
)
const (
Allow TeamsGatewayAction = "allow" // dns|http|l4
Block TeamsGatewayAction = "block" // dns|http|l4
SafeSearch TeamsGatewayAction = "safesearch" // dns
YTRestricted TeamsGatewayAction = "ytrestricted" // dns
On TeamsGatewayAction = "on" // http
Off TeamsGatewayAction = "off" // http
Scan TeamsGatewayAction = "scan" // http
NoScan TeamsGatewayAction = "noscan" // http
Isolate TeamsGatewayAction = "isolate" // http
NoIsolate TeamsGatewayAction = "noisolate" // http
Override TeamsGatewayAction = "override" // http
L4Override TeamsGatewayAction = "l4_override" // l4
Egress TeamsGatewayAction = "egress" // egress
AuditSSH TeamsGatewayAction = "audit_ssh" // l4
Resolve TeamsGatewayAction = "resolve" // resolve
)
func TeamsRulesActionValues() []string {
return []string{
string(Allow),
string(Block),
string(SafeSearch),
string(YTRestricted),
string(On),
string(Off),
string(Scan),
string(NoScan),
string(Isolate),
string(NoIsolate),
string(Override),
string(L4Override),
string(Egress),
string(AuditSSH),
string(Resolve),
}
}
func TeamsRulesUntrustedCertActionValues() []string {
return []string{
string(UntrustedCertPassthrough),
string(UntrustedCertBlock),
string(UntrustedCertError),
}
}
// TeamsRule represents an Teams wirefilter rule.
type TeamsRule struct {
ID string `json:"id,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
DeletedAt *time.Time `json:"deleted_at,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Precedence uint64 `json:"precedence"`
Enabled bool `json:"enabled"`
Action TeamsGatewayAction `json:"action"`
Filters []TeamsFilterType `json:"filters"`
Traffic string `json:"traffic"`
Identity string `json:"identity"`
DevicePosture string `json:"device_posture"`
Version uint64 `json:"version"`
RuleSettings TeamsRuleSettings `json:"rule_settings,omitempty"`
}
// TeamsRuleResponse is the API response, containing a single rule.
type TeamsRuleResponse struct {
Response
Result TeamsRule `json:"result"`
}
// TeamsRuleResponse is the API response, containing an array of rules.
type TeamsRulesResponse struct {
Response
Result []TeamsRule `json:"result"`
}
// TeamsRulePatchRequest is used to patch an existing rule.
type TeamsRulePatchRequest struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Precedence uint64 `json:"precedence"`
Enabled bool `json:"enabled"`
Action TeamsGatewayAction `json:"action"`
RuleSettings TeamsRuleSettings `json:"rule_settings,omitempty"`
}
// TeamsRules returns all rules within an account.
//
// API reference: https://api.cloudflare.com/#teams-rules-properties
func (api *API) TeamsRules(ctx context.Context, accountID string) ([]TeamsRule, error) {
uri := fmt.Sprintf("/accounts/%s/gateway/rules", accountID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []TeamsRule{}, err
}
var teamsRulesResponse TeamsRulesResponse
err = json.Unmarshal(res, &teamsRulesResponse)
if err != nil {
return []TeamsRule{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return teamsRulesResponse.Result, nil
}
// TeamsRule returns the rule with rule ID in the URL.
//
// API reference: https://api.cloudflare.com/#teams-rules-properties
func (api *API) TeamsRule(ctx context.Context, accountID string, ruleId string) (TeamsRule, error) {
uri := fmt.Sprintf("/accounts/%s/gateway/rules/%s", accountID, ruleId)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return TeamsRule{}, err
}
var teamsRuleResponse TeamsRuleResponse
err = json.Unmarshal(res, &teamsRuleResponse)
if err != nil {
return TeamsRule{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return teamsRuleResponse.Result, nil
}
// TeamsCreateRule creates a rule with wirefilter expression.
//
// API reference: https://api.cloudflare.com/#teams-rules-properties
func (api *API) TeamsCreateRule(ctx context.Context, accountID string, rule TeamsRule) (TeamsRule, error) {
uri := fmt.Sprintf("/accounts/%s/gateway/rules", accountID)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, rule)
if err != nil {
return TeamsRule{}, err
}
var teamsRuleResponse TeamsRuleResponse
err = json.Unmarshal(res, &teamsRuleResponse)
if err != nil {
return TeamsRule{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return teamsRuleResponse.Result, nil
}
// TeamsUpdateRule updates a rule with wirefilter expression.
//
// API reference: https://api.cloudflare.com/#teams-rules-properties
func (api *API) TeamsUpdateRule(ctx context.Context, accountID string, ruleId string, rule TeamsRule) (TeamsRule, error) {
uri := fmt.Sprintf("/accounts/%s/gateway/rules/%s", accountID, ruleId)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, rule)
if err != nil {
return TeamsRule{}, err
}
var teamsRuleResponse TeamsRuleResponse
err = json.Unmarshal(res, &teamsRuleResponse)
if err != nil {
return TeamsRule{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return teamsRuleResponse.Result, nil
}
// TeamsPatchRule patches a rule associated values.
//
// API reference: https://api.cloudflare.com/#teams-rules-properties
func (api *API) TeamsPatchRule(ctx context.Context, accountID string, ruleId string, rule TeamsRulePatchRequest) (TeamsRule, error) {
uri := fmt.Sprintf("/accounts/%s/gateway/rules/%s", accountID, ruleId)
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, rule)
if err != nil {
return TeamsRule{}, err
}
var teamsRuleResponse TeamsRuleResponse
err = json.Unmarshal(res, &teamsRuleResponse)
if err != nil {
return TeamsRule{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return teamsRuleResponse.Result, nil
}
// TeamsDeleteRule deletes a rule.
//
// API reference: https://api.cloudflare.com/#teams-rules-properties
func (api *API) TeamsDeleteRule(ctx context.Context, accountID string, ruleId string) error {
uri := fmt.Sprintf("/accounts/%s/gateway/rules/%s", accountID, ruleId)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return nil
}