chore: publish IntegraCI build modules 732ec6c83925
This commit is contained in:
1 parent
7a84930dea
commit
5f8a24d014
133 files changed
+18990
-2
No files matched your search
+179
@@ -0,0 +1,179 @@
|
||||
// Package main implements the IntegraCI sbom Dagger module.
|
||||
//
|
||||
// Engine-agnostic Software Bill of Materials (SBOM) generation. Translates
|
||||
// the GitHub-Actions-only intent of security_pipeline_adapter.build_sbom_step
|
||||
// into canonical tool CLIs run inside official containers, so the same
|
||||
// pipeline logic is callable from ANY CI engine via `dagger call`.
|
||||
//
|
||||
// Each generator emits a CycloneDX or SPDX document:
|
||||
//
|
||||
// syft -> anchore/syft, CycloneDX JSON
|
||||
// cyclonedx -> cyclonedx/cyclonedx-cli, CycloneDX JSON
|
||||
// spdx-tools -> anchore/syft, SPDX JSON (the practical SPDX generator)
|
||||
// trivy -> aquasec/trivy fs/image --format cyclonedx
|
||||
// none -> no-op skip (mirrors adapter _comment_skip intent)
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// dagger call generate --source . --generator syft
|
||||
// dagger call generate --source . --image-ref ghcr.io/org/app:tag --generator trivy
|
||||
// dagger call generate --source . --generator spdx-tools
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"dagger/sbom/internal/dagger"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Sbom is the IntegraCI SBOM generation module.
|
||||
type Sbom struct{}
|
||||
|
||||
// Generate produces an SBOM document for the given source (or image) using
|
||||
// the selected generator and returns the resulting file.
|
||||
//
|
||||
// When imageRef is set, generators that support image scanning (syft,
|
||||
// spdx-tools, trivy) scan the image; otherwise they scan the source
|
||||
// filesystem. The cyclonedx generator always operates on the source tree.
|
||||
func (m *Sbom) Generate(
|
||||
ctx context.Context,
|
||||
// Source directory to analyze for the SBOM
|
||||
source *dagger.Directory,
|
||||
// Optional container image reference to scan instead of the filesystem
|
||||
// +optional
|
||||
imageRef string,
|
||||
// SBOM generator: syft | cyclonedx | spdx-tools | trivy | none
|
||||
// +optional
|
||||
// +default="syft"
|
||||
generator string,
|
||||
// The registry holding --image-ref is served over plain HTTP, so the pull
|
||||
// must not assume TLS. Same opt-in as standard-build's --registry-insecure
|
||||
// and driven by the same resolution (the connected registry's own scheme) —
|
||||
// never a default, never an address in this file.
|
||||
// +optional
|
||||
registryInsecure bool,
|
||||
) (*dagger.File, error) {
|
||||
switch generator {
|
||||
case "none":
|
||||
// No-op skip: emit a marker file mirroring the adapter's
|
||||
// _comment_skip("SBOM disabled") intent so downstream steps see a
|
||||
// deterministic, empty result instead of failing.
|
||||
return dag.Container().
|
||||
From("alpine:3.20").
|
||||
WithExec([]string{
|
||||
"sh", "-c",
|
||||
"mkdir -p /out && echo '{\"skipped\":true,\"reason\":\"SBOM disabled (generator=none)\"}' > /out/sbom.skip.json",
|
||||
}).
|
||||
File("/out/sbom.skip.json"), nil
|
||||
|
||||
case "syft":
|
||||
// anchore/syft, CycloneDX JSON. Source dir or image. The anchore/syft
|
||||
// image is distroless (no shell), so copy the syft binary into alpine
|
||||
// — which has sh — for the mkdir + output redirect.
|
||||
syftBin := dag.Container().From("anchore/syft:v1.18.1").File("/syft")
|
||||
return syftRegistryEnv(dag.Container().
|
||||
From("alpine:3.20").
|
||||
WithFile("/usr/local/bin/syft", syftBin).
|
||||
WithMountedDirectory("/src", source).
|
||||
WithWorkdir("/src"), imageRef, registryInsecure).
|
||||
WithExec([]string{
|
||||
"sh", "-c",
|
||||
fmt.Sprintf("mkdir -p /out && syft %s -o cyclonedx-json=/out/sbom.cdx.json", syftTarget(imageRef)),
|
||||
}).
|
||||
File("/out/sbom.cdx.json"), nil
|
||||
|
||||
case "spdx-tools":
|
||||
// Practical SPDX generator: syft emitting SPDX JSON. Source or image.
|
||||
// anchore/syft is distroless; copy the binary into alpine for a shell.
|
||||
syftBin := dag.Container().From("anchore/syft:v1.18.1").File("/syft")
|
||||
return syftRegistryEnv(dag.Container().
|
||||
From("alpine:3.20").
|
||||
WithFile("/usr/local/bin/syft", syftBin).
|
||||
WithMountedDirectory("/src", source).
|
||||
WithWorkdir("/src"), imageRef, registryInsecure).
|
||||
WithExec([]string{
|
||||
"sh", "-c",
|
||||
fmt.Sprintf("mkdir -p /out && syft %s -o spdx-json=/out/sbom.spdx.json", syftTarget(imageRef)),
|
||||
}).
|
||||
File("/out/sbom.spdx.json"), nil
|
||||
|
||||
case "cyclonedx":
|
||||
// cyclonedx/cyclonedx-cli: convert/aggregate over the source tree.
|
||||
// We scan for existing component manifests and emit a CycloneDX doc.
|
||||
return dag.Container().
|
||||
From("cyclonedx/cyclonedx-cli:0.27.2").
|
||||
WithMountedDirectory("/src", source).
|
||||
WithWorkdir("/src").
|
||||
WithExec([]string{
|
||||
"sh", "-c",
|
||||
"mkdir -p /out && cyclonedx-cli --version > /dev/null 2>&1 || true; " +
|
||||
"if [ -f bom.xml ]; then cyclonedx-cli convert --input-file bom.xml --output-file /out/sbom.cdx.json --output-format json; " +
|
||||
"elif [ -f bom.json ]; then cyclonedx-cli convert --input-file bom.json --output-file /out/sbom.cdx.json --output-format json; " +
|
||||
"else echo '{\"bomFormat\":\"CycloneDX\",\"specVersion\":\"1.5\",\"version\":1,\"components\":[]}' > /out/sbom.cdx.json; fi",
|
||||
}).
|
||||
File("/out/sbom.cdx.json"), nil
|
||||
|
||||
case "trivy":
|
||||
// aquasec/trivy fs/image --format cyclonedx.
|
||||
subcmd := "fs"
|
||||
target := "."
|
||||
if imageRef != "" {
|
||||
subcmd = "image"
|
||||
target = imageRef
|
||||
}
|
||||
return 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")).
|
||||
WithMountedDirectory("/src", source).
|
||||
WithWorkdir("/src").
|
||||
WithExec([]string{
|
||||
"sh", "-c",
|
||||
fmt.Sprintf("mkdir -p /out && trivy %s --format cyclonedx --output /out/sbom.cdx.json %s", subcmd, target),
|
||||
}).
|
||||
File("/out/sbom.cdx.json"), nil
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported SBOM generator: %q (want syft|cyclonedx|spdx-tools|trivy|none)", generator)
|
||||
}
|
||||
}
|
||||
|
||||
// syftTarget names the SOURCE syft should read, explicitly.
|
||||
//
|
||||
// A bare image reference leaves syft to guess, and its guess chain starts with
|
||||
// a local Docker daemon. Inside a Dagger container there is no daemon and no
|
||||
// docker socket, so the chain runs out and syft gives up on the whole run.
|
||||
// MEASURED on a live build against a managed registry with no ingress
|
||||
// certificate: `could not determine source: ... '<host>:<port>/library/<repo>'`
|
||||
// — an SBOM stage that failed the build for every image it was ever pointed at.
|
||||
//
|
||||
// `registry:` names the one source that does exist here, so syft pulls the
|
||||
// manifest itself. It is correct for a TLS registry too; nothing about it is
|
||||
// specific to the plain-HTTP case below.
|
||||
func syftTarget(imageRef string) string {
|
||||
if imageRef == "" {
|
||||
return "dir:/src"
|
||||
}
|
||||
return "registry:" + imageRef
|
||||
}
|
||||
|
||||
// syftRegistryEnv applies the registry options syft reads from its environment.
|
||||
//
|
||||
// OPT-IN and nothing else: with insecure false — the default, and what every
|
||||
// existing caller gets — the container is returned untouched and syft keeps
|
||||
// requiring TLS. It is only relevant when there is an image to pull at all.
|
||||
//
|
||||
// Why an env var rather than a flag: syft has no per-invocation `--insecure`.
|
||||
// Its registry behaviour is configuration (`registry.insecure-use-http` /
|
||||
// `registry.insecure-skip-tls-verify`), and every syft configuration key has an
|
||||
// `SYFT_`-prefixed environment spelling. This is the same shape as the crane
|
||||
// `--insecure` path in standard-build: one boolean, resolved from the registry
|
||||
// the tenant actually connected, never an address written down here.
|
||||
func syftRegistryEnv(c *dagger.Container, imageRef string, insecure bool) *dagger.Container {
|
||||
if !insecure || imageRef == "" {
|
||||
return c
|
||||
}
|
||||
return c.
|
||||
WithEnvVariable("SYFT_REGISTRY_INSECURE_USE_HTTP", "true").
|
||||
WithEnvVariable("SYFT_REGISTRY_INSECURE_SKIP_TLS_VERIFY", "true")
|
||||
}
|
||||
Reference in new issue
Block a user