github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/appsec/custom_rule_action.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 CustomRuleAction interface supports retrieving and updating the actions for the custom 13 // rules of a configuration, or for a specific custom rule. 14 // 15 // https://developer.akamai.com/api/cloud_security/application_security/v1.html#customruleactions 16 CustomRuleAction interface { 17 // https://developer.akamai.com/api/cloud_security/application_security/v1.html#getcustomruleactions 18 GetCustomRuleActions(ctx context.Context, params GetCustomRuleActionsRequest) (*GetCustomRuleActionsResponse, error) 19 20 // https://developer.akamai.com/api/cloud_security/application_security/v1.html#getcustomruleactions 21 GetCustomRuleAction(ctx context.Context, params GetCustomRuleActionRequest) (*GetCustomRuleActionResponse, error) 22 23 // https://developer.akamai.com/api/cloud_security/application_security/v1.html#putactionruleid 24 UpdateCustomRuleAction(ctx context.Context, params UpdateCustomRuleActionRequest) (*UpdateCustomRuleActionResponse, error) 25 } 26 27 // GetCustomRuleActionsRequest is used to retrieve the custom rule actions for a configuration. 28 GetCustomRuleActionsRequest struct { 29 ConfigID int `json:"configId"` 30 Version int `json:"version"` 31 PolicyID string `json:"policyId"` 32 RuleID int `json:"ruleId"` 33 } 34 35 // GetCustomRuleActionsResponse is returned from a call to GetCustomRuleActions. 36 GetCustomRuleActionsResponse []struct { 37 Action string `json:"action,omitempty"` 38 CanUseAdvancedActions bool `json:"canUseAdvancedActions,omitempty"` 39 Link string `json:"link,omitempty"` 40 Name string `json:"name,omitempty"` 41 RuleID int `json:"ruleId,omitempty"` 42 } 43 44 // GetCustomRuleActionRequest is used to retrieve the action for a custom rule. 45 GetCustomRuleActionRequest struct { 46 ConfigID int `json:"configId"` 47 Version int `json:"version"` 48 PolicyID string `json:"policyId"` 49 RuleID int `json:"ruleId"` 50 } 51 52 // GetCustomRuleActionResponse is returned from a call to GetCustomRuleAction. 53 GetCustomRuleActionResponse struct { 54 Action string `json:"action,omitempty"` 55 CanUseAdvancedActions bool `json:"canUseAdvancedActions,omitempty"` 56 Link string `json:"link,omitempty"` 57 Name string `json:"name,omitempty"` 58 RuleID int `json:"ruleId,omitempty"` 59 } 60 61 // UpdateCustomRuleActionRequest is used to modify an existing custom rule. 62 UpdateCustomRuleActionRequest struct { 63 ConfigID int `json:"-"` 64 Version int `json:"-"` 65 PolicyID string `json:"-"` 66 RuleID int `json:"-"` 67 Action string `json:"action"` 68 } 69 70 // UpdateCustomRuleActionResponse is returned from a call to UpdateCustomRuleAction. 71 UpdateCustomRuleActionResponse struct { 72 Action string `json:"action"` 73 CanUseAdvancedActions bool `json:"canUseAdvancedActions"` 74 Link string `json:"link"` 75 Name string `json:"name"` 76 RuleID int `json:"ruleId"` 77 } 78 ) 79 80 // Validate validates a GetCustomRuleActionRequest. 81 func (v GetCustomRuleActionRequest) Validate() error { 82 return validation.Errors{ 83 "ConfigID": validation.Validate(v.ConfigID, validation.Required), 84 "Version": validation.Validate(v.Version, validation.Required), 85 "PolicyID": validation.Validate(v.PolicyID, validation.Required), 86 }.Filter() 87 } 88 89 // Validate validates a GetCustomRuleActionsRequest. 90 func (v GetCustomRuleActionsRequest) Validate() error { 91 return validation.Errors{ 92 "ConfigID": validation.Validate(v.ConfigID, validation.Required), 93 "Version": validation.Validate(v.Version, validation.Required), 94 "PolicyID": validation.Validate(v.PolicyID, validation.Required), 95 }.Filter() 96 } 97 98 // Validate validates an UpdateCustomRuleActionRequest. 99 func (v UpdateCustomRuleActionRequest) Validate() error { 100 return validation.Errors{ 101 "ConfigID": validation.Validate(v.ConfigID, validation.Required), 102 "Version": validation.Validate(v.Version, validation.Required), 103 "PolicyID": validation.Validate(v.PolicyID, validation.Required), 104 "ID": validation.Validate(v.RuleID, validation.Required), 105 }.Filter() 106 } 107 108 func (p *appsec) GetCustomRuleAction(ctx context.Context, params GetCustomRuleActionRequest) (*GetCustomRuleActionResponse, error) { 109 logger := p.Log(ctx) 110 logger.Debug("GetCustomRuleAction") 111 112 if err := params.Validate(); err != nil { 113 return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error()) 114 } 115 116 var result GetCustomRuleActionResponse 117 118 uri := fmt.Sprintf( 119 "/appsec/v1/configs/%d/versions/%d/security-policies/%s/custom-rules", 120 params.ConfigID, 121 params.Version, 122 params.PolicyID) 123 124 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) 125 if err != nil { 126 return nil, fmt.Errorf("failed to create GetCustomRuleAction request: %w", err) 127 } 128 129 var results GetCustomRuleActionsResponse 130 131 resp, err := p.Exec(req, &results) 132 if err != nil { 133 return nil, fmt.Errorf("get custom rule action request failed: %w", err) 134 } 135 136 if resp.StatusCode != http.StatusOK { 137 return nil, p.Error(resp) 138 } 139 140 for _, val := range results { 141 if val.RuleID == params.RuleID { 142 result = val 143 return &result, nil 144 } 145 } 146 147 return &result, nil 148 } 149 150 func (p *appsec) GetCustomRuleActions(ctx context.Context, params GetCustomRuleActionsRequest) (*GetCustomRuleActionsResponse, error) { 151 logger := p.Log(ctx) 152 logger.Debug("GetCustomRuleActions") 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/custom-rules", 160 params.ConfigID, 161 params.Version, 162 params.PolicyID) 163 164 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) 165 if err != nil { 166 return nil, fmt.Errorf("failed to create GetCustomRuleActions request: %w", err) 167 } 168 169 var result GetCustomRuleActionsResponse 170 resp, err := p.Exec(req, &result) 171 if err != nil { 172 return nil, fmt.Errorf("get custom rule actions request failed: %w", err) 173 } 174 if resp.StatusCode != http.StatusOK { 175 return nil, p.Error(resp) 176 } 177 178 if params.RuleID != 0 { 179 var filteredResult GetCustomRuleActionsResponse 180 for _, val := range result { 181 if val.RuleID == params.RuleID { 182 filteredResult = append(filteredResult, val) 183 } 184 } 185 return &filteredResult, nil 186 } 187 188 return &result, nil 189 } 190 191 func (p *appsec) UpdateCustomRuleAction(ctx context.Context, params UpdateCustomRuleActionRequest) (*UpdateCustomRuleActionResponse, error) { 192 logger := p.Log(ctx) 193 logger.Debug("UpdateCustomRuleAction") 194 195 if err := params.Validate(); err != nil { 196 return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error()) 197 } 198 199 uri := fmt.Sprintf( 200 "/appsec/v1/configs/%d/versions/%d/security-policies/%s/custom-rules/%d", 201 params.ConfigID, 202 params.Version, 203 params.PolicyID, 204 params.RuleID, 205 ) 206 207 req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil) 208 if err != nil { 209 return nil, fmt.Errorf("failed to create UpdateCustomRuleAction request: %w", err) 210 } 211 212 var result UpdateCustomRuleActionResponse 213 resp, err := p.Exec(req, &result, params) 214 if err != nil { 215 return nil, fmt.Errorf("update custom rule action request failed: %w", err) 216 } 217 218 if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusNoContent { 219 return nil, p.Error(resp) 220 } 221 222 return &result, nil 223 }