Implement /status API endpoint
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Yvan Janssens 2023-04-07 14:22:47 +02:00
parent 21d3278841
commit 8a61a52fa1
3 changed files with 35 additions and 26 deletions

View File

@ -11,15 +11,15 @@ func banner() {
fmt.Println("")
}
func sanity_check() bool {
vName, error := scan_data([]byte("X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*"))
func sanity_check() (bool, int) {
vName, error, sigNo := scan_data([]byte("X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*"))
if error != nil && vName == "" {
panic(error)
}
if vName == "Win.Test.EICAR_HDB-1" {
return true
return true, sigNo
}
return false
return false, 0
}
func main() {
@ -29,7 +29,8 @@ func main() {
go freshclam_update()
log.Println("Carrying out sanity checks...")
if !sanity_check() {
scannerIsSane, _ := sanity_check()
if !scannerIsSane {
log.Println("Sanity check failed!")
return
}

28
rest.go
View File

@ -1,17 +1,21 @@
package main
import "github.com/gin-gonic/gin"
import (
"net/http"
"github.com/gin-gonic/gin"
)
type status_response struct {
cvdVersion int `json:"cvd_version"`
sanity_check bool `json:"sanity_check"`
scanning_engine string `json:"scanning_engine"`
Scanning_engine string `json:"scanning_engine"`
Signature_count int `json:"signature_count"`
Sanity_check bool `json:"sanity_check"`
}
type scan_response struct {
malware_detected bool `json:"malware_detected"`
malware_name string `json:"malware_name"`
engine status_response `json:"engine"`
Malware_detected bool `json:"malware_detected"`
Malware_name string `json:"malware_name"`
Engine status_response `json:"engine"`
}
func scan_api(c *gin.Context) {
@ -19,11 +23,17 @@ func scan_api(c *gin.Context) {
}
func status_api(c *gin.Context) {
scannerIsSane, sigCount := sanity_check()
responseData := status_response{
Sanity_check: scannerIsSane,
Signature_count: sigCount,
Scanning_engine: "clamav",
}
c.IndentedJSON(http.StatusOK, responseData)
}
func start_api() {
//gin.SetMode(gin.ReleaseMode)
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
router.PUT("/scan", scan_api)

View File

@ -3,33 +3,31 @@ package main
import (
"errors"
"io/ioutil"
"log"
"os"
clamav "git.cyber.gent/friedkiwi/go-clamav"
)
func scan_data(data []byte) (string, error) {
log.Println("scan_data(): scanning data...")
func scan_data(data []byte) (string, error, int) {
// write data out to file to be scanned
tempFile, err := ioutil.TempFile("", "*.bin")
if err != nil {
return "", err
return "", err, 0
}
defer os.Remove(tempFile.Name())
bytesWritten, writeErr := tempFile.Write(data)
if writeErr != nil {
return "", writeErr
return "", writeErr, 0
}
if bytesWritten != len(data) {
return "", errors.New("scan_data(): bytesWritten!= len(data)")
return "", errors.New("scan_data(): bytesWritten!= len(data)"), 0
}
if err := tempFile.Close(); err != nil {
return "", err
return "", err, 0
}
// temporary file is now written to disk
@ -44,22 +42,22 @@ func scan_data(data []byte) (string, error) {
})
if clamInitError != nil {
return "", clamInitError
return "", clamInitError, 0
}
// free clamav memory
defer clamavInstance.Free()
// load db
_, loadDbError := clamavInstance.LoadDB("/usr/local/share/clamav", uint(clamav.CL_DB_DIRECTORY))
sigNo, loadDbError := clamavInstance.LoadDB("/usr/local/share/clamav", uint(clamav.CL_DB_DIRECTORY))
if loadDbError != nil {
return "", err
return "", err, 0
}
// compile engine
err = clamavInstance.CompileEngine()
if err != nil {
return "", err
return "", err, 0
}
clamavInstance.EngineSetNum(clamav.CL_ENGINE_MAX_SCANSIZE, 1024*1024*40)
@ -68,5 +66,5 @@ func scan_data(data []byte) (string, error) {
// scan
_, virusName, ret := clamavInstance.ScanFile(tempFile.Name())
return virusName, ret
return virusName, ret, int(sigNo)
}