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