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