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