github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/slowpost_protection.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 SlowPostProtection interface supports retrieving and updating slow post protection for a configuration and policy.
    13  	// Deprecated: this interface will be removed in a future release. Use the SecurityPolicy interface instead.
    14  	SlowPostProtection interface {
    15  		// GetSlowPostProtections retrieves the current SLOW post protection setting for a configuration and policy.
    16  		// Deprecated: this method will be removed in a future release. Use the GetPolicyProtections method of the PolicyProtections interface instead.
    17  		//
    18  		// See: https://techdocs.akamai.com/application-security/reference/get-policy-protections
    19  		GetSlowPostProtections(ctx context.Context, params GetSlowPostProtectionsRequest) (*GetSlowPostProtectionsResponse, error)
    20  
    21  		// GetSlowPostProtection retrieves the current SLOW post protection setting for a configuration and policy.
    22  		// Deprecated: this method will be removed in a future release. Use the GetPolicyProtections method of the PolicyProtections interface instead.
    23  		//
    24  		// See: https://techdocs.akamai.com/application-security/reference/get-policy-protections
    25  		GetSlowPostProtection(ctx context.Context, params GetSlowPostProtectionRequest) (*GetSlowPostProtectionResponse, error)
    26  
    27  		// UpdateSlowPostProtection updates the SLOW post protection setting for a configuration and policy.
    28  		// Deprecated: this method will be removed in a future release. Use the CreateSecurityPolicyWithDefaultProtections method of the SecurityPolicy interface instead.
    29  		//
    30  		// See: https://techdocs.akamai.com/application-security/reference/put-policy-protections
    31  		UpdateSlowPostProtection(ctx context.Context, params UpdateSlowPostProtectionRequest) (*UpdateSlowPostProtectionResponse, error)
    32  	}
    33  
    34  	// GetSlowPostProtectionRequest is used to retrieve the slow post protecton setting for a policy.
    35  	GetSlowPostProtectionRequest struct {
    36  		ConfigID              int    `json:"-"`
    37  		Version               int    `json:"-"`
    38  		PolicyID              string `json:"-"`
    39  		ApplySlowPostControls bool   `json:"applySlowPostControls"`
    40  	}
    41  
    42  	// GetSlowPostProtectionResponse is returned from a call to GetSlowPostProtection.
    43  	GetSlowPostProtectionResponse ProtectionsResponse
    44  
    45  	// GetSlowPostProtectionsRequest is used to retrieve the slow post protecton setting for a policy.
    46  	// Deprecated: this struct will be removed in a future release.
    47  	GetSlowPostProtectionsRequest struct {
    48  		ConfigID              int    `json:"-"`
    49  		Version               int    `json:"-"`
    50  		PolicyID              string `json:"-"`
    51  		ApplySlowPostControls bool   `json:"applySlowPostControls"`
    52  	}
    53  
    54  	// GetSlowPostProtectionsResponse is returned from a call to GetSlowPostProtections.
    55  	// Deprecated: this struct will be removed in a future release.
    56  	GetSlowPostProtectionsResponse ProtectionsResponse
    57  
    58  	// UpdateSlowPostProtectionRequest is used to modify the slow post protection setting.
    59  	UpdateSlowPostProtectionRequest struct {
    60  		ConfigID              int    `json:"-"`
    61  		Version               int    `json:"-"`
    62  		PolicyID              string `json:"-"`
    63  		ApplySlowPostControls bool   `json:"applySlowPostControls"`
    64  	}
    65  
    66  	// UpdateSlowPostProtectionResponse is returned from a call to UpdateSlowPostProtection.
    67  	UpdateSlowPostProtectionResponse ProtectionsResponse
    68  )
    69  
    70  // Validate validates a GetSlowPostProtectionRequest.
    71  func (v GetSlowPostProtectionRequest) Validate() error {
    72  	return validation.Errors{
    73  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    74  		"Version":  validation.Validate(v.Version, validation.Required),
    75  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
    76  	}.Filter()
    77  }
    78  
    79  // Validate validates a GetSlowPostProtectionsRequest.
    80  func (v GetSlowPostProtectionsRequest) Validate() error {
    81  	return validation.Errors{
    82  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    83  		"Version":  validation.Validate(v.Version, validation.Required),
    84  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
    85  	}.Filter()
    86  }
    87  
    88  // Validate validates an UpdateSlowPostProtectionRequest.
    89  func (v UpdateSlowPostProtectionRequest) Validate() error {
    90  	return validation.Errors{
    91  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    92  		"Version":  validation.Validate(v.Version, validation.Required),
    93  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
    94  	}.Filter()
    95  }
    96  
    97  func (p *appsec) GetSlowPostProtection(ctx context.Context, params GetSlowPostProtectionRequest) (*GetSlowPostProtectionResponse, error) {
    98  	logger := p.Log(ctx)
    99  	logger.Debug("GetSlowPostProtection")
   100  
   101  	if err := params.Validate(); err != nil {
   102  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   103  	}
   104  
   105  	uri := fmt.Sprintf(
   106  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections",
   107  		params.ConfigID,
   108  		params.Version,
   109  		params.PolicyID)
   110  
   111  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   112  	if err != nil {
   113  		return nil, fmt.Errorf("failed to create GetSlowPostProtection request: %w", err)
   114  	}
   115  
   116  	var result GetSlowPostProtectionResponse
   117  	resp, err := p.Exec(req, &result)
   118  	if err != nil {
   119  		return nil, fmt.Errorf("get slow post protection request failed: %w", err)
   120  	}
   121  	if resp.StatusCode != http.StatusOK {
   122  		return nil, p.Error(resp)
   123  	}
   124  
   125  	return &result, nil
   126  }
   127  
   128  func (p *appsec) GetSlowPostProtections(ctx context.Context, params GetSlowPostProtectionsRequest) (*GetSlowPostProtectionsResponse, error) {
   129  	logger := p.Log(ctx)
   130  	logger.Debug("GetSlowPostProtections")
   131  
   132  	if err := params.Validate(); err != nil {
   133  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   134  	}
   135  
   136  	uri := fmt.Sprintf(
   137  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections",
   138  		params.ConfigID,
   139  		params.Version,
   140  		params.PolicyID)
   141  
   142  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   143  	if err != nil {
   144  		return nil, fmt.Errorf("failed to create GetSlowPostProtections request: %w", err)
   145  	}
   146  
   147  	var result GetSlowPostProtectionsResponse
   148  	resp, err := p.Exec(req, &result)
   149  	if err != nil {
   150  		return nil, fmt.Errorf("get slow post protections request failed: %w", err)
   151  	}
   152  	if resp.StatusCode != http.StatusOK {
   153  		return nil, p.Error(resp)
   154  	}
   155  
   156  	return &result, nil
   157  }
   158  
   159  func (p *appsec) UpdateSlowPostProtection(ctx context.Context, params UpdateSlowPostProtectionRequest) (*UpdateSlowPostProtectionResponse, error) {
   160  	logger := p.Log(ctx)
   161  	logger.Debug("UpdateSlowPostProtection")
   162  
   163  	if err := params.Validate(); err != nil {
   164  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   165  	}
   166  
   167  	uri := fmt.Sprintf(
   168  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections",
   169  		params.ConfigID,
   170  		params.Version,
   171  		params.PolicyID,
   172  	)
   173  
   174  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   175  	if err != nil {
   176  		return nil, fmt.Errorf("failed to create UpdateSlowPostProtection request: %w", err)
   177  	}
   178  
   179  	var result UpdateSlowPostProtectionResponse
   180  	resp, err := p.Exec(req, &result, params)
   181  	if err != nil {
   182  		return nil, fmt.Errorf("update slow post protection request failed: %w", err)
   183  	}
   184  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   185  		return nil, p.Error(resp)
   186  	}
   187  
   188  	return &result, nil
   189  }