112 lines
4.0 KiB
Go
112 lines
4.0 KiB
Go
package main
|
|
|
|
// PlatformSign: the pipeline's sign step under platform-held signing
|
|
// (ADR-0093 §5, WP-07). The private key never leaves the platform's OpenBao;
|
|
// this step asks the platform to sign ONE pushed digest and attaches the
|
|
// answer with `cosign attach signature`.
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"dagger/cosign/attestation"
|
|
"dagger/cosign/internal/dagger"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// PlatformSign asks IntegraCI to sign `<repository>@<image-digest>` with the
|
|
// tenant's platform-held key, then attaches that signature to the image.
|
|
//
|
|
// The call is `POST <api-url>/api/v1/artifacts/<digest>/sign` with
|
|
// `{service_id, version, repository}` and the `ci-sign:<service>` token
|
|
// (`--token`, scope `artifacts:sign`, bound to the service). The platform
|
|
// signs only a digest its own registry read ties to the version's tag, with an
|
|
// analysis-of-record image scan of that version and no `block`/`unknown` in
|
|
// the security verdict. Any refusal FAILS this call ("NOT signed (HTTP 409
|
|
// signing-...)"), so a pipeline whose push was refused, or whose image is
|
|
// blocked, carries no signature.
|
|
//
|
|
// Before attaching, the answer is checked: it must be about the digest that
|
|
// was asked for and its payload must name that digest. Nothing goes to a
|
|
// transparency log; the platform's own verifier reads the signature with the
|
|
// tenant's public key (GET /api/v1/artifact-attestations/custody).
|
|
//
|
|
// +cache="never"
|
|
func (m *Cosign) PlatformSign(
|
|
ctx context.Context,
|
|
// IntegraCI API base URL
|
|
apiURL string,
|
|
// The `ci-sign:<service>` token (INTEGRACI_SIGN_TOKEN)
|
|
token *dagger.Secret,
|
|
serviceID string,
|
|
// The commit or image tag the image was built as (its scans are filed under it)
|
|
version string,
|
|
// registry/path of the pushed image, no tag and no digest
|
|
repository string,
|
|
// The pushed digest, sha256:<hex> (the archive digest push-archive verified)
|
|
imageDigest string,
|
|
// Registry username, so cosign can push the signature
|
|
// +optional
|
|
registryUsername string,
|
|
// Registry password/token for --registry-username
|
|
// +optional
|
|
registryPassword *dagger.Secret,
|
|
// The registry is served over plain HTTP / self-signed TLS
|
|
// +optional
|
|
registryInsecure bool,
|
|
) (string, error) {
|
|
body, err := attestation.PlatformSignRequest(serviceID, version, repository, imageDigest)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if strings.TrimSpace(apiURL) == "" || token == nil {
|
|
return "", fmt.Errorf("NOT signed: no --api-url or --token (the platform holds the key)")
|
|
}
|
|
tokenStr, err := token.Plaintext(ctx)
|
|
if err != nil {
|
|
return "", fmt.Errorf("read token secret: %w", err)
|
|
}
|
|
endpoint := strings.TrimRight(apiURL, "/") + fmt.Sprintf(attestation.PlatformSignPath, imageDigest)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body))
|
|
if err != nil {
|
|
return "", fmt.Errorf("build request: %w", err)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "Bearer "+tokenStr)
|
|
status, answer := 0, []byte(nil)
|
|
client := &http.Client{Timeout: 60 * time.Second}
|
|
if resp, err := client.Do(req); err == nil {
|
|
status = resp.StatusCode
|
|
answer, _ = io.ReadAll(io.LimitReader(resp.Body, 64<<10))
|
|
resp.Body.Close()
|
|
}
|
|
sig, err := attestation.ParsePlatformSignature(status, answer, imageDigest)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
ref := repository + "@" + imageDigest
|
|
c, err := withRegistryAuth(ctx, m.cosignContainer(), ref, registryUsername, registryPassword)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
args := []string{
|
|
"cosign", "attach", "signature",
|
|
"--payload", "/tmp/sign/payload.json",
|
|
"--signature", "/tmp/sign/signature.b64",
|
|
}
|
|
args = append(args, insecureRegistryArgs(registryInsecure)...)
|
|
args = append(args, ref)
|
|
_, err = c.
|
|
WithNewFile("/tmp/sign/payload.json", string(sig.Payload)).
|
|
WithNewFile("/tmp/sign/signature.b64", sig.SignatureB64).
|
|
WithExec(args).
|
|
Sync(ctx)
|
|
if err != nil {
|
|
return "", fmt.Errorf("cosign attach signature failed: %w", err)
|
|
}
|
|
return fmt.Sprintf("signed %s (platform key %s)\n", ref, sig.KeyFingerprint), nil
|
|
}
|