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