github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/eval.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 Eval interface supports retrieving and updating the way evaluation rules would respond if
    13  	// they were applied to live traffic.
    14  	Eval interface {
    15  		// GetEvals returns which modes your rules are currently set to.
    16  		//
    17  		// See: https://techdocs.akamai.com/application-security/reference/get-policy-mode-1
    18  		GetEvals(ctx context.Context, params GetEvalsRequest) (*GetEvalsResponse, error)
    19  
    20  		// GetEval returns which mode your rules are currently set to.
    21  		//
    22  		// See: https://techdocs.akamai.com/application-security/reference/get-policy-mode-1
    23  		GetEval(ctx context.Context, params GetEvalRequest) (*GetEvalResponse, error)
    24  
    25  		// UpdateEval updated the rule evaluation mode.
    26  		//
    27  		// See: https://techdocs.akamai.com/application-security/reference/post-policy-eval
    28  		UpdateEval(ctx context.Context, params UpdateEvalRequest) (*UpdateEvalResponse, error)
    29  
    30  		// RemoveEval removes the rule evaluation mode.
    31  		//
    32  		// See: https://techdocs.akamai.com/application-security/reference/post-policy-eval
    33  		RemoveEval(ctx context.Context, params RemoveEvalRequest) (*RemoveEvalResponse, error)
    34  	}
    35  
    36  	// GetEvalsRequest is used to retrieve the mode setting that conveys how rules will be kept up to date.
    37  	// Deprecated: this struct will be removed in a future release.
    38  	GetEvalsRequest struct {
    39  		ConfigID int    `json:"-"`
    40  		Version  int    `json:"-"`
    41  		PolicyID string `json:"-"`
    42  		Current  string `json:"current"`
    43  		Mode     string `json:"mode"`
    44  		Eval     string `json:"eval"`
    45  	}
    46  
    47  	// GetEvalsResponse is returned from a call to GetEvalsResponse.
    48  	// Deprecated: this struct will be removed in a future release.
    49  	GetEvalsResponse struct {
    50  		Current    string `json:"current,omitempty"`
    51  		Mode       string `json:"mode,omitempty"`
    52  		Eval       string `json:"eval,omitempty"`
    53  		Evaluating string `json:"evaluating,omitempty"`
    54  		Expires    string `json:"expires,omitempty"`
    55  	}
    56  
    57  	// GetEvalRequest is used to retrieve the mode setting that conveys how rules will be kept up to date.
    58  	GetEvalRequest struct {
    59  		ConfigID int    `json:"-"`
    60  		Version  int    `json:"-"`
    61  		PolicyID string `json:"-"`
    62  		Current  string `json:"current"`
    63  		Mode     string `json:"mode"`
    64  		Eval     string `json:"eval"`
    65  	}
    66  
    67  	// GetEvalResponse is returned from a call to GetEvalResponse.
    68  	GetEvalResponse struct {
    69  		Current    string `json:"current,omitempty"`
    70  		Mode       string `json:"mode,omitempty"`
    71  		Eval       string `json:"eval,omitempty"`
    72  		Evaluating string `json:"evaluating,omitempty"`
    73  		Expires    string `json:"expires,omitempty"`
    74  	}
    75  
    76  	// RemoveEvalRequest is used to remove an evaluation mode setting.
    77  	RemoveEvalRequest struct {
    78  		ConfigID int    `json:"-"`
    79  		Version  int    `json:"-"`
    80  		PolicyID string `json:"-"`
    81  		Current  string `json:"-"`
    82  		Mode     string `json:"-"`
    83  		Eval     string `json:"eval"`
    84  	}
    85  
    86  	// RemoveEvalResponse is returned from a call to RemoveEval.
    87  	RemoveEvalResponse struct {
    88  		Current string `json:"current"`
    89  		Eval    string `json:"eval"`
    90  		Mode    string `json:"mode"`
    91  	}
    92  
    93  	// UpdateEvalRequest is used to modify an evaluation mode setting.
    94  	UpdateEvalRequest struct {
    95  		ConfigID int    `json:"-"`
    96  		Version  int    `json:"-"`
    97  		PolicyID string `json:"-"`
    98  		Current  string `json:"-"`
    99  		Mode     string `json:"-"`
   100  		Eval     string `json:"eval"`
   101  	}
   102  
   103  	// UpdateEvalResponse is returned from a call to UpdateEval.
   104  	UpdateEvalResponse struct {
   105  		Current string `json:"current"`
   106  		Eval    string `json:"eval"`
   107  		Mode    string `json:"mode"`
   108  	}
   109  )
   110  
   111  // Validate validates a GetEvalRequest.
   112  func (v GetEvalRequest) Validate() error {
   113  	return validation.Errors{
   114  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   115  		"Version":  validation.Validate(v.Version, validation.Required),
   116  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   117  	}.Filter()
   118  }
   119  
   120  // Validate validates a GetEvalsRequest.
   121  // Deprecated: this method will be removed in a future release.
   122  func (v GetEvalsRequest) Validate() error {
   123  	return validation.Errors{
   124  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   125  		"Version":  validation.Validate(v.Version, validation.Required),
   126  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   127  	}.Filter()
   128  }
   129  
   130  // Validate validates an UpdateEvalRequest.
   131  func (v UpdateEvalRequest) Validate() error {
   132  	return validation.Errors{
   133  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   134  		"Version":  validation.Validate(v.Version, validation.Required),
   135  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   136  	}.Filter()
   137  }
   138  
   139  // Validate validates a RemoveEvalRequest.
   140  func (v RemoveEvalRequest) Validate() error {
   141  	return validation.Errors{
   142  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   143  		"Version":  validation.Validate(v.Version, validation.Required),
   144  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   145  	}.Filter()
   146  }
   147  
   148  func (p *appsec) GetEval(ctx context.Context, params GetEvalRequest) (*GetEvalResponse, error) {
   149  	logger := p.Log(ctx)
   150  	logger.Debug("GetEval")
   151  
   152  	if err := params.Validate(); err != nil {
   153  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   154  	}
   155  
   156  	uri := fmt.Sprintf(
   157  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/mode",
   158  		params.ConfigID,
   159  		params.Version,
   160  		params.PolicyID)
   161  
   162  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   163  	if err != nil {
   164  		return nil, fmt.Errorf("failed to create GetEval request: %w", err)
   165  	}
   166  
   167  	var result GetEvalResponse
   168  	resp, err := p.Exec(req, &result)
   169  	if err != nil {
   170  		return nil, fmt.Errorf("get eval request failed: %w", err)
   171  	}
   172  	if resp.StatusCode != http.StatusOK {
   173  		return nil, p.Error(resp)
   174  	}
   175  
   176  	return &result, nil
   177  }
   178  
   179  // Deprecated: this method will be removed in a future release.
   180  func (p *appsec) GetEvals(ctx context.Context, params GetEvalsRequest) (*GetEvalsResponse, error) {
   181  	logger := p.Log(ctx)
   182  	logger.Debug("GetEvals")
   183  
   184  	if err := params.Validate(); err != nil {
   185  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   186  	}
   187  
   188  	uri := fmt.Sprintf(
   189  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/mode",
   190  		params.ConfigID,
   191  		params.Version,
   192  		params.PolicyID)
   193  
   194  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   195  	if err != nil {
   196  		return nil, fmt.Errorf("failed to create GetEvals request: %w", err)
   197  	}
   198  
   199  	var result GetEvalsResponse
   200  	resp, err := p.Exec(req, &result)
   201  	if err != nil {
   202  		return nil, fmt.Errorf("get evals request failed: %w", err)
   203  	}
   204  	if resp.StatusCode != http.StatusOK {
   205  		return nil, p.Error(resp)
   206  	}
   207  
   208  	return &result, nil
   209  }
   210  
   211  func (p *appsec) UpdateEval(ctx context.Context, params UpdateEvalRequest) (*UpdateEvalResponse, error) {
   212  	logger := p.Log(ctx)
   213  	logger.Debug("UpdateEval")
   214  
   215  	if err := params.Validate(); err != nil {
   216  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   217  	}
   218  
   219  	uri := fmt.Sprintf(
   220  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/eval",
   221  		params.ConfigID,
   222  		params.Version,
   223  		params.PolicyID,
   224  	)
   225  
   226  	req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, nil)
   227  	if err != nil {
   228  		return nil, fmt.Errorf("failed to create UpdateEval request: %w", err)
   229  	}
   230  
   231  	var result UpdateEvalResponse
   232  	resp, err := p.Exec(req, &result, params)
   233  	if err != nil {
   234  		return nil, fmt.Errorf("update eval request failed: %w", err)
   235  	}
   236  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   237  		return nil, p.Error(resp)
   238  	}
   239  
   240  	return &result, nil
   241  }
   242  
   243  func (p *appsec) RemoveEval(ctx context.Context, params RemoveEvalRequest) (*RemoveEvalResponse, error) {
   244  	logger := p.Log(ctx)
   245  	logger.Debug("RemoveEval")
   246  
   247  	if err := params.Validate(); err != nil {
   248  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   249  	}
   250  
   251  	uri := fmt.Sprintf(
   252  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/eval",
   253  		params.ConfigID,
   254  		params.Version,
   255  		params.PolicyID,
   256  	)
   257  
   258  	req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, nil)
   259  	if err != nil {
   260  		return nil, fmt.Errorf("failed to create RemoveEval request: %w", err)
   261  	}
   262  
   263  	var result RemoveEvalResponse
   264  	resp, err := p.Exec(req, &result, params)
   265  	if err != nil {
   266  		return nil, fmt.Errorf("remove eval request failed: %w", err)
   267  	}
   268  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   269  		return nil, p.Error(resp)
   270  	}
   271  
   272  	return &result, nil
   273  }