The -autoupdate variant of the checker and status page images adds a launcher layer. The launcher runs the agent as a child process and keeps it on the platform's release: GET /agent/v1/update names the API's own release and the agent image for it, the launcher downloads the binary for its platform from the registry, accepts it only when the index is signed with a release key for exactly that version, swaps the agent gracefully and rolls back when the new release does not become ready or crashes right after starting. Agents never get ahead of the API, and installations on unreleased builds are not followed. CI pushes both variants and signs each index with RELEASE_SIGNING_KEY (minisign signature in a sha256-<digest>.minisig image next to it). The image builder moves to internal/agentimage; the API image job takes the build job's version so the API reports the release its agents are tagged with. Agents gain a version command. Closes #129 Closes #128 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
155 lines
4.5 KiB
Go
155 lines
4.5 KiB
Go
package agentimage
|
|
|
|
import (
|
|
"archive/tar"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/google/go-containerregistry/pkg/name"
|
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
|
"github.com/google/go-containerregistry/pkg/v1/remote"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/signing"
|
|
)
|
|
|
|
// An image's signature is a minisign signature of Message, with the
|
|
// version as trusted comment, stored as a small image under SignatureTag
|
|
// next to it, as cosign does. The message names the agent but not the
|
|
// registry, so mirrors keep the signature valid.
|
|
|
|
// Message is what the signature of an agent's image index covers.
|
|
func Message(agent string, digest v1.Hash) []byte {
|
|
return []byte("uptimesebi-" + agent + "@" + digest.String() + "\n")
|
|
}
|
|
|
|
// SignatureTag is the tag holding the signature of the index digest.
|
|
func SignatureTag(digest v1.Hash) string {
|
|
return digest.Algorithm + "-" + digest.Hex + ".minisig"
|
|
}
|
|
|
|
const signatureFile = "signature.minisig"
|
|
|
|
// Sign pushes the signature of idx, published as the agent's image in
|
|
// repo at version.
|
|
func Sign(ctx context.Context, idx v1.ImageIndex, repo, agent, version string, key signing.PrivateKey, login Credentials, nameOpts ...name.Option) error {
|
|
digest, err := idx.Digest()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
sig, err := signing.Sign(key, Message(agent, digest), version)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
img, err := fileImage(signatureFile, sig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tag, err := name.NewTag(repo+":"+SignatureTag(digest), nameOpts...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return remote.Write(tag, img, remoteOptions(ctx, login)...)
|
|
}
|
|
|
|
func fileImage(path string, data []byte) (v1.Image, error) {
|
|
raw, err := writeTar([]tarEntry{{path: path, mode: 0o644, data: data}}, timeZero)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return singleLayer(raw)
|
|
}
|
|
|
|
// Fetch resolves ref, checks that the index is signed by one of keys for
|
|
// version and returns the agent binary for platform.
|
|
func Fetch(ctx context.Context, ref, agent, version string, platform v1.Platform, keys []signing.PublicKey, nameOpts ...name.Option) ([]byte, error) {
|
|
r, err := name.ParseReference(ref, nameOpts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
opts := remoteOptions(ctx, Credentials{})
|
|
desc, err := remote.Get(r, opts...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("agentimage: %s: %w", ref, err)
|
|
}
|
|
// Everything below is read by digest, which the client verifies.
|
|
pinned := r.Context().Digest(desc.Digest.String())
|
|
sigImg, err := remote.Image(r.Context().Tag(SignatureTag(desc.Digest)), opts...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("agentimage: no signature for %s: %w", pinned, err)
|
|
}
|
|
sig, err := ReadFile(sigImg, signatureFile, 4096)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
signed, err := signing.Verify(keys, Message(agent, desc.Digest), sig)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("agentimage: %s: %w", pinned, err)
|
|
}
|
|
if signed != version {
|
|
return nil, fmt.Errorf("agentimage: %s is signed as %q, not %q", pinned, signed, version)
|
|
}
|
|
idx, err := remote.Index(pinned, opts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
m, err := idx.IndexManifest()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, d := range m.Manifests {
|
|
if d.Platform == nil || !d.Platform.Equals(platform) {
|
|
continue
|
|
}
|
|
img, err := idx.Image(d.Digest)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ReadFile(img, strings.TrimPrefix(BinDir, "/")+"/"+agent, 512<<20)
|
|
}
|
|
return nil, fmt.Errorf("agentimage: %s has no image for %s", pinned, platform)
|
|
}
|
|
|
|
// ReadFile returns a file of an image's layers, up to max bytes.
|
|
func ReadFile(img v1.Image, path string, max int64) ([]byte, error) {
|
|
layers, err := img.Layers()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for i := len(layers) - 1; i >= 0; i-- {
|
|
data, found, err := fileInLayer(layers[i], path, max)
|
|
if err != nil || found {
|
|
return data, err
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("agentimage: %s is not in the image", path)
|
|
}
|
|
|
|
func fileInLayer(l v1.Layer, path string, max int64) ([]byte, bool, error) {
|
|
rc, err := l.Uncompressed()
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
defer func() { _ = rc.Close() }()
|
|
tr := tar.NewReader(rc)
|
|
for {
|
|
h, err := tr.Next()
|
|
if errors.Is(err, io.EOF) {
|
|
return nil, false, nil
|
|
}
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
if strings.TrimPrefix(h.Name, "./") != path || h.Typeflag != tar.TypeReg {
|
|
continue
|
|
}
|
|
if h.Size > max {
|
|
return nil, true, fmt.Errorf("agentimage: %s is larger than %d bytes", path, max)
|
|
}
|
|
data, err := io.ReadAll(io.LimitReader(tr, max))
|
|
return data, true, err
|
|
}
|
|
}
|