package storepkg import ( "bytes" "context" "encoding/json" "fmt" "github.com/samcharles93/archie-core/internal/domain/distribution" domain "github.com/samcharles93/archie-core/internal/domain/storepkg" "github.com/samcharles93/archie-core/internal/infrastructure/signedoci" ) // SignatureAnnotation carries the base64 ed25519 signature over the // catalogue's layer bytes. const SignatureAnnotation = "dev.archie.catalogue.signature.v1" const maxCatalogueBytes = 1 << 20 // catalogueTag is the tag a catalogue source publishes its current catalogue // under. const catalogueTag = "latest" // Catalogue fetches the catalogue a source publishes and refuses a replayed // older one, keeping the freshness mark per source. func Catalogue(source distribution.Source) domain.CatalogueSource { return domain.FreshCatalogueSource{ Source: OCICatalogue{Source: source}, Freshness: &signedoci.FileFreshness{Path: signedoci.MarkPath("catalogue", source.Reference)}, } } // OCICatalogue fetches a catalogue artifact and verifies it against the // source's key: an OCI manifest whose first layer is the catalogue JSON and // whose annotation signs that layer. The tag is mutable; the signature is // what makes the list trustworthy. type OCICatalogue struct { Source distribution.Source } var _ domain.CatalogueSource = OCICatalogue{} func (c OCICatalogue) Fetch(ctx context.Context) (domain.Catalogue, error) { artifact, err := signedoci.Fetch(ctx, c.Source, catalogueTag, SignatureAnnotation, maxCatalogueBytes) if err != nil { return domain.Catalogue{}, fmt.Errorf("catalogue: %w", err) } return decodeCatalogue(artifact.Statement) } // decodeCatalogue decodes a verified catalogue layer. func decodeCatalogue(layer []byte) (domain.Catalogue, error) { 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 }