2019-07-24 00:32:56 +02:00
|
|
|
// Copyright 2019 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
// use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
// the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing permissions and limitations under
|
|
|
|
// the License.
|
|
|
|
|
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 (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
2019-08-03 02:21:21 +02:00
|
|
|
"time"
|
2019-07-23 21:24:51 +02:00
|
|
|
|
|
|
|
"cloud.google.com/go/storage"
|
2019-08-14 18:20:01 +02:00
|
|
|
"github.com/google/nixery/builder"
|
|
|
|
"github.com/google/nixery/config"
|
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-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 (
|
|
|
|
manifestRegex = regexp.MustCompile(`^/v2/([\w|\-|\.|\_|\/]+)/manifests/([\w|\-|\.|\_]+)$`)
|
|
|
|
layerRegex = regexp.MustCompile(`^/v2/([\w|\-|\.|\_|\/]+)/blobs/sha256:(\w+)$`)
|
|
|
|
)
|
2019-07-23 21:24:51 +02:00
|
|
|
|
2019-07-23 23:56:18 +02:00
|
|
|
// layerRedirect constructs the public URL of the layer object in the Cloud
|
2019-08-03 02:21:21 +02:00
|
|
|
// Storage bucket, signs it and redirects the user there.
|
|
|
|
//
|
|
|
|
// Signing the URL allows unauthenticated clients to retrieve objects from the
|
|
|
|
// bucket.
|
2019-07-23 21:24:51 +02:00
|
|
|
//
|
2019-07-23 23:56:18 +02:00
|
|
|
// The Docker client is known to follow redirects, but this might not be true
|
|
|
|
// for all other registry clients.
|
2019-08-14 18:20:01 +02:00
|
|
|
func constructLayerUrl(cfg *config.Config, digest string) (string, error) {
|
|
|
|
log.Printf("Redirecting layer '%s' request to bucket '%s'\n", digest, cfg.Bucket)
|
2019-08-03 02:21:21 +02:00
|
|
|
object := "layers/" + digest
|
|
|
|
|
2019-08-14 18:20:01 +02:00
|
|
|
if cfg.Signing != nil {
|
|
|
|
opts := *cfg.Signing
|
2019-08-03 02:21:21 +02:00
|
|
|
opts.Expires = time.Now().Add(5 * time.Minute)
|
2019-08-14 18:20:01 +02:00
|
|
|
return storage.SignedURL(cfg.Bucket, object, &opts)
|
2019-08-03 02:21:21 +02:00
|
|
|
} else {
|
2019-08-14 18:20:01 +02:00
|
|
|
return ("https://storage.googleapis.com/" + cfg.Bucket + "/" + object), nil
|
2019-08-03 02:21:21 +02:00
|
|
|
}
|
2019-07-23 21:24:51 +02:00
|
|
|
}
|
|
|
|
|
2019-07-23 23:56:18 +02:00
|
|
|
// prepareBucket configures the handle to a Cloud Storage bucket in which
|
|
|
|
// individual layers will be stored after Nix builds. Nixery does not directly
|
|
|
|
// serve layers to registry clients, instead it redirects them to the public
|
|
|
|
// URLs of the Cloud Storage bucket.
|
2019-07-23 21:24:51 +02:00
|
|
|
//
|
2019-07-23 23:56:18 +02:00
|
|
|
// The bucket is required for Nixery to function correctly, hence fatal errors
|
|
|
|
// are generated in case it fails to be set up correctly.
|
2019-10-02 19:00:35 +02:00
|
|
|
func prepareBucket(ctx context.Context, cfg *config.Config) *storage.BucketHandle {
|
|
|
|
client, err := storage.NewClient(ctx)
|
2019-07-23 21:24:51 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Failed to set up Cloud Storage client:", err)
|
|
|
|
}
|
|
|
|
|
2019-08-14 18:20:01 +02:00
|
|
|
bkt := client.Bucket(cfg.Bucket)
|
2019-07-23 21:24:51 +02:00
|
|
|
|
2019-10-02 19:00:35 +02:00
|
|
|
if _, err := bkt.Attrs(ctx); err != nil {
|
2019-07-23 21:24:51 +02:00
|
|
|
log.Fatalln("Could not access configured bucket", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bkt
|
|
|
|
}
|
|
|
|
|
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-10-02 00:26:09 +02:00
|
|
|
ctx context.Context
|
2019-09-30 18:38:41 +02:00
|
|
|
state *builder.State
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-30 14:15:44 +02:00
|
|
|
// Serve the manifest (straight from Nix)
|
|
|
|
manifestMatches := manifestRegex.FindStringSubmatch(r.RequestURI)
|
|
|
|
if len(manifestMatches) == 3 {
|
|
|
|
imageName := manifestMatches[1]
|
2019-07-31 15:17:11 +02:00
|
|
|
imageTag := manifestMatches[2]
|
|
|
|
log.Printf("Requesting manifest for image %q at tag %q", imageName, imageTag)
|
2019-08-14 18:20:01 +02:00
|
|
|
image := builder.ImageFromName(imageName, imageTag)
|
2019-09-30 18:38:41 +02:00
|
|
|
buildResult, err := builder.BuildImage(h.ctx, h.state, &image)
|
2019-07-30 14:15:44 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-08-02 01:45:22 +02:00
|
|
|
writeError(w, 500, "UNKNOWN", "image build failure")
|
2019-07-30 14:15:44 +02:00
|
|
|
log.Println("Failed to build image manifest", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 22:36:25 +02:00
|
|
|
// Some error types have special handling, which is applied
|
|
|
|
// here.
|
|
|
|
if buildResult.Error == "not_found" {
|
2019-08-02 01:45:22 +02:00
|
|
|
s := fmt.Sprintf("Could not find Nix packages: %v", buildResult.Pkgs)
|
|
|
|
writeError(w, 404, "MANIFEST_UNKNOWN", s)
|
|
|
|
log.Println(s)
|
2019-07-31 22:36:25 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// This marshaling error is ignored because we know that this
|
|
|
|
// field represents valid JSON data.
|
|
|
|
manifest, _ := json.Marshal(buildResult.Manifest)
|
2019-07-30 14:15:44 +02:00
|
|
|
w.Header().Add("Content-Type", manifestMediaType)
|
|
|
|
w.Write(manifest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serve an image layer. For this we need to first ask Nix for
|
|
|
|
// the manifest, then proceed to extract the correct layer from
|
|
|
|
// it.
|
|
|
|
layerMatches := layerRegex.FindStringSubmatch(r.RequestURI)
|
|
|
|
if len(layerMatches) == 3 {
|
|
|
|
digest := layerMatches[2]
|
2019-09-30 18:38:41 +02:00
|
|
|
url, err := constructLayerUrl(&h.state.Cfg, digest)
|
2019-08-03 02:21:21 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to sign GCS URL: %s\n", err)
|
|
|
|
writeError(w, 500, "UNKNOWN", "could not serve layer")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Location", url)
|
|
|
|
w.WriteHeader(303)
|
2019-07-30 14:15:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Unsupported registry route: %s\n", r.RequestURI)
|
|
|
|
w.WriteHeader(404)
|
|
|
|
}
|
|
|
|
|
2019-07-23 23:48:16 +02:00
|
|
|
func main() {
|
2019-09-08 22:53:22 +02:00
|
|
|
cfg, err := config.FromEnv()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Failed to load configuration", err)
|
|
|
|
}
|
|
|
|
|
2019-07-23 21:24:51 +02:00
|
|
|
ctx := context.Background()
|
2019-10-02 19:00:35 +02:00
|
|
|
bucket := prepareBucket(ctx, cfg)
|
2019-09-30 18:38:41 +02:00
|
|
|
state := builder.NewState(bucket, *cfg)
|
2019-07-23 21:24:51 +02:00
|
|
|
|
2019-08-14 21:08:40 +02:00
|
|
|
log.Printf("Starting Nixery on port %s\n", cfg.Port)
|
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-10-02 00:26:09 +02:00
|
|
|
ctx: ctx,
|
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
|
|
|
}
|