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