chore: publish IntegraCI build modules a11ec75d3e09

This commit is contained in:
dhimaz0445 committed 2026-11-11 03:41:55 +00:00
1 parent 694710104a
commit 3c2b80914e
78 files changed
+4982 -405

No files matched your search

+201
View File
@@ -0,0 +1,201 @@
// Package pushgate holds the pure half of scan-before-push (ADR-0093 §5,
// WP-06): which digest an OCI archive carries, whether the registry kept it,
// and what the platform's pre-push verdict says.
//
// Standard library only, for the reason evidence/ is: the generated `dagger`
// package panics at init without a Dagger session, so nothing in package main
// runs under a plain `go test`.
//
// The order every rendered engine runs (ADR-0093 §5):
//
// build to an OCI archive -> scan the archive -> ask the verdict for the
// archive's manifest digest -> push only if the answer is not `refuse` ->
// the registry's digest must equal the archive's (mismatch fails).
//
// The push step runs in the build job, which has executed the change's code,
// so it is hygiene, not a control. The control is G3: the deploy gate judges
// the pushed DIGEST (ADR-0093 §5.4).
package pushgate
import (
"encoding/json"
"errors"
"fmt"
"regexp"
"strings"
)
var digestRE = regexp.MustCompile(`^sha256:[0-9a-f]{64}$`)
// versionRE is the tag grammar of the platform's deploy endpoint and of the
// security verdict (`security_verdict._VERSION_RE`).
var versionRE = regexp.MustCompile(`^[A-Za-z0-9_][A-Za-z0-9._-]{0,127}$`)
// manifestMediaTypes are the single-image manifests an archive may name. An
// image INDEX is refused: the scan describes one platform's image, and a
// pushed index would carry a digest no scan describes.
var manifestMediaTypes = map[string]bool{
"application/vnd.oci.image.manifest.v1+json": true,
"application/vnd.docker.distribution.manifest.v2+json": true,
}
// RegistryDigestPrefix starts the line the push script prints with the digest
// the REGISTRY reports for the pushed reference.
const RegistryDigestPrefix = "REGISTRY_DIGEST "
// IsDigest reports whether s is `sha256:<64 lower-case hex>`.
func IsDigest(s string) bool { return digestRE.MatchString(s) }
// ManifestDigestFromIndex returns the manifest digest an OCI image layout's
// index.json names. Exactly one manifest, of a single-image media type.
func ManifestDigestFromIndex(indexJSON []byte) (string, error) {
var index struct {
Manifests []struct {
MediaType string `json:"mediaType"`
Digest string `json:"digest"`
} `json:"manifests"`
}
if err := json.Unmarshal(indexJSON, &index); err != nil {
return "", fmt.Errorf("the archive's index.json is not JSON: %w", err)
}
if len(index.Manifests) != 1 {
return "", fmt.Errorf(
"the archive names %d manifests; one platform per digest is supported", len(index.Manifests))
}
m := index.Manifests[0]
if !manifestMediaTypes[m.MediaType] {
return "", fmt.Errorf("the archive's manifest has media type %q, not a single image", m.MediaType)
}
if !IsDigest(m.Digest) {
return "", fmt.Errorf("the archive's manifest digest %q is not sha256", m.Digest)
}
return m.Digest, nil
}
// RegistryDigestFrom returns the digest on the push script's
// RegistryDigestPrefix line, or "" when there is none (or it is not sha256).
func RegistryDigestFrom(out string) string {
for _, line := range strings.Split(out, "\n") {
line = strings.TrimSpace(line)
if rest, ok := strings.CutPrefix(line, RegistryDigestPrefix); ok {
rest = strings.TrimSpace(rest)
if IsDigest(rest) {
return rest
}
}
}
return ""
}
// SameDigest fails unless the registry kept the archive's manifest byte for
// byte. On a mismatch the caller must FAIL and record nothing: the scan and
// the verdict described the archive's digest, never the registry's
// (ADR-0093 §5.2, "the push step fails rather than rebinding evidence").
func SameDigest(archive, registry string) error {
if !IsDigest(archive) {
return fmt.Errorf("archive digest %q is not sha256", archive)
}
if !IsDigest(registry) {
return errors.New("the registry reported no sha256 digest for the pushed reference")
}
if archive != registry {
return fmt.Errorf(
"registry digest %s differs from the scanned archive's %s: the registry rewrote the image; "+
"nothing is recorded for it", registry, archive)
}
return nil
}
// Request is the body of POST /api/v1/secure-delivery/push-verdict.
type Request struct {
ServiceID string `json:"service_id"`
ImageDigest string `json:"image_digest"`
Version string `json:"version"`
}
// RequestBody validates and encodes the verdict request.
func RequestBody(serviceID, digest, version string) ([]byte, error) {
if strings.TrimSpace(serviceID) == "" {
return nil, errors.New("push-verdict needs --service-id")
}
if !IsDigest(digest) {
return nil, fmt.Errorf("push-verdict: %q is not a sha256 digest", digest)
}
if !versionRE.MatchString(version) {
return nil, fmt.Errorf("push-verdict: version %q is not an image tag", version)
}
return json.Marshal(Request{ServiceID: serviceID, ImageDigest: digest, Version: version})
}
// Answer is the platform's reply (app/security/schemas/push_verdict.py
// PushVerdictRead). apps/api/tests/test_scan_before_push.py holds the JSON
// names equal on both sides.
type Answer struct {
Decision string `json:"decision"`
Result string `json:"result"`
Mode string `json:"mode"`
Reasons []string `json:"reasons"`
WouldBlock []string `json:"would_block"`
}
// Outcome is what the push step does with an answer.
type Outcome struct {
Push bool
Message string
}
// Judge turns the platform's HTTP answer into the push decision.
//
// An explicit, well-formed `refuse` stops the push, and so does an explicit
// AUTHORIZATION refusal (401/403): the caller configured a token, and the
// platform said this token may not ask about this service or revision
// (expired, revoked, `scan-token-wrong-revision`, `scan-token-unbound`).
// Pushing then would let a refused binding publish silently, with no server
// trace (WP-06 review, 2026-11-04).
//
// Anything else -- the platform unreachable (a Jenkins on a customer network
// often cannot call back), a 5xx or other non-2xx, an unreadable body, an
// unknown decision -- pushes and says so loudly: this step is hygiene, and the
// deploy gate (G3) still judges the pushed digest. Failing closed on transport
// errors would stop every build whose CI cannot reach the platform, without
// making anything safer.
func Judge(status int, body []byte) Outcome {
if status == 401 || status == 403 {
return Outcome{false, fmt.Sprintf(
"verdict refused the request (HTTP %d: the token may not ask for this service/revision): "+
"image NOT pushed", status)}
}
if status < 200 || status >= 300 {
return Outcome{true, fmt.Sprintf(
"verdict NOT answered (HTTP %d): pushed; the deploy gate judges this digest", status)}
}
var a Answer
if err := json.Unmarshal(body, &a); err != nil {
return Outcome{true, "verdict NOT readable: pushed; the deploy gate judges this digest"}
}
switch a.Decision {
case "refuse":
families := "unknown"
if len(a.Reasons) > 0 {
names := make([]string, 0, len(a.Reasons))
for _, r := range a.Reasons {
name, _, _ := strings.Cut(r, ":")
names = append(names, strings.TrimSpace(name))
}
families = strings.Join(names, ", ")
}
return Outcome{false, fmt.Sprintf(
"verdict: block (%s): image NOT pushed. %s", families, strings.Join(a.Reasons, "; "))}
case "push":
msg := fmt.Sprintf("verdict: %s (mode %s): pushing", a.Result, a.Mode)
if len(a.WouldBlock) > 0 {
msg += fmt.Sprintf("; would block in enforce: %s (recorded as secure_delivery.would_block)",
strings.Join(a.WouldBlock, ", "))
}
return Outcome{true, msg}
default:
return Outcome{true, fmt.Sprintf(
"verdict answered %q, which is not push/refuse: pushed; the deploy gate judges this digest",
a.Decision)}
}
}
+124
View File
@@ -0,0 +1,124 @@
package pushgate
import (
"strings"
"testing"
)
const (
hexA = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
hexB = "fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210"
)
func TestManifestDigestFromIndexTakesTheOneImage(t *testing.T) {
index := `{"schemaVersion":2,"manifests":[{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:` + hexA + `","size":1}]}`
got, err := ManifestDigestFromIndex([]byte(index))
if err != nil || got != "sha256:"+hexA {
t.Fatalf("got %q, %v", got, err)
}
docker := strings.Replace(index, "application/vnd.oci.image.manifest.v1+json",
"application/vnd.docker.distribution.manifest.v2+json", 1)
if _, err := ManifestDigestFromIndex([]byte(docker)); err != nil {
t.Fatalf("a docker v2 manifest is a single image too: %v", err)
}
}
func TestManifestDigestFromIndexRefusesWhatIsNotOneImage(t *testing.T) {
m := `{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:` + hexA + `"}`
for name, index := range map[string]string{
"two manifests": `{"manifests":[` + m + `,` + m + `]}`,
"none": `{"manifests":[]}`,
"an index": `{"manifests":[{"mediaType":"application/vnd.oci.image.index.v1+json","digest":"sha256:` + hexA + `"}]}`,
"sha512": `{"manifests":[{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha512:` + hexA + `"}]}`,
"upper hex": `{"manifests":[{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:` + strings.ToUpper(hexA) + `"}]}`,
"not json": `manifests`,
} {
if _, err := ManifestDigestFromIndex([]byte(index)); err == nil {
t.Errorf("%s: accepted", name)
}
}
}
func TestRegistryDigestFromReadsOnlyItsLine(t *testing.T) {
out := "logged in\npushed blob sha256:" + hexB + "\nPUSHED x\n" + RegistryDigestPrefix + "sha256:" + hexA + "\n"
if got := RegistryDigestFrom(out); got != "sha256:"+hexA {
t.Fatalf("got %q", got)
}
if got := RegistryDigestFrom("pushed blob sha256:" + hexA); got != "" {
t.Fatalf("a blob digest is not the registry's manifest digest, got %q", got)
}
if got := RegistryDigestFrom(RegistryDigestPrefix + "sha256:abc"); got != "" {
t.Fatalf("a short digest is not a digest, got %q", got)
}
}
// Mutant: make SameDigest return nil unconditionally -> this test fails.
func TestSameDigestFailsOnAMismatch(t *testing.T) {
if err := SameDigest("sha256:"+hexA, "sha256:"+hexA); err != nil {
t.Fatalf("equal digests: %v", err)
}
err := SameDigest("sha256:"+hexA, "sha256:"+hexB)
if err == nil || !strings.Contains(err.Error(), "differs") {
t.Fatalf("mismatch accepted: %v", err)
}
if SameDigest("sha256:"+hexA, "") == nil {
t.Fatal("a registry that reported no digest was accepted")
}
}
func TestRequestBodyValidates(t *testing.T) {
if _, err := RequestBody("svc", "sha256:"+hexA, "0123456789ab"); err != nil {
t.Fatalf("legitimate request refused: %v", err)
}
if _, err := RequestBody("svc", "sha256:"+hexA, "0123456789ab-rc-build.7"); err != nil {
t.Fatalf("a UAT rebuild tag is a legitimate version: %v", err)
}
for name, args := range map[string][3]string{
"no service": {"", "sha256:" + hexA, "v1"},
"tag digest": {"svc", "latest", "v1"},
"bad version": {"svc", "sha256:" + hexA, "../x"},
"empty version": {"svc", "sha256:" + hexA, ""},
} {
if _, err := RequestBody(args[0], args[1], args[2]); err == nil {
t.Errorf("%s: accepted", name)
}
}
}
// Mutant: make Judge push on `refuse` -> this test fails.
func TestJudgeRefusesOnlyAnExplicitRefusal(t *testing.T) {
refused := Judge(200, []byte(`{"decision":"refuse","result":"block","mode":"enforce","reasons":["cve: 2 critical"],"would_block":[]}`))
if refused.Push || !strings.Contains(refused.Message, "verdict: block (cve)") {
t.Fatalf("refusal: %+v", refused)
}
}
// Mutant (legal direction): make Judge refuse everything -> this test fails.
func TestJudgePushesInShadowAndSaysWhatWouldBlock(t *testing.T) {
shadow := Judge(200, []byte(`{"decision":"push","result":"block","mode":"shadow","reasons":[],"would_block":["cve"]}`))
if !shadow.Push || !strings.Contains(shadow.Message, "would block in enforce: cve") {
t.Fatalf("shadow: %+v", shadow)
}
for name, o := range map[string]Outcome{
"unreachable": Judge(0, nil),
"server error": Judge(503, nil),
"not found": Judge(404, nil),
"garbage": Judge(200, []byte(`<html>`)),
"unknown word": Judge(200, []byte(`{"decision":"maybe"}`)),
} {
if !o.Push {
t.Errorf("%s: refused a push without an explicit refusal (%s)", name, o.Message)
}
}
}
// Mutant: drop the 401/403 branch in Judge -> this test fails (a refused token
// binding would push silently). WP-06 review, 2026-11-04.
func TestJudgeRefusesAnAuthorizationRefusal(t *testing.T) {
for _, status := range []int{401, 403} {
o := Judge(status, []byte(`{"type":"scan-token-wrong-revision"}`))
if o.Push || !strings.Contains(o.Message, "image NOT pushed") {
t.Errorf("HTTP %d: %+v", status, o)
}
}
}