github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/appsec/advanced_settings_evasive_path_match.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 AdvancedSettingsEvasivePathMatch interface supports retrieving or modifying the Evasive Path Match setting.
    13  	AdvancedSettingsEvasivePathMatch interface {
    14  		// GetAdvancedSettingsEvasivePathMatch retrieves the Evasive Path Match setting.
    15  		// https://techdocs.akamai.com/application-security/reference/get-evasive-path-match-per-config
    16  		GetAdvancedSettingsEvasivePathMatch(ctx context.Context, params GetAdvancedSettingsEvasivePathMatchRequest) (*GetAdvancedSettingsEvasivePathMatchResponse, error)
    17  		// UpdateAdvancedSettingsEvasivePathMatch modifies the Evasive Path Match setting.
    18  		// https://techdocs.akamai.com/application-security/reference/put-evasive-path-match-per-config
    19  		UpdateAdvancedSettingsEvasivePathMatch(ctx context.Context, params UpdateAdvancedSettingsEvasivePathMatchRequest) (*UpdateAdvancedSettingsEvasivePathMatchResponse, error)
    20  		// RemoveAdvancedSettingsEvasivePathMatch removes the Evasive Path Match setting.
    21  		// Deprecated: this method will be removed in a future release. Use UpdateAdvancedSettingsEvasivePathMatch instead.
    22  		RemoveAdvancedSettingsEvasivePathMatch(ctx context.Context, params RemoveAdvancedSettingsEvasivePathMatchRequest) (*RemoveAdvancedSettingsEvasivePathMatchResponse, error)
    23  	}
    24  
    25  	// GetAdvancedSettingsEvasivePathMatchRequest is used to retrieve the EvasivePathMatch setting
    26  	GetAdvancedSettingsEvasivePathMatchRequest struct {
    27  		ConfigID int    `json:"-"`
    28  		Version  int    `json:"-"`
    29  		PolicyID string `json:"-"`
    30  	}
    31  
    32  	// GetAdvancedSettingsEvasivePathMatchResponse returns the EvasivePathMatch setting
    33  	GetAdvancedSettingsEvasivePathMatchResponse struct {
    34  		EnablePathMatch bool `json:"enablePathMatch"`
    35  	}
    36  
    37  	// UpdateAdvancedSettingsEvasivePathMatchRequest is used to update the EvasivePathMatch setting
    38  	UpdateAdvancedSettingsEvasivePathMatchRequest struct {
    39  		ConfigID        int    `json:"-"`
    40  		Version         int    `json:"-"`
    41  		PolicyID        string `json:"-"`
    42  		EnablePathMatch bool   `json:"enablePathMatch"`
    43  	}
    44  
    45  	// UpdateAdvancedSettingsEvasivePathMatchResponse returns the result of updating the EvasivePathMatch setting
    46  	UpdateAdvancedSettingsEvasivePathMatchResponse struct {
    47  		EnablePathMatch bool `json:"enablePathMatch"`
    48  	}
    49  
    50  	// RemoveAdvancedSettingsEvasivePathMatchRequest is used to clear the EvasivePathMatch setting
    51  	RemoveAdvancedSettingsEvasivePathMatchRequest struct {
    52  		ConfigID        int    `json:"-"`
    53  		Version         int    `json:"-"`
    54  		PolicyID        string `json:"-"`
    55  		EnablePathMatch bool   `json:"enablePathMatch"`
    56  	}
    57  
    58  	// RemoveAdvancedSettingsEvasivePathMatchResponse returns the result of clearing the EvasivePathMatch setting
    59  	RemoveAdvancedSettingsEvasivePathMatchResponse struct {
    60  		ConfigID        int    `json:"-"`
    61  		Version         int    `json:"-"`
    62  		PolicyID        string `json:"-"`
    63  		EnablePathMatch bool   `json:"enablePathMatch"`
    64  	}
    65  )
    66  
    67  // Validate validates GetAdvancedSettingssEvasivePathMatchRequest
    68  func (v GetAdvancedSettingsEvasivePathMatchRequest) Validate() error {
    69  	return validation.Errors{
    70  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    71  		"Version":  validation.Validate(v.Version, validation.Required),
    72  	}.Filter()
    73  }
    74  
    75  // Validate validates UpdateAdvancedSettingsEvasivePathMatchRequest
    76  func (v UpdateAdvancedSettingsEvasivePathMatchRequest) Validate() error {
    77  	return validation.Errors{
    78  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    79  		"Version":  validation.Validate(v.Version, validation.Required),
    80  	}.Filter()
    81  }
    82  
    83  // Validate validates UpdateAdvancedSettingsEvasivePathMatchRequest
    84  func (v RemoveAdvancedSettingsEvasivePathMatchRequest) Validate() error {
    85  	return validation.Errors{
    86  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    87  		"Version":  validation.Validate(v.Version, validation.Required),
    88  	}.Filter()
    89  }
    90  
    91  func (p *appsec) GetAdvancedSettingsEvasivePathMatch(ctx context.Context, params GetAdvancedSettingsEvasivePathMatchRequest) (*GetAdvancedSettingsEvasivePathMatchResponse, error) {
    92  	logger := p.Log(ctx)
    93  	logger.Debug("GetAdvancedSettingsEvasivePathMatch")
    94  
    95  	if err := params.Validate(); err != nil {
    96  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
    97  	}
    98  
    99  	var uri string
   100  	if params.PolicyID != "" {
   101  		uri = fmt.Sprintf(
   102  			"/appsec/v1/configs/%d/versions/%d/security-policies/%s/advanced-settings/evasive-path-match",
   103  			params.ConfigID,
   104  			params.Version,
   105  			params.PolicyID)
   106  	} else {
   107  		uri = fmt.Sprintf(
   108  			"/appsec/v1/configs/%d/versions/%d/advanced-settings/evasive-path-match",
   109  			params.ConfigID,
   110  			params.Version)
   111  	}
   112  
   113  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   114  	if err != nil {
   115  		return nil, fmt.Errorf("failed to create GetAdvancedSettingsEvasivePathMatch request: %w", err)
   116  	}
   117  
   118  	var result GetAdvancedSettingsEvasivePathMatchResponse
   119  	resp, err := p.Exec(req, &result)
   120  	if err != nil {
   121  		return nil, fmt.Errorf("get advanced settings evasive path match request failed: %w", err)
   122  	}
   123  
   124  	if resp.StatusCode != http.StatusOK {
   125  		return nil, p.Error(resp)
   126  	}
   127  
   128  	return &result, nil
   129  }
   130  
   131  func (p *appsec) UpdateAdvancedSettingsEvasivePathMatch(ctx context.Context, params UpdateAdvancedSettingsEvasivePathMatchRequest) (*UpdateAdvancedSettingsEvasivePathMatchResponse, error) {
   132  	logger := p.Log(ctx)
   133  	logger.Debug("UpdateAdvancedSettingsEvasivePathMatch")
   134  
   135  	if err := params.Validate(); err != nil {
   136  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   137  	}
   138  
   139  	var uri string
   140  	if params.PolicyID != "" {
   141  		uri = fmt.Sprintf(
   142  			"/appsec/v1/configs/%d/versions/%d/security-policies/%s/advanced-settings/evasive-path-match",
   143  			params.ConfigID,
   144  			params.Version,
   145  			params.PolicyID)
   146  	} else {
   147  		uri = fmt.Sprintf(
   148  			"/appsec/v1/configs/%d/versions/%d/advanced-settings/evasive-path-match",
   149  			params.ConfigID,
   150  			params.Version)
   151  	}
   152  
   153  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   154  	if err != nil {
   155  		return nil, fmt.Errorf("failed to create UpdateAdvancedSettingsEvasivePathMatch request: %w", err)
   156  	}
   157  	req.Header.Set("Content-Type", "application/json")
   158  
   159  	var result UpdateAdvancedSettingsEvasivePathMatchResponse
   160  	resp, err := p.Exec(req, &result, params)
   161  	if err != nil {
   162  		return nil, fmt.Errorf("update advanced settings evasive path match request failed: %w", err)
   163  	}
   164  
   165  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   166  		return nil, p.Error(resp)
   167  	}
   168  
   169  	return &result, nil
   170  }
   171  
   172  func (p *appsec) RemoveAdvancedSettingsEvasivePathMatch(ctx context.Context, params RemoveAdvancedSettingsEvasivePathMatchRequest) (*RemoveAdvancedSettingsEvasivePathMatchResponse, error) {
   173  	logger := p.Log(ctx)
   174  	logger.Debug("RemoveAdvancedSettingsEvasivePathMatch")
   175  
   176  	request := UpdateAdvancedSettingsEvasivePathMatchRequest{
   177  		ConfigID:        params.ConfigID,
   178  		Version:         params.Version,
   179  		PolicyID:        params.PolicyID,
   180  		EnablePathMatch: false,
   181  	}
   182  	_, err := p.UpdateAdvancedSettingsEvasivePathMatch(ctx, request)
   183  	if err != nil {
   184  		return nil, fmt.Errorf("remove advanced settings evasive path match request failed: %w", err)
   185  	}
   186  	response := RemoveAdvancedSettingsEvasivePathMatchResponse{
   187  		ConfigID:        params.ConfigID,
   188  		Version:         params.Version,
   189  		PolicyID:        params.PolicyID,
   190  		EnablePathMatch: false,
   191  	}
   192  	return &response, nil
   193  }