Skip to content

Commit

Permalink
fix clamav to work with storage library
Browse files Browse the repository at this point in the history
  • Loading branch information
Elbahkiry committed Sep 20, 2024
1 parent 469274f commit 3124fab
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 69 deletions.
66 changes: 38 additions & 28 deletions service/services/clamav/clamav.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (c *Clamav) Processing(partial bool, IcapHeader textproto.MIMEHeader) (int,
isProcess, icapStatus, httpMsg := c.generalFunc.CheckTheExtension(fileExtension, c.extArrs,
c.processExts, c.rejectExts, c.bypassExts, c.return400IfFileExtRejected, isGzip,
c.serviceName, c.methodName, fileHash, c.httpMsg.Request.RequestURI, reqContentType, bytes.NewBuffer(file), ExceptionPagePath, fileSize)
if !isProcess {
if !isProcess && c.methodName == utils.ICAPModeReq {
logging.Logger.Info(utils.PrepareLogMsg(c.xICAPMetadata, c.serviceName+" service has stopped processing"))
msgHeadersAfterProcessing = c.generalFunc.LogHTTPMsgHeaders(c.methodName)
return icapStatus, httpMsg, serviceHeaders,
Expand All @@ -109,7 +109,7 @@ func (c *Clamav) Processing(partial bool, IcapHeader textproto.MIMEHeader) (int,

//check if the file size is greater than max file size of the service
//if yes we will return 200 ok or 204 no modification, it depends on the configuration of the service
if c.maxFileSize != 0 && c.maxFileSize < len(file) {
if c.maxFileSize != 0 && c.maxFileSize < len(file) && isProcess {
status, file, httpMsg := c.generalFunc.IfMaxFileSizeExc(c.returnOrigIfMaxSizeExc, c.serviceName, c.methodName, bytes.NewBuffer(file), c.maxFileSize, ExceptionPagePath, fileSize)
fileAfterPrep, httpMsg := c.generalFunc.IfStatusIs204WithFile(c.methodName, status, file, isGzip, reqContentType, httpMsg, true)
if fileAfterPrep == nil && httpMsg == nil {
Expand Down Expand Up @@ -207,34 +207,44 @@ func (c *Clamav) Processing(partial bool, IcapHeader textproto.MIMEHeader) (int,
}
}
//returning the scanned file if everything is ok
fileAfterPrep, httpMsg := c.generalFunc.IfICAPStatusIs204(c.methodName, utils.NoModificationStatusCodeStr,
bytes.NewBuffer(file), false, reqContentType, c.httpMsg)
if fileAfterPrep == nil && httpMsg == nil {
logging.Logger.Info(utils.PrepareLogMsg(c.xICAPMetadata, c.serviceName+" service has stopped processing"))
return utils.InternalServerErrStatusCodeStr, nil, nil, msgHeadersBeforeProcessing,
msgHeadersAfterProcessing, vendorMsgs
}
// fileAfterPrep, httpMsg := c.generalFunc.IfICAPStatusIs204(c.methodName, utils.NoModificationStatusCodeStr,
// bytes.NewBuffer(file), false, reqContentType, c.httpMsg)
// if fileAfterPrep == nil && httpMsg == nil {
// logging.Logger.Info(utils.PrepareLogMsg(c.xICAPMetadata, c.serviceName+" service has stopped processing"))
// return utils.InternalServerErrStatusCodeStr, nil, nil, msgHeadersBeforeProcessing,
// msgHeadersAfterProcessing, vendorMsgs
// }

// //returning the http message and the ICAP status code
// switch msg := httpMsg.(type) {
// case *http.Request:
// msg.Body = io.NopCloser(bytes.NewBuffer(fileAfterPrep))
// logging.Logger.Info(utils.PrepareLogMsg(c.xICAPMetadata, c.serviceName+" service has stopped processing"))
// msgHeadersAfterProcessing = c.generalFunc.LogHTTPMsgHeaders(c.methodName)
// return utils.NoModificationStatusCodeStr, msg, serviceHeaders, msgHeadersBeforeProcessing,
// msgHeadersAfterProcessing, vendorMsgs
// case *http.Response:
// msg.Body = io.NopCloser(bytes.NewBuffer(fileAfterPrep))
// logging.Logger.Info(utils.PrepareLogMsg(c.xICAPMetadata, c.serviceName+" service has stopped processing"))
// msgHeadersAfterProcessing = c.generalFunc.LogHTTPMsgHeaders(c.methodName)
// return utils.NoModificationStatusCodeStr, msg, serviceHeaders, msgHeadersBeforeProcessing,
// msgHeadersAfterProcessing, vendorMsgs
// }
// c.generalFunc.LogHTTPMsgHeaders(c.methodName)
// logging.Logger.Info(utils.PrepareLogMsg(c.xICAPMetadata, c.serviceName+" service has stopped processing"))
// msgHeadersAfterProcessing = c.generalFunc.LogHTTPMsgHeaders(c.methodName)
// return utils.NoModificationStatusCodeStr, nil, serviceHeaders, msgHeadersBeforeProcessing,
// msgHeadersAfterProcessing, vendorMsgs
scannedFile := file

// Prepare the scanned file
scannedFile = c.generalFunc.PreparingFileAfterScanning(scannedFile, reqContentType, c.methodName)

//returning the http message and the ICAP status code
switch msg := httpMsg.(type) {
case *http.Request:
msg.Body = io.NopCloser(bytes.NewBuffer(fileAfterPrep))
logging.Logger.Info(utils.PrepareLogMsg(c.xICAPMetadata, c.serviceName+" service has stopped processing"))
msgHeadersAfterProcessing = c.generalFunc.LogHTTPMsgHeaders(c.methodName)
return utils.NoModificationStatusCodeStr, msg, serviceHeaders, msgHeadersBeforeProcessing,
msgHeadersAfterProcessing, vendorMsgs
case *http.Response:
msg.Body = io.NopCloser(bytes.NewBuffer(fileAfterPrep))
logging.Logger.Info(utils.PrepareLogMsg(c.xICAPMetadata, c.serviceName+" service has stopped processing"))
msgHeadersAfterProcessing = c.generalFunc.LogHTTPMsgHeaders(c.methodName)
return utils.NoModificationStatusCodeStr, msg, serviceHeaders, msgHeadersBeforeProcessing,
msgHeadersAfterProcessing, vendorMsgs
}
c.generalFunc.LogHTTPMsgHeaders(c.methodName)
logging.Logger.Info(utils.PrepareLogMsg(c.xICAPMetadata, c.serviceName+" service has stopped processing"))
msgHeadersAfterProcessing = c.generalFunc.LogHTTPMsgHeaders(c.methodName)
return utils.NoModificationStatusCodeStr, nil, serviceHeaders, msgHeadersBeforeProcessing,
msgHeadersAfterProcessing, vendorMsgs
logging.Logger.Info(utils.PrepareLogMsg(c.xICAPMetadata, c.serviceName+" service has stopped processing"))

return utils.OkStatusCodeStr, c.generalFunc.ReturningHttpMessageWithFile(c.methodName, scannedFile),
serviceHeaders, msgHeadersBeforeProcessing, msgHeadersAfterProcessing, vendorMsgs
}

func (c *Clamav) ISTagValue() string {
Expand Down
43 changes: 2 additions & 41 deletions service/services/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
utils "icapeg/consts"
"icapeg/logging"
"icapeg/service/services-utilities/ContentTypes"

"io"
"net/http"
"net/textproto"
Expand All @@ -32,7 +32,7 @@ func (e *Echo) Processing(partial bool, IcapHeader textproto.MIMEHeader) (int, i
isGzip := false

//extracting the file from http message
file, reqContentType, err := e.CopyingFileToTheBuffer(e.methodName)
file, reqContentType, err := e.generalFunc.CopyingFileToTheBuffer(e.methodName)
if err != nil {
logging.Logger.Error(utils.PrepareLogMsg(e.xICAPMetadata, e.serviceName+" error: "+err.Error()))
logging.Logger.Info(utils.PrepareLogMsg(e.xICAPMetadata, e.serviceName+" service has stopped processing"))
Expand Down Expand Up @@ -118,42 +118,3 @@ func (e *Echo) ISTagValue() string {
epochTime := strconv.FormatInt(time.Now().Unix(), 10)
return "epoch-" + epochTime
}

// CopyingFileToTheBuffer is a func which used for extracting a file from the body of the http message
func (e *Echo) CopyingFileToTheBuffer(methodName string) ([]byte, ContentTypes.ContentType, error) {
logging.Logger.Info(utils.PrepareLogMsg(e.xICAPMetadata, "extracting the body of HTTP message"))
var file []byte
var err error
var reqContentType ContentTypes.ContentType
reqContentType = nil
switch methodName {
case utils.ICAPModeReq:
file, reqContentType, err = e.copyingFileToTheBufferReq()
case utils.ICAPModeResp:
file, err = e.copyingFileToTheBufferResp()
}
if err != nil {
return nil, nil, err
}
return file, reqContentType, nil
}

// copyingFileToTheBufferResp is a utility function for CopyingFileToTheBuffer func
// it's used for extracting a file from the body of the http response
func (e *Echo) copyingFileToTheBufferResp() ([]byte, error) {
file, err := e.httpMsg.StorageClient.Load(e.httpMsg.StorageKey)
if err != nil {
return file, err
}
return file, nil
}

// copyingFileToTheBufferReq is a utility function for CopyingFileToTheBuffer func
// it's used for extracting a file from the body of the http request
func (e *Echo) copyingFileToTheBufferReq() ([]byte, ContentTypes.ContentType, error) {
reqContentType := ContentTypes.GetContentType(e.httpMsg.Request)
// getting the file from request and store it in buf as a type of bytes.Buffer
file := reqContentType.GetFileFromRequest()
return file.Bytes(), reqContentType, nil

}

0 comments on commit 3124fab

Please sign in to comment.