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