chore: publish IntegraCI build modules c7167a3a27c2

This commit is contained in:
dhimaz0445 committed 2026-10-30 02:20:32 +00:00
1 parent 5f8a24d014
commit f58e80187a
7 files changed
+250 -20

No files matched your search

+96 -16
View File
@@ -23,6 +23,7 @@ package main
import (
"context"
"dagger/sbom/internal/dagger"
"dagger/sbom/pullauth"
"fmt"
)
@@ -52,7 +53,21 @@ func (m *Sbom) Generate(
// never a default, never an address in this file.
// +optional
registryInsecure bool,
// Registry username for pulling --image-ref (paired with
// --registry-password). Same pair standard-build pushed the image with: a
// registry that authenticates reads (Nexus forceBasicAuth) refuses an
// anonymous manifest GET with UNAUTHORIZED.
// +optional
registryUsername string,
// Registry password/token as a Dagger secret — reaches the generator as a
// secret variable only, never a plain env var or a command-line argument.
// +optional
registryPassword *dagger.Secret,
) (*dagger.File, error) {
auth, err := pullauth.Resolve(imageRef, registryUsername, registryPassword != nil)
if err != nil {
return nil, err
}
switch generator {
case "none":
// No-op skip: emit a marker file mirroring the adapter's
@@ -71,11 +86,9 @@ func (m *Sbom) Generate(
// 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().
return syftRegistryAuth(syftRegistryEnv(scanInput(dag.Container().
From("alpine:3.20").
WithFile("/usr/local/bin/syft", syftBin).
WithMountedDirectory("/src", source).
WithWorkdir("/src"), imageRef, registryInsecure).
WithFile("/usr/local/bin/syft", syftBin), source, auth), imageRef, registryInsecure), auth, registryPassword).
WithExec([]string{
"sh", "-c",
fmt.Sprintf("mkdir -p /out && syft %s -o cyclonedx-json=/out/sbom.cdx.json", syftTarget(imageRef)),
@@ -86,11 +99,9 @@ func (m *Sbom) Generate(
// 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().
return syftRegistryAuth(syftRegistryEnv(scanInput(dag.Container().
From("alpine:3.20").
WithFile("/usr/local/bin/syft", syftBin).
WithMountedDirectory("/src", source).
WithWorkdir("/src"), imageRef, registryInsecure).
WithFile("/usr/local/bin/syft", syftBin), source, auth), imageRef, registryInsecure), auth, registryPassword).
WithExec([]string{
"sh", "-c",
fmt.Sprintf("mkdir -p /out && syft %s -o spdx-json=/out/sbom.spdx.json", syftTarget(imageRef)),
@@ -121,16 +132,16 @@ func (m *Sbom) Generate(
subcmd = "image"
target = imageRef
}
return dag.Container().
ctr := scanInput(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),
}).
WithMountedCache("/root/.cache/trivy", dag.CacheVolume("trivy-db")), source, auth)
script := fmt.Sprintf("mkdir -p /out && trivy %s --format cyclonedx --output /out/sbom.cdx.json %s", subcmd, target)
if auth.Enabled() && registryPassword != nil {
ctr, script = trivyRegistryAuth(ctr, auth, registryPassword), trivyDockerConfigScript+" && "+script
}
return ctr.
WithExec([]string{"sh", "-c", script}).
File("/out/sbom.cdx.json"), nil
default:
@@ -177,3 +188,72 @@ func syftRegistryEnv(c *dagger.Container, imageRef string, insecure bool) *dagge
WithEnvVariable("SYFT_REGISTRY_INSECURE_USE_HTTP", "true").
WithEnvVariable("SYFT_REGISTRY_INSECURE_SKIP_TLS_VERIFY", "true")
}
// syftRegistryAuth gives syft the login for the registry holding --image-ref.
//
// syft (v1.18.1, cmd/syft/internal/options/registry.go) reads one credential
// from SYFT_REGISTRY_AUTH_AUTHORITY / _USERNAME / _PASSWORD and prepends it to
// its configured list, so it wins over any on-disk config. The authority is
// the registry host of the image reference — the same host standard-build's
// WithRegistryAuth authenticated the push against.
//
// The password goes in with WithSecretVariable ONLY: Dagger scrubs a secret's
// value from logs and keeps it out of the layer cache key, which a plain env
// var or an argument in the exec line would not. Without credentials the
// container is returned untouched, as before.
func syftRegistryAuth(c *dagger.Container, auth pullauth.Auth, password *dagger.Secret) *dagger.Container {
if !auth.Enabled() || password == nil {
return c
}
return c.
WithEnvVariable("SYFT_REGISTRY_AUTH_AUTHORITY", auth.Authority).
WithEnvVariable("SYFT_REGISTRY_AUTH_USERNAME", auth.Username).
WithSecretVariable("SYFT_REGISTRY_AUTH_PASSWORD", password)
}
// trivyRegistryAuth stages the login for `trivy image` WITHOUT trivy's own
// TRIVY_USERNAME / TRIVY_PASSWORD: those are not scoped to a host, and trivy
// presents them on every OCI pull it makes — the vulnerability DB downloads
// from mirror.gcr.io / ghcr.io included (security review 2026-10-29). Instead
// the exec line (trivyDockerConfigScript) writes a DOCKER_CONFIG holding one
// `auths` entry for the --image-ref authority only, which go-containerregistry's
// keychain answers for that host and no other.
//
// The password is a secret variable under a name trivy does not read; the
// script turns it into the config file without echoing it. Untouched without
// credentials.
func trivyRegistryAuth(c *dagger.Container, auth pullauth.Auth, password *dagger.Secret) *dagger.Container {
if !auth.Enabled() || password == nil {
return c
}
return c.
WithEnvVariable("INTEGRACI_SBOM_REG_AUTHORITY", auth.Authority).
WithEnvVariable("INTEGRACI_SBOM_REG_USER", auth.Username).
WithSecretVariable("INTEGRACI_SBOM_REG_PASS", password).
WithEnvVariable("DOCKER_CONFIG", "/run/integraci-sbom-docker")
}
// trivyDockerConfigScript writes $DOCKER_CONFIG/config.json with the single
// host-keyed login staged by trivyRegistryAuth. umask 077, no `set -x`, values
// only ever expanded inside printf/base64 arguments.
const trivyDockerConfigScript = `umask 077 && mkdir -p "$DOCKER_CONFIG" && ` +
`printf '{"auths":{"%s":{"auth":"%s"}}}' "$INTEGRACI_SBOM_REG_AUTHORITY" ` +
`"$(printf '%s:%s' "$INTEGRACI_SBOM_REG_USER" "$INTEGRACI_SBOM_REG_PASS" | base64 | tr -d '[:space:]')" ` +
`> "$DOCKER_CONFIG/config.json"`
// scanInput mounts the repository as the generator's working directory — except
// when the generator is about to hold a registry login for an image scan.
//
// The image path reads the registry, not the tree, so the checkout is not
// needed there; and the checkout is repository-controlled: syft reads
// ./.syft.yaml (which can set registry.insecure-use-http and send the Basic
// login in cleartext) and trivy reads ./trivy.yaml (which can point its DB
// repository at an attacker's host) from the working directory (security
// review 2026-10-29). So with a login, the working directory is an empty
// /work. Without one, behaviour is exactly as before.
func scanInput(c *dagger.Container, source *dagger.Directory, auth pullauth.Auth) *dagger.Container {
if auth.Enabled() {
return c.WithWorkdir("/work")
}
return c.WithMountedDirectory("/src", source).WithWorkdir("/src")
}