2016-11-17 18:28:05 +00:00
|
|
|
package ocischema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3"
|
|
|
|
"github.com/distribution/distribution/v3/manifest"
|
2016-11-17 18:28:05 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2019-10-09 12:02:21 +00:00
|
|
|
v1 "github.com/opencontainers/image-spec/specs-go/v1"
|
2016-11-17 18:28:05 +00:00
|
|
|
)
|
|
|
|
|
2023-04-21 13:43:21 +00:00
|
|
|
// SchemaVersion provides a pre-initialized version structure for OCI Image
|
|
|
|
// Manifests
|
2022-11-02 21:05:45 +00:00
|
|
|
var SchemaVersion = manifest.Versioned{
|
2023-04-21 13:43:21 +00:00
|
|
|
SchemaVersion: 2,
|
2022-11-02 21:05:45 +00:00
|
|
|
MediaType: v1.MediaTypeImageManifest,
|
|
|
|
}
|
2016-11-17 18:28:05 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
ocischemaFunc := func(b []byte) (distribution.Manifest, distribution.Descriptor, error) {
|
2021-10-21 22:09:39 +00:00
|
|
|
if err := validateManifest(b); err != nil {
|
|
|
|
return nil, distribution.Descriptor{}, err
|
|
|
|
}
|
2016-11-17 18:28:05 +00:00
|
|
|
m := new(DeserializedManifest)
|
|
|
|
err := m.UnmarshalJSON(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dgst := digest.FromBytes(b)
|
2023-10-16 02:14:19 +00:00
|
|
|
return m, distribution.Descriptor{
|
|
|
|
MediaType: v1.MediaTypeImageManifest,
|
|
|
|
Digest: dgst,
|
|
|
|
Size: int64(len(b)),
|
|
|
|
Annotations: m.Annotations,
|
|
|
|
}, err
|
2016-11-17 18:28:05 +00:00
|
|
|
}
|
2017-07-11 19:19:47 +00:00
|
|
|
err := distribution.RegisterManifestSchema(v1.MediaTypeImageManifest, ocischemaFunc)
|
2016-11-17 18:28:05 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Unable to register manifest: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-11 19:19:47 +00:00
|
|
|
// Manifest defines a ocischema manifest.
|
2016-11-17 18:28:05 +00:00
|
|
|
type Manifest struct {
|
|
|
|
manifest.Versioned
|
|
|
|
|
|
|
|
// Config references the image configuration as a blob.
|
|
|
|
Config distribution.Descriptor `json:"config"`
|
|
|
|
|
|
|
|
// Layers lists descriptors for the layers referenced by the
|
|
|
|
// configuration.
|
|
|
|
Layers []distribution.Descriptor `json:"layers"`
|
2017-07-11 19:19:47 +00:00
|
|
|
|
|
|
|
// Annotations contains arbitrary metadata for the image manifest.
|
|
|
|
Annotations map[string]string `json:"annotations,omitempty"`
|
2016-11-17 18:28:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-07 10:13:53 +00:00
|
|
|
// References returns the descriptors of this manifests references.
|
2016-11-17 18:28:05 +00:00
|
|
|
func (m Manifest) References() []distribution.Descriptor {
|
|
|
|
references := make([]distribution.Descriptor, 0, 1+len(m.Layers))
|
|
|
|
references = append(references, m.Config)
|
|
|
|
references = append(references, m.Layers...)
|
|
|
|
return references
|
|
|
|
}
|
|
|
|
|
2017-07-12 23:07:46 +00:00
|
|
|
// Target returns the target of this manifest.
|
2016-11-17 18:28:05 +00:00
|
|
|
func (m Manifest) Target() distribution.Descriptor {
|
|
|
|
return m.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeserializedManifest wraps Manifest with a copy of the original JSON.
|
|
|
|
// It satisfies the distribution.Manifest interface.
|
|
|
|
type DeserializedManifest struct {
|
|
|
|
Manifest
|
|
|
|
|
|
|
|
// canonical is the canonical byte representation of the Manifest.
|
|
|
|
canonical []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromStruct takes a Manifest structure, marshals it to JSON, and returns a
|
|
|
|
// DeserializedManifest which contains the manifest and its JSON representation.
|
|
|
|
func FromStruct(m Manifest) (*DeserializedManifest, error) {
|
|
|
|
var deserialized DeserializedManifest
|
|
|
|
deserialized.Manifest = m
|
|
|
|
|
|
|
|
var err error
|
|
|
|
deserialized.canonical, err = json.MarshalIndent(&m, "", " ")
|
|
|
|
return &deserialized, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON populates a new Manifest struct from JSON data.
|
|
|
|
func (m *DeserializedManifest) UnmarshalJSON(b []byte) error {
|
2019-02-05 00:01:04 +00:00
|
|
|
m.canonical = make([]byte, len(b))
|
2016-11-17 18:28:05 +00:00
|
|
|
// store manifest in canonical
|
|
|
|
copy(m.canonical, b)
|
|
|
|
|
|
|
|
// Unmarshal canonical JSON into Manifest object
|
2022-11-26 12:15:07 +00:00
|
|
|
var mfst Manifest
|
|
|
|
if err := json.Unmarshal(m.canonical, &mfst); err != nil {
|
2016-11-17 18:28:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-26 12:15:07 +00:00
|
|
|
if mfst.MediaType != "" && mfst.MediaType != v1.MediaTypeImageManifest {
|
2018-03-14 21:22:36 +00:00
|
|
|
return fmt.Errorf("if present, mediaType in manifest should be '%s' not '%s'",
|
2022-11-26 12:15:07 +00:00
|
|
|
v1.MediaTypeImageManifest, mfst.MediaType)
|
2018-03-14 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-26 12:15:07 +00:00
|
|
|
m.Manifest = mfst
|
2016-11-17 18:28:05 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON returns the contents of canonical. If canonical is empty,
|
|
|
|
// marshals the inner contents.
|
|
|
|
func (m *DeserializedManifest) MarshalJSON() ([]byte, error) {
|
|
|
|
if len(m.canonical) > 0 {
|
|
|
|
return m.canonical, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("JSON representation not initialized in DeserializedManifest")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Payload returns the raw content of the manifest. The contents can be used to
|
|
|
|
// calculate the content identifier.
|
|
|
|
func (m DeserializedManifest) Payload() (string, []byte, error) {
|
2018-03-14 21:22:36 +00:00
|
|
|
return v1.MediaTypeImageManifest, m.canonical, nil
|
2016-11-17 18:28:05 +00:00
|
|
|
}
|
2021-10-21 22:09:39 +00:00
|
|
|
|
|
|
|
// validateManifest returns an error if the byte slice is invalid JSON or if it
|
|
|
|
// contains fields that belong to a index
|
|
|
|
func validateManifest(b []byte) error {
|
2023-03-31 10:35:30 +00:00
|
|
|
var doc struct {
|
|
|
|
Manifests interface{} `json:"manifests,omitempty"`
|
|
|
|
}
|
2021-10-21 22:09:39 +00:00
|
|
|
if err := json.Unmarshal(b, &doc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if doc.Manifests != nil {
|
|
|
|
return errors.New("ocimanifest: expected manifest but found index")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|