github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/api_constraints_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 ApiConstraintsProtection interface supports retrieving and updating API request constraints protection for a configuration and policy.
    13  	// Deprecated: this interface will be removed in a future release. Use the SecurityPolicy interface instead.
    14  	ApiConstraintsProtection interface {
    15  		// GetAPIConstraintsProtection retrieves the current API constraints 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  		GetAPIConstraintsProtection(ctx context.Context, params GetAPIConstraintsProtectionRequest) (*GetAPIConstraintsProtectionResponse, error)
    20  
    21  		// UpdateAPIConstraintsProtection updates the API constraints protection setting for a configuration and policy.
    22  		// Deprecated: this method will be removed in a future release. Use the CreateSecurityPolicyWithDefaultProtections method of the SecurityPolicy interface instead.
    23  		//
    24  		// See: https://techdocs.akamai.com/application-security/reference/put-policy-protections
    25  		UpdateAPIConstraintsProtection(ctx context.Context, params UpdateAPIConstraintsProtectionRequest) (*UpdateAPIConstraintsProtectionResponse, error)
    26  	}
    27  
    28  	// GetAPIConstraintsProtectionRequest is used to retrieve the API constraints protection setting.
    29  	GetAPIConstraintsProtectionRequest struct {
    30  		ConfigID            int    `json:"-"`
    31  		Version             int    `json:"-"`
    32  		PolicyID            string `json:"-"`
    33  		ApplyAPIConstraints bool   `json:"applyApiConstraints"`
    34  	}
    35  
    36  	// ProtectionsResponse is returned from a call to GetAPIConstraintsProtection and similar security policy protection requests.
    37  	ProtectionsResponse struct {
    38  		ApplyAPIConstraints           bool `json:"applyApiConstraints,omitempty"`
    39  		ApplyApplicationLayerControls bool `json:"applyApplicationLayerControls,omitempty"`
    40  		ApplyBotmanControls           bool `json:"applyBotmanControls,omitempty"`
    41  		ApplyMalwareControls          bool `json:"applyMalwareControls,omitempty"`
    42  		ApplyNetworkLayerControls     bool `json:"applyNetworkLayerControls,omitempty"`
    43  		ApplyRateControls             bool `json:"applyRateControls,omitempty"`
    44  		ApplyReputationControls       bool `json:"applyReputationControls,omitempty"`
    45  		ApplySlowPostControls         bool `json:"applySlowPostControls,omitempty"`
    46  	}
    47  
    48  	// GetAPIConstraintsProtectionResponse contains the status of various protection flags assigned to a security policy.
    49  	GetAPIConstraintsProtectionResponse ProtectionsResponse
    50  
    51  	// UpdateAPIConstraintsProtectionRequest is used to modify the API constraints protection setting.
    52  	UpdateAPIConstraintsProtectionRequest struct {
    53  		ConfigID            int    `json:"-"`
    54  		Version             int    `json:"-"`
    55  		PolicyID            string `json:"-"`
    56  		ApplyAPIConstraints bool   `json:"applyApiConstraints"`
    57  	}
    58  
    59  	// UpdateAPIConstraintsProtectionResponse is returned from a call to UpdateAPIConstraintsProtection.
    60  	UpdateAPIConstraintsProtectionResponse ProtectionsResponse
    61  )
    62  
    63  // Validate validates a GetAPIConstraintsProtectionRequest.
    64  func (v GetAPIConstraintsProtectionRequest) Validate() error {
    65  	return validation.Errors{
    66  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    67  		"Version":  validation.Validate(v.Version, validation.Required),
    68  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
    69  	}.Filter()
    70  }
    71  
    72  // Validate validates an UpdateAPIConstraintsProtectionRequest.
    73  func (v UpdateAPIConstraintsProtectionRequest) Validate() error {
    74  	return validation.Errors{
    75  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    76  		"Version":  validation.Validate(v.Version, validation.Required),
    77  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
    78  	}.Filter()
    79  }
    80  
    81  func (p *appsec) GetAPIConstraintsProtection(ctx context.Context, params GetAPIConstraintsProtectionRequest) (*GetAPIConstraintsProtectionResponse, error) {
    82  	logger := p.Log(ctx)
    83  	logger.Debug("GetAPIConstraintsProtection")
    84  
    85  	if err := params.Validate(); err != nil {
    86  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
    87  	}
    88  
    89  	uri := fmt.Sprintf(
    90  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections",
    91  		params.ConfigID,
    92  		params.Version,
    93  		params.PolicyID)
    94  
    95  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
    96  	if err != nil {
    97  		return nil, fmt.Errorf("failed to create GetAPIConstraintsProtection request: %w", err)
    98  	}
    99  
   100  	var result GetAPIConstraintsProtectionResponse
   101  	resp, err := p.Exec(req, &result)
   102  	if err != nil {
   103  		return nil, fmt.Errorf("get API constraints protection request failed: %w", err)
   104  	}
   105  
   106  	if resp.StatusCode != http.StatusOK {
   107  		return nil, p.Error(resp)
   108  	}
   109  
   110  	return &result, nil
   111  }
   112  
   113  func (p *appsec) UpdateAPIConstraintsProtection(ctx context.Context, params UpdateAPIConstraintsProtectionRequest) (*UpdateAPIConstraintsProtectionResponse, error) {
   114  	logger := p.Log(ctx)
   115  	logger.Debug("UpdateAPIConstraintsProtection")
   116  
   117  	if err := params.Validate(); err != nil {
   118  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   119  	}
   120  
   121  	uri := fmt.Sprintf(
   122  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections",
   123  		params.ConfigID,
   124  		params.Version,
   125  		params.PolicyID,
   126  	)
   127  
   128  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   129  	if err != nil {
   130  		return nil, fmt.Errorf("failed to create UpdateAPIConstraintsProtection request: %w", err)
   131  	}
   132  
   133  	var result UpdateAPIConstraintsProtectionResponse
   134  	resp, err := p.Exec(req, &result, params)
   135  	if err != nil {
   136  		return nil, fmt.Errorf("update API constraints protection request failed: %w", err)
   137  	}
   138  
   139  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   140  		return nil, p.Error(resp)
   141  	}
   142  
   143  	return &result, nil
   144  }