162 lines
5.5 KiB
Go
162 lines
5.5 KiB
Go
// Package main implements the IntegraCI malware-scan Dagger module.
|
|
//
|
|
// Closes the last leg of the vendor code-ingest quarantine suite
|
|
// (sast/secret/sca/license/malware). The quarantine gate
|
|
// (api/app/security/services/code_ingest.py) requires every leg to report
|
|
// clean before a vendor repo can be adopted; the other four legs have
|
|
// scanner modules, malware did not — so the gate could never reach "passed".
|
|
//
|
|
// Scans a source directory for known malware signatures with ClamAV (its
|
|
// official image ships a bundled signature DB, so no rules to author) and
|
|
// can POST the result back to the quarantine as scanner="malware".
|
|
//
|
|
// ClamAV is invoked as a runtime container image (not bundled/linked into the
|
|
// IntegraCI distribution), the same posture as the other scanner modules that
|
|
// pull tool images (e.g. trufflehog in secret-scan).
|
|
//
|
|
// Usage:
|
|
//
|
|
// dagger call scan --source .
|
|
// dagger call scan --source . --tool none
|
|
// dagger call scan --source . --ingest-id <uuid> --api-url https://api.example --token env://INTEGRACI_TOKEN
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"dagger/malware-scan/internal/dagger"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// MalwareScan is the IntegraCI malware-scanning module.
|
|
type MalwareScan struct{}
|
|
|
|
// MalwareResult is the normalised output of a malware scan.
|
|
type MalwareResult struct {
|
|
Tool string `json:"tool"` // clamav | none
|
|
Skipped bool `json:"skipped"` // true for the "none" no-op branch
|
|
Findings int `json:"findings"` // number of infected files detected
|
|
Report string `json:"report"` // raw scanner stdout
|
|
Pass bool `json:"pass"` // false when malware was detected
|
|
Error string `json:"error,omitempty"` // failure detail, if any
|
|
}
|
|
|
|
// Scan runs the selected malware scanner over the source tree and, when
|
|
// quarantine coordinates are supplied, posts the result back as the
|
|
// scanner="malware" leg of the code-ingest gate.
|
|
//
|
|
// The scanner exits non-zero when infected files are found; we swallow that
|
|
// exit, capture the report, and surface Pass/Findings ourselves so the caller
|
|
// (any CI engine) decides how to gate.
|
|
func (m *MalwareScan) Scan(
|
|
ctx context.Context,
|
|
// Source directory to scan
|
|
source *dagger.Directory,
|
|
// Tool: clamav | none
|
|
// +optional
|
|
// +default="clamav"
|
|
tool string,
|
|
// Quarantine ingest id to POST results to (omit to skip posting)
|
|
// +optional
|
|
ingestID string,
|
|
// IntegraCI API base URL (required to POST results)
|
|
// +optional
|
|
apiURL string,
|
|
// Bearer token secret (required to POST results)
|
|
// +optional
|
|
token *dagger.Secret,
|
|
) (*MalwareResult, error) {
|
|
var result *MalwareResult
|
|
switch tool {
|
|
case "none":
|
|
// Mirrors the governance skip branch used by the other scan modules.
|
|
result = &MalwareResult{Tool: "none", Skipped: true, Pass: true}
|
|
case "clamav", "":
|
|
result = runClamAV(ctx, source)
|
|
default:
|
|
return nil, fmt.Errorf("unsupported malware-scan tool: %q (want clamav|none)", tool)
|
|
}
|
|
|
|
if ingestID != "" && apiURL != "" && token != nil {
|
|
if err := postIngestResult(ctx, apiURL, token, ingestID, result); err != nil {
|
|
return result, err
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// runClamAV runs `clamscan -r --infected /src`. The clamav/clamav image
|
|
// bundles a signature DB, so the scan works without a freshclam update.
|
|
// clamscan exits 1 when infected files are found; we swallow that and count
|
|
// the "FOUND" lines (one per infected file) ourselves.
|
|
func runClamAV(ctx context.Context, source *dagger.Directory) *MalwareResult {
|
|
out, _ := dag.Container().
|
|
From("clamav/clamav:1.4").
|
|
// Persist the signature DB across runs (avoids re-downloading it).
|
|
WithMountedCache("/var/lib/clamav", dag.CacheVolume("clamav-db")).
|
|
WithMountedDirectory("/src", source).
|
|
WithWorkdir("/src").
|
|
WithExec([]string{"clamscan", "-r", "--infected", "/src"}).
|
|
Stdout(ctx)
|
|
|
|
result := &MalwareResult{Tool: "clamav", Report: out}
|
|
// clamscan prints one "<file>: <Signature> FOUND" line per infected file.
|
|
result.Findings = strings.Count(out, "FOUND")
|
|
result.Pass = result.Findings == 0
|
|
if !result.Pass {
|
|
result.Error = fmt.Sprintf("clamav: %d infected file(s) detected", result.Findings)
|
|
}
|
|
return result
|
|
}
|
|
|
|
// postIngestResult reports the malware leg to the quarantine gate. Shape
|
|
// matches api/app/security/schemas/code_ingest.py ScanResultReport: infected
|
|
// files map to the critical severity bucket so any hit fails the gate.
|
|
func postIngestResult(
|
|
ctx context.Context,
|
|
apiURL string,
|
|
token *dagger.Secret,
|
|
ingestID string,
|
|
result *MalwareResult,
|
|
) error {
|
|
payload := map[string]any{
|
|
"scanner": "malware",
|
|
"critical": result.Findings,
|
|
"high": 0,
|
|
"medium": 0,
|
|
"low": 0,
|
|
}
|
|
body, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return fmt.Errorf("marshal scan payload: %w", err)
|
|
}
|
|
|
|
endpoint := strings.TrimRight(apiURL, "/") + "/api/v1/code-ingest/" + ingestID + "/scan-result"
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body))
|
|
if err != nil {
|
|
return fmt.Errorf("build request: %w", err)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
tokenStr, err := token.Plaintext(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("read token secret: %w", err)
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+tokenStr)
|
|
|
|
client := &http.Client{Timeout: 30 * time.Second}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("API post to %s failed: %w", endpoint, err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode >= 300 {
|
|
return fmt.Errorf("API post to %s returned %d", endpoint, resp.StatusCode)
|
|
}
|
|
return nil
|
|
}
|