2018-02-05 21:05:59 +00:00
|
|
|
package registry // import "github.com/docker/docker/registry"
|
2013-07-25 00:44:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-11-11 21:31:15 +00:00
|
|
|
"errors"
|
2013-07-25 00:44:55 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-11-11 21:31:15 +00:00
|
|
|
"net"
|
2013-07-25 00:44:55 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2013-07-25 02:22:36 +00:00
|
|
|
"net/url"
|
2013-07-25 19:57:09 +00:00
|
|
|
"strconv"
|
2013-07-31 17:05:06 +00:00
|
|
|
"strings"
|
2013-07-25 00:44:55 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2014-07-24 20:37:44 +00:00
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-09-06 18:18:12 +00:00
|
|
|
registrytypes "github.com/docker/docker/api/types/registry"
|
2014-07-24 20:37:44 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2017-07-26 21:42:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2013-07-25 00:44:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-10-07 01:54:52 +00:00
|
|
|
testHTTPServer *httptest.Server
|
|
|
|
testHTTPSServer *httptest.Server
|
|
|
|
testLayers = map[string]map[string]string{
|
2013-07-25 00:44:55 +00:00
|
|
|
"77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20": {
|
|
|
|
"json": `{"id":"77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
|
|
|
|
"comment":"test base image","created":"2013-03-23T12:53:11.10432-07:00",
|
|
|
|
"container_config":{"Hostname":"","User":"","Memory":0,"MemorySwap":0,
|
|
|
|
"CpuShares":0,"AttachStdin":false,"AttachStdout":false,"AttachStderr":false,
|
2015-05-24 13:17:29 +00:00
|
|
|
"Tty":false,"OpenStdin":false,"StdinOnce":false,
|
2013-07-25 00:44:55 +00:00
|
|
|
"Env":null,"Cmd":null,"Dns":null,"Image":"","Volumes":null,
|
|
|
|
"VolumesFrom":"","Entrypoint":null},"Size":424242}`,
|
|
|
|
"checksum_simple": "sha256:1ac330d56e05eef6d438586545ceff7550d3bdcb6b19961f12c5ba714ee1bb37",
|
|
|
|
"checksum_tarsum": "tarsum+sha256:4409a0685741ca86d38df878ed6f8cbba4c99de5dc73cd71aef04be3bb70be7c",
|
|
|
|
"ancestry": `["77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20"]`,
|
|
|
|
"layer": string([]byte{
|
|
|
|
0x1f, 0x8b, 0x08, 0x08, 0x0e, 0xb0, 0xee, 0x51, 0x02, 0x03, 0x6c, 0x61, 0x79, 0x65,
|
|
|
|
0x72, 0x2e, 0x74, 0x61, 0x72, 0x00, 0xed, 0xd2, 0x31, 0x0e, 0xc2, 0x30, 0x0c, 0x05,
|
|
|
|
0x50, 0xcf, 0x9c, 0xc2, 0x27, 0x48, 0xed, 0x38, 0x4e, 0xce, 0x13, 0x44, 0x2b, 0x66,
|
|
|
|
0x62, 0x24, 0x8e, 0x4f, 0xa0, 0x15, 0x63, 0xb6, 0x20, 0x21, 0xfc, 0x96, 0xbf, 0x78,
|
|
|
|
0xb0, 0xf5, 0x1d, 0x16, 0x98, 0x8e, 0x88, 0x8a, 0x2a, 0xbe, 0x33, 0xef, 0x49, 0x31,
|
|
|
|
0xed, 0x79, 0x40, 0x8e, 0x5c, 0x44, 0x85, 0x88, 0x33, 0x12, 0x73, 0x2c, 0x02, 0xa8,
|
|
|
|
0xf0, 0x05, 0xf7, 0x66, 0xf5, 0xd6, 0x57, 0x69, 0xd7, 0x7a, 0x19, 0xcd, 0xf5, 0xb1,
|
|
|
|
0x6d, 0x1b, 0x1f, 0xf9, 0xba, 0xe3, 0x93, 0x3f, 0x22, 0x2c, 0xb6, 0x36, 0x0b, 0xf6,
|
|
|
|
0xb0, 0xa9, 0xfd, 0xe7, 0x94, 0x46, 0xfd, 0xeb, 0xd1, 0x7f, 0x2c, 0xc4, 0xd2, 0xfb,
|
|
|
|
0x97, 0xfe, 0x02, 0x80, 0xe4, 0xfd, 0x4f, 0x77, 0xae, 0x6d, 0x3d, 0x81, 0x73, 0xce,
|
|
|
|
0xb9, 0x7f, 0xf3, 0x04, 0x41, 0xc1, 0xab, 0xc6, 0x00, 0x0a, 0x00, 0x00,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
"42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d": {
|
|
|
|
"json": `{"id":"42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
|
|
|
|
"parent":"77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20",
|
|
|
|
"comment":"test base image","created":"2013-03-23T12:55:11.10432-07:00",
|
|
|
|
"container_config":{"Hostname":"","User":"","Memory":0,"MemorySwap":0,
|
|
|
|
"CpuShares":0,"AttachStdin":false,"AttachStdout":false,"AttachStderr":false,
|
2015-05-24 13:17:29 +00:00
|
|
|
"Tty":false,"OpenStdin":false,"StdinOnce":false,
|
2013-07-25 00:44:55 +00:00
|
|
|
"Env":null,"Cmd":null,"Dns":null,"Image":"","Volumes":null,
|
|
|
|
"VolumesFrom":"","Entrypoint":null},"Size":424242}`,
|
|
|
|
"checksum_simple": "sha256:bea7bf2e4bacd479344b737328db47b18880d09096e6674165533aa994f5e9f2",
|
|
|
|
"checksum_tarsum": "tarsum+sha256:68fdb56fb364f074eec2c9b3f85ca175329c4dcabc4a6a452b7272aa613a07a2",
|
|
|
|
"ancestry": `["42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
|
|
|
|
"77dbf71da1d00e3fbddc480176eac8994025630c6590d11cfc8fe1209c2a1d20"]`,
|
|
|
|
"layer": string([]byte{
|
|
|
|
0x1f, 0x8b, 0x08, 0x08, 0xbd, 0xb3, 0xee, 0x51, 0x02, 0x03, 0x6c, 0x61, 0x79, 0x65,
|
|
|
|
0x72, 0x2e, 0x74, 0x61, 0x72, 0x00, 0xed, 0xd1, 0x31, 0x0e, 0xc2, 0x30, 0x0c, 0x05,
|
|
|
|
0x50, 0xcf, 0x9c, 0xc2, 0x27, 0x48, 0x9d, 0x38, 0x8e, 0xcf, 0x53, 0x51, 0xaa, 0x56,
|
|
|
|
0xea, 0x44, 0x82, 0xc4, 0xf1, 0x09, 0xb4, 0xea, 0x98, 0x2d, 0x48, 0x08, 0xbf, 0xe5,
|
|
|
|
0x2f, 0x1e, 0xfc, 0xf5, 0xdd, 0x00, 0xdd, 0x11, 0x91, 0x8a, 0xe0, 0x27, 0xd3, 0x9e,
|
|
|
|
0x14, 0xe2, 0x9e, 0x07, 0xf4, 0xc1, 0x2b, 0x0b, 0xfb, 0xa4, 0x82, 0xe4, 0x3d, 0x93,
|
|
|
|
0x02, 0x0a, 0x7c, 0xc1, 0x23, 0x97, 0xf1, 0x5e, 0x5f, 0xc9, 0xcb, 0x38, 0xb5, 0xee,
|
|
|
|
0xea, 0xd9, 0x3c, 0xb7, 0x4b, 0xbe, 0x7b, 0x9c, 0xf9, 0x23, 0xdc, 0x50, 0x6e, 0xb9,
|
|
|
|
0xb8, 0xf2, 0x2c, 0x5d, 0xf7, 0x4f, 0x31, 0xb6, 0xf6, 0x4f, 0xc7, 0xfe, 0x41, 0x55,
|
|
|
|
0x63, 0xdd, 0x9f, 0x89, 0x09, 0x90, 0x6c, 0xff, 0xee, 0xae, 0xcb, 0xba, 0x4d, 0x17,
|
|
|
|
0x30, 0xc6, 0x18, 0xf3, 0x67, 0x5e, 0xc1, 0xed, 0x21, 0x5d, 0x00, 0x0a, 0x00, 0x00,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testRepositories = map[string]map[string]string{
|
2013-07-25 02:22:36 +00:00
|
|
|
"foo42/bar": {
|
2013-07-25 00:44:55 +00:00
|
|
|
"latest": "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
|
2015-06-10 22:18:15 +00:00
|
|
|
"test": "42d718c941f5c532ac049bf0b0ab53f0062f09a03afd4aa4a02c098e46032b9d",
|
2013-07-25 00:44:55 +00:00
|
|
|
},
|
|
|
|
}
|
2014-11-11 21:31:15 +00:00
|
|
|
mockHosts = map[string][]net.IP{
|
|
|
|
"": {net.ParseIP("0.0.0.0")},
|
|
|
|
"localhost": {net.ParseIP("127.0.0.1"), net.ParseIP("::1")},
|
|
|
|
"example.com": {net.ParseIP("42.42.42.42")},
|
2014-10-07 01:54:52 +00:00
|
|
|
"other.com": {net.ParseIP("43.43.43.43")},
|
2014-11-11 21:31:15 +00:00
|
|
|
}
|
2013-07-25 00:44:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
r := mux.NewRouter()
|
2014-10-02 01:26:06 +00:00
|
|
|
|
|
|
|
// /v1/
|
2019-10-12 18:42:44 +00:00
|
|
|
r.HandleFunc("/v1/_ping", handlerGetPing).Methods(http.MethodGet)
|
|
|
|
r.HandleFunc("/v1/images/{image_id:[^/]+}/{action:json|layer|ancestry}", handlerGetImage).Methods(http.MethodGet)
|
|
|
|
r.HandleFunc("/v1/images/{image_id:[^/]+}/{action:json|layer|checksum}", handlerPutImage).Methods(http.MethodPut)
|
|
|
|
r.HandleFunc("/v1/repositories/{repository:.+}/tags", handlerGetDeleteTags).Methods(http.MethodGet, http.MethodDelete)
|
|
|
|
r.HandleFunc("/v1/repositories/{repository:.+}/tags/{tag:.+}", handlerGetTag).Methods(http.MethodGet)
|
|
|
|
r.HandleFunc("/v1/repositories/{repository:.+}/tags/{tag:.+}", handlerPutTag).Methods(http.MethodPut)
|
|
|
|
r.HandleFunc("/v1/users{null:.*}", handlerUsers).Methods(http.MethodGet, http.MethodPost, http.MethodPut)
|
|
|
|
r.HandleFunc("/v1/repositories/{repository:.+}{action:/images|/}", handlerImages).Methods(http.MethodGet, http.MethodPut, http.MethodDelete)
|
|
|
|
r.HandleFunc("/v1/repositories/{repository:.+}/auth", handlerAuth).Methods(http.MethodPut)
|
|
|
|
r.HandleFunc("/v1/search", handlerSearch).Methods(http.MethodGet)
|
2014-10-02 01:26:06 +00:00
|
|
|
|
|
|
|
// /v2/
|
2019-10-12 18:42:44 +00:00
|
|
|
r.HandleFunc("/v2/version", handlerGetPing).Methods(http.MethodGet)
|
2014-10-02 01:26:06 +00:00
|
|
|
|
2014-10-06 19:34:39 +00:00
|
|
|
testHTTPServer = httptest.NewServer(handlerAccessLog(r))
|
2014-10-07 01:54:52 +00:00
|
|
|
testHTTPSServer = httptest.NewTLSServer(handlerAccessLog(r))
|
2014-11-11 21:31:15 +00:00
|
|
|
|
|
|
|
// override net.LookupIP
|
|
|
|
lookupIP = func(host string) ([]net.IP, error) {
|
|
|
|
if host == "127.0.0.1" {
|
|
|
|
// I believe in future Go versions this will fail, so let's fix it later
|
|
|
|
return net.LookupIP(host)
|
|
|
|
}
|
|
|
|
for h, addrs := range mockHosts {
|
|
|
|
if host == h {
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if addr.String() == host {
|
|
|
|
return []net.IP{addr}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errors.New("lookup: no such host")
|
|
|
|
}
|
2013-07-25 19:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerAccessLog(handler http.Handler) http.Handler {
|
|
|
|
logHandler := func(w http.ResponseWriter, r *http.Request) {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("%s \"%s %s\"", r.RemoteAddr, r.Method, r.URL)
|
2013-07-25 19:57:09 +00:00
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(logHandler)
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func makeURL(req string) string {
|
2014-10-06 19:34:39 +00:00
|
|
|
return testHTTPServer.URL + req
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 19:40:36 +00:00
|
|
|
func makeHTTPSURL(req string) string {
|
2014-10-07 01:54:52 +00:00
|
|
|
return testHTTPSServer.URL + req
|
|
|
|
}
|
|
|
|
|
2015-12-12 02:14:52 +00:00
|
|
|
func makeIndex(req string) *registrytypes.IndexInfo {
|
|
|
|
index := ®istrytypes.IndexInfo{
|
2014-10-07 01:54:52 +00:00
|
|
|
Name: makeURL(req),
|
|
|
|
}
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
|
2015-12-12 02:14:52 +00:00
|
|
|
func makeHTTPSIndex(req string) *registrytypes.IndexInfo {
|
|
|
|
index := ®istrytypes.IndexInfo{
|
2015-07-21 19:40:36 +00:00
|
|
|
Name: makeHTTPSURL(req),
|
2014-10-07 01:54:52 +00:00
|
|
|
}
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
|
2015-12-12 02:14:52 +00:00
|
|
|
func makePublicIndex() *registrytypes.IndexInfo {
|
|
|
|
index := ®istrytypes.IndexInfo{
|
2015-07-21 19:40:36 +00:00
|
|
|
Name: IndexServer,
|
2014-10-07 01:54:52 +00:00
|
|
|
Secure: true,
|
|
|
|
Official: true,
|
|
|
|
}
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
|
2017-09-01 14:35:04 +00:00
|
|
|
func makeServiceConfig(mirrors []string, insecureRegistries []string) (*serviceConfig, error) {
|
2016-03-08 21:03:37 +00:00
|
|
|
options := ServiceOptions{
|
|
|
|
Mirrors: mirrors,
|
|
|
|
InsecureRegistries: insecureRegistries,
|
2014-10-07 01:54:52 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 21:03:37 +00:00
|
|
|
return newServiceConfig(options)
|
2014-10-07 01:54:52 +00:00
|
|
|
}
|
|
|
|
|
2013-07-25 00:44:55 +00:00
|
|
|
func writeHeaders(w http.ResponseWriter) {
|
|
|
|
h := w.Header()
|
|
|
|
h.Add("Server", "docker-tests/mock")
|
|
|
|
h.Add("Expires", "-1")
|
|
|
|
h.Add("Content-Type", "application/json")
|
|
|
|
h.Add("Pragma", "no-cache")
|
|
|
|
h.Add("Cache-Control", "no-cache")
|
|
|
|
h.Add("X-Docker-Registry-Version", "0.0.0")
|
|
|
|
h.Add("X-Docker-Registry-Config", "mock")
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeResponse(w http.ResponseWriter, message interface{}, code int) {
|
|
|
|
writeHeaders(w)
|
|
|
|
w.WriteHeader(code)
|
|
|
|
body, err := json.Marshal(message)
|
|
|
|
if err != nil {
|
|
|
|
io.WriteString(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readJSON(r *http.Request, dest interface{}) error {
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return json.Unmarshal(body, dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
func apiError(w http.ResponseWriter, message string, code int) {
|
|
|
|
body := map[string]string{
|
|
|
|
"error": message,
|
|
|
|
}
|
|
|
|
writeResponse(w, body, code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertEqual(t *testing.T, a interface{}, b interface{}, message string) {
|
|
|
|
if a == b {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(message) == 0 {
|
|
|
|
message = fmt.Sprintf("%v != %v", a, b)
|
|
|
|
}
|
|
|
|
t.Fatal(message)
|
|
|
|
}
|
|
|
|
|
2014-10-07 01:54:52 +00:00
|
|
|
func assertNotEqual(t *testing.T, a interface{}, b interface{}, message string) {
|
|
|
|
if a != b {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(message) == 0 {
|
|
|
|
message = fmt.Sprintf("%v == %v", a, b)
|
|
|
|
}
|
|
|
|
t.Fatal(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Similar to assertEqual, but does not stop test
|
|
|
|
func checkEqual(t *testing.T, a interface{}, b interface{}, messagePrefix string) {
|
|
|
|
if a == b {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
message := fmt.Sprintf("%v != %v", a, b)
|
|
|
|
if len(messagePrefix) != 0 {
|
|
|
|
message = messagePrefix + ": " + message
|
|
|
|
}
|
|
|
|
t.Error(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Similar to assertNotEqual, but does not stop test
|
|
|
|
func checkNotEqual(t *testing.T, a interface{}, b interface{}, messagePrefix string) {
|
|
|
|
if a != b {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
message := fmt.Sprintf("%v == %v", a, b)
|
|
|
|
if len(messagePrefix) != 0 {
|
|
|
|
message = messagePrefix + ": " + message
|
|
|
|
}
|
|
|
|
t.Error(message)
|
|
|
|
}
|
|
|
|
|
2013-07-25 00:44:55 +00:00
|
|
|
func requiresAuth(w http.ResponseWriter, r *http.Request) bool {
|
|
|
|
writeCookie := func() {
|
|
|
|
value := fmt.Sprintf("FAKE-SESSION-%d", time.Now().UnixNano())
|
|
|
|
cookie := &http.Cookie{Name: "session", Value: value, MaxAge: 3600}
|
|
|
|
http.SetCookie(w, cookie)
|
2013-07-25 02:22:36 +00:00
|
|
|
//FIXME(sam): this should be sent only on Index routes
|
|
|
|
value = fmt.Sprintf("FAKE-TOKEN-%d", time.Now().UnixNano())
|
|
|
|
w.Header().Add("X-Docker-Token", value)
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
if len(r.Cookies()) > 0 {
|
|
|
|
writeCookie()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if len(r.Header.Get("Authorization")) > 0 {
|
|
|
|
writeCookie()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
w.Header().Add("WWW-Authenticate", "token")
|
2019-10-13 15:25:25 +00:00
|
|
|
apiError(w, "Wrong auth", http.StatusUnauthorized)
|
2013-07-25 00:44:55 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerGetPing(w http.ResponseWriter, r *http.Request) {
|
2019-10-13 15:25:25 +00:00
|
|
|
writeResponse(w, true, http.StatusOK)
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerGetImage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !requiresAuth(w, r) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
layer, exists := testLayers[vars["image_id"]]
|
|
|
|
if !exists {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
writeHeaders(w)
|
2014-10-06 19:34:39 +00:00
|
|
|
layerSize := len(layer["layer"])
|
|
|
|
w.Header().Add("X-Docker-Size", strconv.Itoa(layerSize))
|
2013-07-25 00:44:55 +00:00
|
|
|
io.WriteString(w, layer[vars["action"]])
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerPutImage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !requiresAuth(w, r) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vars := mux.Vars(r)
|
2014-10-06 19:34:39 +00:00
|
|
|
imageID := vars["image_id"]
|
2013-07-25 00:44:55 +00:00
|
|
|
action := vars["action"]
|
2014-10-06 19:34:39 +00:00
|
|
|
layer, exists := testLayers[imageID]
|
2013-07-25 00:44:55 +00:00
|
|
|
if !exists {
|
|
|
|
if action != "json" {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
layer = make(map[string]string)
|
2014-10-06 19:34:39 +00:00
|
|
|
testLayers[imageID] = layer
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
if checksum := r.Header.Get("X-Docker-Checksum"); checksum != "" {
|
|
|
|
if checksum != layer["checksum_simple"] && checksum != layer["checksum_tarsum"] {
|
2019-10-13 15:25:25 +00:00
|
|
|
apiError(w, "Wrong checksum", http.StatusBadRequest)
|
2013-07-25 00:44:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2019-10-13 15:25:25 +00:00
|
|
|
apiError(w, fmt.Sprintf("Error: %s", err), http.StatusInternalServerError)
|
2013-07-25 00:44:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
layer[action] = string(body)
|
2019-10-13 15:25:25 +00:00
|
|
|
writeResponse(w, true, http.StatusOK)
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerGetDeleteTags(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !requiresAuth(w, r) {
|
|
|
|
return
|
|
|
|
}
|
2015-11-18 22:20:54 +00:00
|
|
|
repositoryName, err := reference.WithName(mux.Vars(r)["repository"])
|
|
|
|
if err != nil {
|
2019-10-13 15:25:25 +00:00
|
|
|
apiError(w, "Could not parse repository", http.StatusBadRequest)
|
2015-11-18 22:20:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
tags, exists := testRepositories[repositoryName.String()]
|
2013-07-25 00:44:55 +00:00
|
|
|
if !exists {
|
2019-10-13 15:25:25 +00:00
|
|
|
apiError(w, "Repository not found", http.StatusNotFound)
|
2014-08-25 17:29:38 +00:00
|
|
|
return
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
2019-10-12 18:42:44 +00:00
|
|
|
if r.Method == http.MethodDelete {
|
2015-11-18 22:20:54 +00:00
|
|
|
delete(testRepositories, repositoryName.String())
|
2019-10-13 15:25:25 +00:00
|
|
|
writeResponse(w, true, http.StatusOK)
|
2013-07-25 00:44:55 +00:00
|
|
|
return
|
|
|
|
}
|
2019-10-13 15:25:25 +00:00
|
|
|
writeResponse(w, tags, http.StatusOK)
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerGetTag(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !requiresAuth(w, r) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vars := mux.Vars(r)
|
2015-11-18 22:20:54 +00:00
|
|
|
repositoryName, err := reference.WithName(vars["repository"])
|
|
|
|
if err != nil {
|
2019-10-13 15:25:25 +00:00
|
|
|
apiError(w, "Could not parse repository", http.StatusBadRequest)
|
2015-11-18 22:20:54 +00:00
|
|
|
return
|
|
|
|
}
|
2013-07-25 00:44:55 +00:00
|
|
|
tagName := vars["tag"]
|
2015-11-18 22:20:54 +00:00
|
|
|
tags, exists := testRepositories[repositoryName.String()]
|
2013-07-25 00:44:55 +00:00
|
|
|
if !exists {
|
2019-10-13 15:25:25 +00:00
|
|
|
apiError(w, "Repository not found", http.StatusNotFound)
|
2014-08-25 17:29:38 +00:00
|
|
|
return
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
tag, exists := tags[tagName]
|
|
|
|
if !exists {
|
2019-10-13 15:25:25 +00:00
|
|
|
apiError(w, "Tag not found", http.StatusNotFound)
|
2014-08-25 17:29:38 +00:00
|
|
|
return
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
2019-10-13 15:25:25 +00:00
|
|
|
writeResponse(w, tag, http.StatusOK)
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerPutTag(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !requiresAuth(w, r) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vars := mux.Vars(r)
|
2015-11-18 22:20:54 +00:00
|
|
|
repositoryName, err := reference.WithName(vars["repository"])
|
|
|
|
if err != nil {
|
2019-10-13 15:25:25 +00:00
|
|
|
apiError(w, "Could not parse repository", http.StatusBadRequest)
|
2015-11-18 22:20:54 +00:00
|
|
|
return
|
|
|
|
}
|
2013-07-25 00:44:55 +00:00
|
|
|
tagName := vars["tag"]
|
2015-11-18 22:20:54 +00:00
|
|
|
tags, exists := testRepositories[repositoryName.String()]
|
2013-07-25 00:44:55 +00:00
|
|
|
if !exists {
|
2015-11-18 22:20:54 +00:00
|
|
|
tags = make(map[string]string)
|
|
|
|
testRepositories[repositoryName.String()] = tags
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
tagValue := ""
|
|
|
|
readJSON(r, tagValue)
|
|
|
|
tags[tagName] = tagValue
|
2019-10-13 15:25:25 +00:00
|
|
|
writeResponse(w, true, http.StatusOK)
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerUsers(w http.ResponseWriter, r *http.Request) {
|
2019-10-13 15:25:25 +00:00
|
|
|
code := http.StatusOK
|
2019-10-12 18:42:44 +00:00
|
|
|
if r.Method == http.MethodPost {
|
2019-10-13 15:25:25 +00:00
|
|
|
code = http.StatusCreated
|
2019-10-12 18:42:44 +00:00
|
|
|
} else if r.Method == http.MethodPut {
|
2019-10-13 15:25:25 +00:00
|
|
|
code = http.StatusNoContent
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
writeResponse(w, "", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerImages(w http.ResponseWriter, r *http.Request) {
|
2014-10-06 19:34:39 +00:00
|
|
|
u, _ := url.Parse(testHTTPServer.URL)
|
2014-04-14 18:32:47 +00:00
|
|
|
w.Header().Add("X-Docker-Endpoints", fmt.Sprintf("%s , %s ", u.Host, "test.example.com"))
|
2013-07-31 17:05:06 +00:00
|
|
|
w.Header().Add("X-Docker-Token", fmt.Sprintf("FAKE-SESSION-%d", time.Now().UnixNano()))
|
2019-10-12 18:42:44 +00:00
|
|
|
if r.Method == http.MethodPut {
|
2013-07-31 17:05:06 +00:00
|
|
|
if strings.HasSuffix(r.URL.Path, "images") {
|
2019-10-13 15:25:25 +00:00
|
|
|
writeResponse(w, "", http.StatusNoContent)
|
2013-07-31 17:05:06 +00:00
|
|
|
return
|
|
|
|
}
|
2019-10-13 15:25:25 +00:00
|
|
|
writeResponse(w, "", http.StatusOK)
|
2013-07-25 00:44:55 +00:00
|
|
|
return
|
|
|
|
}
|
2019-10-12 18:42:44 +00:00
|
|
|
if r.Method == http.MethodDelete {
|
2019-10-13 15:25:25 +00:00
|
|
|
writeResponse(w, "", http.StatusNoContent)
|
2013-07-25 00:44:55 +00:00
|
|
|
return
|
|
|
|
}
|
2018-05-19 11:38:54 +00:00
|
|
|
var images []map[string]string
|
2014-10-06 19:34:39 +00:00
|
|
|
for imageID, layer := range testLayers {
|
2013-07-25 00:44:55 +00:00
|
|
|
image := make(map[string]string)
|
2014-10-06 19:34:39 +00:00
|
|
|
image["id"] = imageID
|
2013-07-25 00:44:55 +00:00
|
|
|
image["checksum"] = layer["checksum_tarsum"]
|
2013-07-25 19:57:09 +00:00
|
|
|
image["Tag"] = "latest"
|
2013-07-25 02:22:36 +00:00
|
|
|
images = append(images, image)
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
2019-10-13 15:25:25 +00:00
|
|
|
writeResponse(w, images, http.StatusOK)
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerAuth(w http.ResponseWriter, r *http.Request) {
|
2019-10-13 15:25:25 +00:00
|
|
|
writeResponse(w, "OK", http.StatusOK)
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerSearch(w http.ResponseWriter, r *http.Request) {
|
2015-12-15 16:44:20 +00:00
|
|
|
result := ®istrytypes.SearchResults{
|
2014-03-13 17:40:34 +00:00
|
|
|
Query: "fakequery",
|
|
|
|
NumResults: 1,
|
2015-12-15 16:44:20 +00:00
|
|
|
Results: []registrytypes.SearchResult{{Name: "fakeimage", StarCount: 42}},
|
2014-03-13 17:40:34 +00:00
|
|
|
}
|
2019-10-13 15:25:25 +00:00
|
|
|
writeResponse(w, result, http.StatusOK)
|
2013-07-25 00:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPing(t *testing.T) {
|
|
|
|
res, err := http.Get(makeURL("/v1/_ping"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-10-13 15:25:25 +00:00
|
|
|
assertEqual(t, res.StatusCode, http.StatusOK, "")
|
2013-07-25 00:44:55 +00:00
|
|
|
assertEqual(t, res.Header.Get("X-Docker-Registry-Config"), "mock",
|
|
|
|
"This is not a Mocked Registry")
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Uncomment this to test Mocked Registry locally with curl
|
|
|
|
* WARNING: Don't push on the repos uncommented, it'll block the tests
|
2013-07-27 00:28:17 +00:00
|
|
|
*
|
2013-07-25 00:44:55 +00:00
|
|
|
func TestWait(t *testing.T) {
|
2015-07-21 19:40:36 +00:00
|
|
|
logrus.Println("Test HTTP server ready and waiting:", testHTTPServer.URL)
|
2013-07-25 00:44:55 +00:00
|
|
|
c := make(chan int)
|
|
|
|
<-c
|
|
|
|
}
|
2013-07-25 19:57:09 +00:00
|
|
|
|
2013-08-15 11:42:40 +00:00
|
|
|
//*/
|