| Lorenz Brun | 901c732 | 2023-07-13 20:10:37 +0200 | [diff] [blame] | 1 | // Package localregistry implements a read-only OCI Distribution / Docker |
| 2 | // V2 container image registry backed by local layers. |
| 3 | package localregistry |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "encoding/json" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "log" |
| 11 | "net/http" |
| 12 | "os" |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 13 | "path/filepath" |
| Lorenz Brun | 901c732 | 2023-07-13 20:10:37 +0200 | [diff] [blame] | 14 | "regexp" |
| 15 | "strconv" |
| 16 | |
| Serge Bazanski | f779b8f | 2024-04-17 16:30:55 +0200 | [diff] [blame] | 17 | "github.com/bazelbuild/rules_go/go/runfiles" |
| Lorenz Brun | 901c732 | 2023-07-13 20:10:37 +0200 | [diff] [blame] | 18 | "github.com/docker/distribution" |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 19 | "github.com/docker/distribution/manifest/manifestlist" |
| 20 | "github.com/docker/distribution/manifest/ocischema" |
| Lorenz Brun | 901c732 | 2023-07-13 20:10:37 +0200 | [diff] [blame] | 21 | "github.com/docker/distribution/manifest/schema2" |
| 22 | "github.com/docker/distribution/reference" |
| 23 | "github.com/opencontainers/go-digest" |
| 24 | "google.golang.org/protobuf/encoding/prototext" |
| 25 | |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame^] | 26 | "source.monogon.dev/metropolis/test/localregistry/spec" |
| Lorenz Brun | 901c732 | 2023-07-13 20:10:37 +0200 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | type Server struct { |
| 30 | manifests map[string][]byte |
| 31 | blobs map[digest.Digest]blobMeta |
| 32 | } |
| 33 | |
| 34 | type blobMeta struct { |
| 35 | filePath string |
| 36 | mediaType string |
| 37 | contentLength int64 |
| 38 | } |
| 39 | |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 40 | func manifestDescriptorFromBazel(image *spec.Image) (manifestlist.ManifestDescriptor, error) { |
| Serge Bazanski | f779b8f | 2024-04-17 16:30:55 +0200 | [diff] [blame] | 41 | indexPath, err := runfiles.Rlocation(filepath.Join("_main", image.Path, "index.json")) |
| 42 | if err != nil { |
| 43 | return manifestlist.ManifestDescriptor{}, fmt.Errorf("while locating manifest list file: %w", err) |
| 44 | } |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 45 | |
| 46 | manifestListRaw, err := os.ReadFile(indexPath) |
| Lorenz Brun | 901c732 | 2023-07-13 20:10:37 +0200 | [diff] [blame] | 47 | if err != nil { |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 48 | return manifestlist.ManifestDescriptor{}, fmt.Errorf("while opening manifest list file: %w", err) |
| Lorenz Brun | 901c732 | 2023-07-13 20:10:37 +0200 | [diff] [blame] | 49 | } |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 50 | |
| 51 | var imageManifestList manifestlist.ManifestList |
| 52 | if err := json.Unmarshal(manifestListRaw, &imageManifestList); err != nil { |
| 53 | return manifestlist.ManifestDescriptor{}, fmt.Errorf("while unmarshaling manifest list for %q: %w", image.Name, err) |
| Lorenz Brun | 901c732 | 2023-07-13 20:10:37 +0200 | [diff] [blame] | 54 | } |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 55 | |
| 56 | if len(imageManifestList.Manifests) != 1 { |
| 57 | return manifestlist.ManifestDescriptor{}, fmt.Errorf("unexpected manifest list length > 1") |
| 58 | } |
| 59 | |
| 60 | return imageManifestList.Manifests[0], nil |
| Lorenz Brun | 901c732 | 2023-07-13 20:10:37 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 63 | func manifestFromBazel(s *Server, image *spec.Image, md manifestlist.ManifestDescriptor) (ocischema.Manifest, error) { |
| Serge Bazanski | f779b8f | 2024-04-17 16:30:55 +0200 | [diff] [blame] | 64 | manifestPath, err := runfiles.Rlocation(filepath.Join("_main", image.Path, "blobs", md.Digest.Algorithm().String(), md.Digest.Hex())) |
| 65 | if err != nil { |
| 66 | return ocischema.Manifest{}, fmt.Errorf("while locating manifest file: %w", err) |
| 67 | } |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 68 | manifestRaw, err := os.ReadFile(manifestPath) |
| 69 | if err != nil { |
| 70 | return ocischema.Manifest{}, fmt.Errorf("while opening manifest file: %w", err) |
| 71 | } |
| 72 | |
| 73 | var imageManifest ocischema.Manifest |
| 74 | if err := json.Unmarshal(manifestRaw, &imageManifest); err != nil { |
| 75 | return ocischema.Manifest{}, fmt.Errorf("while unmarshaling manifest for %q: %w", image.Name, err) |
| 76 | } |
| 77 | |
| 78 | // For Digest lookups |
| 79 | s.manifests[image.Name] = manifestRaw |
| 80 | s.manifests[md.Digest.String()] = manifestRaw |
| 81 | |
| 82 | return imageManifest, nil |
| 83 | } |
| 84 | |
| Serge Bazanski | 6ea5762 | 2024-04-17 21:02:32 +0200 | [diff] [blame] | 85 | func addBazelBlobFromDescriptor(s *Server, image *spec.Image, dd distribution.Descriptor) error { |
| 86 | path, err := runfiles.Rlocation(filepath.Join("_main", image.Path, "blobs", dd.Digest.Algorithm().String(), dd.Digest.Hex())) |
| 87 | if err != nil { |
| 88 | return fmt.Errorf("while locating blob: %w", err) |
| 89 | } |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 90 | s.blobs[dd.Digest] = blobMeta{filePath: path, mediaType: dd.MediaType, contentLength: dd.Size} |
| Serge Bazanski | 6ea5762 | 2024-04-17 21:02:32 +0200 | [diff] [blame] | 91 | return nil |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | func FromBazelManifest(mb []byte) (*Server, error) { |
| 95 | var bazelManifest spec.Manifest |
| 96 | if err := prototext.Unmarshal(mb, &bazelManifest); err != nil { |
| Lorenz Brun | 901c732 | 2023-07-13 20:10:37 +0200 | [diff] [blame] | 97 | log.Fatalf("failed to parse manifest: %v", err) |
| 98 | } |
| 99 | s := Server{ |
| 100 | manifests: make(map[string][]byte), |
| 101 | blobs: make(map[digest.Digest]blobMeta), |
| 102 | } |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 103 | for _, i := range bazelManifest.Images { |
| 104 | md, err := manifestDescriptorFromBazel(i) |
| Lorenz Brun | 901c732 | 2023-07-13 20:10:37 +0200 | [diff] [blame] | 105 | if err != nil { |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 106 | return nil, err |
| Lorenz Brun | 901c732 | 2023-07-13 20:10:37 +0200 | [diff] [blame] | 107 | } |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 108 | |
| Serge Bazanski | 6ea5762 | 2024-04-17 21:02:32 +0200 | [diff] [blame] | 109 | if err := addBazelBlobFromDescriptor(&s, i, md.Descriptor); err != nil { |
| 110 | return nil, err |
| 111 | } |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 112 | |
| 113 | m, err := manifestFromBazel(&s, i, md) |
| Lorenz Brun | 901c732 | 2023-07-13 20:10:37 +0200 | [diff] [blame] | 114 | if err != nil { |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 115 | return nil, err |
| Lorenz Brun | 901c732 | 2023-07-13 20:10:37 +0200 | [diff] [blame] | 116 | } |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 117 | |
| Serge Bazanski | 6ea5762 | 2024-04-17 21:02:32 +0200 | [diff] [blame] | 118 | if err := addBazelBlobFromDescriptor(&s, i, m.Config); err != nil { |
| 119 | return nil, err |
| 120 | } |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 121 | for _, l := range m.Layers { |
| Serge Bazanski | 6ea5762 | 2024-04-17 21:02:32 +0200 | [diff] [blame] | 122 | if err := addBazelBlobFromDescriptor(&s, i, l); err != nil { |
| 123 | return nil, err |
| 124 | } |
| Tim Windelschmidt | 0974b22 | 2024-01-16 14:04:15 +0100 | [diff] [blame] | 125 | } |
| Lorenz Brun | 901c732 | 2023-07-13 20:10:37 +0200 | [diff] [blame] | 126 | } |
| 127 | return &s, nil |
| 128 | } |
| 129 | |
| 130 | var ( |
| 131 | versionCheckEp = regexp.MustCompile(`^/v2/$`) |
| 132 | manifestEp = regexp.MustCompile("^/v2/(" + reference.NameRegexp.String() + ")/manifests/(" + reference.TagRegexp.String() + "|" + digest.DigestRegexp.String() + ")$") |
| 133 | blobEp = regexp.MustCompile("^/v2/(" + reference.NameRegexp.String() + ")/blobs/(" + digest.DigestRegexp.String() + ")$") |
| 134 | ) |
| 135 | |
| 136 | func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
| 137 | if req.Method != http.MethodGet && req.Method != http.MethodHead { |
| 138 | w.WriteHeader(http.StatusMethodNotAllowed) |
| 139 | fmt.Fprintf(w, "Registry is read-only, only GET and HEAD are allowed\n") |
| 140 | return |
| 141 | } |
| 142 | w.Header().Set("Content-Type", "application/json") |
| 143 | if versionCheckEp.MatchString(req.URL.Path) { |
| 144 | w.WriteHeader(http.StatusOK) |
| 145 | fmt.Fprintf(w, "{}") |
| 146 | return |
| 147 | } else if matches := manifestEp.FindStringSubmatch(req.URL.Path); len(matches) > 0 { |
| 148 | name := matches[1] |
| 149 | manifest, ok := s.manifests[name] |
| 150 | if !ok { |
| 151 | w.WriteHeader(http.StatusNotFound) |
| 152 | fmt.Fprintf(w, "Image not found") |
| 153 | return |
| 154 | } |
| 155 | w.Header().Set("Content-Type", schema2.MediaTypeManifest) |
| 156 | w.Header().Set("Content-Length", strconv.FormatInt(int64(len(manifest)), 10)) |
| 157 | w.WriteHeader(http.StatusOK) |
| 158 | io.Copy(w, bytes.NewReader(manifest)) |
| 159 | } else if matches := blobEp.FindStringSubmatch(req.URL.Path); len(matches) > 0 { |
| 160 | bm, ok := s.blobs[digest.Digest(matches[2])] |
| 161 | if !ok { |
| 162 | w.WriteHeader(http.StatusNotFound) |
| 163 | fmt.Fprintf(w, "Blob not found") |
| 164 | return |
| 165 | } |
| 166 | w.Header().Set("Content-Type", bm.mediaType) |
| 167 | w.Header().Set("Content-Length", strconv.FormatInt(bm.contentLength, 10)) |
| 168 | http.ServeFile(w, req, bm.filePath) |
| 169 | } else { |
| 170 | w.WriteHeader(http.StatusNotFound) |
| 171 | } |
| 172 | } |