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