github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/threat_intel.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 ThreatIntel interface supports retrieving and modifying the operational settings for adaptive intelligence.
    13  	ThreatIntel interface {
    14  		// GetThreatIntel retrieves the current threat intel settings.
    15  		//
    16  		// See: https://techdocs.akamai.com/application-security/reference/get-rules-threat-intel-1
    17  		GetThreatIntel(ctx context.Context, params GetThreatIntelRequest) (*GetThreatIntelResponse, error)
    18  
    19  		// UpdateThreatIntel modifies the current threat intel settings.
    20  		//
    21  		// See: https://techdocs.akamai.com/application-security/reference/put-rules-threat-intel-1
    22  		UpdateThreatIntel(ctx context.Context, params UpdateThreatIntelRequest) (*UpdateThreatIntelResponse, error)
    23  	}
    24  
    25  	// GetThreatIntelRequest is used to retrieve the threat intel settings.
    26  	GetThreatIntelRequest struct {
    27  		ConfigID int    `json:"-"`
    28  		Version  int    `json:"-"`
    29  		PolicyID string `json:"-"`
    30  	}
    31  
    32  	// GetThreatIntelResponse is returned from a call to GetThreatIntel.
    33  	GetThreatIntelResponse struct {
    34  		ThreatIntel string `json:"threatIntel,omitempty"`
    35  	}
    36  
    37  	// UpdateThreatIntelRequest is used to update the threat intel settings.
    38  	UpdateThreatIntelRequest struct {
    39  		ConfigID    int    `json:"-"`
    40  		Version     int    `json:"-"`
    41  		PolicyID    string `json:"-"`
    42  		ThreatIntel string `json:"threatIntel"`
    43  	}
    44  
    45  	// UpdateThreatIntelResponse is returned from a call to UpdateThreatIntel.
    46  	UpdateThreatIntelResponse struct {
    47  		ThreatIntel string `json:"threatIntel,omitempty"`
    48  	}
    49  )
    50  
    51  // Validate validates a GetAttackGroupConditionExceptionRequest.
    52  func (v GetThreatIntelRequest) Validate() error {
    53  	return validation.Errors{
    54  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    55  		"Version":  validation.Validate(v.Version, validation.Required),
    56  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
    57  	}.Filter()
    58  }
    59  
    60  // Validate validates an UpdateAttackGroupConditionExceptionRequest.
    61  func (v UpdateThreatIntelRequest) Validate() error {
    62  	return validation.Errors{
    63  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    64  		"Version":  validation.Validate(v.Version, validation.Required),
    65  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
    66  	}.Filter()
    67  }
    68  
    69  func (p *appsec) GetThreatIntel(ctx context.Context, params GetThreatIntelRequest) (*GetThreatIntelResponse, error) {
    70  	logger := p.Log(ctx)
    71  	logger.Debug("GetThreatIntel")
    72  
    73  	if err := params.Validate(); err != nil {
    74  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
    75  	}
    76  
    77  	uri := fmt.Sprintf(
    78  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/threat-intel",
    79  		params.ConfigID,
    80  		params.Version,
    81  		params.PolicyID)
    82  
    83  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
    84  	if err != nil {
    85  		return nil, fmt.Errorf("failed to create GetThreatIntel request: %w", err)
    86  	}
    87  
    88  	var result GetThreatIntelResponse
    89  	resp, err := p.Exec(req, &result)
    90  	if err != nil {
    91  		return nil, fmt.Errorf("get threat intel request failed: %w", err)
    92  	}
    93  	if resp.StatusCode != http.StatusOK {
    94  		return nil, p.Error(resp)
    95  	}
    96  
    97  	return &result, nil
    98  }
    99  
   100  func (p *appsec) UpdateThreatIntel(ctx context.Context, params UpdateThreatIntelRequest) (*UpdateThreatIntelResponse, error) {
   101  	logger := p.Log(ctx)
   102  	logger.Debug("UpdateThreatIntel")
   103  
   104  	if err := params.Validate(); err != nil {
   105  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   106  	}
   107  
   108  	uri := fmt.Sprintf(
   109  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/threat-intel",
   110  		params.ConfigID,
   111  		params.Version,
   112  		params.PolicyID,
   113  	)
   114  
   115  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   116  	if err != nil {
   117  		return nil, fmt.Errorf("failed to create UpdateThreatIntel request: %w", err)
   118  	}
   119  
   120  	var result UpdateThreatIntelResponse
   121  	resp, err := p.Exec(req, &result, params)
   122  	if err != nil {
   123  		return nil, fmt.Errorf("update threat intel request failed: %w", err)
   124  	}
   125  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   126  		return nil, p.Error(resp)
   127  	}
   128  
   129  	return &result, nil
   130  }