github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/botman/bot_detection_action.go (about)

     1  package botman
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/http"
     8  
     9  	validation "github.com/go-ozzo/ozzo-validation/v4"
    10  )
    11  
    12  type (
    13  	// The BotDetectionAction interface supports retrieving and updating the actions for bot detections of a configuration.
    14  	//
    15  	BotDetectionAction interface {
    16  		// GetBotDetectionActionList todo: add link
    17  		GetBotDetectionActionList(ctx context.Context, params GetBotDetectionActionListRequest) (*GetBotDetectionActionListResponse, error)
    18  		// GetBotDetectionAction todo: add link
    19  		GetBotDetectionAction(ctx context.Context, params GetBotDetectionActionRequest) (map[string]interface{}, error)
    20  		// UpdateBotDetectionAction todo: add link
    21  		UpdateBotDetectionAction(ctx context.Context, params UpdateBotDetectionActionRequest) (map[string]interface{}, error)
    22  	}
    23  
    24  	// GetBotDetectionActionListRequest is used to retrieve the bot detection actions for a configuration.
    25  	GetBotDetectionActionListRequest struct {
    26  		ConfigID         int64
    27  		Version          int64
    28  		SecurityPolicyID string
    29  		DetectionID      string
    30  	}
    31  
    32  	// GetBotDetectionActionListResponse is used to retrieve the bot detection actions for a configuration.
    33  	GetBotDetectionActionListResponse struct {
    34  		Actions []map[string]interface{} `json:"actions"`
    35  	}
    36  
    37  	// GetBotDetectionActionRequest is used to retrieve the action for a bot detection.
    38  	GetBotDetectionActionRequest struct {
    39  		ConfigID         int64
    40  		Version          int64
    41  		SecurityPolicyID string
    42  		DetectionID      string
    43  	}
    44  
    45  	// UpdateBotDetectionActionRequest is used to modify a bot detection action.
    46  	UpdateBotDetectionActionRequest struct {
    47  		ConfigID         int64
    48  		Version          int64
    49  		SecurityPolicyID string
    50  		DetectionID      string
    51  		JsonPayload      json.RawMessage
    52  	}
    53  )
    54  
    55  // Validate validates a GetBotDetectionActionRequest.
    56  func (v GetBotDetectionActionRequest) Validate() error {
    57  	return validation.Errors{
    58  		"ConfigID":         validation.Validate(v.ConfigID, validation.Required),
    59  		"Version":          validation.Validate(v.Version, validation.Required),
    60  		"SecurityPolicyID": validation.Validate(v.SecurityPolicyID, validation.Required),
    61  		"DetectionID":      validation.Validate(v.DetectionID, validation.Required),
    62  	}.Filter()
    63  }
    64  
    65  // Validate validates a GetBotDetectionActionListRequest.
    66  func (v GetBotDetectionActionListRequest) Validate() error {
    67  	return validation.Errors{
    68  		"ConfigID":         validation.Validate(v.ConfigID, validation.Required),
    69  		"Version":          validation.Validate(v.Version, validation.Required),
    70  		"SecurityPolicyID": validation.Validate(v.SecurityPolicyID, validation.Required),
    71  	}.Filter()
    72  }
    73  
    74  // Validate validates an UpdateBotDetectionActionRequest.
    75  func (v UpdateBotDetectionActionRequest) Validate() error {
    76  	return validation.Errors{
    77  		"ConfigID":         validation.Validate(v.ConfigID, validation.Required),
    78  		"Version":          validation.Validate(v.Version, validation.Required),
    79  		"SecurityPolicyID": validation.Validate(v.SecurityPolicyID, validation.Required),
    80  		"DetectionID":      validation.Validate(v.DetectionID, validation.Required),
    81  		"JsonPayload":      validation.Validate(v.JsonPayload, validation.Required),
    82  	}.Filter()
    83  }
    84  
    85  func (b *botman) GetBotDetectionAction(ctx context.Context, params GetBotDetectionActionRequest) (map[string]interface{}, error) {
    86  	logger := b.Log(ctx)
    87  	logger.Debug("GetBotDetectionAction")
    88  
    89  	if err := params.Validate(); err != nil {
    90  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
    91  	}
    92  
    93  	uri := fmt.Sprintf(
    94  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/bot-detection-actions/%s",
    95  		params.ConfigID,
    96  		params.Version,
    97  		params.SecurityPolicyID,
    98  		params.DetectionID)
    99  
   100  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   101  	if err != nil {
   102  		return nil, fmt.Errorf("failed to create GetBotDetectionAction request: %w", err)
   103  	}
   104  
   105  	var result map[string]interface{}
   106  	resp, err := b.Exec(req, &result)
   107  	if err != nil {
   108  		return nil, fmt.Errorf("GetBotDetectionAction request failed: %w", err)
   109  	}
   110  
   111  	if resp.StatusCode != http.StatusOK {
   112  		return nil, b.Error(resp)
   113  	}
   114  
   115  	return result, nil
   116  }
   117  
   118  func (b *botman) GetBotDetectionActionList(ctx context.Context, params GetBotDetectionActionListRequest) (*GetBotDetectionActionListResponse, error) {
   119  	logger := b.Log(ctx)
   120  	logger.Debug("GetBotDetectionActionList")
   121  
   122  	if err := params.Validate(); err != nil {
   123  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   124  	}
   125  
   126  	uri := fmt.Sprintf(
   127  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/bot-detection-actions",
   128  		params.ConfigID,
   129  		params.Version,
   130  		params.SecurityPolicyID)
   131  
   132  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   133  	if err != nil {
   134  		return nil, fmt.Errorf("failed to create GetBotDetectionActionList request: %w", err)
   135  	}
   136  
   137  	var result GetBotDetectionActionListResponse
   138  	resp, err := b.Exec(req, &result)
   139  	if err != nil {
   140  		return nil, fmt.Errorf("GetBotDetectionActionList request failed: %w", err)
   141  	}
   142  
   143  	if resp.StatusCode != http.StatusOK {
   144  		return nil, b.Error(resp)
   145  	}
   146  
   147  	var filteredResult GetBotDetectionActionListResponse
   148  	if params.DetectionID != "" {
   149  		for _, val := range result.Actions {
   150  			if val["detectionId"].(string) == params.DetectionID {
   151  				filteredResult.Actions = append(filteredResult.Actions, val)
   152  			}
   153  		}
   154  	} else {
   155  		filteredResult = result
   156  	}
   157  	return &filteredResult, nil
   158  }
   159  
   160  func (b *botman) UpdateBotDetectionAction(ctx context.Context, params UpdateBotDetectionActionRequest) (map[string]interface{}, error) {
   161  	logger := b.Log(ctx)
   162  	logger.Debug("UpdateBotDetectionAction")
   163  
   164  	if err := params.Validate(); err != nil {
   165  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   166  	}
   167  
   168  	putURL := fmt.Sprintf(
   169  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/bot-detection-actions/%s",
   170  		params.ConfigID,
   171  		params.Version,
   172  		params.SecurityPolicyID,
   173  		params.DetectionID,
   174  	)
   175  
   176  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, putURL, nil)
   177  	if err != nil {
   178  		return nil, fmt.Errorf("failed to create UpdateBotDetectionAction request: %w", err)
   179  	}
   180  
   181  	var result map[string]interface{}
   182  	resp, err := b.Exec(req, &result, params.JsonPayload)
   183  	if err != nil {
   184  		return nil, fmt.Errorf("UpdateBotDetectionAction request failed: %w", err)
   185  	}
   186  
   187  	if resp.StatusCode != http.StatusOK {
   188  		return nil, b.Error(resp)
   189  	}
   190  
   191  	return result, nil
   192  }