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