From f58e80187a91e746078a3b3cc7145631bb22c8fd Mon Sep 17 00:00:00 2001 From: dhimaz0445 Date: Fri, 30 Oct 2026 02:20:32 +0000 Subject: [PATCH] chore: publish IntegraCI build modules c7167a3a27c2 --- .integraci/modules-c7167a3a27c2 | 1 + SHA256SUMS | 8 ++- sbom/README.md | 31 +++++++++ sbom/dagger.json | 3 +- sbom/main.go | 112 +++++++++++++++++++++++++++----- sbom/pullauth/pullauth.go | 61 +++++++++++++++++ sbom/pullauth/pullauth_test.go | 54 +++++++++++++++ 7 files changed, 250 insertions(+), 20 deletions(-) create mode 100644 .integraci/modules-c7167a3a27c2 create mode 100644 sbom/pullauth/pullauth.go create mode 100644 sbom/pullauth/pullauth_test.go diff --git a/.integraci/modules-c7167a3a27c2 b/.integraci/modules-c7167a3a27c2 new file mode 100644 index 0000000..4d99016 --- /dev/null +++ b/.integraci/modules-c7167a3a27c2 @@ -0,0 +1 @@ +c7167a3a27c2 \ No newline at end of file diff --git a/SHA256SUMS b/SHA256SUMS index 0a4cc84..8d3c7ca 100644 --- a/SHA256SUMS +++ b/SHA256SUMS @@ -99,9 +99,11 @@ d59d142d4dd45fa8b88dbbeddc823b295837988d1e53697c33bb1d80aabc7b81 sast-scan/gate 7c851f2052519be229cd129369489919a8c583f8069b1d13fe48ccdccf33cd08 sast-scan/main.go 3f3393851222017215220595f0f5d64a1e3d80737359b5af68581570f0464fe7 sast-scan/qualitygate.go 01049098cdd9eca7dbba403048d00375403d8bc908d463204e7b5373daa24da9 sast-scan/qualitygate_test.go -f72d25aef761141e41677c02b260f0a6492a3ec529ca00056a5c02ddbfd96061 sbom/README.md -43d00928ea6cefb68f805179aa8f8858889378d0c67b766462d7362ef2bc892e sbom/dagger.json -0768796b1f0ac5d197a8c7834a32f5a818c2739e43f379ee2dc95cb15f488972 sbom/main.go +8ea5387206da3d738c1be264f2e3981c0f7c72339dc0de5ec9b9159012e79f94 sbom/README.md +6cb4a1ec6f25f79d6793e90251661c193e6ce699300effcf7eb442fe176f1615 sbom/dagger.json +8601ef0ffd9ff5b8bcf8efdaac262532027d08ca502364a340c5291479f05143 sbom/main.go +94c17dc2b770dc38bea8587fcbed8ef1f89329aaa122cb24eb147c71d3dad551 sbom/pullauth/pullauth.go +9aca87f5be9e90e3fb8204d5891a3d2707b7c67035d6b8301e4de80834dbd68c sbom/pullauth/pullauth_test.go 66beccb4d75bf7f615aa68d657b81298067fb27a59ee76fce8a6fbf47685e7cb secret-scan/README.md 37c26c209ef6347363573933d603ec08dfda91c28f46b83046aacf9967c9189b secret-scan/dagger.json dd4809b411a47584256cdafbc513ed2f9a1f2fe9c095381255ad8c8c008ada30 secret-scan/main.go diff --git a/sbom/README.md b/sbom/README.md index 17b6bde..eb047e3 100644 --- a/sbom/README.md +++ b/sbom/README.md @@ -18,9 +18,36 @@ dagger call generate --source . [--image-ref REF] [--generator NAME] | `--source` | yes | — | source directory to analyze | | `--image-ref`| no | "" | container image to scan instead of the filesystem | | `--generator`| no | `syft` | `syft \| cyclonedx \| spdx-tools \| trivy \| none` | +| `--registry-insecure` | no | false | the `--image-ref` registry is plain HTTP / self-signed (syft only) | +| `--registry-username` | no | "" | login for pulling `--image-ref` (paired with `--registry-password`) | +| `--registry-password` | no | — | Dagger secret (`env://VAR`), never a literal | Returns the SBOM document as a `File`. +### Registry login + +A registry that authenticates reads (Nexus with `forceBasicAuth`, most private +Harbor projects) refuses an anonymous manifest GET with `UNAUTHORIZED`. Give the +pull the same login the build pushed with: + +- `syft` / `spdx-tools`: `SYFT_REGISTRY_AUTH_AUTHORITY` (the registry host of + `--image-ref`), `SYFT_REGISTRY_AUTH_USERNAME`, and `SYFT_REGISTRY_AUTH_PASSWORD` + as a secret variable — the names syft v1.18.1 reads + (`cmd/syft/internal/options/registry.go`). +- `trivy`: NOT `TRIVY_USERNAME`/`TRIVY_PASSWORD`, which trivy presents on every + OCI pull including its vulnerability-DB downloads. The exec line writes a + private `DOCKER_CONFIG` with one `auths` entry for the `--image-ref` host only, + from a secret variable. +- With a login, the repository checkout is **not** mounted and the working + directory is an empty `/work`: the image scan does not need the tree, and a + repository-controlled `.syft.yaml` / `trivy.yaml` could otherwise send the + login over plain HTTP or to another host. + +Only both halves turn the login on. A complete pair with an `--image-ref` that +names no registry host is refused rather than sent to a guessed host. The +host-resolution logic lives in the stdlib-only `pullauth/` package so it can be +unit-tested without an engine session. + ## Generators | generator | image | output | notes | @@ -40,6 +67,10 @@ dagger call generate --source . -o sbom.cdx.json # Trivy scanning a built image dagger call generate --source . --image-ref ghcr.io/org/app:tag --generator trivy -o sbom.cdx.json +# Private registry: the password is read from the environment as a secret +dagger call generate --source . --image-ref registry.example.test:8082/app:tag \ + --registry-username "$REG_USER" --registry-password env://REG_PASS -o sbom.cdx.json + # SPDX document dagger call generate --source . --generator spdx-tools -o sbom.spdx.json ``` diff --git a/sbom/dagger.json b/sbom/dagger.json index 40b7934..813c98e 100644 --- a/sbom/dagger.json +++ b/sbom/dagger.json @@ -6,6 +6,7 @@ }, "source": ".", "include": [ - "./main.go" + "./main.go", + "./pullauth/pullauth.go" ] } diff --git a/sbom/main.go b/sbom/main.go index 19485ef..d1f904c 100644 --- a/sbom/main.go +++ b/sbom/main.go @@ -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") +} diff --git a/sbom/pullauth/pullauth.go b/sbom/pullauth/pullauth.go new file mode 100644 index 0000000..a506db5 --- /dev/null +++ b/sbom/pullauth/pullauth.go @@ -0,0 +1,61 @@ +// Package pullauth resolves the non-secret half of the registry login the +// sbom module's generators pull --image-ref with. +// +// Stdlib-only on purpose: the generated `dagger` package panics at init +// without an engine session, so logic in package main cannot run under a plain +// `go test`. Same split as cosign/attestation and standard-build/evidence, and +// tested the same way (.github/workflows/dagger-modules.yml, evidence-unit). +// +// The password never passes through here: it stays a *dagger.Secret from the +// caller to the generator's secret variable. +package pullauth + +import ( + "fmt" + "strings" +) + +// Auth is who to log in as, and where. +type Auth struct { + Authority string + Username string +} + +// Enabled reports whether a login should be given to the generator at all. +func (a Auth) Enabled() bool { return a.Authority != "" && a.Username != "" } + +// Resolve decides whether the generator gets a login. +// +// No image, or no complete username+password pair: no login, which is the +// behaviour every caller had before this existed. A half pair is the same — +// cosign, security-scan and standard-build all require both halves too. +// +// A complete pair for an image reference with no registry host (docker.io +// shorthand) is an ERROR, not a silent anonymous pull: there is no authority +// to scope the credential to, and guessing one would either send it to the +// wrong host or drop it and fail later with a misleading UNAUTHORIZED. The +// error names the image reference only, never the username or password. +func Resolve(imageRef, username string, hasPassword bool) (Auth, error) { + if imageRef == "" || username == "" || !hasPassword { + return Auth{}, nil + } + host := RegistryHost(imageRef) + if host == "" { + return Auth{}, fmt.Errorf( + "--registry-username/--registry-password given, but --image-ref %q names no registry host to log in to", + imageRef) + } + return Auth{Authority: host, Username: username}, nil +} + +// RegistryHost is the host portion of an image reference — "harbor.internal:30002" +// out of "harbor.internal:30002/library/orders:abc". A reference with no host +// segment (docker.io shorthand) yields "". Same rule as standard-build's +// registryHost, which authenticated the push this pull reads back. +func RegistryHost(imageRef string) string { + head, _, found := strings.Cut(imageRef, "/") + if !found || (!strings.Contains(head, ".") && !strings.Contains(head, ":") && head != "localhost") { + return "" + } + return head +} diff --git a/sbom/pullauth/pullauth_test.go b/sbom/pullauth/pullauth_test.go new file mode 100644 index 0000000..87ab747 --- /dev/null +++ b/sbom/pullauth/pullauth_test.go @@ -0,0 +1,54 @@ +package pullauth + +import ( + "strings" + "testing" +) + +func TestResolve(t *testing.T) { + const ref = "artifact.example.test:8082/sample-svc1:11f8b56d0fdd" + cases := []struct { + name string + imageRef string + username string + hasPassword bool + want Auth + wantErr bool + }{ + {"no credentials: unchanged", ref, "", false, Auth{}, false}, + {"username without password: no login", ref, "ci", false, Auth{}, false}, + {"password without username: no login", ref, "", true, Auth{}, false}, + {"source scan: no login", "", "ci", true, Auth{}, false}, + {"host with port", ref, "ci", true, Auth{Authority: "artifact.example.test:8082", Username: "ci"}, false}, + {"host with namespace", "harbor.internal/library/orders:abc", "ci", true, Auth{Authority: "harbor.internal", Username: "ci"}, false}, + {"digest-pinned", "harbor.internal/orders@sha256:" + "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", "ci", true, Auth{Authority: "harbor.internal", Username: "ci"}, false}, + {"localhost", "localhost/app:1", "ci", true, Auth{Authority: "localhost", Username: "ci"}, false}, + {"docker.io shorthand refuses", "library/app:1", "ci", true, Auth{}, true}, + {"bare name refuses", "app:1", "ci", true, Auth{}, true}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got, err := Resolve(tc.imageRef, tc.username, tc.hasPassword) + if (err != nil) != tc.wantErr { + t.Fatalf("err = %v, wantErr %v", err, tc.wantErr) + } + if got != tc.want { + t.Fatalf("got %+v, want %+v", got, tc.want) + } + if got.Enabled() != (tc.want.Authority != "") { + t.Fatalf("Enabled() = %v for %+v", got.Enabled(), got) + } + }) + } +} + +// The refusal must not echo the credential it refused to place. +func TestResolveErrorNamesNoCredential(t *testing.T) { + _, err := Resolve("app:1", "the-user-name", true) + if err == nil { + t.Fatal("expected an error") + } + if got := err.Error(); strings.Contains(got, "the-user-name") { + t.Fatalf("error leaks the username: %s", got) + } +}