github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/appsec/custom_deny.go (about)

     1  package appsec
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/http"
     8  	"reflect"
     9  	"strconv"
    10  
    11  	validation "github.com/go-ozzo/ozzo-validation/v4"
    12  )
    13  
    14  type (
    15  	// The CustomDeny interface supports creating, retrievinfg, modifying and removing custom deny actions
    16  	// for a configuration.
    17  	// https://developer.akamai.com/api/cloud_security/application_security/v1.html#customdeny
    18  	//
    19  	CustomDeny interface {
    20  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#getcustomdeny
    21  		GetCustomDenyList(ctx context.Context, params GetCustomDenyListRequest) (*GetCustomDenyListResponse, error)
    22  
    23  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#getcustomdenyaction
    24  		GetCustomDeny(ctx context.Context, params GetCustomDenyRequest) (*GetCustomDenyResponse, error)
    25  
    26  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#postcustomdeny
    27  		CreateCustomDeny(ctx context.Context, params CreateCustomDenyRequest) (*CreateCustomDenyResponse, error)
    28  
    29  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#putcustomdenyaction
    30  		UpdateCustomDeny(ctx context.Context, params UpdateCustomDenyRequest) (*UpdateCustomDenyResponse, error)
    31  
    32  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#deletecustomdenyaction
    33  		RemoveCustomDeny(ctx context.Context, params RemoveCustomDenyRequest) (*RemoveCustomDenyResponse, error)
    34  	}
    35  
    36  	customDenyID string
    37  
    38  	// GetCustomDenyListRequest is used to retrieve the custom deny actions for a configuration.
    39  	GetCustomDenyListRequest struct {
    40  		ConfigID int    `json:"configId"`
    41  		Version  int    `json:"version"`
    42  		ID       string `json:"id,omitempty"`
    43  	}
    44  
    45  	// GetCustomDenyListResponse is returned from a call to GetCustomDenyList.
    46  	GetCustomDenyListResponse struct {
    47  		CustomDenyList []struct {
    48  			Description string       `json:"description,omitempty"`
    49  			Name        string       `json:"name"`
    50  			ID          customDenyID `json:"id"`
    51  			Parameters  []struct {
    52  				DisplayName string `json:"-"`
    53  				Name        string `json:"name"`
    54  				Value       string `json:"value"`
    55  			} `json:"parameters"`
    56  		} `json:"customDenyList"`
    57  	}
    58  
    59  	// GetCustomDenyRequest is used to retrieve a specific custom deny action.
    60  	GetCustomDenyRequest struct {
    61  		ConfigID int    `json:"configId"`
    62  		Version  int    `json:"version"`
    63  		ID       string `json:"id,omitempty"`
    64  	}
    65  
    66  	// GetCustomDenyResponse is returned from a call to GetCustomDeny.
    67  	GetCustomDenyResponse struct {
    68  		Description string       `json:"description,omitempty"`
    69  		Name        string       `json:"name"`
    70  		ID          customDenyID `json:"-"`
    71  		Parameters  []struct {
    72  			DisplayName string `json:"-"`
    73  			Name        string `json:"name"`
    74  			Value       string `json:"value"`
    75  		} `json:"parameters"`
    76  	}
    77  
    78  	// CreateCustomDenyRequest is used to create a new custom deny action for a specific configuration.
    79  	CreateCustomDenyRequest struct {
    80  		ConfigID       int             `json:"-"`
    81  		Version        int             `json:"-"`
    82  		JsonPayloadRaw json.RawMessage `json:"-"`
    83  	}
    84  
    85  	// CreateCustomDenyResponse is returned from a call to CreateCustomDeny.
    86  	CreateCustomDenyResponse struct {
    87  		Description string       `json:"description,omitempty"`
    88  		Name        string       `json:"name"`
    89  		ID          customDenyID `json:"id"`
    90  		Parameters  []struct {
    91  			DisplayName string `json:"-"`
    92  			Name        string `json:"name"`
    93  			Value       string `json:"value"`
    94  		} `json:"parameters"`
    95  	}
    96  
    97  	// UpdateCustomDenyRequest is used to details for a specific custom deny action.
    98  	UpdateCustomDenyRequest struct {
    99  		ConfigID       int             `json:"-"`
   100  		Version        int             `json:"-"`
   101  		ID             string          `json:"id"`
   102  		JsonPayloadRaw json.RawMessage `json:"-"`
   103  	}
   104  
   105  	// UpdateCustomDenyResponse is returned from a call to UpdateCustomDeny.
   106  	UpdateCustomDenyResponse struct {
   107  		Description string       `json:"description,omitempty"`
   108  		Name        string       `json:"name"`
   109  		ID          customDenyID `json:"-"`
   110  		Parameters  []struct {
   111  			DisplayName string `json:"-"`
   112  			Name        string `json:"name"`
   113  			Value       string `json:"value"`
   114  		} `json:"parameters"`
   115  	}
   116  
   117  	// RemoveCustomDenyRequest is used to remove an existing custom deny action.
   118  	RemoveCustomDenyRequest struct {
   119  		ConfigID int    `json:"-"`
   120  		Version  int    `json:"-"`
   121  		ID       string `json:"id,omitempty"`
   122  	}
   123  
   124  	// RemoveCustomDenyResponse is returned from a call to RemoveCustomDeny.
   125  	RemoveCustomDenyResponse struct {
   126  		Empty string `json:"-"`
   127  	}
   128  )
   129  
   130  // UnmarshalJSON reads a customDenyID struct from its data argument.
   131  func (c *customDenyID) UnmarshalJSON(data []byte) error {
   132  	var nums interface{}
   133  	err := json.Unmarshal(data, &nums)
   134  	if err != nil {
   135  		return err
   136  	}
   137  
   138  	items := reflect.ValueOf(nums)
   139  	switch items.Kind() {
   140  	case reflect.String:
   141  		*c = customDenyID(nums.(string))
   142  	case reflect.Int:
   143  
   144  		*c = customDenyID(strconv.Itoa(nums.(int)))
   145  
   146  	}
   147  	return nil
   148  }
   149  
   150  // Validate validates a GetCustomDenyRequest.
   151  func (v GetCustomDenyRequest) Validate() error {
   152  	return validation.Errors{
   153  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   154  		"Version":  validation.Validate(v.Version, validation.Required),
   155  		"ID":       validation.Validate(v.ID, validation.Required),
   156  	}.Filter()
   157  }
   158  
   159  // Validate validates a GetCustomDenysRequest.
   160  func (v GetCustomDenyListRequest) Validate() error {
   161  	return validation.Errors{
   162  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   163  		"Version":  validation.Validate(v.Version, validation.Required),
   164  	}.Filter()
   165  }
   166  
   167  // Validate validates a CreateCustomDenyRequest.
   168  func (v CreateCustomDenyRequest) Validate() error {
   169  	return validation.Errors{
   170  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   171  		"Version":  validation.Validate(v.Version, validation.Required),
   172  	}.Filter()
   173  }
   174  
   175  // Validate validates an UpdateCustomDenyRequest.
   176  func (v UpdateCustomDenyRequest) Validate() error {
   177  	return validation.Errors{
   178  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   179  		"Version":  validation.Validate(v.Version, validation.Required),
   180  		"ID":       validation.Validate(v.ID, validation.Required),
   181  	}.Filter()
   182  }
   183  
   184  // Validate validates a RemoveCustomDenyRequest.
   185  func (v RemoveCustomDenyRequest) Validate() error {
   186  	return validation.Errors{
   187  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   188  		"Version":  validation.Validate(v.Version, validation.Required),
   189  		"ID":       validation.Validate(v.ID, validation.Required),
   190  	}.Filter()
   191  }
   192  
   193  func (p *appsec) GetCustomDeny(ctx context.Context, params GetCustomDenyRequest) (*GetCustomDenyResponse, error) {
   194  	logger := p.Log(ctx)
   195  	logger.Debug("GetCustomDeny")
   196  
   197  	if err := params.Validate(); err != nil {
   198  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   199  	}
   200  
   201  	uri := fmt.Sprintf(
   202  		"/appsec/v1/configs/%d/versions/%d/custom-deny/%s",
   203  		params.ConfigID,
   204  		params.Version,
   205  		params.ID)
   206  
   207  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   208  	if err != nil {
   209  		return nil, fmt.Errorf("failed to create GetCustomDeny request: %w", err)
   210  	}
   211  
   212  	var result GetCustomDenyResponse
   213  	resp, err := p.Exec(req, &result)
   214  	if err != nil {
   215  		return nil, fmt.Errorf("get custom deny request failed: %w", err)
   216  	}
   217  	if resp.StatusCode != http.StatusOK {
   218  		return nil, p.Error(resp)
   219  	}
   220  
   221  	return &result, nil
   222  }
   223  
   224  func (p *appsec) GetCustomDenyList(ctx context.Context, params GetCustomDenyListRequest) (*GetCustomDenyListResponse, error) {
   225  	logger := p.Log(ctx)
   226  	logger.Debug("GetCustomDenyList")
   227  
   228  	if err := params.Validate(); err != nil {
   229  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   230  	}
   231  
   232  	uri := fmt.Sprintf(
   233  		"/appsec/v1/configs/%d/versions/%d/custom-deny",
   234  		params.ConfigID,
   235  		params.Version,
   236  	)
   237  
   238  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   239  	if err != nil {
   240  		return nil, fmt.Errorf("failed to create GetCustomDenyList request: %w", err)
   241  	}
   242  
   243  	var result GetCustomDenyListResponse
   244  	resp, err := p.Exec(req, &result)
   245  	if err != nil {
   246  		return nil, fmt.Errorf("get custom deny list request failed: %w", err)
   247  	}
   248  	if resp.StatusCode != http.StatusOK {
   249  		return nil, p.Error(resp)
   250  	}
   251  
   252  	if params.ID != "" {
   253  		var filteredResult GetCustomDenyListResponse
   254  		for _, val := range result.CustomDenyList {
   255  			if string(val.ID) == params.ID {
   256  				filteredResult.CustomDenyList = append(filteredResult.CustomDenyList, val)
   257  			}
   258  		}
   259  		return &filteredResult, nil
   260  	}
   261  
   262  	return &result, nil
   263  }
   264  
   265  func (p *appsec) UpdateCustomDeny(ctx context.Context, params UpdateCustomDenyRequest) (*UpdateCustomDenyResponse, error) {
   266  	logger := p.Log(ctx)
   267  	logger.Debug("UpdateCustomDeny")
   268  
   269  	if err := params.Validate(); err != nil {
   270  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   271  	}
   272  
   273  	uri := fmt.Sprintf(
   274  		"/appsec/v1/configs/%d/versions/%d/custom-deny/%s",
   275  		params.ConfigID,
   276  		params.Version,
   277  		params.ID,
   278  	)
   279  
   280  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   281  	if err != nil {
   282  		return nil, fmt.Errorf("failed to create UpdateCustomDeny request: %w", err)
   283  	}
   284  
   285  	var result UpdateCustomDenyResponse
   286  	req.Header.Set("Content-Type", "application/json")
   287  	resp, err := p.Exec(req, &result, params.JsonPayloadRaw)
   288  	if err != nil {
   289  		return nil, fmt.Errorf("update custom deny request failed: %w", err)
   290  	}
   291  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   292  		return nil, p.Error(resp)
   293  	}
   294  
   295  	return &result, nil
   296  }
   297  
   298  func (p *appsec) CreateCustomDeny(ctx context.Context, params CreateCustomDenyRequest) (*CreateCustomDenyResponse, error) {
   299  	logger := p.Log(ctx)
   300  	logger.Debug("CreateCustomDeny")
   301  
   302  	if err := params.Validate(); err != nil {
   303  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   304  	}
   305  
   306  	uri := fmt.Sprintf(
   307  		"/appsec/v1/configs/%d/versions/%d/custom-deny",
   308  		params.ConfigID,
   309  		params.Version,
   310  	)
   311  
   312  	req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, nil)
   313  	if err != nil {
   314  		return nil, fmt.Errorf("failed to create CreateCustomDeny request: %w", err)
   315  	}
   316  
   317  	var result CreateCustomDenyResponse
   318  	req.Header.Set("Content-Type", "application/json")
   319  	resp, err := p.Exec(req, &result, params.JsonPayloadRaw)
   320  	if err != nil {
   321  		return nil, fmt.Errorf("create custom deny request failed: %w", err)
   322  	}
   323  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   324  		return nil, p.Error(resp)
   325  	}
   326  
   327  	return &result, nil
   328  }
   329  
   330  func (p *appsec) RemoveCustomDeny(ctx context.Context, params RemoveCustomDenyRequest) (*RemoveCustomDenyResponse, error) {
   331  	logger := p.Log(ctx)
   332  	logger.Debug("RemoveCustomDeny")
   333  
   334  	if err := params.Validate(); err != nil {
   335  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   336  	}
   337  
   338  	uri := fmt.Sprintf("/appsec/v1/configs/%d/versions/%d/custom-deny/%s", params.ConfigID, params.Version, params.ID)
   339  	req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uri, nil)
   340  	if err != nil {
   341  		return nil, fmt.Errorf("failed to create RemoveCustomDeny request: %w", err)
   342  	}
   343  
   344  	var result RemoveCustomDenyResponse
   345  	resp, err := p.Exec(req, &result)
   346  	if err != nil {
   347  		return nil, fmt.Errorf("remove custom deny request failed: %w", err)
   348  	}
   349  	if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusOK {
   350  		return nil, p.Error(resp)
   351  	}
   352  
   353  	return &result, nil
   354  }