chore: publish IntegraCI build modules a11ec75d3e09
This commit is contained in:
1 parent
694710104a
commit
3c2b80914e
78 files changed
+4982
-405
No files matched your search
@@ -0,0 +1,128 @@
|
||||
package attestation
|
||||
|
||||
// Pure helpers for `platform-sign` (ADR-0093 §5, WP-07): the request the sign
|
||||
// step sends to the platform, and the checks on the platform's answer before
|
||||
// `cosign attach signature` publishes it. Standard library only.
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// PlatformSignPath is the platform route, with the digest as one path segment.
|
||||
const PlatformSignPath = "/api/v1/artifacts/%s/sign"
|
||||
|
||||
var (
|
||||
platformDigestRE = regexp.MustCompile(`^sha256:[0-9a-f]{64}$`)
|
||||
// The API's own patterns (app/security/schemas/platform_signing.py).
|
||||
platformVersionRE = regexp.MustCompile(`^[A-Za-z0-9_][A-Za-z0-9._-]{0,127}$`)
|
||||
platformRepositoryRE = regexp.MustCompile(`^[a-z0-9][a-z0-9.:/_-]{2,254}$`)
|
||||
platformServiceRE = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
|
||||
// A problem `type` URI's last segment: what a refusal is called.
|
||||
problemSlugRE = regexp.MustCompile(`[a-z0-9-]{1,80}$`)
|
||||
)
|
||||
|
||||
// PlatformSignRequest returns the JSON body of the sign request, after checking
|
||||
// every value against the API's own grammar (so a refusal is about the image,
|
||||
// never about a malformed call).
|
||||
func PlatformSignRequest(serviceID, version, repository, digest string) ([]byte, error) {
|
||||
if !platformDigestRE.MatchString(digest) {
|
||||
return nil, fmt.Errorf("--image-digest %q is not sha256:<64 hex>", digest)
|
||||
}
|
||||
if !platformServiceRE.MatchString(serviceID) {
|
||||
return nil, fmt.Errorf("--service-id %q is not a UUID", serviceID)
|
||||
}
|
||||
if !platformVersionRE.MatchString(version) {
|
||||
return nil, fmt.Errorf("--version %q is not an image tag or commit", version)
|
||||
}
|
||||
if !platformRepositoryRE.MatchString(repository) || strings.ContainsAny(repository, "@") {
|
||||
return nil, fmt.Errorf("--repository %q must be a lower-case registry/path with no tag or digest", repository)
|
||||
}
|
||||
return json.Marshal(map[string]string{
|
||||
"service_id": serviceID,
|
||||
"version": version,
|
||||
"repository": repository,
|
||||
})
|
||||
}
|
||||
|
||||
// PlatformSignature is what `cosign attach signature` needs from the answer.
|
||||
type PlatformSignature struct {
|
||||
// Payload is the exact simple-signing payload the platform signed.
|
||||
Payload []byte
|
||||
// SignatureB64 is the base64 signature over SHA-256(Payload).
|
||||
SignatureB64 string
|
||||
KeyFingerprint string
|
||||
}
|
||||
|
||||
// ParsePlatformSignature judges the platform's answer to a sign request.
|
||||
//
|
||||
// Anything but 200 is a refusal and an error naming the HTTP status and the
|
||||
// problem slug (never the request's token). A 200 is accepted only when it is
|
||||
// about the digest that was asked for, its payload is the cosign simple-signing
|
||||
// payload for that SAME digest, and the signature is base64: a payload about
|
||||
// another image must never be attached to this one.
|
||||
func ParsePlatformSignature(status int, body []byte, digest string) (*PlatformSignature, error) {
|
||||
if status != 200 {
|
||||
var problem struct {
|
||||
Type string `json:"type"`
|
||||
Detail any `json:"detail"`
|
||||
}
|
||||
_ = json.Unmarshal(body, &problem)
|
||||
slug := problemSlugRE.FindString(problem.Type)
|
||||
detail := fmt.Sprint(problem.Detail)
|
||||
if problem.Detail == nil {
|
||||
detail = ""
|
||||
}
|
||||
if len(detail) > 300 {
|
||||
detail = detail[:300]
|
||||
}
|
||||
if status == 0 {
|
||||
return nil, fmt.Errorf("NOT signed: the platform did not answer")
|
||||
}
|
||||
return nil, fmt.Errorf("NOT signed (HTTP %d %s): %s", status, slug, detail)
|
||||
}
|
||||
var answer struct {
|
||||
ImageDigest string `json:"image_digest"`
|
||||
Payload string `json:"payload"`
|
||||
Signature string `json:"signature"`
|
||||
KeyFingerprint string `json:"key_fingerprint"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &answer); err != nil {
|
||||
return nil, fmt.Errorf("NOT signed: the platform's answer is not JSON: %w", err)
|
||||
}
|
||||
if answer.ImageDigest != digest {
|
||||
return nil, fmt.Errorf("NOT signed: the platform signed %q, not %q", answer.ImageDigest, digest)
|
||||
}
|
||||
payload, err := base64.StdEncoding.DecodeString(answer.Payload)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("NOT signed: the payload is not base64: %w", err)
|
||||
}
|
||||
var doc struct {
|
||||
Critical struct {
|
||||
Image struct {
|
||||
Digest string `json:"docker-manifest-digest"`
|
||||
} `json:"image"`
|
||||
Type string `json:"type"`
|
||||
} `json:"critical"`
|
||||
}
|
||||
if err := json.Unmarshal(payload, &doc); err != nil {
|
||||
return nil, fmt.Errorf("NOT signed: the payload is not a simple-signing document: %w", err)
|
||||
}
|
||||
if doc.Critical.Image.Digest != digest {
|
||||
return nil, fmt.Errorf("NOT signed: the payload names %q, not %q", doc.Critical.Image.Digest, digest)
|
||||
}
|
||||
if doc.Critical.Type != "cosign container image signature" {
|
||||
return nil, fmt.Errorf("NOT signed: the payload type %q is not a cosign image signature", doc.Critical.Type)
|
||||
}
|
||||
if _, err := base64.StdEncoding.DecodeString(answer.Signature); err != nil || answer.Signature == "" {
|
||||
return nil, fmt.Errorf("NOT signed: the signature is not base64")
|
||||
}
|
||||
return &PlatformSignature{
|
||||
Payload: payload,
|
||||
SignatureB64: answer.Signature,
|
||||
KeyFingerprint: answer.KeyFingerprint,
|
||||
}, nil
|
||||
}
|
||||
Reference in new issue
Block a user