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