| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | package oci |
| 5 | |
| 6 | import ( |
| 7 | "encoding/json" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "os" |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 11 | "path/filepath" |
| 12 | |
| 13 | "github.com/opencontainers/go-digest" |
| 14 | ocispec "github.com/opencontainers/image-spec/specs-go" |
| 15 | ocispecv1 "github.com/opencontainers/image-spec/specs-go/v1" |
| 16 | |
| 17 | "source.monogon.dev/osbase/structfs" |
| 18 | ) |
| 19 | |
| Jan Schär | 2963b68 | 2025-07-17 17:03:44 +0200 | [diff] [blame^] | 20 | // ReadLayoutIndex reads the index from an OS path to an OCI layout directory. |
| 21 | func ReadLayoutIndex(path string) (*Index, error) { |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 22 | // Read the oci-layout marker file. |
| 23 | layoutBytes, err := os.ReadFile(filepath.Join(path, "oci-layout")) |
| 24 | if err != nil { |
| 25 | return nil, err |
| 26 | } |
| 27 | layout := ocispecv1.ImageLayout{} |
| 28 | err = json.Unmarshal(layoutBytes, &layout) |
| 29 | if err != nil { |
| 30 | return nil, fmt.Errorf("failed to parse oci-layout: %w", err) |
| 31 | } |
| 32 | if layout.Version != "1.0.0" { |
| 33 | return nil, fmt.Errorf("unknown oci-layout version %q", layout.Version) |
| 34 | } |
| 35 | |
| 36 | // Read the index. |
| Jan Schär | 2963b68 | 2025-07-17 17:03:44 +0200 | [diff] [blame^] | 37 | indexBytes, err := os.ReadFile(filepath.Join(path, "index.json")) |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
| Jan Schär | 2963b68 | 2025-07-17 17:03:44 +0200 | [diff] [blame^] | 41 | blobs := &layoutBlobs{path: path} |
| 42 | ref, err := NewRef(indexBytes, ocispecv1.MediaTypeImageIndex, "", blobs) |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 43 | if err != nil { |
| Jan Schär | 2963b68 | 2025-07-17 17:03:44 +0200 | [diff] [blame^] | 44 | return nil, err |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 45 | } |
| Jan Schär | 2963b68 | 2025-07-17 17:03:44 +0200 | [diff] [blame^] | 46 | return ref.(*Index), nil |
| 47 | } |
| 48 | |
| 49 | // ReadLayout reads a manifest from an OS path to an OCI layout directory. |
| 50 | // It expects the index to point to exactly one manifest, which is common. |
| 51 | func ReadLayout(path string) (Ref, error) { |
| 52 | index, err := ReadLayoutIndex(path) |
| 53 | if err != nil { |
| 54 | return nil, err |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 55 | } |
| Jan Schär | 2963b68 | 2025-07-17 17:03:44 +0200 | [diff] [blame^] | 56 | |
| 57 | if len(index.Manifest.Manifests) == 0 { |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 58 | return nil, fmt.Errorf("index.json contains no manifests") |
| 59 | } |
| Jan Schär | 2963b68 | 2025-07-17 17:03:44 +0200 | [diff] [blame^] | 60 | if len(index.Manifest.Manifests) != 1 { |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 61 | return nil, fmt.Errorf("index.json files containing multiple manifests are not supported") |
| 62 | } |
| Jan Schär | 2963b68 | 2025-07-17 17:03:44 +0200 | [diff] [blame^] | 63 | return index.Ref(&index.Manifest.Manifests[0]) |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | type layoutBlobs struct { |
| 67 | path string |
| 68 | } |
| 69 | |
| 70 | func (r *layoutBlobs) Blob(descriptor *ocispecv1.Descriptor) (io.ReadCloser, error) { |
| Jan Schär | 2963b68 | 2025-07-17 17:03:44 +0200 | [diff] [blame^] | 71 | algorithm, encoded, err := ParseDigest(string(descriptor.Digest)) |
| 72 | if err != nil { |
| 73 | return nil, fmt.Errorf("failed to parse digest in descriptor: %w", err) |
| 74 | } |
| 75 | return os.Open(filepath.Join(r.path, "blobs", algorithm, encoded)) |
| 76 | } |
| 77 | |
| 78 | func (r *layoutBlobs) Manifest(descriptor *ocispecv1.Descriptor) ([]byte, error) { |
| 79 | blob, err := r.Blob(descriptor) |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 80 | if err != nil { |
| 81 | return nil, err |
| 82 | } |
| Jan Schär | 2963b68 | 2025-07-17 17:03:44 +0200 | [diff] [blame^] | 83 | defer blob.Close() |
| 84 | manifestBytes := make([]byte, descriptor.Size) |
| 85 | _, err = io.ReadFull(blob, manifestBytes) |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 86 | if err != nil { |
| Jan Schär | 2963b68 | 2025-07-17 17:03:44 +0200 | [diff] [blame^] | 87 | return nil, fmt.Errorf("failed to read manifest: %w", err) |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 88 | } |
| Jan Schär | 2963b68 | 2025-07-17 17:03:44 +0200 | [diff] [blame^] | 89 | return manifestBytes, nil |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| Jan Schär | 2963b68 | 2025-07-17 17:03:44 +0200 | [diff] [blame^] | 92 | func (r *layoutBlobs) Blobs(_ *ocispecv1.Descriptor) (Blobs, error) { |
| 93 | return r, nil |
| 94 | } |
| 95 | |
| 96 | // CreateLayout builds an OCI layout from a Ref. |
| 97 | func CreateLayout(ref Ref) (structfs.Tree, error) { |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 98 | // Build the index. |
| Jan Schär | 2963b68 | 2025-07-17 17:03:44 +0200 | [diff] [blame^] | 99 | artifactType := "" |
| 100 | if image, ok := ref.(*Image); ok { |
| 101 | // According to the OCI spec, the artifactType is the config descriptor |
| 102 | // mediaType, and is only set when the descriptor references the image |
| 103 | // manifest of an artifact. |
| 104 | artifactType = image.Manifest.Config.MediaType |
| 105 | if artifactType == ocispecv1.MediaTypeImageConfig { |
| 106 | artifactType = "" |
| 107 | } |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 108 | } |
| 109 | imageIndex := ocispecv1.Index{ |
| 110 | Versioned: ocispec.Versioned{SchemaVersion: 2}, |
| 111 | MediaType: ocispecv1.MediaTypeImageIndex, |
| 112 | Manifests: []ocispecv1.Descriptor{{ |
| 113 | MediaType: ocispecv1.MediaTypeImageManifest, |
| 114 | ArtifactType: artifactType, |
| Jan Schär | 2963b68 | 2025-07-17 17:03:44 +0200 | [diff] [blame^] | 115 | Digest: digest.Digest(ref.Digest()), |
| 116 | Size: int64(len(ref.RawManifest())), |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 117 | }}, |
| 118 | } |
| 119 | imageIndexBytes, err := json.MarshalIndent(imageIndex, "", "\t") |
| 120 | if err != nil { |
| 121 | return nil, fmt.Errorf("failed to marshal image index: %w", err) |
| 122 | } |
| 123 | imageIndexBytes = append(imageIndexBytes, '\n') |
| 124 | |
| 125 | root := structfs.Tree{ |
| 126 | structfs.File("oci-layout", structfs.Bytes(`{"imageLayoutVersion": "1.0.0"}`+"\n")), |
| 127 | structfs.File("index.json", structfs.Bytes(imageIndexBytes)), |
| 128 | } |
| 129 | |
| Jan Schär | 2963b68 | 2025-07-17 17:03:44 +0200 | [diff] [blame^] | 130 | hasBlob := make(map[string]bool) |
| 131 | blobDirs := make(map[string]*structfs.Node) |
| 132 | addBlob := func(digest string, blob structfs.Blob) error { |
| 133 | if hasBlob[digest] { |
| 134 | // If multiple blobs have the same digest, we only need the first one. |
| 135 | return nil |
| 136 | } |
| 137 | hasBlob[digest] = true |
| 138 | algorithm, encoded, err := ParseDigest(digest) |
| 139 | if err != nil { |
| 140 | return fmt.Errorf("failed to parse manifest digest: %w", err) |
| 141 | } |
| 142 | blobDir, ok := blobDirs[algorithm] |
| 143 | if !ok { |
| 144 | blobDir = structfs.Dir(algorithm, nil) |
| 145 | err = root.Place("blobs", blobDir) |
| 146 | if err != nil { |
| 147 | return err |
| 148 | } |
| 149 | blobDirs[algorithm] = blobDir |
| 150 | } |
| 151 | // root.PlaceFile is not used here because then running time would be |
| 152 | // quadratic in the number of blobs. |
| 153 | blobDir.Children = append(blobDir.Children, structfs.File(encoded, blob)) |
| 154 | return nil |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 155 | } |
| Jan Schär | 2963b68 | 2025-07-17 17:03:44 +0200 | [diff] [blame^] | 156 | err = WalkRefs(string(imageIndex.Manifests[0].Digest), ref, func(digest string, ref Ref) error { |
| 157 | err := addBlob(digest, structfs.Bytes(ref.RawManifest())) |
| 158 | if err != nil { |
| 159 | return err |
| 160 | } |
| 161 | if image, ok := ref.(*Image); ok { |
| 162 | for descriptor := range image.Descriptors() { |
| 163 | err := addBlob(string(descriptor.Digest), image.StructfsBlob(descriptor)) |
| 164 | if err != nil { |
| 165 | return err |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | return nil |
| 170 | }) |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 171 | if err != nil { |
| 172 | return nil, err |
| 173 | } |
| 174 | |
| Jan Schär | b48174d | 2025-04-14 10:13:02 +0000 | [diff] [blame] | 175 | return root, nil |
| 176 | } |