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