Files
archie/internal/infrastructure/storepkg/catalogue.go
T

192 lines
5.9 KiB
Go

package storepkg
import (
"bytes"
"context"
"crypto/ed25519"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"sync"
"time"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/registry/remote"
domain "github.com/samcharles93/archie-core/internal/domain/storepkg"
"github.com/samcharles93/archie-core/internal/fsutil"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
)
// SignatureAnnotation carries the base64 ed25519 signature over the
// catalogue's layer bytes.
const SignatureAnnotation = "dev.archie.catalogue.signature.v1"
const maxCatalogueBytes = 1 << 20
// DefaultCatalogue is the archipelago marketplace every instance trusts: its
// reference, the public half of the key that signs it, and the durable
// freshness mark that refuses a replayed older catalogue.
func DefaultCatalogue() domain.CatalogueSource {
source := OCICatalogue{
Reference: "ghcr.io/samcharles93/archipelago/catalogue:latest",
Key: mustKey("YL4nvn07uq/5mXZOt9cEmLYu/kB6C2+MLTnbHyoX4Pw="),
}
return domain.FreshCatalogueSource{
Source: source,
Freshness: &FileFreshness{Path: defaultFreshnessPath(source.Reference)},
}
}
func mustKey(encoded string) ed25519.PublicKey {
key, err := base64.StdEncoding.DecodeString(encoded)
if err != nil || len(key) != ed25519.PublicKeySize {
panic("storepkg: invalid catalogue key")
}
return key
}
// OCICatalogue fetches a catalogue artifact and verifies it against Key: a
// one-layer OCI manifest whose layer is the catalogue JSON and whose
// annotation signs that layer. The reference may be a tag; the signature,
// not the tag, is what makes the list trustworthy.
type OCICatalogue struct {
Reference string
Key ed25519.PublicKey
}
var _ domain.CatalogueSource = OCICatalogue{}
func (c OCICatalogue) Fetch(ctx context.Context) (domain.Catalogue, error) {
if len(c.Key) != ed25519.PublicKeySize {
return domain.Catalogue{}, errors.New("catalogue: no valid signing key")
}
repository, err := remote.NewRepository(c.Reference)
if err != nil {
return domain.Catalogue{}, err
}
repository.PlainHTTP = loopbackReference(c.Reference)
_, stream, err := repository.FetchReference(ctx, c.Reference)
if err != nil {
return domain.Catalogue{}, fmt.Errorf("catalogue: %w", err)
}
manifest, err := readBounded(stream, maxManifestBytes)
if err != nil {
return domain.Catalogue{}, err
}
var image ocispec.Manifest
if err := json.Unmarshal(manifest, &image); err != nil {
return domain.Catalogue{}, fmt.Errorf("catalogue manifest: %w", err)
}
if len(image.Layers) != 1 {
return domain.Catalogue{}, errors.New("catalogue must have one layer")
}
layerStream, err := repository.Fetch(ctx, image.Layers[0])
if err != nil {
return domain.Catalogue{}, fmt.Errorf("catalogue layer: %w", err)
}
layer, err := readBounded(layerStream, maxCatalogueBytes)
if err != nil {
return domain.Catalogue{}, err
}
return verifyCatalogue(image, layer, c.Key)
}
// verifyCatalogue checks the layer is the one the manifest names and that the
// key signed it, then decodes it.
func verifyCatalogue(image ocispec.Manifest, layer []byte, key ed25519.PublicKey) (domain.Catalogue, error) {
if contentDigest(layer) != image.Layers[0].Digest.String() {
return domain.Catalogue{}, errors.New("catalogue layer does not match its manifest digest")
}
signature, err := base64.StdEncoding.DecodeString(image.Annotations[SignatureAnnotation])
if err != nil || !ed25519.Verify(key, layer, signature) {
return domain.Catalogue{}, errors.New("catalogue signature does not verify")
}
decoder := json.NewDecoder(bytes.NewReader(layer))
decoder.DisallowUnknownFields()
var catalogue domain.Catalogue
if err := decoder.Decode(&catalogue); err != nil {
return domain.Catalogue{}, fmt.Errorf("catalogue: %w", err)
}
if err := catalogue.Validate(); err != nil {
return domain.Catalogue{}, err
}
return catalogue, nil
}
// FileFreshness is the durable Freshness mark, kept as one small JSON file
// written atomically so a crash never leaves a half-written mark. A missing
// file is no mark yet; a file that exists but does not parse is an error, so
// a corrupt mark fails closed instead of opening a replay window.
type FileFreshness struct {
Path string
mu sync.Mutex
}
var _ domain.Freshness = (*FileFreshness)(nil)
func (f *FileFreshness) Newest(context.Context) (time.Time, error) {
f.mu.Lock()
defer f.mu.Unlock()
return f.read()
}
func (f *FileFreshness) Accept(_ context.Context, issuedAt time.Time) error {
f.mu.Lock()
defer f.mu.Unlock()
current, err := f.read()
if err != nil {
return err
}
if !issuedAt.After(current) {
return nil
}
encoded, err := json.Marshal(struct {
IssuedAt time.Time `json:"issued_at"`
}{IssuedAt: issuedAt.UTC()})
if err != nil {
return err
}
return fsutil.WriteFileAtomic(f.Path, encoded, 0o600)
}
// read returns the mark, or the zero time when no catalogue has been
// accepted yet.
func (f *FileFreshness) read() (time.Time, error) {
raw, err := os.ReadFile(f.Path)
if errors.Is(err, fs.ErrNotExist) {
return time.Time{}, nil
}
if err != nil {
return time.Time{}, err
}
var mark struct {
IssuedAt time.Time `json:"issued_at"`
}
if err := json.Unmarshal(raw, &mark); err != nil {
return time.Time{}, fmt.Errorf("catalogue freshness mark %s: %w", f.Path, err)
}
return mark.IssuedAt, nil
}
// defaultFreshnessPath is where the default catalogue's freshness mark
// lives: $XDG_STATE_HOME/<instance>/catalogue/<key>.json, the state home the
// update journal uses too. The key names the catalogue reference, so two
// catalogues never share a mark.
func defaultFreshnessPath(reference string) string {
dir := os.Getenv("XDG_STATE_HOME")
if dir == "" {
home, _ := os.UserHomeDir()
dir = filepath.Join(home, ".local", "state")
}
sum := sha256.Sum256([]byte(reference))
return filepath.Join(dir, configuration.InstanceName(), "catalogue", hex.EncodeToString(sum[:8])+".json")
}