github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/waf_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 WAFProtection interface supports retrieving and updating application layer protection for a configuration and policy.
    13  	// Deprecated: this interface will be removed in a future release. Use the SecurityPolicy interface instead.
    14  	WAFProtection interface {
    15  		// GetWAFProtections retrieves the current WAF 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  		GetWAFProtections(ctx context.Context, params GetWAFProtectionsRequest) (*GetWAFProtectionsResponse, error)
    20  
    21  		// GetWAFProtection retrieves the current WAF 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  		GetWAFProtection(ctx context.Context, params GetWAFProtectionRequest) (*GetWAFProtectionResponse, error)
    26  
    27  		// UpdateWAFProtection updates the WAF 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  		UpdateWAFProtection(ctx context.Context, params UpdateWAFProtectionRequest) (*UpdateWAFProtectionResponse, error)
    32  	}
    33  
    34  	// GetWAFProtectionRequest is used to retrieve the WAF protection setting.
    35  	GetWAFProtectionRequest struct {
    36  		ConfigID                      int    `json:"-"`
    37  		Version                       int    `json:"-"`
    38  		PolicyID                      string `json:"-"`
    39  		ApplyApplicationLayerControls bool   `json:"applyApplicationLayerControls"`
    40  	}
    41  
    42  	// GetWAFProtectionResponse is returned from a call to GetWAFProtection.
    43  	GetWAFProtectionResponse ProtectionsResponse
    44  
    45  	// GetWAFProtectionsRequest is used to retrieve the WAF protection setting.
    46  	// Deprecated: this struct will be removed in a future release.
    47  	GetWAFProtectionsRequest struct {
    48  		ConfigID                      int    `json:"-"`
    49  		Version                       int    `json:"-"`
    50  		PolicyID                      string `json:"-"`
    51  		ApplyApplicationLayerControls bool   `json:"applyApplicationLayerControls"`
    52  	}
    53  
    54  	// GetWAFProtectionsResponse is returned from a call to GetWAFProtections.
    55  	// Deprecated: this struct will be removed in a future release.
    56  	GetWAFProtectionsResponse ProtectionsResponse
    57  
    58  	// UpdateWAFProtectionRequest is used to modify the WAF protection setting.
    59  	UpdateWAFProtectionRequest struct {
    60  		ConfigID                      int    `json:"-"`
    61  		Version                       int    `json:"-"`
    62  		PolicyID                      string `json:"-"`
    63  		ApplyApplicationLayerControls bool   `json:"applyApplicationLayerControls"`
    64  	}
    65  
    66  	// UpdateWAFProtectionResponse is returned from a call to UpdateWAFProtection.
    67  	UpdateWAFProtectionResponse ProtectionsResponse
    68  )
    69  
    70  // Validate validates a GetWAFProtectionRequest.
    71  func (v GetWAFProtectionRequest) 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 GetWAFProtectionsRequest.
    80  func (v GetWAFProtectionsRequest) 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 UpdateWAFProtectionRequest.
    89  func (v UpdateWAFProtectionRequest) 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) GetWAFProtection(ctx context.Context, params GetWAFProtectionRequest) (*GetWAFProtectionResponse, error) {
    98  	logger := p.Log(ctx)
    99  	logger.Debug("GetWAFProtection")
   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 GetWAFProtection request: %w", err)
   114  	}
   115  
   116  	var result GetWAFProtectionResponse
   117  	resp, err := p.Exec(req, &result)
   118  	if err != nil {
   119  		return nil, fmt.Errorf("get WAF 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) GetWAFProtections(ctx context.Context, params GetWAFProtectionsRequest) (*GetWAFProtectionsResponse, error) {
   129  	logger := p.Log(ctx)
   130  	logger.Debug("GetWAFProtections")
   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 GetWAFProtections request: %w", err)
   145  	}
   146  
   147  	var result GetWAFProtectionsResponse
   148  	resp, err := p.Exec(req, &result)
   149  	if err != nil {
   150  		return nil, fmt.Errorf("get WAF 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) UpdateWAFProtection(ctx context.Context, params UpdateWAFProtectionRequest) (*UpdateWAFProtectionResponse, error) {
   160  	logger := p.Log(ctx)
   161  	logger.Debug("UpdateWAFProtection")
   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 UpdateWAFProtection request: %w", err)
   177  	}
   178  
   179  	var result UpdateWAFProtectionResponse
   180  	resp, err := p.Exec(req, &result, params)
   181  	if err != nil {
   182  		return nil, fmt.Errorf("update WAF 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  }