Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/malwarescan-async-scan' into v1.…
Browse files Browse the repository at this point in the history
…364.0-edp

* origin/malwarescan-async-scan:
  add new malwarescan async api
  • Loading branch information
maxatsap committed May 28, 2024
2 parents 79519eb + 72c3b78 commit ef22384
Show file tree
Hide file tree
Showing 6 changed files with 412 additions and 33 deletions.
66 changes: 52 additions & 14 deletions cmd/malwareExecuteScan.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package cmd
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"

piperDocker "github.com/SAP/jenkins-library/pkg/docker"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/log"
Expand All @@ -11,14 +17,11 @@ import (
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/SAP/jenkins-library/pkg/toolrecord"
"github.com/pkg/errors"
"io"
"os"
"strings"
"time"
)

type malwareScanUtils interface {
OpenFile(name string, flag int, perm os.FileMode) (io.ReadCloser, error)
Stat(path string) (os.FileInfo, error)
SHA256(path string) (string, error)

newDockerClient(piperDocker.ClientOptions) piperDocker.Download
Expand All @@ -36,6 +39,10 @@ func (utils *malwareScanUtilsBundle) OpenFile(name string, flag int, perm os.Fil
return utils.Files.FileOpen(name, flag, perm)
}

func (utils *malwareScanUtilsBundle) Stat(path string) (os.FileInfo, error) {
return utils.Files.Stat(path)
}

func (utils *malwareScanUtilsBundle) newDockerClient(options piperDocker.ClientOptions) piperDocker.Download {
dClient := piperDocker.Client{}
dClient.SetOptions(options)
Expand All @@ -45,10 +52,22 @@ func (utils *malwareScanUtilsBundle) newDockerClient(options piperDocker.ClientO
func newMalwareScanUtilsBundle(config malwareExecuteScanOptions) *malwareScanUtilsBundle {
timeout, err := time.ParseDuration(fmt.Sprintf("%ss", config.Timeout))
if err != nil {
timeout = 60
timeout = 60 * time.Second
log.Entry().Warnf("Unable to parse timeout for malwareScan: '%v'. Falling back to %ds", err, timeout)
}

pollinterval, err := time.ParseDuration(fmt.Sprintf("%ss", config.PollingInterval))
if err != nil {
pollinterval = 10 * time.Second
log.Entry().Warnf("Unable to parse poll interval for malwareScan: '%v'. Falling back to %ds", err, pollinterval)
}

pollingTimeout, err := time.ParseDuration(fmt.Sprintf("%ss", config.PollingTimeout))
if err != nil {
pollingTimeout = 600 * time.Second
log.Entry().Warnf("Unable to parse poll timeout for malwareScan: '%v'. Falling back to %ds", err, pollingTimeout)
}

httpClientOptions := piperhttp.ClientOptions{
Username: config.Username,
Password: config.Password,
Expand All @@ -61,8 +80,10 @@ func newMalwareScanUtilsBundle(config malwareExecuteScanOptions) *malwareScanUti

return &malwareScanUtilsBundle{
Client: &malwarescan.ClientImpl{
HTTPClient: httpClient,
Host: config.Host,
HTTPClient: httpClient,
Host: config.Host,
PollInterval: pollinterval,
PollingTimeout: pollingTimeout,
},
Files: &piperutils.Files{},
}
Expand All @@ -83,27 +104,44 @@ func runMalwareScan(config *malwareExecuteScanOptions, telemetryData *telemetry.
return err
}

log.Entry().Infof("Scanning file \"%s\" for malware using service \"%s\"", file, config.Host)
scannerInfo, err := utils.Info()
if err != nil {
return err
}

candidate, err := utils.OpenFile(file, os.O_RDONLY, 0666)
if err != nil {
return err
}
defer candidate.Close()

scannerInfo, err := utils.Info()
candidateInfo, err := utils.Stat(file)
if err != nil {
return err
}
candidateSize := candidateInfo.Size()

log.Entry().Infof("***************************************")
log.Entry().Infof("* Engine: %s", scannerInfo.EngineVersion)
log.Entry().Infof("* Signatures: %s", scannerInfo.SignatureTimestamp)
log.Entry().Infof("***************************************")
log.Entry().Infof("********************************************************************************")
log.Entry().Infof("* Malware Scan Service *")
log.Entry().Infof("********************************************************************************")
log.Entry().Infof("* Host: %s", config.Host)
log.Entry().Infof("* Engine: %s", scannerInfo.EngineVersion)
log.Entry().Infof("* Signatures: %s", scannerInfo.SignatureTimestamp)
log.Entry().Infof("* File: %s", file)
log.Entry().Infof("* File Size: %d", candidateSize)
log.Entry().Infof("********************************************************************************")

if _, err = createToolRecordMalwareScan(utils, "./", config, scannerInfo); err != nil {
return err
}

scanResponse, err := utils.Scan(candidate)
candidateExt := filepath.Ext(file)
candidateType := ""
if candidateExt != "" {
candidateType = candidateExt[1:]
}

scanResponse, err := utils.Scan(candidate, config.Asynchronous, candidateSize, candidateType)
if err != nil {
return err
}
Expand Down
33 changes: 33 additions & 0 deletions cmd/malwareExecuteScan_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion cmd/malwareExecuteScan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (utils *malwareScanUtilsMockBundle) OpenFile(path string, flag int, perm os
return utils.FilesMock.OpenFile(path, flag, perm)
}

func (utils *malwareScanUtilsMockBundle) Stat(path string) (os.FileInfo, error) {
return utils.FilesMock.Stat(path)
}

func (utils *malwareScanUtilsMockBundle) FileWrite(path string, content []byte, perm os.FileMode) error {
return utils.FilesMock.FileWrite(path, content, perm)
}
Expand All @@ -52,7 +56,7 @@ func (utils *malwareScanUtilsMockBundle) Info() (*malwarescan.Info, error) {
return &malwarescan.Info{EngineVersion: "Mock Malware Scanner", SignatureTimestamp: "n/a"}, nil
}

func (utils *malwareScanUtilsMockBundle) Scan(candidate io.Reader) (*malwarescan.ScanResult, error) {
func (utils *malwareScanUtilsMockBundle) Scan(candidate io.Reader, async bool, fileSize int64, fileType string) (*malwarescan.ScanResult, error) {
return utils.returnScanResult, nil
}

Expand Down
Loading

0 comments on commit ef22384

Please sign in to comment.