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