/
cache.go
456 lines (391 loc) · 18.6 KB
/
cache.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package botman
import (
"context"
"errors"
"fmt"
"sync"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v7/pkg/botman"
"github.com/akamai/terraform-provider-akamai/v5/pkg/cache"
akameta "github.com/akamai/terraform-provider-akamai/v5/pkg/meta"
"github.com/apex/log"
)
var (
botDetectionActionMutex sync.Mutex
customBotCategoryActionMutex sync.Mutex
akamaiBotCategoryActionMutex sync.Mutex
transactionalEndpointMutex sync.Mutex
akamaiBotCategoryMutex sync.Mutex
akamaiDefinedBotMutex sync.Mutex
botDetectionMutex sync.Mutex
)
// getBotDetectionAction reads from the cache if present, or makes a getAll call to fetch all Bot Detection Actions for a security policy, stores in the cache and filters the required Bot Detection Action using ID.
func getBotDetectionAction(ctx context.Context, request botman.GetBotDetectionActionRequest, m interface{}) (map[string]interface{}, error) {
meta := akameta.Must(m)
client := inst.Client(meta)
logger := meta.Log("BotMan", "getBotDetectionAction")
cacheKey := fmt.Sprintf("%s:%d:%d:%s", "getBotDetectionAction", request.ConfigID, request.Version, request.SecurityPolicyID)
botDetectionActions := &botman.GetBotDetectionActionListResponse{}
err := cache.Get(cache.BucketName(SubproviderName), cacheKey, botDetectionActions)
// if cache is disabled use GetBotDetectionAction to fetch one action at a time
if errors.Is(err, cache.ErrDisabled) {
return client.GetBotDetectionAction(ctx, request)
}
if err == nil {
return filterBotDetectionAction(botDetectionActions, request, logger)
}
botDetectionActionMutex.Lock()
defer func() {
logger.Debugf("Unlocking mutex")
botDetectionActionMutex.Unlock()
}()
err = cache.Get(cache.BucketName(SubproviderName), cacheKey, botDetectionActions)
if err == nil {
return filterBotDetectionAction(botDetectionActions, request, logger)
}
if !errors.Is(err, cache.ErrEntryNotFound) && !errors.Is(err, cache.ErrDisabled) {
logger.Errorf("error reading from cache: %s", err.Error())
return nil, err
}
botDetectionActions, err = client.GetBotDetectionActionList(ctx, botman.GetBotDetectionActionListRequest{
ConfigID: request.ConfigID,
Version: request.Version,
SecurityPolicyID: request.SecurityPolicyID,
})
if err != nil {
logger.Errorf("calling 'GetBotDetectionActionList': %s", err.Error())
return nil, err
}
err = cache.Set(cache.BucketName(SubproviderName), cacheKey, botDetectionActions)
if err != nil && !errors.Is(err, cache.ErrDisabled) {
logger.Errorf("error caching botDetectionActions into cache: %s", err.Error())
return nil, err
}
return filterBotDetectionAction(botDetectionActions, request, logger)
}
func filterBotDetectionAction(botDetectionActions *botman.GetBotDetectionActionListResponse, request botman.GetBotDetectionActionRequest, logger log.Interface) (map[string]interface{}, error) {
for _, action := range botDetectionActions.Actions {
if action["detectionId"].(string) == request.DetectionID {
logger.Debugf("Found bot detection action for config %d version %d security policy %s bot detection %s", request.ConfigID, request.Version, request.SecurityPolicyID, request.DetectionID)
return action, nil
}
}
return nil, fmt.Errorf("BotDetectionAction with id: %s does not exist", request.DetectionID)
}
// getCustomBotCategoryAction reads from the cache if present, or makes a getAll call to fetch all Custom Bot Category Actions for a security policy, stores in the cache and filters the required Custom Bot Category Action using ID.
func getCustomBotCategoryAction(ctx context.Context, request botman.GetCustomBotCategoryActionRequest, m interface{}) (map[string]interface{}, error) {
meta := akameta.Must(m)
client := inst.Client(meta)
logger := meta.Log("BotMan", "getCustomBotCategoryAction")
cacheKey := fmt.Sprintf("%s:%d:%d:%s", "getCustomBotCategoryAction", request.ConfigID, request.Version, request.SecurityPolicyID)
customBotCategoryActions := &botman.GetCustomBotCategoryActionListResponse{}
err := cache.Get(cache.BucketName(SubproviderName), cacheKey, customBotCategoryActions)
// if cache is disabled use GetCustomBotCategoryAction to fetch one action at a time
if errors.Is(err, cache.ErrDisabled) {
return client.GetCustomBotCategoryAction(ctx, request)
}
if err == nil {
return filterCustomBotCategoryAction(customBotCategoryActions, request, logger)
}
customBotCategoryActionMutex.Lock()
defer func() {
logger.Debugf("Unlocking mutex")
customBotCategoryActionMutex.Unlock()
}()
err = cache.Get(cache.BucketName(SubproviderName), cacheKey, customBotCategoryActions)
if err == nil {
return filterCustomBotCategoryAction(customBotCategoryActions, request, logger)
}
if !errors.Is(err, cache.ErrEntryNotFound) && !errors.Is(err, cache.ErrDisabled) {
logger.Errorf("error reading from cache: %s", err.Error())
return nil, err
}
customBotCategoryActions, err = client.GetCustomBotCategoryActionList(ctx, botman.GetCustomBotCategoryActionListRequest{
ConfigID: request.ConfigID,
Version: request.Version,
SecurityPolicyID: request.SecurityPolicyID,
})
if err != nil {
logger.Errorf("calling 'GetCustomBotCategoryActionList': %s", err.Error())
return nil, err
}
err = cache.Set(cache.BucketName(SubproviderName), cacheKey, customBotCategoryActions)
if err != nil && !errors.Is(err, cache.ErrDisabled) {
logger.Errorf("error caching customBotCategoryActions into cache: %s", err.Error())
return nil, err
}
return filterCustomBotCategoryAction(customBotCategoryActions, request, logger)
}
func filterCustomBotCategoryAction(customBotCategoryActions *botman.GetCustomBotCategoryActionListResponse, request botman.GetCustomBotCategoryActionRequest, logger log.Interface) (map[string]interface{}, error) {
for _, action := range customBotCategoryActions.Actions {
if action["categoryId"].(string) == request.CategoryID {
logger.Debugf("Found custom bot category action for config %d version %d security policy %s category %s", request.ConfigID, request.Version, request.SecurityPolicyID, request.CategoryID)
return action, nil
}
}
return nil, fmt.Errorf("CustomBotCategoryAction with id: %s does not exist", request.CategoryID)
}
// getAkamaiBotCategoryAction reads from the cache if present, or makes a getAll call to fetch all Akamai Bot Category Actions for a security policy, stores in the cache and filters the required Akamai Bot Category Action using ID.
func getAkamaiBotCategoryAction(ctx context.Context, request botman.GetAkamaiBotCategoryActionRequest, m interface{}) (map[string]interface{}, error) {
meta := akameta.Must(m)
client := inst.Client(meta)
logger := meta.Log("BotMan", "getAkamaiBotCategoryAction")
cacheKey := fmt.Sprintf("%s:%d:%d:%s", "getAkamaiBotCategoryAction", request.ConfigID, request.Version, request.SecurityPolicyID)
akamaiBotCategoryActions := &botman.GetAkamaiBotCategoryActionListResponse{}
err := cache.Get(cache.BucketName(SubproviderName), cacheKey, akamaiBotCategoryActions)
// if cache is disabled use GetAkamaiBotCategoryAction to fetch one action at a time
if errors.Is(err, cache.ErrDisabled) {
return client.GetAkamaiBotCategoryAction(ctx, request)
}
if err == nil {
return filterAkamaiBotCategoryAction(akamaiBotCategoryActions, request, logger)
}
akamaiBotCategoryActionMutex.Lock()
defer func() {
logger.Debugf("Unlocking mutex")
akamaiBotCategoryActionMutex.Unlock()
}()
err = cache.Get(cache.BucketName(SubproviderName), cacheKey, akamaiBotCategoryActions)
if err == nil {
return filterAkamaiBotCategoryAction(akamaiBotCategoryActions, request, logger)
}
if !errors.Is(err, cache.ErrEntryNotFound) && !errors.Is(err, cache.ErrDisabled) {
logger.Errorf("error reading from cache: %s", err.Error())
return nil, err
}
akamaiBotCategoryActions, err = client.GetAkamaiBotCategoryActionList(ctx, botman.GetAkamaiBotCategoryActionListRequest{
ConfigID: request.ConfigID,
Version: request.Version,
SecurityPolicyID: request.SecurityPolicyID,
})
if err != nil {
logger.Errorf("calling 'GetAkamaiBotCategoryActionList': %s", err.Error())
return nil, err
}
err = cache.Set(cache.BucketName(SubproviderName), cacheKey, akamaiBotCategoryActions)
if err != nil && !errors.Is(err, cache.ErrDisabled) {
logger.Errorf("error caching akamaiBotCategoryActions into cache: %s", err.Error())
return nil, err
}
return filterAkamaiBotCategoryAction(akamaiBotCategoryActions, request, logger)
}
func filterAkamaiBotCategoryAction(akamaiBotCategoryActions *botman.GetAkamaiBotCategoryActionListResponse, request botman.GetAkamaiBotCategoryActionRequest, logger log.Interface) (map[string]interface{}, error) {
for _, action := range akamaiBotCategoryActions.Actions {
if action["categoryId"].(string) == request.CategoryID {
logger.Debugf("Found akamai bot category action for config %d version %d security policy %s category %s", request.ConfigID, request.Version, request.SecurityPolicyID, request.CategoryID)
return action, nil
}
}
return nil, fmt.Errorf("AkamaiBotCategoryAction with id: %s does not exist", request.CategoryID)
}
// getTransactionalEndpoint reads from the cache if present, or makes a getAll call to fetch all Transactional Endpoints for a security policy, stores in the cache and filters the required Transactional Endpoint using ID.
func getTransactionalEndpoint(ctx context.Context, request botman.GetTransactionalEndpointRequest, m interface{}) (map[string]interface{}, error) {
meta := akameta.Must(m)
client := inst.Client(meta)
logger := meta.Log("BotMan", "getTransactionalEndpoint")
cacheKey := fmt.Sprintf("%s:%d:%d:%s", "getTransactionalEndpoint", request.ConfigID, request.Version, request.SecurityPolicyID)
transactionalEndpoints := &botman.GetTransactionalEndpointListResponse{}
err := cache.Get(cache.BucketName(SubproviderName), cacheKey, transactionalEndpoints)
// if cache is disabled use GetTransactionalEndpoint to fetch one action at a time
if errors.Is(err, cache.ErrDisabled) {
return client.GetTransactionalEndpoint(ctx, request)
}
if err == nil {
return filterTransactionalEndpoint(transactionalEndpoints, request, logger)
}
transactionalEndpointMutex.Lock()
defer func() {
logger.Debugf("Unlocking mutex")
transactionalEndpointMutex.Unlock()
}()
err = cache.Get(cache.BucketName(SubproviderName), cacheKey, transactionalEndpoints)
if err == nil {
return filterTransactionalEndpoint(transactionalEndpoints, request, logger)
}
if !errors.Is(err, cache.ErrEntryNotFound) && !errors.Is(err, cache.ErrDisabled) {
logger.Errorf("error reading from cache: %s", err.Error())
return nil, err
}
transactionalEndpoints, err = client.GetTransactionalEndpointList(ctx, botman.GetTransactionalEndpointListRequest{
ConfigID: request.ConfigID,
Version: request.Version,
SecurityPolicyID: request.SecurityPolicyID,
})
if err != nil {
logger.Errorf("calling 'GetTransactionalEndpointList': %s", err.Error())
return nil, err
}
err = cache.Set(cache.BucketName(SubproviderName), cacheKey, transactionalEndpoints)
if err != nil && !errors.Is(err, cache.ErrDisabled) {
logger.Errorf("error caching transactionalEndpoints into cache: %s", err.Error())
return nil, err
}
return filterTransactionalEndpoint(transactionalEndpoints, request, logger)
}
func filterTransactionalEndpoint(transactionalEndpoints *botman.GetTransactionalEndpointListResponse, request botman.GetTransactionalEndpointRequest, logger log.Interface) (map[string]interface{}, error) {
for _, action := range transactionalEndpoints.Operations {
if action["operationId"].(string) == request.OperationID {
logger.Debugf("Found transactional endpoint for config %d version %d security policy %s operation %s", request.ConfigID, request.Version, request.SecurityPolicyID, request.OperationID)
return action, nil
}
}
return nil, fmt.Errorf("TransactionalEndpoint with id: %s does not exist", request.OperationID)
}
func getAkamaiBotCategoryList(ctx context.Context, request botman.GetAkamaiBotCategoryListRequest, m interface{}) (*botman.GetAkamaiBotCategoryListResponse, error) {
meta := akameta.Must(m)
client := inst.Client(meta)
logger := meta.Log("BotMan", "getAkamaiBotCategory")
cacheKey := fmt.Sprintf("%s", "getAkamaiBotCategory")
akamaiBotCategoryList := &botman.GetAkamaiBotCategoryListResponse{}
err := cache.Get(cache.BucketName(SubproviderName), cacheKey, akamaiBotCategoryList)
// if cache is disabled make a direct all to GetAkamaiBotCategoryList
if errors.Is(err, cache.ErrDisabled) {
return client.GetAkamaiBotCategoryList(ctx, request)
}
if err == nil {
return filterAkamaiBotCategoryList(akamaiBotCategoryList, request), nil
}
akamaiBotCategoryMutex.Lock()
defer func() {
logger.Debugf("Unlocking mutex")
akamaiBotCategoryMutex.Unlock()
}()
err = cache.Get(cache.BucketName(SubproviderName), cacheKey, akamaiBotCategoryList)
if err == nil {
return filterAkamaiBotCategoryList(akamaiBotCategoryList, request), nil
}
if !errors.Is(err, cache.ErrEntryNotFound) && !errors.Is(err, cache.ErrDisabled) {
logger.Errorf("error reading from cache: %s", err.Error())
return nil, err
}
// fetch all akamaiBotCategoryList to store in cache and then filter based on request
akamaiBotCategoryList, err = client.GetAkamaiBotCategoryList(ctx, botman.GetAkamaiBotCategoryListRequest{})
if err != nil {
logger.Errorf("calling 'GetAkamaiBotCategoryList': %s", err.Error())
return nil, err
}
err = cache.Set(cache.BucketName(SubproviderName), cacheKey, akamaiBotCategoryList)
if err != nil && !errors.Is(err, cache.ErrDisabled) {
logger.Errorf("error caching akamaiBotCategoryList into cache: %s", err.Error())
return nil, err
}
return filterAkamaiBotCategoryList(akamaiBotCategoryList, request), nil
}
func filterAkamaiBotCategoryList(akamaiBotCategoryList *botman.GetAkamaiBotCategoryListResponse, request botman.GetAkamaiBotCategoryListRequest) *botman.GetAkamaiBotCategoryListResponse {
if request.CategoryName == "" {
return akamaiBotCategoryList
}
var filteredResult botman.GetAkamaiBotCategoryListResponse
for _, category := range akamaiBotCategoryList.Categories {
if category["categoryName"].(string) == request.CategoryName {
filteredResult.Categories = append(filteredResult.Categories, category)
return &filteredResult
}
}
return &filteredResult
}
func getAkamaiDefinedBotList(ctx context.Context, request botman.GetAkamaiDefinedBotListRequest, m interface{}) (*botman.GetAkamaiDefinedBotListResponse, error) {
meta := akameta.Must(m)
client := inst.Client(meta)
logger := meta.Log("BotMan", "getAkamaiDefinedBot")
cacheKey := fmt.Sprintf("%s", "getAkamaiDefinedBot")
akamaiDefinedBotList := &botman.GetAkamaiDefinedBotListResponse{}
err := cache.Get(cache.BucketName(SubproviderName), cacheKey, akamaiDefinedBotList)
// if cache is disabled make a direct all to GetAkamaiDefinedBotList
if errors.Is(err, cache.ErrDisabled) {
return client.GetAkamaiDefinedBotList(ctx, request)
}
if err == nil {
return filterAkamaiDefinedBotList(akamaiDefinedBotList, request), nil
}
akamaiDefinedBotMutex.Lock()
defer func() {
logger.Debugf("Unlocking mutex")
akamaiDefinedBotMutex.Unlock()
}()
err = cache.Get(cache.BucketName(SubproviderName), cacheKey, akamaiDefinedBotList)
if err == nil {
return filterAkamaiDefinedBotList(akamaiDefinedBotList, request), nil
}
if !errors.Is(err, cache.ErrEntryNotFound) && !errors.Is(err, cache.ErrDisabled) {
logger.Errorf("error reading from cache: %s", err.Error())
return nil, err
}
// fetch all akamaiDefinedBotList to store in cache and then filter based on request
akamaiDefinedBotList, err = client.GetAkamaiDefinedBotList(ctx, botman.GetAkamaiDefinedBotListRequest{})
if err != nil {
logger.Errorf("calling 'GetAkamaiDefinedBotList': %s", err.Error())
return nil, err
}
err = cache.Set(cache.BucketName(SubproviderName), cacheKey, akamaiDefinedBotList)
if err != nil && !errors.Is(err, cache.ErrDisabled) {
logger.Errorf("error caching akamaiDefinedBotList into cache: %s", err.Error())
return nil, err
}
return filterAkamaiDefinedBotList(akamaiDefinedBotList, request), nil
}
func filterAkamaiDefinedBotList(akamaiDefinedBotList *botman.GetAkamaiDefinedBotListResponse, request botman.GetAkamaiDefinedBotListRequest) *botman.GetAkamaiDefinedBotListResponse {
if request.BotName == "" {
return akamaiDefinedBotList
}
var filteredResult botman.GetAkamaiDefinedBotListResponse
for _, bot := range akamaiDefinedBotList.Bots {
if bot["botName"].(string) == request.BotName {
filteredResult.Bots = append(filteredResult.Bots, bot)
return &filteredResult
}
}
return &filteredResult
}
func getBotDetectionList(ctx context.Context, request botman.GetBotDetectionListRequest, m interface{}) (*botman.GetBotDetectionListResponse, error) {
meta := akameta.Must(m)
client := inst.Client(meta)
logger := meta.Log("BotMan", "getBotDetection")
cacheKey := fmt.Sprintf("%s", "getBotDetection")
botDetectionList := &botman.GetBotDetectionListResponse{}
err := cache.Get(cache.BucketName(SubproviderName), cacheKey, botDetectionList)
// if cache is disabled make a direct all to GetBotDetectionList
if errors.Is(err, cache.ErrDisabled) {
return client.GetBotDetectionList(ctx, request)
}
if err == nil {
return filterBotDetectionList(botDetectionList, request), nil
}
botDetectionMutex.Lock()
defer func() {
logger.Debugf("Unlocking mutex")
botDetectionMutex.Unlock()
}()
err = cache.Get(cache.BucketName(SubproviderName), cacheKey, botDetectionList)
if err == nil {
return filterBotDetectionList(botDetectionList, request), nil
}
if !errors.Is(err, cache.ErrEntryNotFound) && !errors.Is(err, cache.ErrDisabled) {
logger.Errorf("error reading from cache: %s", err.Error())
return nil, err
}
// fetch all botDetectionList to store in cache and then filter based on request
botDetectionList, err = client.GetBotDetectionList(ctx, botman.GetBotDetectionListRequest{})
if err != nil {
logger.Errorf("calling 'GetBotDetectionList': %s", err.Error())
return nil, err
}
err = cache.Set(cache.BucketName(SubproviderName), cacheKey, botDetectionList)
if err != nil && !errors.Is(err, cache.ErrDisabled) {
logger.Errorf("error caching botDetectionList into cache: %s", err.Error())
return nil, err
}
return filterBotDetectionList(botDetectionList, request), nil
}
func filterBotDetectionList(botDetectionList *botman.GetBotDetectionListResponse, request botman.GetBotDetectionListRequest) *botman.GetBotDetectionListResponse {
if request.DetectionName == "" {
return botDetectionList
}
var filteredResult botman.GetBotDetectionListResponse
for _, detection := range botDetectionList.Detections {
if detection["detectionName"].(string) == request.DetectionName {
filteredResult.Detections = append(filteredResult.Detections, detection)
return &filteredResult
}
}
return &filteredResult
}