forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_group.go
383 lines (326 loc) · 12.1 KB
/
access_group.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
// AccessGroup defines a group for allowing or disallowing access to
// one or more Access applications.
type AccessGroup struct {
ID string `json:"id,omitempty"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
Name string `json:"name"`
// The include group works like an OR logical operator. The user must
// satisfy one of the rules.
Include []interface{} `json:"include"`
// The exclude group works like a NOT logical operator. The user must
// not satisfy all of the rules in exclude.
Exclude []interface{} `json:"exclude"`
// The require group works like a AND logical operator. The user must
// satisfy all of the rules in require.
Require []interface{} `json:"require"`
}
// AccessGroupEmail is used for managing access based on the email.
// For example, restrict access to users with the email addresses
// `test@example.com` or `someone@example.com`.
type AccessGroupEmail struct {
Email struct {
Email string `json:"email"`
} `json:"email"`
}
// AccessGroupEmailDomain is used for managing access based on an email
// domain domain such as `example.com` instead of individual addresses.
type AccessGroupEmailDomain struct {
EmailDomain struct {
Domain string `json:"domain"`
} `json:"email_domain"`
}
// AccessGroupIP is used for managing access based in the IP. It
// accepts individual IPs or CIDRs.
type AccessGroupIP struct {
IP struct {
IP string `json:"ip"`
} `json:"ip"`
}
// AccessGroupGeo is used for managing access based on the country code.
type AccessGroupGeo struct {
Geo struct {
CountryCode string `json:"country_code"`
} `json:"geo"`
}
// AccessGroupEveryone is used for managing access to everyone.
type AccessGroupEveryone struct {
Everyone struct{} `json:"everyone"`
}
// AccessGroupServiceToken is used for managing access based on a specific
// service token.
type AccessGroupServiceToken struct {
ServiceToken struct {
ID string `json:"token_id"`
} `json:"service_token"`
}
// AccessGroupAnyValidServiceToken is used for managing access for all valid
// service tokens (not restricted).
type AccessGroupAnyValidServiceToken struct {
AnyValidServiceToken struct{} `json:"any_valid_service_token"`
}
// AccessGroupAccessGroup is used for managing access based on an
// access group.
type AccessGroupAccessGroup struct {
Group struct {
ID string `json:"id"`
} `json:"group"`
}
// AccessGroupCertificate is used for managing access to based on a valid
// mTLS certificate being presented.
type AccessGroupCertificate struct {
Certificate struct{} `json:"certificate"`
}
// AccessGroupCertificateCommonName is used for managing access based on a
// common name within a certificate.
type AccessGroupCertificateCommonName struct {
CommonName struct {
CommonName string `json:"common_name"`
} `json:"common_name"`
}
// AccessGroupExternalEvaluation is used for passing user identity to an external url.
type AccessGroupExternalEvaluation struct {
ExternalEvaluation struct {
EvaluateURL string `json:"evaluate_url"`
KeysURL string `json:"keys_url"`
} `json:"external_evaluation"`
}
// AccessGroupGSuite is used to configure access based on GSuite group.
type AccessGroupGSuite struct {
Gsuite struct {
Email string `json:"email"`
IdentityProviderID string `json:"identity_provider_id"`
} `json:"gsuite"`
}
// AccessGroupGitHub is used to configure access based on a GitHub organisation.
type AccessGroupGitHub struct {
GitHubOrganization struct {
Name string `json:"name"`
Team string `json:"team,omitempty"`
IdentityProviderID string `json:"identity_provider_id"`
} `json:"github-organization"`
}
// AccessGroupAzure is used to configure access based on a Azure group.
type AccessGroupAzure struct {
AzureAD struct {
ID string `json:"id"`
IdentityProviderID string `json:"identity_provider_id"`
} `json:"azureAD"`
}
// AccessGroupOkta is used to configure access based on a Okta group.
type AccessGroupOkta struct {
Okta struct {
Name string `json:"name"`
IdentityProviderID string `json:"identity_provider_id"`
} `json:"okta"`
}
// AccessGroupSAML is used to allow SAML users with a specific attribute
// configuration.
type AccessGroupSAML struct {
Saml struct {
AttributeName string `json:"attribute_name"`
AttributeValue string `json:"attribute_value"`
IdentityProviderID string `json:"identity_provider_id"`
} `json:"saml"`
}
// AccessGroupAuthMethod is used for managing access by the "amr"
// (Authentication Methods References) identifier. For example, an
// application may want to require that users authenticate using a hardware
// key by setting the "auth_method" to "swk". A list of values are listed
// here: https://tools.ietf.org/html/rfc8176#section-2. Custom values are
// supported as well.
type AccessGroupAuthMethod struct {
AuthMethod struct {
AuthMethod string `json:"auth_method"`
} `json:"auth_method"`
}
// AccessGroupLoginMethod restricts the application to specific IdP instances.
type AccessGroupLoginMethod struct {
LoginMethod struct {
ID string `json:"id"`
} `json:"login_method"`
}
// AccessGroupDevicePosture restricts the application to specific devices.
type AccessGroupDevicePosture struct {
DevicePosture struct {
ID string `json:"integration_uid"`
} `json:"device_posture"`
}
// AccessGroupListResponse represents the response from the list
// access group endpoint.
type AccessGroupListResponse struct {
Result []AccessGroup `json:"result"`
Response
ResultInfo `json:"result_info"`
}
// AccessGroupIPList restricts the application to specific teams_list of ips.
type AccessGroupIPList struct {
IPList struct {
ID string `json:"id"`
} `json:"ip_list"`
}
// AccessGroupDetailResponse is the API response, containing a single
// access group.
type AccessGroupDetailResponse struct {
Success bool `json:"success"`
Errors []string `json:"errors"`
Messages []string `json:"messages"`
Result AccessGroup `json:"result"`
}
// AccessGroups returns all access groups for an access application.
//
// API reference: https://api.cloudflare.com/#access-groups-list-access-groups
func (api *API) AccessGroups(ctx context.Context, accountID string, pageOpts PaginationOptions) ([]AccessGroup, ResultInfo, error) {
return api.accessGroups(ctx, accountID, pageOpts, AccountRouteRoot)
}
// ZoneLevelAccessGroups returns all zone level access groups for an access application.
//
// API reference: https://api.cloudflare.com/#zone-level-access-groups-list-access-groups
func (api *API) ZoneLevelAccessGroups(ctx context.Context, zoneID string, pageOpts PaginationOptions) ([]AccessGroup, ResultInfo, error) {
return api.accessGroups(ctx, zoneID, pageOpts, ZoneRouteRoot)
}
func (api *API) accessGroups(ctx context.Context, id string, pageOpts PaginationOptions, routeRoot RouteRoot) ([]AccessGroup, ResultInfo, error) {
uri := buildURI(
fmt.Sprintf(
"/%s/%s/access/groups",
routeRoot,
id,
),
pageOpts,
)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []AccessGroup{}, ResultInfo{}, err
}
var accessGroupListResponse AccessGroupListResponse
err = json.Unmarshal(res, &accessGroupListResponse)
if err != nil {
return []AccessGroup{}, ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return accessGroupListResponse.Result, accessGroupListResponse.ResultInfo, nil
}
// AccessGroup returns a single group based on the group ID.
//
// API reference: https://api.cloudflare.com/#access-groups-access-group-details
func (api *API) AccessGroup(ctx context.Context, accountID, groupID string) (AccessGroup, error) {
return api.accessGroup(ctx, accountID, groupID, AccountRouteRoot)
}
// ZoneLevelAccessGroup returns a single zone level group based on the group ID.
//
// API reference: https://api.cloudflare.com/#zone-level-access-groups-access-group-details
func (api *API) ZoneLevelAccessGroup(ctx context.Context, zoneID, groupID string) (AccessGroup, error) {
return api.accessGroup(ctx, zoneID, groupID, ZoneRouteRoot)
}
func (api *API) accessGroup(ctx context.Context, id, groupID string, routeRoot RouteRoot) (AccessGroup, error) {
uri := fmt.Sprintf(
"/%s/%s/access/groups/%s",
routeRoot,
id,
groupID,
)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return AccessGroup{}, err
}
var accessGroupDetailResponse AccessGroupDetailResponse
err = json.Unmarshal(res, &accessGroupDetailResponse)
if err != nil {
return AccessGroup{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return accessGroupDetailResponse.Result, nil
}
// CreateAccessGroup creates a new access group.
//
// API reference: https://api.cloudflare.com/#access-groups-create-access-group
func (api *API) CreateAccessGroup(ctx context.Context, accountID string, accessGroup AccessGroup) (AccessGroup, error) {
return api.createAccessGroup(ctx, accountID, accessGroup, AccountRouteRoot)
}
// CreateZoneLevelAccessGroup creates a new zone level access group.
//
// API reference: https://api.cloudflare.com/#zone-level-access-groups-create-access-group
func (api *API) CreateZoneLevelAccessGroup(ctx context.Context, zoneID string, accessGroup AccessGroup) (AccessGroup, error) {
return api.createAccessGroup(ctx, zoneID, accessGroup, ZoneRouteRoot)
}
func (api *API) createAccessGroup(ctx context.Context, id string, accessGroup AccessGroup, routeRoot RouteRoot) (AccessGroup, error) {
uri := fmt.Sprintf(
"/%s/%s/access/groups",
routeRoot,
id,
)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, accessGroup)
if err != nil {
return AccessGroup{}, err
}
var accessGroupDetailResponse AccessGroupDetailResponse
err = json.Unmarshal(res, &accessGroupDetailResponse)
if err != nil {
return AccessGroup{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return accessGroupDetailResponse.Result, nil
}
// UpdateAccessGroup updates an existing access group.
//
// API reference: https://api.cloudflare.com/#access-groups-update-access-group
func (api *API) UpdateAccessGroup(ctx context.Context, accountID string, accessGroup AccessGroup) (AccessGroup, error) {
return api.updateAccessGroup(ctx, accountID, accessGroup, AccountRouteRoot)
}
// UpdateZoneLevelAccessGroup updates an existing zone level access group.
//
// API reference: https://api.cloudflare.com/#zone-level-access-groups-update-access-group
func (api *API) UpdateZoneLevelAccessGroup(ctx context.Context, zoneID string, accessGroup AccessGroup) (AccessGroup, error) {
return api.updateAccessGroup(ctx, zoneID, accessGroup, ZoneRouteRoot)
}
func (api *API) updateAccessGroup(ctx context.Context, id string, accessGroup AccessGroup, routeRoot RouteRoot) (AccessGroup, error) {
if accessGroup.ID == "" {
return AccessGroup{}, fmt.Errorf("access group ID cannot be empty")
}
uri := fmt.Sprintf(
"/%s/%s/access/groups/%s",
routeRoot,
id,
accessGroup.ID,
)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, accessGroup)
if err != nil {
return AccessGroup{}, err
}
var accessGroupDetailResponse AccessGroupDetailResponse
err = json.Unmarshal(res, &accessGroupDetailResponse)
if err != nil {
return AccessGroup{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return accessGroupDetailResponse.Result, nil
}
// DeleteAccessGroup deletes an access group.
//
// API reference: https://api.cloudflare.com/#access-groups-delete-access-group
func (api *API) DeleteAccessGroup(ctx context.Context, accountID, groupID string) error {
return api.deleteAccessGroup(ctx, accountID, groupID, AccountRouteRoot)
}
// DeleteZoneLevelAccessGroup deletes a zone level access group.
//
// API reference: https://api.cloudflare.com/#zone-level-access-groups-delete-access-group
func (api *API) DeleteZoneLevelAccessGroup(ctx context.Context, zoneID, groupID string) error {
return api.deleteAccessGroup(ctx, zoneID, groupID, ZoneRouteRoot)
}
func (api *API) deleteAccessGroup(ctx context.Context, id string, groupID string, routeRoot RouteRoot) error {
uri := fmt.Sprintf(
"/%s/%s/access/groups/%s",
routeRoot,
id,
groupID,
)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return nil
}