134 lines
4.9 KiB
Go
134 lines
4.9 KiB
Go
package storepkg
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// ErrCatalogueUnavailable means no catalogue is configured for this instance.
|
|
var ErrCatalogueUnavailable = errors.New("no package catalogue is configured")
|
|
|
|
// ErrCatalogueStale means the catalogue is older than the newest one this
|
|
// instance has accepted. A registry or a network attacker can serve an older
|
|
// validly signed catalogue and pin outdated package digests, so an issue time
|
|
// is only ever accepted going forward.
|
|
var ErrCatalogueStale = errors.New("catalogue is older than the newest accepted catalogue")
|
|
|
|
// CatalogueEntry is one package a catalogue offers: where it is and the
|
|
// digest that pins it.
|
|
type CatalogueEntry struct {
|
|
Name string `json:"name"`
|
|
Surface string `json:"surface"`
|
|
Version string `json:"version"`
|
|
Description string `json:"description"`
|
|
Reference string `json:"reference"`
|
|
Digest string `json:"digest"`
|
|
}
|
|
|
|
// Catalogue is a verified list of packages.
|
|
type Catalogue struct {
|
|
// IssuedAt is when the publisher issued this catalogue. The signature
|
|
// covers it, so it can be neither stripped nor altered, and it is
|
|
// ordered against the newest issue time accepted so far. A catalogue
|
|
// without one is refused: accepting it would leave the replay hole open
|
|
// for exactly the catalogues published before the field existed.
|
|
IssuedAt time.Time `json:"issued_at"`
|
|
Packages []CatalogueEntry `json:"packages"`
|
|
}
|
|
|
|
// Validate refuses a catalogue an install could not trust: one with no signed
|
|
// issue time to order it by, or an entry an install could not pin.
|
|
func (c Catalogue) Validate() error {
|
|
if c.IssuedAt.IsZero() {
|
|
return errors.New("catalogue has no issue time")
|
|
}
|
|
seen := make(map[string]bool, len(c.Packages))
|
|
for _, entry := range c.Packages {
|
|
switch {
|
|
case entry.Name == "" || entry.Reference == "":
|
|
return fmt.Errorf("catalogue entry %q: name and reference are required", entry.Name)
|
|
case !digestPattern.MatchString(entry.Digest):
|
|
return fmt.Errorf("catalogue entry %q: digest must be a sha256 digest", entry.Name)
|
|
case seen[entry.Name]:
|
|
return fmt.Errorf("catalogue entry %q: listed twice", entry.Name)
|
|
}
|
|
seen[entry.Name] = true
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CatalogueSource fetches a catalogue and verifies its signature. An
|
|
// unverifiable catalogue is an error, never an empty list.
|
|
type CatalogueSource interface {
|
|
Fetch(context.Context) (Catalogue, error)
|
|
}
|
|
|
|
// Freshness is the durable high-water mark of accepted catalogue issue
|
|
// times: the newest a verified catalogue issued at that this instance has
|
|
// accepted. It survives restarts, so a replayed catalogue is refused after
|
|
// one too.
|
|
type Freshness interface {
|
|
// Newest returns the newest accepted issue time, or the zero time when
|
|
// no catalogue has been accepted yet.
|
|
Newest(context.Context) (time.Time, error)
|
|
// Accept records a catalogue issued at issuedAt as accepted. It never
|
|
// lowers the mark.
|
|
Accept(ctx context.Context, issuedAt time.Time) error
|
|
}
|
|
|
|
// FreshCatalogueSource refuses a replayed catalogue: one issued before the
|
|
// newest issue time Freshness holds. The wrapped source's own verification
|
|
// runs first, so a catalogue is refused as stale only once it is known to be
|
|
// genuine -- the signature proves an older catalogue is authentic, and the
|
|
// issue time is what stops it.
|
|
type FreshCatalogueSource struct {
|
|
Source CatalogueSource
|
|
Freshness Freshness
|
|
}
|
|
|
|
var _ CatalogueSource = FreshCatalogueSource{}
|
|
|
|
func (f FreshCatalogueSource) Fetch(ctx context.Context) (Catalogue, error) {
|
|
catalogue, err := f.Source.Fetch(ctx)
|
|
if err != nil {
|
|
return Catalogue{}, err
|
|
}
|
|
newest, err := f.Freshness.Newest(ctx)
|
|
if err != nil {
|
|
return Catalogue{}, fmt.Errorf("read catalogue freshness: %w", err)
|
|
}
|
|
if !newest.IsZero() && catalogue.IssuedAt.Before(newest) {
|
|
return Catalogue{}, fmt.Errorf("%w: issued %s, newest accepted %s",
|
|
ErrCatalogueStale, catalogue.IssuedAt.Format(time.RFC3339), newest.Format(time.RFC3339))
|
|
}
|
|
if err := f.Freshness.Accept(ctx, catalogue.IssuedAt); err != nil {
|
|
return Catalogue{}, fmt.Errorf("record catalogue freshness: %w", err)
|
|
}
|
|
return catalogue, nil
|
|
}
|
|
|
|
// ListCatalogue returns the verified catalogue.
|
|
func (s Service) ListCatalogue(ctx context.Context) (Catalogue, error) {
|
|
if s.Catalogue == nil {
|
|
return Catalogue{}, ErrCatalogueUnavailable
|
|
}
|
|
return s.Catalogue.Fetch(ctx)
|
|
}
|
|
|
|
// InstallFromCatalogue installs name at the reference and digest the verified
|
|
// catalogue lists, so a package is never installed from an unverified tag.
|
|
func (s Service) InstallFromCatalogue(ctx context.Context, orgID, name string) (Installed, error) {
|
|
catalogue, err := s.ListCatalogue(ctx)
|
|
if err != nil {
|
|
return Installed{}, err
|
|
}
|
|
for _, entry := range catalogue.Packages {
|
|
if entry.Name == name {
|
|
return s.Install(ctx, orgID, entry.Name, entry.Reference, entry.Digest)
|
|
}
|
|
}
|
|
return Installed{}, fmt.Errorf("catalogue package %q: %w", name, ErrNotFound)
|
|
}
|