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