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