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

     1  package appsec
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	validation "github.com/go-ozzo/ozzo-validation/v4"
     9  )
    10  
    11  type (
    12  	// The CustomRuleAction interface supports retrieving and updating the actions for the custom
    13  	// rules of a configuration, or for a specific custom rule.
    14  	CustomRuleAction interface {
    15  		// GetCustomRuleActions returns a list of all configured custom rules for the specified configuration.
    16  		//
    17  		// See: https://techdocs.akamai.com/application-security/reference/get-custom-rules
    18  		GetCustomRuleActions(ctx context.Context, params GetCustomRuleActionsRequest) (*GetCustomRuleActionsResponse, error)
    19  
    20  		// GetCustomRuleAction returns a specified action of a custom rule.
    21  		//
    22  		// See: https://techdocs.akamai.com/application-security/reference/get-custom-rules
    23  		GetCustomRuleAction(ctx context.Context, params GetCustomRuleActionRequest) (*GetCustomRuleActionResponse, error)
    24  
    25  		// UpdateCustomRuleAction updates the action of a custom rule.
    26  		//
    27  		// See: https://techdocs.akamai.com/application-security/reference/put-custom-rule
    28  		UpdateCustomRuleAction(ctx context.Context, params UpdateCustomRuleActionRequest) (*UpdateCustomRuleActionResponse, error)
    29  	}
    30  
    31  	// GetCustomRuleActionsRequest is used to retrieve the custom rule actions for a configuration.
    32  	GetCustomRuleActionsRequest struct {
    33  		ConfigID int    `json:"configId"`
    34  		Version  int    `json:"version"`
    35  		PolicyID string `json:"policyId"`
    36  		RuleID   int    `json:"ruleId"`
    37  	}
    38  
    39  	// GetCustomRuleActionsResponse is returned from a call to GetCustomRuleActions.
    40  	GetCustomRuleActionsResponse []struct {
    41  		Action                string `json:"action,omitempty"`
    42  		CanUseAdvancedActions bool   `json:"canUseAdvancedActions,omitempty"`
    43  		Link                  string `json:"link,omitempty"`
    44  		Name                  string `json:"name,omitempty"`
    45  		RuleID                int    `json:"ruleId,omitempty"`
    46  	}
    47  
    48  	// GetCustomRuleActionRequest is used to retrieve the action for a custom rule.
    49  	GetCustomRuleActionRequest struct {
    50  		ConfigID int    `json:"configId"`
    51  		Version  int    `json:"version"`
    52  		PolicyID string `json:"policyId"`
    53  		RuleID   int    `json:"ruleId"`
    54  	}
    55  
    56  	// GetCustomRuleActionResponse is returned from a call to GetCustomRuleAction.
    57  	GetCustomRuleActionResponse struct {
    58  		Action                string `json:"action,omitempty"`
    59  		CanUseAdvancedActions bool   `json:"canUseAdvancedActions,omitempty"`
    60  		Link                  string `json:"link,omitempty"`
    61  		Name                  string `json:"name,omitempty"`
    62  		RuleID                int    `json:"ruleId,omitempty"`
    63  	}
    64  
    65  	// UpdateCustomRuleActionRequest is used to modify an existing custom rule.
    66  	UpdateCustomRuleActionRequest struct {
    67  		ConfigID int    `json:"-"`
    68  		Version  int    `json:"-"`
    69  		PolicyID string `json:"-"`
    70  		RuleID   int    `json:"-"`
    71  		Action   string `json:"action"`
    72  	}
    73  
    74  	// UpdateCustomRuleActionResponse is returned from a call to UpdateCustomRuleAction.
    75  	UpdateCustomRuleActionResponse struct {
    76  		Action                string `json:"action"`
    77  		CanUseAdvancedActions bool   `json:"canUseAdvancedActions"`
    78  		Link                  string `json:"link"`
    79  		Name                  string `json:"name"`
    80  		RuleID                int    `json:"ruleId"`
    81  	}
    82  )
    83  
    84  // Validate validates a GetCustomRuleActionRequest.
    85  func (v GetCustomRuleActionRequest) Validate() error {
    86  	return validation.Errors{
    87  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    88  		"Version":  validation.Validate(v.Version, validation.Required),
    89  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
    90  	}.Filter()
    91  }
    92  
    93  // Validate validates a GetCustomRuleActionsRequest.
    94  func (v GetCustomRuleActionsRequest) Validate() error {
    95  	return validation.Errors{
    96  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    97  		"Version":  validation.Validate(v.Version, validation.Required),
    98  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
    99  	}.Filter()
   100  }
   101  
   102  // Validate validates an UpdateCustomRuleActionRequest.
   103  func (v UpdateCustomRuleActionRequest) Validate() error {
   104  	return validation.Errors{
   105  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   106  		"Version":  validation.Validate(v.Version, validation.Required),
   107  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   108  		"ID":       validation.Validate(v.RuleID, validation.Required),
   109  	}.Filter()
   110  }
   111  
   112  func (p *appsec) GetCustomRuleAction(ctx context.Context, params GetCustomRuleActionRequest) (*GetCustomRuleActionResponse, error) {
   113  	logger := p.Log(ctx)
   114  	logger.Debug("GetCustomRuleAction")
   115  
   116  	if err := params.Validate(); err != nil {
   117  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   118  	}
   119  
   120  	var result GetCustomRuleActionResponse
   121  
   122  	uri := fmt.Sprintf(
   123  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/custom-rules",
   124  		params.ConfigID,
   125  		params.Version,
   126  		params.PolicyID)
   127  
   128  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   129  	if err != nil {
   130  		return nil, fmt.Errorf("failed to create GetCustomRuleAction request: %w", err)
   131  	}
   132  
   133  	var results GetCustomRuleActionsResponse
   134  
   135  	resp, err := p.Exec(req, &results)
   136  	if err != nil {
   137  		return nil, fmt.Errorf("get custom rule action request failed: %w", err)
   138  	}
   139  
   140  	if resp.StatusCode != http.StatusOK {
   141  		return nil, p.Error(resp)
   142  	}
   143  
   144  	for _, val := range results {
   145  		if val.RuleID == params.RuleID {
   146  			result = val
   147  			return &result, nil
   148  		}
   149  	}
   150  
   151  	return &result, nil
   152  }
   153  
   154  func (p *appsec) GetCustomRuleActions(ctx context.Context, params GetCustomRuleActionsRequest) (*GetCustomRuleActionsResponse, error) {
   155  	logger := p.Log(ctx)
   156  	logger.Debug("GetCustomRuleActions")
   157  
   158  	if err := params.Validate(); err != nil {
   159  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   160  	}
   161  
   162  	uri := fmt.Sprintf(
   163  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/custom-rules",
   164  		params.ConfigID,
   165  		params.Version,
   166  		params.PolicyID)
   167  
   168  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   169  	if err != nil {
   170  		return nil, fmt.Errorf("failed to create GetCustomRuleActions request: %w", err)
   171  	}
   172  
   173  	var result GetCustomRuleActionsResponse
   174  	resp, err := p.Exec(req, &result)
   175  	if err != nil {
   176  		return nil, fmt.Errorf("get custom rule actions request failed: %w", err)
   177  	}
   178  	if resp.StatusCode != http.StatusOK {
   179  		return nil, p.Error(resp)
   180  	}
   181  
   182  	if params.RuleID != 0 {
   183  		var filteredResult GetCustomRuleActionsResponse
   184  		for _, val := range result {
   185  			if val.RuleID == params.RuleID {
   186  				filteredResult = append(filteredResult, val)
   187  			}
   188  		}
   189  		return &filteredResult, nil
   190  	}
   191  
   192  	return &result, nil
   193  }
   194  
   195  func (p *appsec) UpdateCustomRuleAction(ctx context.Context, params UpdateCustomRuleActionRequest) (*UpdateCustomRuleActionResponse, error) {
   196  	logger := p.Log(ctx)
   197  	logger.Debug("UpdateCustomRuleAction")
   198  
   199  	if err := params.Validate(); err != nil {
   200  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   201  	}
   202  
   203  	uri := fmt.Sprintf(
   204  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/custom-rules/%d",
   205  		params.ConfigID,
   206  		params.Version,
   207  		params.PolicyID,
   208  		params.RuleID,
   209  	)
   210  
   211  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   212  	if err != nil {
   213  		return nil, fmt.Errorf("failed to create UpdateCustomRuleAction request: %w", err)
   214  	}
   215  
   216  	var result UpdateCustomRuleActionResponse
   217  	resp, err := p.Exec(req, &result, params)
   218  	if err != nil {
   219  		return nil, fmt.Errorf("update custom rule action request failed: %w", err)
   220  	}
   221  
   222  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusNoContent {
   223  		return nil, p.Error(resp)
   224  	}
   225  
   226  	return &result, nil
   227  }