github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/slow_post_protection_setting.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 SlowPostProtectionSetting interface supports retrieving and updating the slow POST protection settings for a configuration and policy.
    13  	SlowPostProtectionSetting interface {
    14  		// GetSlowPostProtectionSettings retrieves the current SLOW post protection settings for a configuration and policy.
    15  		//
    16  		// See: https://techdocs.akamai.com/application-security/reference/get-policy-slow-post
    17  		GetSlowPostProtectionSettings(ctx context.Context, params GetSlowPostProtectionSettingsRequest) (*GetSlowPostProtectionSettingsResponse, error)
    18  
    19  		// GetSlowPostProtectionSetting retrieves the current SLOW post protection settings for a configuration and policy.
    20  		//
    21  		// See: https://techdocs.akamai.com/application-security/reference/get-policy-slow-post
    22  		// Deprecated: this method will be removed in a future release. Use GetSlowPostProtectionSettings instead.
    23  		GetSlowPostProtectionSetting(ctx context.Context, params GetSlowPostProtectionSettingRequest) (*GetSlowPostProtectionSettingResponse, error)
    24  
    25  		// UpdateSlowPostProtectionSetting updates the SLOW post protection settings for a configuration and policy.
    26  		//
    27  		// See: https://techdocs.akamai.com/application-security/reference/put-policy-slow-post
    28  		UpdateSlowPostProtectionSetting(ctx context.Context, params UpdateSlowPostProtectionSettingRequest) (*UpdateSlowPostProtectionSettingResponse, error)
    29  	}
    30  
    31  	// GetSlowPostProtectionSettingsRequest is used to retrieve the slow post protection settings for a configuration.
    32  	GetSlowPostProtectionSettingsRequest struct {
    33  		ConfigID          int                                         `json:"configId"`
    34  		Version           int                                         `json:"version"`
    35  		PolicyID          string                                      `json:"policyId"`
    36  		Action            string                                      `json:"action"`
    37  		SlowRateThreshold *SlowPostProtectionSettingSlowRateThreshold `json:"slowRateThreshold,omitempty"`
    38  		DurationThreshold *SlowPostProtectionSettingDurationThreshold `json:"durationThreshold,omitempty"`
    39  	}
    40  
    41  	// GetSlowPostProtectionSettingsResponse is returned from a call to GetSlowPostProtectionSettings.
    42  	GetSlowPostProtectionSettingsResponse struct {
    43  		Action            string                                      `json:"action,omitempty"`
    44  		SlowRateThreshold *SlowPostProtectionSettingSlowRateThreshold `json:"slowRateThreshold,omitempty"`
    45  		DurationThreshold *SlowPostProtectionSettingDurationThreshold `json:"durationThreshold,omitempty"`
    46  	}
    47  
    48  	// GetSlowPostProtectionSettingRequest is used to retrieve the slow post protection settings for a configuration.
    49  	// Deprecated: this struct will be removed in a future release.
    50  	GetSlowPostProtectionSettingRequest struct {
    51  		ConfigID          int    `json:"configId"`
    52  		Version           int    `json:"version"`
    53  		PolicyID          string `json:"policyId"`
    54  		Action            string `json:"action"`
    55  		SlowRateThreshold struct {
    56  			Rate   int `json:"rate"`
    57  			Period int `json:"period"`
    58  		} `json:"slowRateThreshold"`
    59  		DurationThreshold struct {
    60  			Timeout int `json:"timeout"`
    61  		} `json:"durationThreshold"`
    62  	}
    63  
    64  	// GetSlowPostProtectionSettingResponse is returned from a call to GetSlowPostProtectionSetting.
    65  	// Deprecated: this struct will be removed in a future release.
    66  	GetSlowPostProtectionSettingResponse struct {
    67  		Action            string                                      `json:"action,omitempty"`
    68  		SlowRateThreshold *SlowPostProtectionSettingSlowRateThreshold `json:"slowRateThreshold,omitempty"`
    69  		DurationThreshold *SlowPostProtectionSettingDurationThreshold `json:"durationThreshold,omitempty"`
    70  	}
    71  
    72  	// UpdateSlowPostProtectionSettingRequest is used to modify the slow post protection settings for a configuration.
    73  	UpdateSlowPostProtectionSettingRequest struct {
    74  		ConfigID          int    `json:"configId"`
    75  		Version           int    `json:"version"`
    76  		PolicyID          string `json:"policyId"`
    77  		Action            string `json:"action"`
    78  		SlowRateThreshold struct {
    79  			Rate   int `json:"rate"`
    80  			Period int `json:"period"`
    81  		} `json:"slowRateThreshold"`
    82  		DurationThreshold struct {
    83  			Timeout int `json:"timeout"`
    84  		} `json:"durationThreshold"`
    85  	}
    86  
    87  	// UpdateSlowPostProtectionSettingResponse is returned from a call to UpdateSlowPostProtection.
    88  	UpdateSlowPostProtectionSettingResponse struct {
    89  		Action            string `json:"action"`
    90  		SlowRateThreshold struct {
    91  			Rate   int `json:"rate"`
    92  			Period int `json:"period"`
    93  		} `json:"slowRateThreshold"`
    94  		DurationThreshold struct {
    95  			Timeout int `json:"timeout"`
    96  		} `json:"durationThreshold"`
    97  	}
    98  
    99  	// SlowPostProtectionSettingSlowRateThreshold describes the average rate in bytes per second over a specified period of time before an action
   100  	// (alert or abort) in the policy triggers.
   101  	SlowPostProtectionSettingSlowRateThreshold struct {
   102  		Rate   int `json:"rate"`
   103  		Period int `json:"period"`
   104  	}
   105  
   106  	// SlowPostProtectionSettingDurationThreshold describes the length of time in seconds within which the first eight kilobytes of the POST body must be transferred to
   107  	// avoid applying the action specified in the policy.
   108  	SlowPostProtectionSettingDurationThreshold struct {
   109  		Timeout int `json:"timeout"`
   110  	}
   111  )
   112  
   113  // Validate validates a GetSlowPostProtectionSettingRequest.
   114  func (v GetSlowPostProtectionSettingRequest) Validate() error {
   115  	return validation.Errors{
   116  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   117  		"Version":  validation.Validate(v.Version, validation.Required),
   118  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   119  	}.Filter()
   120  }
   121  
   122  // Validate validates a GetSlowPostProtectionSettingsRequest.
   123  // Deprecated: this method will be removed in a future release.
   124  func (v GetSlowPostProtectionSettingsRequest) Validate() error {
   125  	return validation.Errors{
   126  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   127  		"Version":  validation.Validate(v.Version, validation.Required),
   128  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   129  	}.Filter()
   130  }
   131  
   132  // Validate validates an UpdateSlowPostProtectionSettingRequest.
   133  func (v UpdateSlowPostProtectionSettingRequest) Validate() error {
   134  	return validation.Errors{
   135  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   136  		"Version":  validation.Validate(v.Version, validation.Required),
   137  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   138  	}.Filter()
   139  }
   140  
   141  // Deprecated: this method will be removed in a future release.
   142  func (p *appsec) GetSlowPostProtectionSetting(ctx context.Context, params GetSlowPostProtectionSettingRequest) (*GetSlowPostProtectionSettingResponse, error) {
   143  	logger := p.Log(ctx)
   144  	logger.Debug("GetSlowPostProtectionSetting")
   145  
   146  	if err := params.Validate(); err != nil {
   147  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   148  	}
   149  
   150  	uri := fmt.Sprintf(
   151  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/slow-post",
   152  		params.ConfigID,
   153  		params.Version,
   154  		params.PolicyID)
   155  
   156  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   157  	if err != nil {
   158  		return nil, fmt.Errorf("failed to create GetSlowPostProtectionSetting request: %w", err)
   159  	}
   160  
   161  	var result GetSlowPostProtectionSettingResponse
   162  	resp, err := p.Exec(req, &result)
   163  	if err != nil {
   164  		return nil, fmt.Errorf("get slow post protection setting request failed: %w", err)
   165  	}
   166  	if resp.StatusCode != http.StatusOK {
   167  		return nil, p.Error(resp)
   168  	}
   169  
   170  	return &result, nil
   171  }
   172  
   173  func (p *appsec) GetSlowPostProtectionSettings(ctx context.Context, params GetSlowPostProtectionSettingsRequest) (*GetSlowPostProtectionSettingsResponse, error) {
   174  	logger := p.Log(ctx)
   175  	logger.Debug("GetSlowPostProtectionSettings")
   176  
   177  	if err := params.Validate(); err != nil {
   178  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   179  	}
   180  
   181  	uri := fmt.Sprintf(
   182  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/slow-post",
   183  		params.ConfigID,
   184  		params.Version,
   185  		params.PolicyID)
   186  
   187  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   188  	if err != nil {
   189  		return nil, fmt.Errorf("failed to create GetSlowPostProtectionSettings request: %w", err)
   190  	}
   191  
   192  	var result GetSlowPostProtectionSettingsResponse
   193  	resp, err := p.Exec(req, &result)
   194  	if err != nil {
   195  		return nil, fmt.Errorf("get slow post protection settings request failed: %w", err)
   196  	}
   197  	if resp.StatusCode != http.StatusOK {
   198  		return nil, p.Error(resp)
   199  	}
   200  
   201  	return &result, nil
   202  }
   203  
   204  func (p *appsec) UpdateSlowPostProtectionSetting(ctx context.Context, params UpdateSlowPostProtectionSettingRequest) (*UpdateSlowPostProtectionSettingResponse, error) {
   205  	logger := p.Log(ctx)
   206  	logger.Debug("UpdateSlowPostProtectionSetting")
   207  
   208  	if err := params.Validate(); err != nil {
   209  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   210  	}
   211  
   212  	uri := fmt.Sprintf(
   213  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/slow-post",
   214  		params.ConfigID,
   215  		params.Version,
   216  		params.PolicyID,
   217  	)
   218  
   219  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   220  	if err != nil {
   221  		return nil, fmt.Errorf("failed to create update UpdateSlowPostProtectionSetting request: %w", err)
   222  	}
   223  
   224  	var result UpdateSlowPostProtectionSettingResponse
   225  	resp, err := p.Exec(req, &result, params)
   226  	if err != nil {
   227  		return nil, fmt.Errorf("update slow post protection setting request failed: %w", err)
   228  	}
   229  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   230  		return nil, p.Error(resp)
   231  	}
   232  
   233  	return &result, nil
   234  }