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