github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/reputation_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 ReputationProtection interface supports retrieving and updating reputation protection for a configuration and policy.
    13  	// Deprecated: this interface will be removed in a future release. Use the SecurityPolicy interface instead.
    14  	ReputationProtection interface {
    15  		// GetReputationProtections retrieves the current reputation 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  		// Deprecated: this method will be removed in a future release. Use GetReputationProtection instead.
    20  		GetReputationProtections(ctx context.Context, params GetReputationProtectionsRequest) (*GetReputationProtectionsResponse, error)
    21  
    22  		// GetReputationProtection retrieves the current reputation protection setting for a configuration and policy.
    23  		// Deprecated: this method will be removed in a future release. Use the GetPolicyProtections method of the PolicyProtections interface instead.
    24  		//
    25  		// See: https://techdocs.akamai.com/application-security/reference/get-policy-protections
    26  		GetReputationProtection(ctx context.Context, params GetReputationProtectionRequest) (*GetReputationProtectionResponse, error)
    27  
    28  		// UpdateReputationProtection updates the reputation protection setting for a configuration and policy.
    29  		// Deprecated: this method will be removed in a future release. Use the CreateSecurityPolicyWithDefaultProtections method of the SecurityPolicy interface instead.
    30  		//
    31  		// See: https://techdocs.akamai.com/application-security/reference/put-policy-protections
    32  		UpdateReputationProtection(ctx context.Context, params UpdateReputationProtectionRequest) (*UpdateReputationProtectionResponse, error)
    33  
    34  		// RemoveReputationProtection removes reputation protection for a configuration and policy.
    35  		// Deprecated: this method will be removed in a future release. Use the CreateSecurityPolicyWithDefaultProtections method of the SecurityPolicy interface instead.
    36  		//
    37  		// See: https://techdocs.akamai.com/application-security/reference/put-policy-protections
    38  		RemoveReputationProtection(ctx context.Context, params RemoveReputationProtectionRequest) (*RemoveReputationProtectionResponse, error)
    39  	}
    40  
    41  	// GetReputationProtectionRequest is used to retrieve the reputation protection setting.
    42  	GetReputationProtectionRequest struct {
    43  		ConfigID                int    `json:"-"`
    44  		Version                 int    `json:"-"`
    45  		PolicyID                string `json:"-"`
    46  		ApplyReputationControls bool   `json:"applyReputationControls"`
    47  	}
    48  
    49  	// GetReputationProtectionResponse is returned from a call to GetReputationProtection.
    50  	GetReputationProtectionResponse ProtectionsResponse
    51  
    52  	// GetReputationProtectionsRequest is used to retrieve the reputation protection setting.
    53  	// Deprecated: this struct will be removed in a future release.
    54  	GetReputationProtectionsRequest struct {
    55  		ConfigID                int    `json:"-"`
    56  		Version                 int    `json:"-"`
    57  		PolicyID                string `json:"-"`
    58  		ApplyReputationControls bool   `json:"applyReputationControls"`
    59  	}
    60  
    61  	// GetReputationProtectionsResponse is returned from a call to GetReputationProtection.
    62  	// Deprecated: this struct will be removed in a future release.
    63  	GetReputationProtectionsResponse ProtectionsResponse
    64  
    65  	// UpdateReputationProtectionRequest is used to modify the reputation protection setting.
    66  	UpdateReputationProtectionRequest struct {
    67  		ConfigID                int    `json:"-"`
    68  		Version                 int    `json:"-"`
    69  		PolicyID                string `json:"-"`
    70  		ApplyReputationControls bool   `json:"applyReputationControls"`
    71  	}
    72  
    73  	// UpdateReputationProtectionResponse is returned from a call to UpdateReputationProtection.
    74  	UpdateReputationProtectionResponse ProtectionsResponse
    75  
    76  	// RemoveReputationProtectionRequest is used to remove the reputation protection settings.
    77  	// Deprecated: this struct will be removed in a future release.
    78  	RemoveReputationProtectionRequest struct {
    79  		ConfigID                int    `json:"-"`
    80  		Version                 int    `json:"-"`
    81  		PolicyID                string `json:"-"`
    82  		ApplyReputationControls bool   `json:"applyReputationControls"`
    83  	}
    84  
    85  	// RemoveReputationProtectionResponse is returned from a call to RemoveReputationProtection.
    86  	// Deprecated: this struct will be removed in a future release.
    87  	RemoveReputationProtectionResponse ProtectionsResponse
    88  )
    89  
    90  // Validate validates a GetReputationProtectionRequest.
    91  func (v GetReputationProtectionRequest) Validate() error {
    92  	return validation.Errors{
    93  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    94  		"Version":  validation.Validate(v.Version, validation.Required),
    95  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
    96  	}.Filter()
    97  }
    98  
    99  // Validate validates a GetReputationProtectionsRequest.
   100  func (v GetReputationProtectionsRequest) Validate() error {
   101  	return validation.Errors{
   102  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   103  		"Version":  validation.Validate(v.Version, validation.Required),
   104  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   105  	}.Filter()
   106  }
   107  
   108  // Validate validates an UpdateReputationProtectionRequest.
   109  func (v UpdateReputationProtectionRequest) Validate() error {
   110  	return validation.Errors{
   111  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   112  		"Version":  validation.Validate(v.Version, validation.Required),
   113  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   114  	}.Filter()
   115  }
   116  
   117  // Validate validates a RemoveReputationProtectionRequest.
   118  func (v RemoveReputationProtectionRequest) Validate() error {
   119  	return validation.Errors{
   120  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   121  		"Version":  validation.Validate(v.Version, validation.Required),
   122  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   123  	}.Filter()
   124  }
   125  
   126  func (p *appsec) GetReputationProtection(ctx context.Context, params GetReputationProtectionRequest) (*GetReputationProtectionResponse, error) {
   127  	logger := p.Log(ctx)
   128  	logger.Debug("GetReputationProtection")
   129  
   130  	if err := params.Validate(); err != nil {
   131  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   132  	}
   133  
   134  	uri := fmt.Sprintf(
   135  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections",
   136  		params.ConfigID,
   137  		params.Version,
   138  		params.PolicyID)
   139  
   140  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   141  	if err != nil {
   142  		return nil, fmt.Errorf("failed to create GetReputationProtection request: %w", err)
   143  	}
   144  
   145  	var result GetReputationProtectionResponse
   146  	resp, err := p.Exec(req, &result)
   147  	if err != nil {
   148  		return nil, fmt.Errorf("get reputation protection request failed: %w", err)
   149  	}
   150  	if resp.StatusCode != http.StatusOK {
   151  		return nil, p.Error(resp)
   152  	}
   153  
   154  	return &result, nil
   155  }
   156  
   157  func (p *appsec) GetReputationProtections(ctx context.Context, params GetReputationProtectionsRequest) (*GetReputationProtectionsResponse, error) {
   158  	logger := p.Log(ctx)
   159  	logger.Debug("GetReputationProtections")
   160  
   161  	if err := params.Validate(); err != nil {
   162  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   163  	}
   164  
   165  	uri := fmt.Sprintf(
   166  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections",
   167  		params.ConfigID,
   168  		params.Version,
   169  		params.PolicyID)
   170  
   171  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   172  	if err != nil {
   173  		return nil, fmt.Errorf("failed to create GetReputationProtections request: %w", err)
   174  	}
   175  
   176  	var result GetReputationProtectionsResponse
   177  	resp, err := p.Exec(req, &result)
   178  	if err != nil {
   179  		return nil, fmt.Errorf("get reputation protections request failed: %w", err)
   180  	}
   181  	if resp.StatusCode != http.StatusOK {
   182  		return nil, p.Error(resp)
   183  	}
   184  
   185  	return &result, nil
   186  }
   187  
   188  func (p *appsec) UpdateReputationProtection(ctx context.Context, params UpdateReputationProtectionRequest) (*UpdateReputationProtectionResponse, error) {
   189  	logger := p.Log(ctx)
   190  	logger.Debug("UpdateReputationProtection")
   191  
   192  	if err := params.Validate(); err != nil {
   193  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   194  	}
   195  
   196  	uri := fmt.Sprintf(
   197  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections",
   198  		params.ConfigID,
   199  		params.Version,
   200  		params.PolicyID,
   201  	)
   202  
   203  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   204  	if err != nil {
   205  		return nil, fmt.Errorf("failed to create UpdateReputationProtection request: %w", err)
   206  	}
   207  
   208  	var result UpdateReputationProtectionResponse
   209  	resp, err := p.Exec(req, &result, params)
   210  	if err != nil {
   211  		return nil, fmt.Errorf("update reputation protection request failed: %w", err)
   212  	}
   213  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   214  		return nil, p.Error(resp)
   215  	}
   216  
   217  	return &result, nil
   218  }
   219  
   220  func (p *appsec) RemoveReputationProtection(ctx context.Context, params RemoveReputationProtectionRequest) (*RemoveReputationProtectionResponse, error) {
   221  	logger := p.Log(ctx)
   222  	logger.Debug("RemoveReputationProtection")
   223  
   224  	if err := params.Validate(); err != nil {
   225  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   226  	}
   227  
   228  	uri := fmt.Sprintf(
   229  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections",
   230  		params.ConfigID,
   231  		params.Version,
   232  		params.PolicyID,
   233  	)
   234  
   235  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   236  	if err != nil {
   237  		return nil, fmt.Errorf("failed to create RemoveReputationProtection request: %w", err)
   238  	}
   239  
   240  	var result RemoveReputationProtectionResponse
   241  	resp, err := p.Exec(req, &result, params)
   242  	if err != nil {
   243  		return nil, fmt.Errorf("remove reputation protection request failed: %w", err)
   244  	}
   245  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   246  		return nil, p.Error(resp)
   247  	}
   248  
   249  	return &result, nil
   250  }