/
custom_deny.go
354 lines (297 loc) · 11 KB
/
custom_deny.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
package appsec
import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
"strconv"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// The CustomDeny interface supports creating, retrievinfg, modifying and removing custom deny actions
// for a configuration.
// https://developer.akamai.com/api/cloud_security/application_security/v1.html#customdeny
//
CustomDeny interface {
// https://developer.akamai.com/api/cloud_security/application_security/v1.html#getcustomdeny
GetCustomDenyList(ctx context.Context, params GetCustomDenyListRequest) (*GetCustomDenyListResponse, error)
// https://developer.akamai.com/api/cloud_security/application_security/v1.html#getcustomdenyaction
GetCustomDeny(ctx context.Context, params GetCustomDenyRequest) (*GetCustomDenyResponse, error)
// https://developer.akamai.com/api/cloud_security/application_security/v1.html#postcustomdeny
CreateCustomDeny(ctx context.Context, params CreateCustomDenyRequest) (*CreateCustomDenyResponse, error)
// https://developer.akamai.com/api/cloud_security/application_security/v1.html#putcustomdenyaction
UpdateCustomDeny(ctx context.Context, params UpdateCustomDenyRequest) (*UpdateCustomDenyResponse, error)
// https://developer.akamai.com/api/cloud_security/application_security/v1.html#deletecustomdenyaction
RemoveCustomDeny(ctx context.Context, params RemoveCustomDenyRequest) (*RemoveCustomDenyResponse, error)
}
customDenyID string
// GetCustomDenyListRequest is used to retrieve the custom deny actions for a configuration.
GetCustomDenyListRequest struct {
ConfigID int `json:"configId"`
Version int `json:"version"`
ID string `json:"id,omitempty"`
}
// GetCustomDenyListResponse is returned from a call to GetCustomDenyList.
GetCustomDenyListResponse struct {
CustomDenyList []struct {
Description string `json:"description,omitempty"`
Name string `json:"name"`
ID customDenyID `json:"id"`
Parameters []struct {
DisplayName string `json:"-"`
Name string `json:"name"`
Value string `json:"value"`
} `json:"parameters"`
} `json:"customDenyList"`
}
// GetCustomDenyRequest is used to retrieve a specific custom deny action.
GetCustomDenyRequest struct {
ConfigID int `json:"configId"`
Version int `json:"version"`
ID string `json:"id,omitempty"`
}
// GetCustomDenyResponse is returned from a call to GetCustomDeny.
GetCustomDenyResponse struct {
Description string `json:"description,omitempty"`
Name string `json:"name"`
ID customDenyID `json:"-"`
Parameters []struct {
DisplayName string `json:"-"`
Name string `json:"name"`
Value string `json:"value"`
} `json:"parameters"`
}
// CreateCustomDenyRequest is used to create a new custom deny action for a specific configuration.
CreateCustomDenyRequest struct {
ConfigID int `json:"-"`
Version int `json:"-"`
JsonPayloadRaw json.RawMessage `json:"-"`
}
// CreateCustomDenyResponse is returned from a call to CreateCustomDeny.
CreateCustomDenyResponse struct {
Description string `json:"description,omitempty"`
Name string `json:"name"`
ID customDenyID `json:"id"`
Parameters []struct {
DisplayName string `json:"-"`
Name string `json:"name"`
Value string `json:"value"`
} `json:"parameters"`
}
// UpdateCustomDenyRequest is used to details for a specific custom deny action.
UpdateCustomDenyRequest struct {
ConfigID int `json:"-"`
Version int `json:"-"`
ID string `json:"id"`
JsonPayloadRaw json.RawMessage `json:"-"`
}
// UpdateCustomDenyResponse is returned from a call to UpdateCustomDeny.
UpdateCustomDenyResponse struct {
Description string `json:"description,omitempty"`
Name string `json:"name"`
ID customDenyID `json:"-"`
Parameters []struct {
DisplayName string `json:"-"`
Name string `json:"name"`
Value string `json:"value"`
} `json:"parameters"`
}
// RemoveCustomDenyRequest is used to remove an existing custom deny action.
RemoveCustomDenyRequest struct {
ConfigID int `json:"-"`
Version int `json:"-"`
ID string `json:"id,omitempty"`
}
// RemoveCustomDenyResponse is returned from a call to RemoveCustomDeny.
RemoveCustomDenyResponse struct {
Empty string `json:"-"`
}
)
// UnmarshalJSON reads a customDenyID struct from its data argument.
func (c *customDenyID) UnmarshalJSON(data []byte) error {
var nums interface{}
err := json.Unmarshal(data, &nums)
if err != nil {
return err
}
items := reflect.ValueOf(nums)
switch items.Kind() {
case reflect.String:
*c = customDenyID(nums.(string))
case reflect.Int:
*c = customDenyID(strconv.Itoa(nums.(int)))
}
return nil
}
// Validate validates a GetCustomDenyRequest.
func (v GetCustomDenyRequest) Validate() error {
return validation.Errors{
"ConfigID": validation.Validate(v.ConfigID, validation.Required),
"Version": validation.Validate(v.Version, validation.Required),
"ID": validation.Validate(v.ID, validation.Required),
}.Filter()
}
// Validate validates a GetCustomDenysRequest.
func (v GetCustomDenyListRequest) Validate() error {
return validation.Errors{
"ConfigID": validation.Validate(v.ConfigID, validation.Required),
"Version": validation.Validate(v.Version, validation.Required),
}.Filter()
}
// Validate validates a CreateCustomDenyRequest.
func (v CreateCustomDenyRequest) Validate() error {
return validation.Errors{
"ConfigID": validation.Validate(v.ConfigID, validation.Required),
"Version": validation.Validate(v.Version, validation.Required),
}.Filter()
}
// Validate validates an UpdateCustomDenyRequest.
func (v UpdateCustomDenyRequest) Validate() error {
return validation.Errors{
"ConfigID": validation.Validate(v.ConfigID, validation.Required),
"Version": validation.Validate(v.Version, validation.Required),
"ID": validation.Validate(v.ID, validation.Required),
}.Filter()
}
// Validate validates a RemoveCustomDenyRequest.
func (v RemoveCustomDenyRequest) Validate() error {
return validation.Errors{
"ConfigID": validation.Validate(v.ConfigID, validation.Required),
"Version": validation.Validate(v.Version, validation.Required),
"ID": validation.Validate(v.ID, validation.Required),
}.Filter()
}
func (p *appsec) GetCustomDeny(ctx context.Context, params GetCustomDenyRequest) (*GetCustomDenyResponse, error) {
logger := p.Log(ctx)
logger.Debug("GetCustomDeny")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
}
uri := fmt.Sprintf(
"/appsec/v1/configs/%d/versions/%d/custom-deny/%s",
params.ConfigID,
params.Version,
params.ID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("failed to create GetCustomDeny request: %w", err)
}
var result GetCustomDenyResponse
resp, err := p.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("get custom deny request failed: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, p.Error(resp)
}
return &result, nil
}
func (p *appsec) GetCustomDenyList(ctx context.Context, params GetCustomDenyListRequest) (*GetCustomDenyListResponse, error) {
logger := p.Log(ctx)
logger.Debug("GetCustomDenyList")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
}
uri := fmt.Sprintf(
"/appsec/v1/configs/%d/versions/%d/custom-deny",
params.ConfigID,
params.Version,
)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("failed to create GetCustomDenyList request: %w", err)
}
var result GetCustomDenyListResponse
resp, err := p.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("get custom deny list request failed: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, p.Error(resp)
}
if params.ID != "" {
var filteredResult GetCustomDenyListResponse
for _, val := range result.CustomDenyList {
if string(val.ID) == params.ID {
filteredResult.CustomDenyList = append(filteredResult.CustomDenyList, val)
}
}
return &filteredResult, nil
}
return &result, nil
}
func (p *appsec) UpdateCustomDeny(ctx context.Context, params UpdateCustomDenyRequest) (*UpdateCustomDenyResponse, error) {
logger := p.Log(ctx)
logger.Debug("UpdateCustomDeny")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
}
uri := fmt.Sprintf(
"/appsec/v1/configs/%d/versions/%d/custom-deny/%s",
params.ConfigID,
params.Version,
params.ID,
)
req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
if err != nil {
return nil, fmt.Errorf("failed to create UpdateCustomDeny request: %w", err)
}
var result UpdateCustomDenyResponse
req.Header.Set("Content-Type", "application/json")
resp, err := p.Exec(req, &result, params.JsonPayloadRaw)
if err != nil {
return nil, fmt.Errorf("update custom deny request failed: %w", err)
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return nil, p.Error(resp)
}
return &result, nil
}
func (p *appsec) CreateCustomDeny(ctx context.Context, params CreateCustomDenyRequest) (*CreateCustomDenyResponse, error) {
logger := p.Log(ctx)
logger.Debug("CreateCustomDeny")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
}
uri := fmt.Sprintf(
"/appsec/v1/configs/%d/versions/%d/custom-deny",
params.ConfigID,
params.Version,
)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return nil, fmt.Errorf("failed to create CreateCustomDeny request: %w", err)
}
var result CreateCustomDenyResponse
req.Header.Set("Content-Type", "application/json")
resp, err := p.Exec(req, &result, params.JsonPayloadRaw)
if err != nil {
return nil, fmt.Errorf("create custom deny request failed: %w", err)
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return nil, p.Error(resp)
}
return &result, nil
}
func (p *appsec) RemoveCustomDeny(ctx context.Context, params RemoveCustomDenyRequest) (*RemoveCustomDenyResponse, error) {
logger := p.Log(ctx)
logger.Debug("RemoveCustomDeny")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
}
uri := fmt.Sprintf("/appsec/v1/configs/%d/versions/%d/custom-deny/%s", params.ConfigID, params.Version, params.ID)
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return nil, fmt.Errorf("failed to create RemoveCustomDeny request: %w", err)
}
var result RemoveCustomDenyResponse
resp, err := p.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("remove custom deny request failed: %w", err)
}
if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusOK {
return nil, p.Error(resp)
}
return &result, nil
}