github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/malware_policy_action.go (about) 1 package appsec 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "net/http" 8 9 validation "github.com/go-ozzo/ozzo-validation/v4" 10 ) 11 12 type ( 13 // The MalwarePolicyAction interface supports retrieving and modifying the action associated with 14 // a specified malware policy, or with all malware policies in a security policy. 15 MalwarePolicyAction interface { 16 // GetMalwarePolicyActions retrieves the actions for a specific malware protection policy. 17 // 18 // See: https://techdocs.akamai.com/application-security/reference/get-malware-policies-actions 19 GetMalwarePolicyActions(ctx context.Context, params GetMalwarePolicyActionsRequest) (*GetMalwarePolicyActionsResponse, error) 20 21 // UpdateMalwarePolicyAction modifies the actions for a specific malware protection policy. 22 // 23 // See: https://techdocs.akamai.com/application-security/reference/put-malware-policy-action 24 UpdateMalwarePolicyAction(ctx context.Context, params UpdateMalwarePolicyActionRequest) (*UpdateMalwarePolicyActionResponse, error) 25 26 // UpdateMalwarePolicyActions is for Akamai internal use only. 27 UpdateMalwarePolicyActions(ctx context.Context, params UpdateMalwarePolicyActionsRequest) (*UpdateMalwarePolicyActionsResponse, error) 28 } 29 30 // GetMalwarePolicyActionsRequest is used to retrieve a configuration's malware policies and their associated actions. 31 GetMalwarePolicyActionsRequest struct { 32 ConfigID int `json:"configID"` 33 Version int `json:"version"` 34 PolicyID string `json:"policyID"` 35 MalwarePolicyID int `json:"id"` 36 } 37 38 // MalwarePolicyActionBody defines the actions for a specific malware policy. 39 MalwarePolicyActionBody struct { 40 MalwarePolicyID int `json:"id"` 41 Action string `json:"action"` 42 UnscannedAction string `json:"unscannedAction"` 43 } 44 45 // GetMalwarePolicyActionsResponse is returned from a call to GetMalwarePolicyActions. 46 GetMalwarePolicyActionsResponse struct { 47 MalwarePolicyActions []MalwarePolicyActionBody `json:"malwarePolicyActions"` 48 } 49 50 // UpdateMalwarePolicyActionRequest is used to update the actions for a malware policy. 51 UpdateMalwarePolicyActionRequest struct { 52 ConfigID int `json:"configID"` 53 Version int `json:"version"` 54 PolicyID string `json:"policyID"` 55 MalwarePolicyID int `json:"id"` 56 Action string `json:"action"` 57 UnscannedAction string `json:"unscannedAction"` 58 } 59 60 // UpdateMalwarePolicyActionResponse is returned from a call to UpdateMalwarePolicy. 61 UpdateMalwarePolicyActionResponse struct { 62 Action string `json:"action"` 63 UnscannedAction string `json:"unscannedAction"` 64 } 65 66 // UpdateMalwarePolicyActionsRequest is used to update the actions for multiple malware policies. 67 UpdateMalwarePolicyActionsRequest struct { 68 ConfigID int 69 Version int 70 PolicyID string 71 MalwarePolicyActions json.RawMessage `json:"-"` 72 } 73 74 // UpdateMalwarePolicyActionsResponse is returned from a call to UpdateMalwarePolicyActions. 75 UpdateMalwarePolicyActionsResponse GetMalwarePolicyActionsResponse 76 ) 77 78 // Validate validates a GetMalwarePolicyActionsRequest. 79 func (v GetMalwarePolicyActionsRequest) Validate() error { 80 return validation.Errors{ 81 "ConfigID": validation.Validate(v.ConfigID, validation.Required), 82 "Version": validation.Validate(v.Version, validation.Required), 83 "PolicyID": validation.Validate(v.PolicyID, validation.Required), 84 }.Filter() 85 } 86 87 // Validate validates an UpdateMalwarePolicyActionRequest. 88 func (v UpdateMalwarePolicyActionRequest) Validate() error { 89 return validation.Errors{ 90 "ConfigID": validation.Validate(v.ConfigID, validation.Required), 91 "Version": validation.Validate(v.Version, validation.Required), 92 "PolicyID": validation.Validate(v.PolicyID, validation.Required), 93 "MalwarePolicyID": validation.Validate(v.MalwarePolicyID, validation.Required), 94 "Action": validation.Validate(v.Action, validation.Required), 95 "UnscannedAction": validation.Validate(v.UnscannedAction, validation.Required), 96 }.Filter() 97 } 98 99 // Validate validates an UpdateMalwarePolicyActionsRequest. 100 func (v UpdateMalwarePolicyActionsRequest) Validate() error { 101 return validation.Errors{ 102 "ConfigID": validation.Validate(v.ConfigID, validation.Required), 103 "Version": validation.Validate(v.Version, validation.Required), 104 "PolicyID": validation.Validate(v.PolicyID, validation.Required), 105 "MalwarePolicyActions": validation.Validate(v.MalwarePolicyActions, validation.Required), 106 }.Filter() 107 } 108 109 func (p *appsec) GetMalwarePolicyActions(ctx context.Context, params GetMalwarePolicyActionsRequest) (*GetMalwarePolicyActionsResponse, error) { 110 logger := p.Log(ctx) 111 logger.Debug("GetMalwarePolicyActions") 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/malware-policies", 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 GetMalwarePolicyActions request: %w", err) 126 } 127 128 var result GetMalwarePolicyActionsResponse 129 resp, err := p.Exec(req, &result) 130 if err != nil { 131 return nil, fmt.Errorf("get malware policy actions request failed: %w", err) 132 } 133 if resp.StatusCode != http.StatusOK { 134 return nil, p.Error(resp) 135 } 136 137 if params.MalwarePolicyID != 0 { 138 var filteredResult GetMalwarePolicyActionsResponse 139 for _, val := range result.MalwarePolicyActions { 140 if val.MalwarePolicyID == params.MalwarePolicyID { 141 filteredResult.MalwarePolicyActions = append(filteredResult.MalwarePolicyActions, val) 142 } 143 } 144 return &filteredResult, nil 145 } 146 147 return &result, nil 148 } 149 150 func (p *appsec) UpdateMalwarePolicyAction(ctx context.Context, params UpdateMalwarePolicyActionRequest) (*UpdateMalwarePolicyActionResponse, error) { 151 logger := p.Log(ctx) 152 logger.Debug("UpdateMalwarePolicyAction") 153 154 if err := params.Validate(); err != nil { 155 return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error()) 156 } 157 158 uri := fmt.Sprintf( 159 "/appsec/v1/configs/%d/versions/%d/security-policies/%s/malware-policies/%d", 160 params.ConfigID, 161 params.Version, 162 params.PolicyID, 163 params.MalwarePolicyID, 164 ) 165 166 req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil) 167 if err != nil { 168 return nil, fmt.Errorf("failed to create UpdateMalwarePolicyAction request: %w", err) 169 } 170 171 var result UpdateMalwarePolicyActionResponse 172 resp, err := p.Exec(req, &result, params) 173 if err != nil { 174 return nil, fmt.Errorf("update malware policy action request failed: %w", err) 175 } 176 if resp.StatusCode != http.StatusOK { 177 return nil, p.Error(resp) 178 } 179 180 return &result, nil 181 } 182 183 func (p *appsec) UpdateMalwarePolicyActions(ctx context.Context, params UpdateMalwarePolicyActionsRequest) (*UpdateMalwarePolicyActionsResponse, error) { 184 logger := p.Log(ctx) 185 logger.Debug("UpdateMalwarePolicyActions") 186 187 if err := params.Validate(); err != nil { 188 return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error()) 189 } 190 191 uri := fmt.Sprintf( 192 "/appsec/v1/configs/%d/versions/%d/security-policies/%s/malware-policies", 193 params.ConfigID, 194 params.Version, 195 params.PolicyID, 196 ) 197 198 req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil) 199 if err != nil { 200 return nil, fmt.Errorf("failed to create UpdateMalwarePolicyActions request: %w", err) 201 } 202 203 var result UpdateMalwarePolicyActionsResponse 204 resp, err := p.Exec(req, &result, params.MalwarePolicyActions) 205 if err != nil { 206 return nil, fmt.Errorf("update malware policy actions request failed: %w", err) 207 } 208 if resp.StatusCode != http.StatusOK { 209 return nil, p.Error(resp) 210 } 211 212 return &result, nil 213 }