Files
integraci-dagger-modules/license-scan/main.go
T

150 lines
4.1 KiB
Go

// Package main implements the IntegraCI license-scan Dagger module.
//
// Runs Trivy in license mode (S-05). Outputs a JSON license report and
// fails the pipeline when any blocked license (default GPL/AGPL/SSPL) is
// found in the dependency tree. Result can be POSTed to the IntegraCI
// /api/v1/scans endpoint with scan_type=license.
//
// Usage:
//
// dagger call scan-fs --source . --strict
// dagger call scan-image --image ghcr.io/org/svc:1.2.3 --blocklist GPL-2.0,GPL-3.0,AGPL-3.0
package main
import (
"context"
"dagger/license-scan/internal/dagger"
"fmt"
"strings"
)
// LicenseScan is the IntegraCI Trivy license scanning module.
type LicenseScan struct{}
// LicenseResult contains the output of a license scan.
type LicenseResult struct {
Success bool `json:"success"`
Target string `json:"target"`
BlockedHits []string `json:"blocked_hits"`
Report string `json:"report,omitempty"`
}
const defaultBlocklist = "GPL-2.0,GPL-3.0,AGPL-1.0,AGPL-3.0,SSPL-1.0"
// ScanFs runs Trivy filesystem license scan against a source directory.
func (m *LicenseScan) ScanFs(
ctx context.Context,
// Source directory to scan
source *dagger.Directory,
// Comma-separated list of blocked SPDX license IDs
// +optional
// +default="GPL-2.0,GPL-3.0,AGPL-1.0,AGPL-3.0,SSPL-1.0"
blocklist string,
// Strict mode: fail on any blocked license hit
// +optional
// +default=true
strict bool,
) (*LicenseResult, error) {
if blocklist == "" {
blocklist = defaultBlocklist
}
report, err := dag.Container().
From("aquasec/trivy:0.58.1").
// Persist Trivy's vulnerability DB across runs (avoids re-downloading it).
WithMountedCache("/root/.cache/trivy", dag.CacheVolume("trivy-db")).
WithDirectory("/src", source).
WithWorkdir("/src").
WithExec([]string{
"trivy", "fs",
"--scanners", "license",
"--license-full",
"--format", "json",
"--output", "/tmp/license-report.json",
"--exit-code", "0",
".",
}).
File("/tmp/license-report.json").
Contents(ctx)
if err != nil {
return nil, fmt.Errorf("trivy license scan failed: %w", err)
}
hits := findBlockedLicenses(report, strings.Split(blocklist, ","))
result := &LicenseResult{
Target: "filesystem",
Report: report,
BlockedHits: hits,
Success: len(hits) == 0 || !strict,
}
return result, nil
}
// ScanImage runs Trivy license scan against a container image reference.
func (m *LicenseScan) ScanImage(
ctx context.Context,
// Image reference (e.g. ghcr.io/org/service:tag)
image string,
// Comma-separated list of blocked SPDX license IDs
// +optional
// +default="GPL-2.0,GPL-3.0,AGPL-1.0,AGPL-3.0,SSPL-1.0"
blocklist string,
// Strict mode: fail on any blocked license hit
// +optional
// +default=true
strict bool,
) (*LicenseResult, error) {
if blocklist == "" {
blocklist = defaultBlocklist
}
report, err := dag.Container().
From("aquasec/trivy:0.58.1").
// Persist Trivy's vulnerability DB across runs (avoids re-downloading it).
WithMountedCache("/root/.cache/trivy", dag.CacheVolume("trivy-db")).
WithExec([]string{
"trivy", "image",
"--scanners", "license",
"--license-full",
"--format", "json",
"--output", "/tmp/license-report.json",
"--exit-code", "0",
image,
}).
File("/tmp/license-report.json").
Contents(ctx)
if err != nil {
return nil, fmt.Errorf("trivy license scan failed: %w", err)
}
hits := findBlockedLicenses(report, strings.Split(blocklist, ","))
result := &LicenseResult{
Target: image,
Report: report,
BlockedHits: hits,
Success: len(hits) == 0 || !strict,
}
return result, nil
}
// findBlockedLicenses returns the unique blocked license IDs found in the JSON report.
func findBlockedLicenses(report string, blocklist []string) []string {
seen := map[string]bool{}
hits := []string{}
for _, lic := range blocklist {
needle := strings.TrimSpace(lic)
if needle == "" {
continue
}
// Trivy's license report includes "Name":"<SPDX>" entries.
token := fmt.Sprintf(`"Name":%q`, needle)
if strings.Contains(report, token) && !seen[needle] {
seen[needle] = true
hits = append(hits, needle)
}
}
return hits
}