forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlp_profile.go
218 lines (176 loc) · 6.42 KB
/
dlp_profile.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
package cloudflare
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
)
var (
ErrMissingProfileID = errors.New("missing required profile ID")
)
// DLPPattern represents a DLP Pattern that matches an entry.
type DLPPattern struct {
Regex string `json:"regex,omitempty"`
Validation string `json:"validation,omitempty"`
}
// DLPEntry represents a DLP Entry, which can be matched in HTTP bodies or files.
type DLPEntry struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
ProfileID string `json:"profile_id,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
Type string `json:"type,omitempty"`
// The following fields are only present for custom entries.
Pattern *DLPPattern `json:"pattern,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
}
// DLPProfile represents a DLP Profile, which contains a set
// of entries.
type DLPProfile struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
AllowedMatchCount int `json:"allowed_match_count"`
// The following fields are omitted for predefined DLP
// profiles
Entries []DLPEntry `json:"entries,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
}
// DLPProfilesCreateRequest represents a request to create a
// set of profiles.
type DLPProfilesCreateRequest struct {
Profiles []DLPProfile `json:"profiles"`
}
// DLPProfileListResponse represents the response from the list
// dlp profiles endpoint.
type DLPProfileListResponse struct {
Result []DLPProfile `json:"result"`
Response
}
// DLPProfileResponse is the API response, containing a single
// access application.
type DLPProfileResponse struct {
Success bool `json:"success"`
Errors []string `json:"errors"`
Messages []string `json:"messages"`
Result DLPProfile `json:"result"`
}
type ListDLPProfilesParams struct{}
type CreateDLPProfilesParams struct {
Profiles []DLPProfile `json:"profiles"`
Type string
}
type UpdateDLPProfileParams struct {
ProfileID string
Profile DLPProfile
Type string
}
// ListDLPProfiles returns all DLP profiles within an account.
//
// API reference: https://api.cloudflare.com/#dlp-profiles-list-all-profiles
func (api *API) ListDLPProfiles(ctx context.Context, rc *ResourceContainer, params ListDLPProfilesParams) ([]DLPProfile, error) {
if rc.Identifier == "" {
return []DLPProfile{}, ErrMissingResourceIdentifier
}
uri := buildURI(fmt.Sprintf("/%s/%s/dlp/profiles", rc.Level, rc.Identifier), nil)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []DLPProfile{}, err
}
var dlpProfilesListResponse DLPProfileListResponse
err = json.Unmarshal(res, &dlpProfilesListResponse)
if err != nil {
return []DLPProfile{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return dlpProfilesListResponse.Result, nil
}
// GetDLPProfile returns a single DLP profile (custom or predefined) based on
// the profile ID.
//
// API reference: https://api.cloudflare.com/#dlp-profiles-get-dlp-profile
func (api *API) GetDLPProfile(ctx context.Context, rc *ResourceContainer, profileID string) (DLPProfile, error) {
if rc.Identifier == "" {
return DLPProfile{}, ErrMissingResourceIdentifier
}
if profileID == "" {
return DLPProfile{}, ErrMissingProfileID
}
uri := buildURI(fmt.Sprintf("/%s/%s/dlp/profiles/%s", rc.Level, rc.Identifier, profileID), nil)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return DLPProfile{}, err
}
var dlpProfileResponse DLPProfileResponse
err = json.Unmarshal(res, &dlpProfileResponse)
if err != nil {
return DLPProfile{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return dlpProfileResponse.Result, nil
}
// CreateDLPProfiles creates a set of DLP Profile.
//
// API reference: https://api.cloudflare.com/#dlp-profiles-create-custom-profiles
func (api *API) CreateDLPProfiles(ctx context.Context, rc *ResourceContainer, params CreateDLPProfilesParams) ([]DLPProfile, error) {
if rc.Identifier == "" {
return []DLPProfile{}, ErrMissingResourceIdentifier
}
if params.Type == "" || params.Type != "custom" {
return []DLPProfile{}, fmt.Errorf("unsupported DLP profile type: %q", params.Type)
}
uri := buildURI(fmt.Sprintf("/%s/%s/dlp/profiles/%s", rc.Level, rc.Identifier, params.Type), nil)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params)
if err != nil {
return []DLPProfile{}, err
}
var dLPCustomProfilesResponse DLPProfileListResponse
err = json.Unmarshal(res, &dLPCustomProfilesResponse)
if err != nil {
return []DLPProfile{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return dLPCustomProfilesResponse.Result, nil
}
// DeleteDLPProfile deletes a DLP profile. Only custom profiles can be deleted.
//
// API reference: https://api.cloudflare.com/#dlp-profiles-delete-custom-profile
func (api *API) DeleteDLPProfile(ctx context.Context, rc *ResourceContainer, profileID string) error {
if rc.Identifier == "" {
return ErrMissingResourceIdentifier
}
if profileID == "" {
return ErrMissingProfileID
}
uri := buildURI(fmt.Sprintf("/%s/%s/dlp/profiles/custom/%s", rc.Level, rc.Identifier, profileID), nil)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
return err
}
// UpdateDLPProfile updates a DLP profile.
//
// API reference: https://api.cloudflare.com/#dlp-profiles-update-custom-profile
// API reference: https://api.cloudflare.com/#dlp-profiles-update-predefined-profile
func (api *API) UpdateDLPProfile(ctx context.Context, rc *ResourceContainer, params UpdateDLPProfileParams) (DLPProfile, error) {
if rc.Identifier == "" {
return DLPProfile{}, ErrMissingResourceIdentifier
}
if params.Type == "" {
params.Type = "custom"
}
if params.ProfileID == "" {
return DLPProfile{}, ErrMissingProfileID
}
uri := buildURI(fmt.Sprintf("/%s/%s/dlp/profiles/%s/%s", rc.Level, rc.Identifier, params.Type, params.ProfileID), nil)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params.Profile)
if err != nil {
return DLPProfile{}, err
}
var dlpProfileResponse DLPProfileResponse
err = json.Unmarshal(res, &dlpProfileResponse)
if err != nil {
return DLPProfile{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return dlpProfileResponse.Result, nil
}