chore: publish IntegraCI build modules a11ec75d3e09
This commit is contained in:
1 parent
694710104a
commit
3c2b80914e
78 files changed
+4982
-405
No files matched your search
+1
-1
@@ -1,7 +1,7 @@
|
||||
# iac-scan
|
||||
|
||||
> **UNVERIFIED SCAFFOLD** — run `dagger develop` + `dagger functions` + test
|
||||
> against a matching Dagger engine (engineVersion `v0.21.7`) before use. The
|
||||
> against a matching Dagger engine (engineVersion `v0.21.11`) before use. The
|
||||
> Go in `main.go` has NOT been compiled or validated by Dagger codegen in
|
||||
> this environment (`dagger develop` is unavailable, and the `dag` global
|
||||
> plus the `*Directory` / `*Container` / `*Secret` / `*File` types are
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
{
|
||||
"name": "iac-scan",
|
||||
"engineVersion": "v0.21.7",
|
||||
"engineVersion": "v0.21.11",
|
||||
"sdk": {
|
||||
"source": "go"
|
||||
},
|
||||
"source": ".",
|
||||
"include": [
|
||||
"./main.go"
|
||||
"./main.go",
|
||||
"./scanofrecord.go"
|
||||
]
|
||||
}
|
||||
+40
-14
@@ -45,6 +45,8 @@ type ScanResult struct {
|
||||
Report string `json:"report,omitempty"`
|
||||
Skipped bool `json:"skipped"`
|
||||
Success bool `json:"success"`
|
||||
// Suppression files the scan of record removed from the scanned copy.
|
||||
IgnoredSuppressions []string `json:"ignored_suppressions,omitempty"`
|
||||
}
|
||||
|
||||
// Scan runs an IaC misconfiguration pass over the source directory with the
|
||||
@@ -70,8 +72,24 @@ func (m *IacScan) Scan(
|
||||
// Version (commit SHA / image tag) the scan applies to
|
||||
// +optional
|
||||
version string,
|
||||
// The analysis job of record (ADR-0093 §4, WP-05): the repository's
|
||||
// suppression files (trivy.yaml, .trivyignore[.yaml], .checkov.yaml, ...)
|
||||
// are removed from the scanned copy and Trivy reads no configuration from
|
||||
// the tree. Off for every other caller.
|
||||
// +optional
|
||||
scanOfRecord bool,
|
||||
) (*ScanResult, error) {
|
||||
result := &ScanResult{Tool: tool}
|
||||
if scanOfRecord {
|
||||
var err error
|
||||
source, result.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) },
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
report string
|
||||
@@ -84,7 +102,7 @@ func (m *IacScan) Scan(
|
||||
result.Success = true
|
||||
return result, nil
|
||||
case "trivy":
|
||||
report, err = m.runTrivyConfig(ctx, source)
|
||||
report, err = m.runTrivyConfig(ctx, source, scanOfRecord)
|
||||
case "checkov":
|
||||
report, err = m.runCheckov(ctx, source)
|
||||
default:
|
||||
@@ -120,22 +138,15 @@ func (m *IacScan) Scan(
|
||||
// runTrivyConfig runs "trivy config" over the source directory in JSON mode.
|
||||
// Trivy exits non-zero when misconfigurations are present; we force
|
||||
// --exit-code 0 and read the JSON report rather than relying on the exit code.
|
||||
func (m *IacScan) runTrivyConfig(ctx context.Context, source *dagger.Directory) (string, error) {
|
||||
const reportPath = "/tmp/iac-report.json"
|
||||
func (m *IacScan) runTrivyConfig(ctx context.Context, source *dagger.Directory, scanOfRecord bool) (string, error) {
|
||||
report, err := dag.Container().
|
||||
From("aquasec/trivy:0.58.1").
|
||||
From("aquasec/trivy:0.76.1").
|
||||
// Persist Trivy's misconfig/policy DB across runs (avoids re-downloading it).
|
||||
WithMountedCache("/root/.cache/trivy", dag.CacheVolume("trivy-db")).
|
||||
WithMountedDirectory("/src", source).
|
||||
WithWorkdir("/src").
|
||||
WithExec([]string{
|
||||
"trivy", "config",
|
||||
"--format", "json",
|
||||
"--output", reportPath,
|
||||
"--exit-code", "0",
|
||||
".",
|
||||
}).
|
||||
File(reportPath).
|
||||
WithExec(iacTrivyArgs(scanOfRecord)).
|
||||
File(iacReportPath).
|
||||
Contents(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read trivy config JSON output: %w", err)
|
||||
@@ -143,6 +154,20 @@ func (m *IacScan) runTrivyConfig(ctx context.Context, source *dagger.Directory)
|
||||
return report, nil
|
||||
}
|
||||
|
||||
const iacReportPath = "/tmp/iac-report.json"
|
||||
|
||||
// iacTrivyArgs is the `trivy config` argv; the scan of record adds
|
||||
// trustedConfigArgs before the target.
|
||||
func iacTrivyArgs(scanOfRecord bool) []string {
|
||||
return withTrustedConfig([]string{
|
||||
"trivy", "config",
|
||||
"--format", "json",
|
||||
"--output", iacReportPath,
|
||||
"--exit-code", "0",
|
||||
".",
|
||||
}, scanOfRecord)
|
||||
}
|
||||
|
||||
// runCheckov runs Checkov over the source directory in JSON mode. Checkov
|
||||
// exits non-zero when failed checks are present, so we tolerate the exit code
|
||||
// and read the JSON emitted on stdout.
|
||||
@@ -195,8 +220,9 @@ func (m *IacScan) postResults(
|
||||
"medium_count": result.Medium,
|
||||
"low_count": result.Low,
|
||||
"report_data": map[string]any{
|
||||
"iac_critical": result.Critical,
|
||||
"iac_high": result.High,
|
||||
"iac_critical": result.Critical,
|
||||
"iac_high": result.High,
|
||||
"ignored_suppressions": result.IgnoredSuppressions,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package main
|
||||
|
||||
// The scan of record (ADR-0093 §4 condition 3, WP-05): the repository being
|
||||
// judged must not be able to steer the scanner that judges it. By default
|
||||
// Trivy reads trivy.yaml, .trivyignore[.yaml] and trivy-secret.yaml from the
|
||||
// tree it scans, so a change could ignore its own licence or misconfiguration
|
||||
// finding (WP-13 review, 2026-11-05, CONFIRMED). With --scan-of-record those
|
||||
// files are removed from the scanned copy (reported as ignored_suppressions)
|
||||
// and Trivy is told to read no configuration from the tree at all. Mirrors
|
||||
// security-scan's withoutSuppression / trustedConfigArgs.
|
||||
//
|
||||
// Pure where it can be: dropSuppression takes the tree's glob and "without"
|
||||
// as functions, so a test passes an in-memory tree and checks the tree that
|
||||
// comes back.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// suppressionFiles are the files Trivy (and Checkov) read from the scanned
|
||||
// tree and obey. Kept equal to security-scan's list by
|
||||
// apps/api/tests/test_analysis_of_record.py.
|
||||
var suppressionFiles = []string{
|
||||
".gitleaksignore",
|
||||
".gitleaks.toml",
|
||||
".trivyignore",
|
||||
".trivyignore.yaml",
|
||||
"trivy.yaml",
|
||||
"trivy.yml",
|
||||
".semgrepignore",
|
||||
".checkov.yaml",
|
||||
".checkov.yml",
|
||||
"trivy-secret.yaml",
|
||||
"trivy-secret.yml",
|
||||
}
|
||||
|
||||
// dropSuppression returns source minus every suppressionFiles entry, at any
|
||||
// depth, and the paths it removed. A tree that cannot be listed is an error:
|
||||
// a scan of record that could not drop the suppressions is not one.
|
||||
func dropSuppression[T any](
|
||||
ctx context.Context,
|
||||
source T,
|
||||
glob func(context.Context, string) ([]string, error),
|
||||
without func(T, []string) T,
|
||||
) (T, []string, error) {
|
||||
var found []string
|
||||
for _, name := range suppressionFiles {
|
||||
for _, pattern := range []string{name, "**/" + name} {
|
||||
matches, err := glob(ctx, pattern)
|
||||
if err != nil {
|
||||
var none T
|
||||
return none, nil, fmt.Errorf("list %s for the scan of record: %w", name, err)
|
||||
}
|
||||
found = append(found, matches...)
|
||||
}
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
var removed []string
|
||||
for _, p := range found {
|
||||
if !seen[p] {
|
||||
seen[p] = true
|
||||
removed = append(removed, p)
|
||||
}
|
||||
}
|
||||
if len(removed) == 0 {
|
||||
return source, nil, nil
|
||||
}
|
||||
return without(source, removed), removed, nil
|
||||
}
|
||||
|
||||
// trustedConfigArgs are the Trivy flags the scan of record adds: no
|
||||
// configuration, ignore file or secret configuration is read from the tree
|
||||
// it scans (Trivy 0.76, "Disabling configuration files").
|
||||
func trustedConfigArgs(scanOfRecord bool) []string {
|
||||
if !scanOfRecord {
|
||||
return nil
|
||||
}
|
||||
return []string{"--config=", "--ignorefile=", "--secret-config="}
|
||||
}
|
||||
|
||||
// withTrustedConfig inserts trustedConfigArgs before the final target
|
||||
// argument of a Trivy argv (the target must stay last).
|
||||
func withTrustedConfig(args []string, scanOfRecord bool) []string {
|
||||
extra := trustedConfigArgs(scanOfRecord)
|
||||
if len(extra) == 0 || len(args) == 0 {
|
||||
return args
|
||||
}
|
||||
out := append([]string{}, args[:len(args)-1]...)
|
||||
out = append(out, extra...)
|
||||
return append(out, args[len(args)-1])
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package main
|
||||
|
||||
// Written on the build VM, not run there (owner decision 2026-10-11). Runs
|
||||
// with `go test ./...` after `dagger develop` has generated internal/dagger.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"path"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type memTree struct {
|
||||
files map[string]bool
|
||||
globErr error
|
||||
}
|
||||
|
||||
func newTree(paths ...string) memTree {
|
||||
files := map[string]bool{}
|
||||
for _, p := range paths {
|
||||
files[p] = true
|
||||
}
|
||||
return memTree{files: files}
|
||||
}
|
||||
|
||||
func (t memTree) Glob(_ context.Context, pattern string) ([]string, error) {
|
||||
if t.globErr != nil {
|
||||
return nil, t.globErr
|
||||
}
|
||||
var out []string
|
||||
for p := range t.files {
|
||||
if name, ok := strings.CutPrefix(pattern, "**/"); ok {
|
||||
if strings.Contains(p, "/") && path.Base(p) == name {
|
||||
out = append(out, p)
|
||||
}
|
||||
} else if p == pattern {
|
||||
out = append(out, p)
|
||||
}
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (t memTree) WithoutFiles(paths []string) memTree {
|
||||
files := map[string]bool{}
|
||||
for p := range t.files {
|
||||
files[p] = true
|
||||
}
|
||||
for _, p := range paths {
|
||||
delete(files, p)
|
||||
}
|
||||
return memTree{files: files}
|
||||
}
|
||||
|
||||
func (t memTree) list() []string {
|
||||
var out []string
|
||||
for p := range t.files {
|
||||
out = append(out, p)
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// A change that ships trivy.yaml / .trivyignore to hide its own misconfiguration
|
||||
// is scanned WITHOUT them. Mutant: return `source, removed, nil` -> the tree
|
||||
// still holds them and this fails.
|
||||
func TestTheIacScanOfRecordTreeHasNoSuppressionFile(t *testing.T) {
|
||||
tree := newTree("main.tf", "trivy.yaml", ".trivyignore", "svc/.trivyignore.yaml", "trivy-secret.yaml")
|
||||
got, removed, err := dropSuppression(context.Background(), tree, tree.Glob, memTree.WithoutFiles)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := []string{"main.tf"}; !reflect.DeepEqual(got.list(), want) {
|
||||
t.Fatalf("scanned tree = %q, want %q", got.list(), want)
|
||||
}
|
||||
if len(removed) != 4 {
|
||||
t.Fatalf("removed %q, want the four suppression files", removed)
|
||||
}
|
||||
}
|
||||
|
||||
// Legal direction: a tree without suppression is scanned whole.
|
||||
func TestAnIacTreeWithoutSuppressionIsScannedWhole(t *testing.T) {
|
||||
tree := newTree("main.tf", "src/trivy.go")
|
||||
got, removed, err := dropSuppression(context.Background(), tree, tree.Glob, memTree.WithoutFiles)
|
||||
if err != nil || len(removed) != 0 || !reflect.DeepEqual(got.list(), tree.list()) {
|
||||
t.Fatalf("got %q removed %q err %v", got.list(), removed, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnIacTreeThatCannotBeListedIsNotAScanOfRecord(t *testing.T) {
|
||||
tree := memTree{globErr: errors.New("boom")}
|
||||
if _, _, err := dropSuppression(context.Background(), tree, tree.Glob, memTree.WithoutFiles); err == nil {
|
||||
t.Fatal("an unlistable tree must be an error, never a whole-tree scan")
|
||||
}
|
||||
}
|
||||
|
||||
// Mirrors security-scan's TestTrivyReadsNoConfigFromTheTreeInTheScanOfRecord.
|
||||
// Mutant: drop trustedConfigArgs from iacTrivyArgs -> fails; legal direction:
|
||||
// add them outside the scan of record -> the second loop fails.
|
||||
func TestIacTrivyReadsNoConfigFromTheTreeInTheScanOfRecord(t *testing.T) {
|
||||
args := iacTrivyArgs(true)
|
||||
for _, flag := range []string{"--config=", "--ignorefile=", "--secret-config="} {
|
||||
if !contains(args, flag) {
|
||||
t.Errorf("iac scan of record lacks %s: %q", flag, args)
|
||||
}
|
||||
}
|
||||
if args[len(args)-1] != "." {
|
||||
t.Errorf("the target must stay last: %q", args)
|
||||
}
|
||||
for _, a := range iacTrivyArgs(false) {
|
||||
if strings.HasPrefix(a, "--config=") || strings.HasPrefix(a, "--ignorefile=") {
|
||||
t.Errorf("outside the scan of record the argv must not change: %q", a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func contains(xs []string, s string) bool {
|
||||
for _, x := range xs {
|
||||
if x == s {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in new issue
Block a user