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