package main // Report: the licence LIST, not a decision (ADR-0093 §10 item 3, WP-13 13f). // // A tenant's licence posture is its legal decision (OD-15): the tenant's // allow / deny lists in secure_delivery_policies decide, in the platform's // security verdict. This function posts what Trivy found, one entry per // package, and never fails on a licence. The --blocklist of ScanFs/ScanImage is // IntegraCI's own rule 3 for what IntegraCI bundles and stays as it is for // that use; it is not the tenant's policy. // // dagger call report --source . --api-url $API --token env:TOKEN --service-id $SVC --version $SHA import ( "bytes" "context" "dagger/license-scan/internal/dagger" "encoding/json" "fmt" "net/http" "sort" "strings" "time" ) // PackageLicence is one package's licence as Trivy reports it. type PackageLicence struct { Package string `json:"package"` License string `json:"license"` } // ReportResult is what Report returns. type ReportResult struct { Outcome string `json:"outcome"` // ran | could_not_run Licences []PackageLicence `json:"licenses"` Posted bool `json:"posted"` Error string `json:"error,omitempty"` // Suppression files the scan of record removed from the scanned copy. IgnoredSuppressions []string `json:"ignored_suppressions,omitempty"` } // licenceArgs is the `trivy fs --scanners license` argv; the scan of record // adds trustedConfigArgs before the target. func licenceArgs(scanOfRecord bool) []string { return withTrustedConfig([]string{ "trivy", "fs", "--scanners", "license", "--format", "json", "--output", "/tmp/license-report.json", "--exit-code", "0", ".", }, scanOfRecord) } // parseLicences reads `trivy fs --scanners license --format json` output. func parseLicences(report string) ([]PackageLicence, error) { var doc struct { Results []struct { Licenses []struct { PkgName string `json:"PkgName"` Name string `json:"Name"` } `json:"Licenses"` } `json:"Results"` } if err := json.Unmarshal([]byte(report), &doc); err != nil { return nil, fmt.Errorf("trivy licence report unreadable: %w", err) } seen := map[string]bool{} out := []PackageLicence{} for _, r := range doc.Results { for _, l := range r.Licenses { if l.PkgName == "" { continue // file-level licence of the repository's own code } key := l.PkgName + "\x00" + l.Name if seen[key] { continue } seen[key] = true out = append(out, PackageLicence{Package: l.PkgName, License: l.Name}) } } sort.Slice(out, func(i, j int) bool { if out[i].Package != out[j].Package { return out[i].Package < out[j].Package } return out[i].License < out[j].License }) return out, nil } // Report posts the dependency licence list as scan_type=license. func (m *LicenseScan) Report( ctx context.Context, source *dagger.Directory, // +optional apiURL string, // +optional token *dagger.Secret, // +optional serviceID string, // +optional version string, // The analysis job of record (ADR-0093 §4, WP-05): the repository's // suppression files are removed from the scanned copy, Trivy reads no // configuration from the tree, and what was ignored is posted as // information. Off for every other caller. // +optional scanOfRecord bool, ) (*ReportResult, error) { res := &ReportResult{Outcome: "ran", Licences: []PackageLicence{}} var err error if scanOfRecord { source, res.IgnoredSuppressions, err = dropSuppression(ctx, source, func(ctx context.Context, pattern string) ([]string, error) { return source.Glob(ctx, pattern) }, func(d *dagger.Directory, paths []string) *dagger.Directory { return d.WithoutFiles(paths) }, ) } var report string if err == nil { report, err = dag.Container(). From("aquasec/trivy:0.76.1"). WithMountedCache("/root/.cache/trivy", dag.CacheVolume("trivy-db")). WithDirectory("/src", source). WithWorkdir("/src"). WithExec(licenceArgs(scanOfRecord)). File("/tmp/license-report.json"). Contents(ctx) } if err == nil { res.Licences, err = parseLicences(report) } if err != nil { res.Outcome, res.Error, res.Licences = "could_not_run", err.Error(), []PackageLicence{} } if apiURL == "" || token == nil { return res, nil } licences := res.Licences if len(licences) > 5000 { licences = licences[:5000] } data := map[string]any{"outcome": res.Outcome, "licenses": licences} if res.Outcome == "could_not_run" { data = map[string]any{"outcome": res.Outcome, "error": res.Error} } if len(res.IgnoredSuppressions) > 0 { data["ignored_suppressions"] = res.IgnoredSuppressions } body, err := json.Marshal(map[string]any{ "service_id": serviceID, "version": version, "scan_type": "license", "scanner": "trivy", "report_format": "json", "report_data": data, }) if err != nil { return res, fmt.Errorf("marshal licence payload: %w", err) } req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(apiURL, "/")+"/api/v1/scans", bytes.NewReader(body)) if err != nil { return res, fmt.Errorf("build request: %w", err) } tok, err := token.Plaintext(ctx) if err != nil { return res, fmt.Errorf("read token secret: %w", err) } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+tok) resp, err := (&http.Client{Timeout: 60 * time.Second}).Do(req) if err != nil { return res, fmt.Errorf("post licence report: %w", err) } defer resp.Body.Close() if resp.StatusCode >= 300 { return res, fmt.Errorf("post licence report returned %d", resp.StatusCode) } res.Posted = true return res, nil }