2015-01-13 00:26:49 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-01-31 22:28:11 +03:00
|
|
|
"net/http"
|
2015-01-13 00:26:49 +03:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2015-04-18 19:46:47 +03:00
|
|
|
|
2015-08-01 09:27:19 +03:00
|
|
|
"github.com/docker/distribution/digest"
|
2015-04-18 19:46:47 +03:00
|
|
|
"github.com/go-check/check"
|
2015-01-13 00:26:49 +03:00
|
|
|
)
|
|
|
|
|
2015-12-19 02:06:23 +03:00
|
|
|
const (
|
|
|
|
v2binary = "registry-v2"
|
|
|
|
v2binarySchema1 = "registry-v2-schema1"
|
|
|
|
)
|
2015-01-13 00:26:49 +03:00
|
|
|
|
|
|
|
type testRegistryV2 struct {
|
2016-01-23 21:45:01 +03:00
|
|
|
cmd *exec.Cmd
|
|
|
|
dir string
|
2016-03-14 23:11:35 +03:00
|
|
|
auth string
|
2016-01-23 21:45:01 +03:00
|
|
|
username string
|
|
|
|
password string
|
|
|
|
email string
|
2015-01-13 00:26:49 +03:00
|
|
|
}
|
|
|
|
|
2016-03-14 23:11:35 +03:00
|
|
|
func newTestRegistryV2(c *check.C, schema1 bool, auth, tokenURL string) (*testRegistryV2, error) {
|
2016-01-23 21:45:01 +03:00
|
|
|
tmp, err := ioutil.TempDir("", "registry-test-")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-13 00:26:49 +03:00
|
|
|
template := `version: 0.1
|
|
|
|
loglevel: debug
|
|
|
|
storage:
|
|
|
|
filesystem:
|
|
|
|
rootdirectory: %s
|
|
|
|
http:
|
2016-01-23 21:45:01 +03:00
|
|
|
addr: %s
|
|
|
|
%s`
|
|
|
|
var (
|
2016-03-14 23:11:35 +03:00
|
|
|
authTemplate string
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
email string
|
2016-01-23 21:45:01 +03:00
|
|
|
)
|
2016-03-14 23:11:35 +03:00
|
|
|
switch auth {
|
|
|
|
case "htpasswd":
|
2016-01-23 21:45:01 +03:00
|
|
|
htpasswdPath := filepath.Join(tmp, "htpasswd")
|
|
|
|
// generated with: htpasswd -Bbn testuser testpassword
|
|
|
|
userpasswd := "testuser:$2y$05$sBsSqk0OpSD1uTZkHXc4FeJ0Z70wLQdAX/82UiHuQOKbNbBrzs63m"
|
|
|
|
username = "testuser"
|
|
|
|
password = "testpassword"
|
|
|
|
email = "test@test.org"
|
|
|
|
if err := ioutil.WriteFile(htpasswdPath, []byte(userpasswd), os.FileMode(0644)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-14 23:11:35 +03:00
|
|
|
authTemplate = fmt.Sprintf(`auth:
|
2016-01-23 21:45:01 +03:00
|
|
|
htpasswd:
|
|
|
|
realm: basic-realm
|
|
|
|
path: %s
|
|
|
|
`, htpasswdPath)
|
2016-03-14 23:11:35 +03:00
|
|
|
case "token":
|
|
|
|
authTemplate = fmt.Sprintf(`auth:
|
|
|
|
token:
|
|
|
|
realm: %s
|
|
|
|
service: "registry"
|
|
|
|
issuer: "auth-registry"
|
|
|
|
rootcertbundle: "fixtures/registry/cert.pem"
|
|
|
|
`, tokenURL)
|
2015-01-13 00:26:49 +03:00
|
|
|
}
|
2016-01-23 21:45:01 +03:00
|
|
|
|
2015-01-13 00:26:49 +03:00
|
|
|
confPath := filepath.Join(tmp, "config.yaml")
|
|
|
|
config, err := os.Create(confPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-25 06:57:21 +03:00
|
|
|
defer config.Close()
|
|
|
|
|
2016-03-14 23:11:35 +03:00
|
|
|
if _, err := fmt.Fprintf(config, template, tmp, privateRegistryURL, authTemplate); err != nil {
|
2015-01-13 00:26:49 +03:00
|
|
|
os.RemoveAll(tmp)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-19 02:06:23 +03:00
|
|
|
binary := v2binary
|
|
|
|
if schema1 {
|
|
|
|
binary = v2binarySchema1
|
|
|
|
}
|
|
|
|
cmd := exec.Command(binary, confPath)
|
2015-01-13 00:26:49 +03:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
os.RemoveAll(tmp)
|
|
|
|
if os.IsNotExist(err) {
|
2015-04-18 19:46:47 +03:00
|
|
|
c.Skip(err.Error())
|
2015-01-13 00:26:49 +03:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &testRegistryV2{
|
2016-01-23 21:45:01 +03:00
|
|
|
cmd: cmd,
|
|
|
|
dir: tmp,
|
2016-03-14 23:11:35 +03:00
|
|
|
auth: auth,
|
2016-01-23 21:45:01 +03:00
|
|
|
username: username,
|
|
|
|
password: password,
|
|
|
|
email: email,
|
2015-01-13 00:26:49 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-01-31 22:28:11 +03:00
|
|
|
func (t *testRegistryV2) Ping() error {
|
|
|
|
// We always ping through HTTP for our test registry.
|
|
|
|
resp, err := http.Get(fmt.Sprintf("http://%s/v2/", privateRegistryURL))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-23 21:45:01 +03:00
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
fail := resp.StatusCode != http.StatusOK
|
2016-03-14 23:11:35 +03:00
|
|
|
if t.auth != "" {
|
2016-01-23 21:45:01 +03:00
|
|
|
// unauthorized is a _good_ status when pinging v2/ and it needs auth
|
|
|
|
fail = fail && resp.StatusCode != http.StatusUnauthorized
|
|
|
|
}
|
|
|
|
if fail {
|
2015-02-03 01:53:20 +03:00
|
|
|
return fmt.Errorf("registry ping replied with an unexpected status code %d", resp.StatusCode)
|
2015-01-31 22:28:11 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-22 15:59:24 +03:00
|
|
|
func (t *testRegistryV2) Close() {
|
|
|
|
t.cmd.Process.Kill()
|
|
|
|
os.RemoveAll(t.dir)
|
2015-01-13 00:26:49 +03:00
|
|
|
}
|
2015-08-01 09:27:19 +03:00
|
|
|
|
|
|
|
func (t *testRegistryV2) getBlobFilename(blobDigest digest.Digest) string {
|
2016-07-21 13:03:37 +03:00
|
|
|
// Split the digest into its algorithm and hex components.
|
2015-08-01 09:27:19 +03:00
|
|
|
dgstAlg, dgstHex := blobDigest.Algorithm(), blobDigest.Hex()
|
|
|
|
|
|
|
|
// The path to the target blob data looks something like:
|
|
|
|
// baseDir + "docker/registry/v2/blobs/sha256/a3/a3ed...46d4/data"
|
|
|
|
return fmt.Sprintf("%s/docker/registry/v2/blobs/%s/%s/%s/data", t.dir, dgstAlg, dgstHex[:2], dgstHex)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testRegistryV2) readBlobContents(c *check.C, blobDigest digest.Digest) []byte {
|
|
|
|
// Load the target manifest blob.
|
|
|
|
manifestBlob, err := ioutil.ReadFile(t.getBlobFilename(blobDigest))
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("unable to read blob: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return manifestBlob
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testRegistryV2) writeBlobContents(c *check.C, blobDigest digest.Digest, data []byte) {
|
|
|
|
if err := ioutil.WriteFile(t.getBlobFilename(blobDigest), data, os.FileMode(0644)); err != nil {
|
|
|
|
c.Fatalf("unable to write malicious data blob: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testRegistryV2) tempMoveBlobData(c *check.C, blobDigest digest.Digest) (undo func()) {
|
|
|
|
tempFile, err := ioutil.TempFile("", "registry-temp-blob-")
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("unable to get temporary blob file: %s", err)
|
|
|
|
}
|
|
|
|
tempFile.Close()
|
|
|
|
|
|
|
|
blobFilename := t.getBlobFilename(blobDigest)
|
|
|
|
|
|
|
|
// Move the existing data file aside, so that we can replace it with a
|
|
|
|
// another blob of data.
|
|
|
|
if err := os.Rename(blobFilename, tempFile.Name()); err != nil {
|
|
|
|
os.Remove(tempFile.Name())
|
|
|
|
c.Fatalf("unable to move data blob: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
os.Rename(tempFile.Name(), blobFilename)
|
|
|
|
os.Remove(tempFile.Name())
|
|
|
|
}
|
|
|
|
}
|