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