2022-04-20 16:41:20 +02:00
|
|
|
// Copyright 2022 The TVL Contributors
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2019-07-24 00:32:56 +02:00
|
|
|
|
2019-08-14 18:20:01 +02:00
|
|
|
// The nixery server implements a container registry that transparently builds
|
|
|
|
// container images based on Nix derivations.
|
2019-07-23 21:24:51 +02:00
|
|
|
//
|
|
|
|
// The Nix derivation used for image creation is responsible for creating
|
|
|
|
// objects that are compatible with the registry API. The targeted registry
|
|
|
|
// protocol is currently Docker's.
|
|
|
|
//
|
|
|
|
// When an image is requested, the required contents are parsed out of the
|
|
|
|
// request and a Nix-build is initiated that eventually responds with the
|
|
|
|
// manifest as well as information linking each layer digest to a local
|
|
|
|
// filesystem path.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-10-27 14:50:02 +01:00
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
2019-07-23 21:24:51 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-10-27 14:50:02 +01:00
|
|
|
"io"
|
2019-10-03 14:02:48 +02:00
|
|
|
"io/ioutil"
|
2019-07-23 21:24:51 +02:00
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
|
2019-11-11 22:07:16 +01:00
|
|
|
"github.com/google/nixery/builder"
|
|
|
|
"github.com/google/nixery/config"
|
2022-05-13 17:54:06 +02:00
|
|
|
"github.com/google/nixery/layers"
|
2019-11-11 22:07:16 +01:00
|
|
|
"github.com/google/nixery/logs"
|
2020-10-29 16:13:53 +01:00
|
|
|
mf "github.com/google/nixery/manifest"
|
2019-11-11 22:07:16 +01:00
|
|
|
"github.com/google/nixery/storage"
|
2019-10-04 23:17:11 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-07-23 21:24:51 +02:00
|
|
|
)
|
|
|
|
|
2019-07-23 23:56:18 +02:00
|
|
|
// ManifestMediaType is the Content-Type used for the manifest itself. This
|
|
|
|
// corresponds to the "Image Manifest V2, Schema 2" described on this page:
|
2019-07-23 21:24:51 +02:00
|
|
|
//
|
|
|
|
// https://docs.docker.com/registry/spec/manifest-v2-2/
|
2019-07-23 23:48:16 +02:00
|
|
|
const manifestMediaType string = "application/vnd.docker.distribution.manifest.v2+json"
|
2019-07-23 21:24:51 +02:00
|
|
|
|
2019-10-05 23:33:41 +02:00
|
|
|
// This variable will be initialised during the build process and set
|
|
|
|
// to the hash of the entire Nixery source tree.
|
|
|
|
var version string = "devel"
|
|
|
|
|
2019-08-14 18:20:01 +02:00
|
|
|
// Regexes matching the V2 Registry API routes. This only includes the
|
|
|
|
// routes required for serving images, since pushing and other such
|
|
|
|
// functionality is not available.
|
|
|
|
var (
|
2020-10-27 14:24:14 +01:00
|
|
|
manifestRegex = regexp.MustCompile(`^/v2/([\w|\-|\.|\_|\/]+)/manifests/([\w|\-|\.|\_]+)$`)
|
|
|
|
blobRegex = regexp.MustCompile(`^/v2/([\w|\-|\.|\_|\/]+)/(blobs|manifests)/sha256:(\w+)$`)
|
2019-08-14 18:20:01 +02:00
|
|
|
)
|
2019-07-23 21:24:51 +02:00
|
|
|
|
2019-10-03 14:02:48 +02:00
|
|
|
// Downloads the popularity information for the package set from the
|
|
|
|
// URL specified in Nixery's configuration.
|
2022-05-13 17:54:06 +02:00
|
|
|
func downloadPopularity(url string) (layers.Popularity, error) {
|
2019-10-03 14:02:48 +02:00
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return nil, fmt.Errorf("popularity download from '%s' returned status: %s\n", url, resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
j, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-13 17:54:06 +02:00
|
|
|
var pop layers.Popularity
|
2019-10-03 14:02:48 +02:00
|
|
|
err = json.Unmarshal(j, &pop)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pop, nil
|
|
|
|
}
|
|
|
|
|
2019-08-02 01:45:22 +02:00
|
|
|
// Error format corresponding to the registry protocol V2 specification. This
|
|
|
|
// allows feeding back errors to clients in a way that can be presented to
|
|
|
|
// users.
|
|
|
|
type registryError struct {
|
|
|
|
Code string `json:"code"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type registryErrors struct {
|
|
|
|
Errors []registryError `json:"errors"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeError(w http.ResponseWriter, status int, code, message string) {
|
|
|
|
err := registryErrors{
|
|
|
|
Errors: []registryError{
|
|
|
|
{code, message},
|
|
|
|
},
|
2019-07-23 21:24:51 +02:00
|
|
|
}
|
2019-08-02 01:45:22 +02:00
|
|
|
json, _ := json.Marshal(err)
|
2019-07-23 21:24:51 +02:00
|
|
|
|
2019-08-02 01:45:22 +02:00
|
|
|
w.WriteHeader(status)
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
w.Write(json)
|
2019-07-23 23:48:16 +02:00
|
|
|
}
|
|
|
|
|
2019-07-30 14:15:44 +02:00
|
|
|
type registryHandler struct {
|
2019-09-30 18:38:41 +02:00
|
|
|
state *builder.State
|
2019-07-30 14:15:44 +02:00
|
|
|
}
|
|
|
|
|
2020-10-27 13:30:12 +01:00
|
|
|
// Serve a manifest by tag, building it via Nix and populating caches
|
|
|
|
// if necessary.
|
|
|
|
func (h *registryHandler) serveManifestTag(w http.ResponseWriter, r *http.Request, name string, tag string) {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"image": name,
|
|
|
|
"tag": tag,
|
|
|
|
}).Info("requesting image manifest")
|
|
|
|
|
|
|
|
image := builder.ImageFromName(name, tag)
|
|
|
|
buildResult, err := builder.BuildImage(r.Context(), h.state, &image)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
writeError(w, 500, "UNKNOWN", "image build failure")
|
|
|
|
|
|
|
|
log.WithError(err).WithFields(log.Fields{
|
|
|
|
"image": name,
|
|
|
|
"tag": tag,
|
|
|
|
}).Error("failed to build image manifest")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some error types have special handling, which is applied
|
|
|
|
// here.
|
|
|
|
if buildResult.Error == "not_found" {
|
|
|
|
s := fmt.Sprintf("Could not find Nix packages: %v", buildResult.Pkgs)
|
|
|
|
writeError(w, 404, "MANIFEST_UNKNOWN", s)
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"image": name,
|
|
|
|
"tag": tag,
|
|
|
|
"packages": buildResult.Pkgs,
|
|
|
|
}).Warn("could not find Nix packages")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// This marshaling error is ignored because we know that this
|
|
|
|
// field represents valid JSON data.
|
|
|
|
manifest, _ := json.Marshal(buildResult.Manifest)
|
|
|
|
w.Header().Add("Content-Type", manifestMediaType)
|
2020-10-27 14:50:02 +01:00
|
|
|
|
|
|
|
// The manifest needs to be persisted to the blob storage (to become
|
|
|
|
// available for clients that fetch manifests by their hash, e.g.
|
|
|
|
// containerd) and served to the client.
|
|
|
|
//
|
|
|
|
// Since we have no stable key to address this manifest (it may be
|
|
|
|
// uncacheable, yet still addressable by blob) we need to separate
|
|
|
|
// out the hashing, uploading and serving phases. The latter is
|
|
|
|
// especially important as clients may start to fetch it by digest
|
|
|
|
// as soon as they see a response.
|
|
|
|
sha256sum := fmt.Sprintf("%x", sha256.Sum256(manifest))
|
|
|
|
path := "layers/" + sha256sum
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
2020-10-29 16:13:53 +01:00
|
|
|
_, _, err = h.state.Storage.Persist(ctx, path, mf.ManifestType, func(sw io.Writer) (string, int64, error) {
|
2020-10-27 14:50:02 +01:00
|
|
|
// We already know the hash, so no additional hash needs to be
|
|
|
|
// constructed here.
|
|
|
|
written, err := sw.Write(manifest)
|
|
|
|
return sha256sum, int64(written), err
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
writeError(w, 500, "MANIFEST_UPLOAD", "could not upload manifest to blob store")
|
|
|
|
|
|
|
|
log.WithError(err).WithFields(log.Fields{
|
|
|
|
"image": name,
|
|
|
|
"tag": tag,
|
|
|
|
}).Error("could not upload manifest")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-27 13:30:12 +01:00
|
|
|
w.Write(manifest)
|
|
|
|
}
|
|
|
|
|
2020-10-27 14:24:14 +01:00
|
|
|
// serveBlob serves a blob from storage by digest
|
|
|
|
func (h *registryHandler) serveBlob(w http.ResponseWriter, r *http.Request, blobType, digest string) {
|
2020-10-27 13:30:12 +01:00
|
|
|
storage := h.state.Storage
|
2020-10-27 13:47:08 +01:00
|
|
|
err := storage.Serve(digest, r, w)
|
2020-10-27 13:30:12 +01:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).WithFields(log.Fields{
|
2020-10-27 14:24:14 +01:00
|
|
|
"type": blobType,
|
|
|
|
"digest": digest,
|
2020-10-27 13:30:12 +01:00
|
|
|
"backend": storage.Name(),
|
2020-10-27 14:24:14 +01:00
|
|
|
}).Error("failed to serve blob from storage backend")
|
2020-10-27 13:30:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP dispatches HTTP requests to the matching handlers.
|
2019-07-30 14:15:44 +02:00
|
|
|
func (h *registryHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-08-03 02:18:14 +02:00
|
|
|
// Acknowledge that we speak V2 with an empty response
|
|
|
|
if r.RequestURI == "/v2/" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-27 13:30:12 +01:00
|
|
|
// Build & serve a manifest by tag
|
2020-10-27 14:24:14 +01:00
|
|
|
manifestMatches := manifestRegex.FindStringSubmatch(r.RequestURI)
|
2019-07-30 14:15:44 +02:00
|
|
|
if len(manifestMatches) == 3 {
|
2020-10-27 13:30:12 +01:00
|
|
|
h.serveManifestTag(w, r, manifestMatches[1], manifestMatches[2])
|
2019-07-30 14:15:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-27 14:24:14 +01:00
|
|
|
// Serve a blob by digest
|
|
|
|
layerMatches := blobRegex.FindStringSubmatch(r.RequestURI)
|
|
|
|
if len(layerMatches) == 4 {
|
|
|
|
h.serveBlob(w, r, layerMatches[2], layerMatches[3])
|
2019-07-30 14:15:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-06 15:48:24 +02:00
|
|
|
log.WithField("uri", r.RequestURI).Info("unsupported registry route")
|
2019-10-06 04:18:38 +02:00
|
|
|
|
2019-07-30 14:15:44 +02:00
|
|
|
w.WriteHeader(404)
|
|
|
|
}
|
|
|
|
|
2019-07-23 23:48:16 +02:00
|
|
|
func main() {
|
2019-11-11 22:07:16 +01:00
|
|
|
logs.Init(version)
|
2019-09-08 22:53:22 +02:00
|
|
|
cfg, err := config.FromEnv()
|
|
|
|
if err != nil {
|
2019-10-06 15:48:24 +02:00
|
|
|
log.WithError(err).Fatal("failed to load configuration")
|
2019-09-08 22:53:22 +02:00
|
|
|
}
|
|
|
|
|
2019-10-27 18:33:57 +01:00
|
|
|
var s storage.Backend
|
|
|
|
|
|
|
|
switch cfg.Backend {
|
|
|
|
case config.GCS:
|
|
|
|
s, err = storage.NewGCSBackend()
|
2019-10-28 18:19:06 +01:00
|
|
|
case config.FileSystem:
|
|
|
|
s, err = storage.NewFSBackend()
|
2019-10-27 18:33:57 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Fatal("failed to initialise storage backend")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithField("backend", s.Name()).Info("initialised storage backend")
|
|
|
|
|
2019-10-03 13:49:26 +02:00
|
|
|
cache, err := builder.NewCache()
|
|
|
|
if err != nil {
|
2019-10-06 15:48:24 +02:00
|
|
|
log.WithError(err).Fatal("failed to instantiate build cache")
|
2019-10-03 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2022-05-13 17:54:06 +02:00
|
|
|
var pop layers.Popularity
|
2019-10-03 14:02:48 +02:00
|
|
|
if cfg.PopUrl != "" {
|
|
|
|
pop, err = downloadPopularity(cfg.PopUrl)
|
|
|
|
if err != nil {
|
2019-10-06 15:48:24 +02:00
|
|
|
log.WithError(err).WithField("popURL", cfg.PopUrl).
|
|
|
|
Fatal("failed to fetch popularity information")
|
2019-10-03 14:02:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 13:49:26 +02:00
|
|
|
state := builder.State{
|
2019-10-27 18:33:57 +01:00
|
|
|
Cache: &cache,
|
|
|
|
Cfg: cfg,
|
|
|
|
Pop: pop,
|
|
|
|
Storage: s,
|
2019-10-03 13:49:26 +02:00
|
|
|
}
|
2019-07-23 21:24:51 +02:00
|
|
|
|
2019-10-05 23:48:19 +02:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"version": version,
|
|
|
|
"port": cfg.Port,
|
2019-10-06 15:48:24 +02:00
|
|
|
}).Info("starting Nixery")
|
2019-07-23 21:24:51 +02:00
|
|
|
|
2019-08-03 02:18:14 +02:00
|
|
|
// All /v2/ requests belong to the registry handler.
|
2019-07-30 14:15:44 +02:00
|
|
|
http.Handle("/v2/", ®istryHandler{
|
2019-09-30 18:38:41 +02:00
|
|
|
state: &state,
|
2019-07-30 14:15:44 +02:00
|
|
|
})
|
2019-07-23 21:24:51 +02:00
|
|
|
|
2019-07-30 14:15:44 +02:00
|
|
|
// All other roots are served by the static file server.
|
2019-08-14 18:20:01 +02:00
|
|
|
webDir := http.Dir(cfg.WebDir)
|
2019-08-03 02:21:21 +02:00
|
|
|
http.Handle("/", http.FileServer(webDir))
|
2019-07-23 21:24:51 +02:00
|
|
|
|
2019-08-14 18:20:01 +02:00
|
|
|
log.Fatal(http.ListenAndServe(":"+cfg.Port, nil))
|
2019-07-23 21:24:51 +02:00
|
|
|
}
|