github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/security_policy_protections.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 PolicyProtections interface supports retrieving and updating protections for a configuration and policy. 13 PolicyProtections interface { 14 // GetPolicyProtections retrieves the current protection settings for a configuration and policy. 15 // 16 // See: https://techdocs.akamai.com/application-security/reference/get-policy-protections 17 GetPolicyProtections(ctx context.Context, params GetPolicyProtectionsRequest) (*PolicyProtectionsResponse, error) 18 19 // UpdatePolicyProtections updates the protection settings for a configuration and policy. 20 // 21 // See: https://techdocs.akamai.com/application-security/reference/put-policy-protections 22 UpdatePolicyProtections(ctx context.Context, params UpdatePolicyProtectionsRequest) (*PolicyProtectionsResponse, error) 23 24 // RemovePolicyProtections removes protection settings for a configuration and policy. 25 // 26 // See: https://techdocs.akamai.com/application-security/reference/put-policy-protections 27 // Deprecated: this method will be removed in a future release. Use UpdatePolicyProtections instead. 28 RemovePolicyProtections(ctx context.Context, params UpdatePolicyProtectionsRequest) (*PolicyProtectionsResponse, error) 29 } 30 31 // GetPolicyProtectionsRequest is used to retrieve the policy protection setting. 32 GetPolicyProtectionsRequest struct { 33 ConfigID int `json:"-"` 34 Version int `json:"-"` 35 PolicyID string `json:"-"` 36 } 37 38 // UpdatePolicyProtectionsRequest is used to modify the policy protection setting. 39 UpdatePolicyProtectionsRequest struct { 40 ConfigID int `json:"-"` 41 Version int `json:"-"` 42 PolicyID string `json:"-"` 43 ApplyAPIConstraints bool `json:"applyApiConstraints"` 44 ApplyApplicationLayerControls bool `json:"applyApplicationLayerControls"` 45 ApplyBotmanControls bool `json:"applyBotmanControls"` 46 ApplyNetworkLayerControls bool `json:"applyNetworkLayerControls"` 47 ApplyRateControls bool `json:"applyRateControls"` 48 ApplyReputationControls bool `json:"applyReputationControls"` 49 ApplySlowPostControls bool `json:"applySlowPostControls"` 50 ApplyMalwareControls bool `json:"applyMalwareControls"` 51 } 52 53 // RemovePolicyProtectionsRequest is used to remove the policy protection setting. 54 // Deprecated: this struct will be removed in a future release. 55 RemovePolicyProtectionsRequest struct { 56 ConfigID int `json:"-"` 57 Version int `json:"-"` 58 PolicyID string `json:"-"` 59 ApplyAPIConstraints bool `json:"applyApiConstraints"` 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 ApplyMalwareControls bool `json:"applyMalwareControls"` 67 } 68 69 // PolicyProtectionsResponse is returned from GetPolicyProtections, UpdatePolicyProtections, and RemovePolicyProtections. 70 PolicyProtectionsResponse struct { 71 ApplyAPIConstraints bool `json:"applyApiConstraints"` 72 ApplyApplicationLayerControls bool `json:"applyApplicationLayerControls"` 73 ApplyBotmanControls bool `json:"applyBotmanControls"` 74 ApplyMalwareControls bool `json:"applyMalwareControls"` 75 ApplyNetworkLayerControls bool `json:"applyNetworkLayerControls"` 76 ApplyRateControls bool `json:"applyRateControls"` 77 ApplyReputationControls bool `json:"applyReputationControls"` 78 ApplySlowPostControls bool `json:"applySlowPostControls"` 79 } 80 ) 81 82 // Validate validates a GetPolicyProtectionsRequest. 83 func (v GetPolicyProtectionsRequest) Validate() error { 84 return validation.Errors{ 85 "ConfigID": validation.Validate(v.ConfigID, validation.Required), 86 "Version": validation.Validate(v.Version, validation.Required), 87 "PolicyID": validation.Validate(v.PolicyID, validation.Required), 88 }.Filter() 89 } 90 91 // Validate validates an UpdatePolicyProtectionsRequest. 92 func (v UpdatePolicyProtectionsRequest) Validate() error { 93 return validation.Errors{ 94 "ConfigID": validation.Validate(v.ConfigID, validation.Required), 95 "Version": validation.Validate(v.Version, validation.Required), 96 "PolicyID": validation.Validate(v.PolicyID, validation.Required), 97 }.Filter() 98 } 99 100 // Validate validates a RemovePolicyProtectionsRequest. 101 func (v RemovePolicyProtectionsRequest) Validate() error { 102 return validation.Errors{ 103 "ConfigID": validation.Validate(v.ConfigID, validation.Required), 104 "Version": validation.Validate(v.Version, validation.Required), 105 "PolicyID": validation.Validate(v.PolicyID, validation.Required), 106 }.Filter() 107 } 108 109 func (p *appsec) GetPolicyProtections(ctx context.Context, params GetPolicyProtectionsRequest) (*PolicyProtectionsResponse, error) { 110 logger := p.Log(ctx) 111 logger.Debug("GetPolicyProtections") 112 113 if err := params.Validate(); err != nil { 114 return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error()) 115 } 116 117 uri := fmt.Sprintf( 118 "/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections", 119 params.ConfigID, 120 params.Version, 121 params.PolicyID) 122 123 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) 124 if err != nil { 125 return nil, fmt.Errorf("failed to create GetPolicyProtections request: %w", err) 126 } 127 128 var result PolicyProtectionsResponse 129 resp, err := p.Exec(req, &result) 130 if err != nil { 131 return nil, fmt.Errorf("get policy protections request failed: %w", err) 132 } 133 if resp.StatusCode != http.StatusOK { 134 return nil, p.Error(resp) 135 } 136 137 return &result, nil 138 } 139 140 func (p *appsec) UpdatePolicyProtections(ctx context.Context, params UpdatePolicyProtectionsRequest) (*PolicyProtectionsResponse, error) { 141 logger := p.Log(ctx) 142 logger.Debug("UpdatePolicyProtections") 143 144 if err := params.Validate(); err != nil { 145 return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error()) 146 } 147 148 uri := fmt.Sprintf( 149 "/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections", 150 params.ConfigID, 151 params.Version, 152 params.PolicyID, 153 ) 154 155 req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil) 156 if err != nil { 157 return nil, fmt.Errorf("failed to create UpdatePolicyProtections request: %w", err) 158 } 159 160 var result PolicyProtectionsResponse 161 resp, err := p.Exec(req, &result, params) 162 if err != nil { 163 return nil, fmt.Errorf("update policy protections request failed: %w", err) 164 } 165 if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { 166 return nil, p.Error(resp) 167 } 168 169 return &result, nil 170 } 171 172 func (p *appsec) RemovePolicyProtections(ctx context.Context, params UpdatePolicyProtectionsRequest) (*PolicyProtectionsResponse, error) { 173 logger := p.Log(ctx) 174 logger.Debug("RemovePolicyProtections") 175 176 if err := params.Validate(); err != nil { 177 return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error()) 178 } 179 180 uri := fmt.Sprintf( 181 "/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections", 182 params.ConfigID, 183 params.Version, 184 params.PolicyID, 185 ) 186 187 req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil) 188 if err != nil { 189 return nil, fmt.Errorf("failed to create RemovePolicyProtections request: %w", err) 190 } 191 192 var result PolicyProtectionsResponse 193 resp, err := p.Exec(req, &result, params) 194 if err != nil { 195 return nil, fmt.Errorf("remove policy protections request failed: %w", err) 196 } 197 if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { 198 return nil, p.Error(resp) 199 } 200 201 return &result, nil 202 }