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