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

+61
View File
@@ -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
}
+54
View File
@@ -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)
}
}