github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/appsec/waf_mode.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 WAFMode interface supports retrieving and modifying the mode setting that determines how
    13  	// rule sets are upgraded.
    14  	//
    15  	// https://developer.akamai.com/api/cloud_security/application_security/v1.html#mode
    16  	WAFMode 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 GetWAFMode instead.
    19  		GetWAFModes(ctx context.Context, params GetWAFModesRequest) (*GetWAFModesResponse, error)
    20  
    21  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#getmode
    22  		GetWAFMode(ctx context.Context, params GetWAFModeRequest) (*GetWAFModeResponse, error)
    23  
    24  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#putmode
    25  		UpdateWAFMode(ctx context.Context, params UpdateWAFModeRequest) (*UpdateWAFModeResponse, error)
    26  	}
    27  
    28  	// GetWAFModesRequest is used to retrieve the setting that determines this mode how rules will be kept up to date.
    29  	// Deprecated: this struct will be removed in a future release.
    30  	GetWAFModesRequest struct {
    31  		ConfigID int    `json:"-"`
    32  		Version  int    `json:"-"`
    33  		PolicyID string `json:"-"`
    34  		Current  string `json:"current"`
    35  		Mode     string `json:"mode"`
    36  		Eval     string `json:"eval"`
    37  	}
    38  
    39  	// GetWAFModesResponse is returned from a call to GetWAFModes.
    40  	// Deprecated: this struct will be removed in a future release.
    41  	GetWAFModesResponse struct {
    42  		Current    string `json:"current,omitempty"`
    43  		Mode       string `json:"mode,omitempty"`
    44  		Eval       string `json:"eval,omitempty"`
    45  		Evaluating string `json:"evaluating,omitempty"`
    46  		Expires    string `json:"expires,omitempty"`
    47  	}
    48  
    49  	// GetWAFModeRequest is used to retrieve the setting that determines this mode how rules will be kept up to date.
    50  	GetWAFModeRequest struct {
    51  		ConfigID int    `json:"-"`
    52  		Version  int    `json:"-"`
    53  		PolicyID string `json:"-"`
    54  		Current  string `json:"current"`
    55  		Mode     string `json:"mode"`
    56  		Eval     string `json:"eval"`
    57  	}
    58  
    59  	// GetWAFModeResponse is returned from a call to GetWAFMode.
    60  	GetWAFModeResponse struct {
    61  		Current    string `json:"current,omitempty"`
    62  		Mode       string `json:"mode,omitempty"`
    63  		Eval       string `json:"eval,omitempty"`
    64  		Evaluating string `json:"evaluating,omitempty"`
    65  		Expires    string `json:"expires,omitempty"`
    66  	}
    67  
    68  	// UpdateWAFModeRequest is used to modify the setting that determines this mode how rules will be kept up to date.
    69  	UpdateWAFModeRequest struct {
    70  		ConfigID int    `json:"-"`
    71  		Version  int    `json:"-"`
    72  		PolicyID string `json:"-"`
    73  		Current  string `json:"-"`
    74  		Mode     string `json:"mode"`
    75  		Eval     string `json:"-"`
    76  	}
    77  
    78  	// UpdateWAFModeResponse is returned from a call to UpdateWAFMode.
    79  	UpdateWAFModeResponse struct {
    80  		Current string `json:"current"`
    81  		Mode    string `json:"mode"`
    82  	}
    83  )
    84  
    85  // Validate validates a GetWAFModeRequest.
    86  func (v GetWAFModeRequest) Validate() error {
    87  	return validation.Errors{
    88  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    89  		"Version":  validation.Validate(v.Version, validation.Required),
    90  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
    91  	}.Filter()
    92  }
    93  
    94  // Validate validates a GetWAFModesRequest.
    95  // Deprecated: this method will be removed in a future release.
    96  func (v GetWAFModesRequest) Validate() error {
    97  	return validation.Errors{
    98  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    99  		"Version":  validation.Validate(v.Version, validation.Required),
   100  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   101  	}.Filter()
   102  }
   103  
   104  // Validate validates an UpdateWAFModeRequest.
   105  func (v UpdateWAFModeRequest) Validate() error {
   106  	return validation.Errors{
   107  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   108  		"Version":  validation.Validate(v.Version, validation.Required),
   109  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   110  	}.Filter()
   111  }
   112  
   113  func (p *appsec) GetWAFMode(ctx context.Context, params GetWAFModeRequest) (*GetWAFModeResponse, error) {
   114  	logger := p.Log(ctx)
   115  	logger.Debug("GetWAFMode")
   116  
   117  	if err := params.Validate(); err != nil {
   118  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   119  	}
   120  
   121  	uri := fmt.Sprintf(
   122  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/mode",
   123  		params.ConfigID,
   124  		params.Version,
   125  		params.PolicyID)
   126  
   127  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   128  	if err != nil {
   129  		return nil, fmt.Errorf("failed to create GetWAFMode request: %w", err)
   130  	}
   131  
   132  	var result GetWAFModeResponse
   133  	resp, err := p.Exec(req, &result)
   134  	if err != nil {
   135  		return nil, fmt.Errorf("get WAF mode request failed: %w", err)
   136  	}
   137  	if resp.StatusCode != http.StatusOK {
   138  		return nil, p.Error(resp)
   139  	}
   140  
   141  	return &result, nil
   142  }
   143  
   144  // Deprecated: this method will be removed in a future release.
   145  func (p *appsec) GetWAFModes(ctx context.Context, params GetWAFModesRequest) (*GetWAFModesResponse, error) {
   146  	logger := p.Log(ctx)
   147  	logger.Debug("GetWAFModes")
   148  
   149  	if err := params.Validate(); err != nil {
   150  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   151  	}
   152  
   153  	uri := fmt.Sprintf(
   154  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/mode",
   155  		params.ConfigID,
   156  		params.Version,
   157  		params.PolicyID)
   158  
   159  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   160  	if err != nil {
   161  		return nil, fmt.Errorf("failed to create GetWAFModes request: %w", err)
   162  	}
   163  
   164  	var result GetWAFModesResponse
   165  	resp, err := p.Exec(req, &result)
   166  	if err != nil {
   167  		return nil, fmt.Errorf("get WAF modes request failed: %w", err)
   168  	}
   169  	if resp.StatusCode != http.StatusOK {
   170  		return nil, p.Error(resp)
   171  	}
   172  
   173  	return &result, nil
   174  }
   175  
   176  func (p *appsec) UpdateWAFMode(ctx context.Context, params UpdateWAFModeRequest) (*UpdateWAFModeResponse, error) {
   177  	logger := p.Log(ctx)
   178  	logger.Debug("UpdateWAFMode")
   179  
   180  	if err := params.Validate(); err != nil {
   181  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   182  	}
   183  
   184  	uri := fmt.Sprintf(
   185  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/mode",
   186  		params.ConfigID,
   187  		params.Version,
   188  		params.PolicyID,
   189  	)
   190  
   191  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   192  	if err != nil {
   193  		return nil, fmt.Errorf("failed to create UpdateWAFMode request: %w", err)
   194  	}
   195  
   196  	var result UpdateWAFModeResponse
   197  	resp, err := p.Exec(req, &result, params)
   198  	if err != nil {
   199  		return nil, fmt.Errorf("update WAF mode request failed: %w", err)
   200  	}
   201  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   202  		return nil, p.Error(resp)
   203  	}
   204  
   205  	return &result, nil
   206  }