github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/api_hostname_coverage_match_targets.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 ApiHostnameCoverageMatchTargets interface supports retrieving the API and website
    13  	// match targets that protect a hostname.
    14  	ApiHostnameCoverageMatchTargets interface {
    15  		// GetApiHostnameCoverageMatchTargets lists the API and website match targets that protect a hostname.
    16  		//
    17  		// See: https://techdocs.akamai.com/application-security/reference/get-coverage-match-targets
    18  		GetApiHostnameCoverageMatchTargets(ctx context.Context, params GetApiHostnameCoverageMatchTargetsRequest) (*GetApiHostnameCoverageMatchTargetsResponse, error)
    19  	}
    20  
    21  	// GetApiHostnameCoverageMatchTargetsRequest is used to retrieve the API and website match targets that protect a hostname.
    22  	GetApiHostnameCoverageMatchTargetsRequest struct {
    23  		ConfigID int    `json:"-"`
    24  		Version  int    `json:"-"`
    25  		Hostname string `json:"-"`
    26  	}
    27  
    28  	// GetApiHostnameCoverageMatchTargetsResponse is returned from a call to GetApiHostnameCoverageMatchTargets.
    29  	GetApiHostnameCoverageMatchTargetsResponse struct {
    30  		MatchTargets struct {
    31  			WebsiteTargets []struct {
    32  				Type                         string                                                `json:"type"`
    33  				BypassNetworkLists           *HostnameCoverageMatchTargetBypassNetworkLists        `json:"bypassNetworkLists,omitempty"`
    34  				ConfigID                     int                                                   `json:"configId"`
    35  				ConfigVersion                int                                                   `json:"configVersion"`
    36  				DefaultFile                  string                                                `json:"defaultFile"`
    37  				EffectiveSecurityControls    *HostnameCoverageMatchTargetEffectiveSecurityControls `json:"effectiveSecurityControls,omitempty"`
    38  				FilePaths                    []string                                              `json:"filePaths"`
    39  				Hostnames                    []string                                              `json:"hostnames"`
    40  				IsNegativeFileExtensionMatch bool                                                  `json:"isNegativeFileExtensionMatch"`
    41  				IsNegativePathMatch          bool                                                  `json:"isNegativePathMatch"`
    42  				SecurityPolicy               struct {
    43  					PolicyID string `json:"policyId"`
    44  				} `json:"securityPolicy"`
    45  				Sequence int `json:"sequence"`
    46  				TargetID int `json:"targetId"`
    47  			} `json:"websiteTargets"`
    48  			APITargets []interface{} `json:"apiTargets"`
    49  		} `json:"matchTargets"`
    50  	}
    51  
    52  	// HostnameCoverageMatchTargetBypassNetworkLists describes a network list included in the list of bypass network lists.
    53  	HostnameCoverageMatchTargetBypassNetworkLists []struct {
    54  		ID   string `json:"id"`
    55  		Name string `json:"name"`
    56  	}
    57  
    58  	// HostnameCoverageMatchTargetEffectiveSecurityControls describes the effective security controls for a website target.
    59  	HostnameCoverageMatchTargetEffectiveSecurityControls struct {
    60  		ApplyApplicationLayerControls bool `json:"applyApplicationLayerControls"`
    61  		ApplyBotmanControls           bool `json:"applyBotmanControls"`
    62  		ApplyNetworkLayerControls     bool `json:"applyNetworkLayerControls"`
    63  		ApplyRateControls             bool `json:"applyRateControls"`
    64  		ApplyReputationControls       bool `json:"applyReputationControls"`
    65  		ApplySlowPostControls         bool `json:"applySlowPostControls"`
    66  	}
    67  )
    68  
    69  // Validate validates a GetApiHostnameCoverageMatchTargetsRequest.
    70  func (v GetApiHostnameCoverageMatchTargetsRequest) Validate() error {
    71  	return validation.Errors{
    72  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    73  		"Version":  validation.Validate(v.Version, validation.Required),
    74  	}.Filter()
    75  }
    76  
    77  func (p *appsec) GetApiHostnameCoverageMatchTargets(ctx context.Context, params GetApiHostnameCoverageMatchTargetsRequest) (*GetApiHostnameCoverageMatchTargetsResponse, error) {
    78  	logger := p.Log(ctx)
    79  	logger.Debug("GetApiHostnameCoverageMatchTargets")
    80  
    81  	if err := params.Validate(); err != nil {
    82  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
    83  	}
    84  
    85  	uri := fmt.Sprintf(
    86  		"/appsec/v1/configs/%d/versions/%d/hostname-coverage/match-targets?hostname=%s",
    87  		params.ConfigID,
    88  		params.Version,
    89  		params.Hostname)
    90  
    91  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
    92  	if err != nil {
    93  		return nil, fmt.Errorf("failed to create GetApiHostnameCoverageMatchTargets request: %w", err)
    94  	}
    95  
    96  	var result GetApiHostnameCoverageMatchTargetsResponse
    97  	resp, err := p.Exec(req, &result)
    98  	if err != nil {
    99  		return nil, fmt.Errorf("get API hostname coverage match targets request failed: %w", err)
   100  	}
   101  	if resp.StatusCode != http.StatusOK {
   102  		return nil, p.Error(resp)
   103  	}
   104  
   105  	return &result, nil
   106  }