github.com/akamai/AkamaiOPEN-edgegrid-golang/v4@v4.1.0/pkg/appsec/api_constraints_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 ApiConstraintsProtection interface supports retrieving and updating API request constraints protection for a configuration and policy. 13 ApiConstraintsProtection interface { 14 // GetAPIConstraintsProtection retrieves the current API constraints protection setting for a configuration and policy. 15 // 16 // See: https://techdocs.akamai.com/application-security/reference/get-policy-protections 17 GetAPIConstraintsProtection(ctx context.Context, params GetAPIConstraintsProtectionRequest) (*GetAPIConstraintsProtectionResponse, error) 18 19 // UpdateAPIConstraintsProtection updates the API constraints protection setting for a configuration and policy. 20 // 21 // See: https://techdocs.akamai.com/application-security/reference/put-policy-protections 22 UpdateAPIConstraintsProtection(ctx context.Context, params UpdateAPIConstraintsProtectionRequest) (*UpdateAPIConstraintsProtectionResponse, error) 23 } 24 25 // GetAPIConstraintsProtectionRequest is used to retrieve the API constraints protection setting. 26 GetAPIConstraintsProtectionRequest struct { 27 ConfigID int `json:"-"` 28 Version int `json:"-"` 29 PolicyID string `json:"-"` 30 ApplyAPIConstraints bool `json:"applyApiConstraints"` 31 } 32 33 // ProtectionsResponse is returned from a call to GetAPIConstraintsProtection and similar security policy protection requests. 34 ProtectionsResponse struct { 35 ApplyAPIConstraints bool `json:"applyApiConstraints,omitempty"` 36 ApplyApplicationLayerControls bool `json:"applyApplicationLayerControls,omitempty"` 37 ApplyBotmanControls bool `json:"applyBotmanControls,omitempty"` 38 ApplyMalwareControls bool `json:"applyMalwareControls,omitempty"` 39 ApplyNetworkLayerControls bool `json:"applyNetworkLayerControls,omitempty"` 40 ApplyRateControls bool `json:"applyRateControls,omitempty"` 41 ApplyReputationControls bool `json:"applyReputationControls,omitempty"` 42 ApplySlowPostControls bool `json:"applySlowPostControls,omitempty"` 43 } 44 45 // GetAPIConstraintsProtectionResponse contains the status of various protection flags assigned to a security policy. 46 GetAPIConstraintsProtectionResponse ProtectionsResponse 47 48 // UpdateAPIConstraintsProtectionRequest is used to modify the API constraints protection setting. 49 UpdateAPIConstraintsProtectionRequest struct { 50 ConfigID int `json:"-"` 51 Version int `json:"-"` 52 PolicyID string `json:"-"` 53 ApplyAPIConstraints bool `json:"applyApiConstraints"` 54 } 55 56 // UpdateAPIConstraintsProtectionResponse is returned from a call to UpdateAPIConstraintsProtection. 57 UpdateAPIConstraintsProtectionResponse ProtectionsResponse 58 ) 59 60 // Validate validates a GetAPIConstraintsProtectionRequest. 61 func (v GetAPIConstraintsProtectionRequest) 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 // Validate validates an UpdateAPIConstraintsProtectionRequest. 70 func (v UpdateAPIConstraintsProtectionRequest) Validate() error { 71 return validation.Errors{ 72 "ConfigID": validation.Validate(v.ConfigID, validation.Required), 73 "Version": validation.Validate(v.Version, validation.Required), 74 "PolicyID": validation.Validate(v.PolicyID, validation.Required), 75 }.Filter() 76 } 77 78 func (p *appsec) GetAPIConstraintsProtection(ctx context.Context, params GetAPIConstraintsProtectionRequest) (*GetAPIConstraintsProtectionResponse, error) { 79 logger := p.Log(ctx) 80 logger.Debug("GetAPIConstraintsProtection") 81 82 if err := params.Validate(); err != nil { 83 return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error()) 84 } 85 86 uri := fmt.Sprintf( 87 "/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections", 88 params.ConfigID, 89 params.Version, 90 params.PolicyID) 91 92 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) 93 if err != nil { 94 return nil, fmt.Errorf("failed to create GetAPIConstraintsProtection request: %w", err) 95 } 96 97 var result GetAPIConstraintsProtectionResponse 98 resp, err := p.Exec(req, &result) 99 if err != nil { 100 return nil, fmt.Errorf("get API constraints protection request failed: %w", err) 101 } 102 103 if resp.StatusCode != http.StatusOK { 104 return nil, p.Error(resp) 105 } 106 107 return &result, nil 108 } 109 110 func (p *appsec) UpdateAPIConstraintsProtection(ctx context.Context, params UpdateAPIConstraintsProtectionRequest) (*UpdateAPIConstraintsProtectionResponse, error) { 111 logger := p.Log(ctx) 112 logger.Debug("UpdateAPIConstraintsProtection") 113 114 if err := params.Validate(); err != nil { 115 return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error()) 116 } 117 118 uri := fmt.Sprintf( 119 "/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections", 120 params.ConfigID, 121 params.Version, 122 params.PolicyID, 123 ) 124 125 req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil) 126 if err != nil { 127 return nil, fmt.Errorf("failed to create UpdateAPIConstraintsProtection request: %w", err) 128 } 129 130 var result UpdateAPIConstraintsProtectionResponse 131 resp, err := p.Exec(req, &result, params) 132 if err != nil { 133 return nil, fmt.Errorf("update API constraints protection request failed: %w", err) 134 } 135 136 if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { 137 return nil, p.Error(resp) 138 } 139 140 return &result, nil 141 }