forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devices_policy.go
225 lines (186 loc) · 8.21 KB
/
devices_policy.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
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
type Enabled struct {
Enabled bool `json:"enabled"`
}
// DeviceClientCertificatesZone identifies if the zero trust zone is configured for an account.
type DeviceClientCertificatesZone struct {
Response
Result Enabled
}
type ServiceModeV2 struct {
Mode string `json:"mode,omitempty"`
Port int `json:"port,omitempty"`
}
type DeviceSettingsPolicy struct {
ServiceModeV2 *ServiceModeV2 `json:"service_mode_v2"`
DisableAutoFallback *bool `json:"disable_auto_fallback"`
FallbackDomains *[]FallbackDomain `json:"fallback_domains"`
Include *[]SplitTunnel `json:"include"`
Exclude *[]SplitTunnel `json:"exclude"`
GatewayUniqueID *string `json:"gateway_unique_id"`
SupportURL *string `json:"support_url"`
CaptivePortal *int `json:"captive_portal"`
AllowModeSwitch *bool `json:"allow_mode_switch"`
SwitchLocked *bool `json:"switch_locked"`
AllowUpdates *bool `json:"allow_updates"`
AutoConnect *int `json:"auto_connect"`
AllowedToLeave *bool `json:"allowed_to_leave"`
PolicyID *string `json:"policy_id"`
Enabled *bool `json:"enabled"`
Name *string `json:"name"`
Match *string `json:"match"`
Precedence *int `json:"precedence"`
Default bool `json:"default"`
ExcludeOfficeIps *bool `json:"exclude_office_ips"`
}
type DeviceSettingsPolicyResponse struct {
Response
Result DeviceSettingsPolicy
}
type DeleteDeviceSettingsPolicyResponse struct {
Response
Result []DeviceSettingsPolicy
}
type DeviceSettingsPolicyRequest struct {
DisableAutoFallback *bool `json:"disable_auto_fallback,omitempty"`
CaptivePortal *int `json:"captive_portal,omitempty"`
AllowModeSwitch *bool `json:"allow_mode_switch,omitempty"`
SwitchLocked *bool `json:"switch_locked,omitempty"`
AllowUpdates *bool `json:"allow_updates,omitempty"`
AutoConnect *int `json:"auto_connect,omitempty"`
AllowedToLeave *bool `json:"allowed_to_leave,omitempty"`
SupportURL *string `json:"support_url,omitempty"`
ServiceModeV2 *ServiceModeV2 `json:"service_mode_v2,omitempty"`
Precedence *int `json:"precedence,omitempty"`
Name *string `json:"name,omitempty"`
Match *string `json:"match,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
ExcludeOfficeIps *bool `json:"exclude_office_ips"`
}
// UpdateDeviceClientCertificates controls the zero trust zone used to provision client certificates.
//
// API reference: https://api.cloudflare.com/#device-client-certificates
func (api *API) UpdateDeviceClientCertificatesZone(ctx context.Context, zoneID string, enable bool) (DeviceClientCertificatesZone, error) {
uri := fmt.Sprintf("/%s/%s/devices/policy/certificates", ZoneRouteRoot, zoneID)
result := DeviceClientCertificatesZone{}
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, Enabled{enable})
if err != nil {
return result, err
}
if err := json.Unmarshal(res, &result); err != nil {
return result, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result, err
}
// GetDeviceClientCertificatesZone controls the zero trust zone used to provision client certificates.
//
// API reference: https://api.cloudflare.com/#device-client-certificates
func (api *API) GetDeviceClientCertificatesZone(ctx context.Context, zoneID string) (DeviceClientCertificatesZone, error) {
uri := fmt.Sprintf("/%s/%s/devices/policy/certificates", ZoneRouteRoot, zoneID)
result := DeviceClientCertificatesZone{}
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return result, err
}
if err := json.Unmarshal(res, &result); err != nil {
return result, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result, err
}
// CreateDeviceSettingsPolicy creates a settings policy against devices that match the policy
//
// API reference: https://api.cloudflare.com/#devices-create-device-settings-policy
func (api *API) CreateDeviceSettingsPolicy(ctx context.Context, accountID string, req DeviceSettingsPolicyRequest) (DeviceSettingsPolicyResponse, error) {
uri := fmt.Sprintf("/%s/%s/devices/policy", AccountRouteRoot, accountID)
result := DeviceSettingsPolicyResponse{}
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, req)
if err != nil {
return result, err
}
if err := json.Unmarshal(res, &result); err != nil {
return result, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result, err
}
// UpdateDefaultDeviceSettingsPolicy updates the default settings policy for an account
//
// API reference: https://api.cloudflare.com/#devices-update-default-device-settings-policy
func (api *API) UpdateDefaultDeviceSettingsPolicy(ctx context.Context, accountID string, req DeviceSettingsPolicyRequest) (DeviceSettingsPolicyResponse, error) {
result := DeviceSettingsPolicyResponse{}
uri := fmt.Sprintf("/%s/%s/devices/policy", AccountRouteRoot, accountID)
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, req)
if err != nil {
return result, err
}
if err := json.Unmarshal(res, &result); err != nil {
return result, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result, err
}
// UpdateDeviceSettingsPolicy updates a settings policy
//
// API reference: https://api.cloudflare.com/#devices-update-device-settings-policy
func (api *API) UpdateDeviceSettingsPolicy(ctx context.Context, accountID, policyID string, req DeviceSettingsPolicyRequest) (DeviceSettingsPolicyResponse, error) {
uri := fmt.Sprintf("/%s/%s/devices/policy/%s", AccountRouteRoot, accountID, policyID)
result := DeviceSettingsPolicyResponse{}
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, req)
if err != nil {
return result, err
}
if err := json.Unmarshal(res, &result); err != nil {
return result, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result, err
}
// DeleteDeviceSettingsPolicy deletes a settings policy and returns a list
// of all of the other policies in the account
//
// API reference: https://api.cloudflare.com/#devices-delete-device-settings-policy
func (api *API) DeleteDeviceSettingsPolicy(ctx context.Context, accountID, policyID string) (DeleteDeviceSettingsPolicyResponse, error) {
uri := fmt.Sprintf("/%s/%s/devices/policy/%s", AccountRouteRoot, accountID, policyID)
result := DeleteDeviceSettingsPolicyResponse{}
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return result, err
}
if err := json.Unmarshal(res, &result); err != nil {
return result, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result, err
}
// GetDefaultDeviceSettings gets the default device settings policy
//
// API reference: https://api.cloudflare.com/#devices-get-default-device-settings-policy
func (api *API) GetDefaultDeviceSettingsPolicy(ctx context.Context, accountID string) (DeviceSettingsPolicyResponse, error) {
uri := fmt.Sprintf("/%s/%s/devices/policy", AccountRouteRoot, accountID)
result := DeviceSettingsPolicyResponse{}
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return result, err
}
if err := json.Unmarshal(res, &result); err != nil {
return result, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result, err
}
// GetDefaultDeviceSettings gets the device settings policy by its policyID
//
// API reference: https://api.cloudflare.com/#devices-get-device-settings-policy-by-id
func (api *API) GetDeviceSettingsPolicy(ctx context.Context, accountID, policyID string) (DeviceSettingsPolicyResponse, error) {
uri := fmt.Sprintf("/%s/%s/devices/policy/%s", AccountRouteRoot, accountID, policyID)
result := DeviceSettingsPolicyResponse{}
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return result, err
}
if err := json.Unmarshal(res, &result); err != nil {
return result, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return result, err
}