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