2019-08-29 20:52:40 +00:00
|
|
|
package registry // import "github.com/docker/docker/testutil/registry"
|
2015-01-12 21:26:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-02-11 00:31:04 +00:00
|
|
|
"io"
|
2015-01-31 19:28:11 +00:00
|
|
|
"net/http"
|
2015-01-12 21:26:49 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2019-09-23 11:54:51 +00:00
|
|
|
"testing"
|
2018-04-13 08:45:34 +00:00
|
|
|
"time"
|
2015-04-18 16:46:47 +00:00
|
|
|
|
2019-09-23 12:23:01 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2015-01-12 21:26:49 +00:00
|
|
|
)
|
|
|
|
|
2015-12-18 23:06:23 +00:00
|
|
|
const (
|
2018-04-13 08:45:34 +00:00
|
|
|
// V2binary is the name of the registry v2 binary
|
|
|
|
V2binary = "registry-v2"
|
|
|
|
// V2binarySchema1 is the name of the registry that serve schema1
|
|
|
|
V2binarySchema1 = "registry-v2-schema1"
|
|
|
|
// DefaultURL is the default url that will be used by the registry (if not specified otherwise)
|
|
|
|
DefaultURL = "127.0.0.1:5000"
|
2015-12-18 23:06:23 +00:00
|
|
|
)
|
2015-01-12 21:26:49 +00:00
|
|
|
|
2016-12-30 18:10:04 +00:00
|
|
|
// V2 represent a registry version 2
|
|
|
|
type V2 struct {
|
|
|
|
cmd *exec.Cmd
|
|
|
|
registryURL string
|
|
|
|
dir string
|
|
|
|
auth string
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
email string
|
|
|
|
}
|
|
|
|
|
2018-04-13 08:45:34 +00:00
|
|
|
// Config contains the test registry configuration
|
|
|
|
type Config struct {
|
|
|
|
schema1 bool
|
|
|
|
auth string
|
|
|
|
tokenURL string
|
|
|
|
registryURL string
|
2020-02-11 00:31:04 +00:00
|
|
|
stdout io.Writer
|
|
|
|
stderr io.Writer
|
2018-04-13 08:45:34 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 18:10:04 +00:00
|
|
|
// NewV2 creates a v2 registry server
|
2019-09-23 11:54:51 +00:00
|
|
|
func NewV2(t testing.TB, ops ...func(*Config)) *V2 {
|
2019-09-23 12:06:27 +00:00
|
|
|
t.Helper()
|
2018-04-13 08:45:34 +00:00
|
|
|
c := &Config{
|
|
|
|
registryURL: DefaultURL,
|
2016-01-23 18:45:01 +00:00
|
|
|
}
|
2018-04-13 08:45:34 +00:00
|
|
|
for _, op := range ops {
|
|
|
|
op(c)
|
|
|
|
}
|
2021-08-24 10:10:50 +00:00
|
|
|
tmp, err := os.MkdirTemp("", "registry-test-")
|
2018-04-13 08:45:34 +00:00
|
|
|
assert.NilError(t, err)
|
2015-01-12 21:26:49 +00:00
|
|
|
template := `version: 0.1
|
|
|
|
loglevel: debug
|
|
|
|
storage:
|
|
|
|
filesystem:
|
|
|
|
rootdirectory: %s
|
|
|
|
http:
|
2016-01-23 18:45:01 +00:00
|
|
|
addr: %s
|
|
|
|
%s`
|
|
|
|
var (
|
2016-03-14 20:11:35 +00:00
|
|
|
authTemplate string
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
email string
|
2016-01-23 18:45:01 +00:00
|
|
|
)
|
2018-04-13 08:45:34 +00:00
|
|
|
switch c.auth {
|
2016-03-14 20:11:35 +00:00
|
|
|
case "htpasswd":
|
2016-01-23 18:45:01 +00:00
|
|
|
htpasswdPath := filepath.Join(tmp, "htpasswd")
|
|
|
|
// generated with: htpasswd -Bbn testuser testpassword
|
2019-08-06 17:11:25 +00:00
|
|
|
// #nosec G101
|
2016-01-23 18:45:01 +00:00
|
|
|
userpasswd := "testuser:$2y$05$sBsSqk0OpSD1uTZkHXc4FeJ0Z70wLQdAX/82UiHuQOKbNbBrzs63m"
|
|
|
|
username = "testuser"
|
|
|
|
password = "testpassword"
|
|
|
|
email = "test@test.org"
|
2022-01-20 12:42:50 +00:00
|
|
|
err := os.WriteFile(htpasswdPath, []byte(userpasswd), os.FileMode(0o644))
|
2018-04-13 08:45:34 +00:00
|
|
|
assert.NilError(t, err)
|
2016-03-14 20:11:35 +00:00
|
|
|
authTemplate = fmt.Sprintf(`auth:
|
2016-01-23 18:45:01 +00:00
|
|
|
htpasswd:
|
|
|
|
realm: basic-realm
|
|
|
|
path: %s
|
|
|
|
`, htpasswdPath)
|
2016-03-14 20:11:35 +00:00
|
|
|
case "token":
|
|
|
|
authTemplate = fmt.Sprintf(`auth:
|
|
|
|
token:
|
|
|
|
realm: %s
|
|
|
|
service: "registry"
|
|
|
|
issuer: "auth-registry"
|
|
|
|
rootcertbundle: "fixtures/registry/cert.pem"
|
2018-04-13 08:45:34 +00:00
|
|
|
`, c.tokenURL)
|
2015-01-12 21:26:49 +00:00
|
|
|
}
|
2016-01-23 18:45:01 +00:00
|
|
|
|
2015-01-12 21:26:49 +00:00
|
|
|
confPath := filepath.Join(tmp, "config.yaml")
|
|
|
|
config, err := os.Create(confPath)
|
2018-04-13 08:45:34 +00:00
|
|
|
assert.NilError(t, err)
|
2016-06-25 03:57:21 +00:00
|
|
|
defer config.Close()
|
|
|
|
|
2018-04-13 08:45:34 +00:00
|
|
|
if _, err := fmt.Fprintf(config, template, tmp, c.registryURL, authTemplate); err != nil {
|
|
|
|
// FIXME(vdemeester) use a defer/clean func
|
2015-01-12 21:26:49 +00:00
|
|
|
os.RemoveAll(tmp)
|
2018-04-13 08:45:34 +00:00
|
|
|
t.Fatal(err)
|
2015-01-12 21:26:49 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 08:45:34 +00:00
|
|
|
binary := V2binary
|
2023-08-24 09:24:07 +00:00
|
|
|
args := []string{"serve", confPath}
|
2018-04-13 08:45:34 +00:00
|
|
|
if c.schema1 {
|
|
|
|
binary = V2binarySchema1
|
2023-08-24 09:24:07 +00:00
|
|
|
args = []string{confPath}
|
2015-12-18 23:06:23 +00:00
|
|
|
}
|
2023-08-24 09:24:07 +00:00
|
|
|
cmd := exec.Command(binary, args...)
|
2020-02-11 00:31:04 +00:00
|
|
|
cmd.Stdout = c.stdout
|
|
|
|
cmd.Stderr = c.stderr
|
2015-01-12 21:26:49 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
2018-04-13 08:45:34 +00:00
|
|
|
// FIXME(vdemeester) use a defer/clean func
|
2015-01-12 21:26:49 +00:00
|
|
|
os.RemoveAll(tmp)
|
2018-04-13 08:45:34 +00:00
|
|
|
t.Fatal(err)
|
2015-01-12 21:26:49 +00:00
|
|
|
}
|
2016-12-30 18:10:04 +00:00
|
|
|
return &V2{
|
|
|
|
cmd: cmd,
|
|
|
|
dir: tmp,
|
2018-04-13 08:45:34 +00:00
|
|
|
auth: c.auth,
|
2016-12-30 18:10:04 +00:00
|
|
|
username: username,
|
|
|
|
password: password,
|
|
|
|
email: email,
|
2018-04-13 08:45:34 +00:00
|
|
|
registryURL: c.registryURL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitReady waits for the registry to be ready to serve requests (or fail after a while)
|
2019-09-23 11:54:51 +00:00
|
|
|
func (r *V2) WaitReady(t testing.TB) {
|
2019-09-23 12:06:27 +00:00
|
|
|
t.Helper()
|
2018-04-13 08:45:34 +00:00
|
|
|
var err error
|
|
|
|
for i := 0; i != 50; i++ {
|
|
|
|
if err = r.Ping(); err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
t.Fatalf("timeout waiting for test registry to become available: %v", err)
|
2015-01-12 21:26:49 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 18:10:04 +00:00
|
|
|
// Ping sends an http request to the current registry, and fail if it doesn't respond correctly
|
|
|
|
func (r *V2) Ping() error {
|
2015-01-31 19:28:11 +00:00
|
|
|
// We always ping through HTTP for our test registry.
|
2016-12-30 18:10:04 +00:00
|
|
|
resp, err := http.Get(fmt.Sprintf("http://%s/v2/", r.registryURL))
|
2015-01-31 19:28:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-23 18:45:01 +00:00
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
fail := resp.StatusCode != http.StatusOK
|
2016-12-30 18:10:04 +00:00
|
|
|
if r.auth != "" {
|
2016-01-23 18:45:01 +00:00
|
|
|
// unauthorized is a _good_ status when pinging v2/ and it needs auth
|
|
|
|
fail = fail && resp.StatusCode != http.StatusUnauthorized
|
|
|
|
}
|
|
|
|
if fail {
|
2015-02-02 22:53:20 +00:00
|
|
|
return fmt.Errorf("registry ping replied with an unexpected status code %d", resp.StatusCode)
|
2015-01-31 19:28:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-30 18:10:04 +00:00
|
|
|
// Close kills the registry server
|
|
|
|
func (r *V2) Close() {
|
|
|
|
r.cmd.Process.Kill()
|
2017-01-14 01:45:14 +00:00
|
|
|
r.cmd.Process.Wait()
|
2016-12-30 18:10:04 +00:00
|
|
|
os.RemoveAll(r.dir)
|
2015-01-12 21:26:49 +00:00
|
|
|
}
|
2015-08-01 06:27:19 +00:00
|
|
|
|
2016-12-30 18:10:04 +00:00
|
|
|
func (r *V2) getBlobFilename(blobDigest digest.Digest) string {
|
2016-07-21 10:03:37 +00:00
|
|
|
// Split the digest into its algorithm and hex components.
|
2022-11-08 15:42:13 +00:00
|
|
|
dgstAlg, dgstHex := blobDigest.Algorithm(), blobDigest.Encoded()
|
2015-08-01 06:27:19 +00:00
|
|
|
|
|
|
|
// The path to the target blob data looks something like:
|
|
|
|
// baseDir + "docker/registry/v2/blobs/sha256/a3/a3ed...46d4/data"
|
2016-12-30 18:10:04 +00:00
|
|
|
return fmt.Sprintf("%s/docker/registry/v2/blobs/%s/%s/%s/data", r.dir, dgstAlg, dgstHex[:2], dgstHex)
|
2015-08-01 06:27:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 18:10:04 +00:00
|
|
|
// ReadBlobContents read the file corresponding to the specified digest
|
2019-09-23 12:23:01 +00:00
|
|
|
func (r *V2) ReadBlobContents(t testing.TB, blobDigest digest.Digest) []byte {
|
|
|
|
t.Helper()
|
2015-08-01 06:27:19 +00:00
|
|
|
// Load the target manifest blob.
|
2021-08-24 10:10:50 +00:00
|
|
|
manifestBlob, err := os.ReadFile(r.getBlobFilename(blobDigest))
|
2018-04-13 08:45:34 +00:00
|
|
|
assert.NilError(t, err, "unable to read blob")
|
2015-08-01 06:27:19 +00:00
|
|
|
return manifestBlob
|
|
|
|
}
|
|
|
|
|
2016-12-30 18:10:04 +00:00
|
|
|
// WriteBlobContents write the file corresponding to the specified digest with the given content
|
2019-09-23 12:23:01 +00:00
|
|
|
func (r *V2) WriteBlobContents(t testing.TB, blobDigest digest.Digest, data []byte) {
|
|
|
|
t.Helper()
|
2022-01-20 12:42:50 +00:00
|
|
|
err := os.WriteFile(r.getBlobFilename(blobDigest), data, os.FileMode(0o644))
|
2018-04-13 08:45:34 +00:00
|
|
|
assert.NilError(t, err, "unable to write malicious data blob")
|
2015-08-01 06:27:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 18:10:04 +00:00
|
|
|
// TempMoveBlobData moves the existing data file aside, so that we can replace it with a
|
|
|
|
// malicious blob of data for example.
|
2019-09-23 11:54:51 +00:00
|
|
|
func (r *V2) TempMoveBlobData(t testing.TB, blobDigest digest.Digest) (undo func()) {
|
2019-09-23 12:06:27 +00:00
|
|
|
t.Helper()
|
2021-08-24 10:10:50 +00:00
|
|
|
tempFile, err := os.CreateTemp("", "registry-temp-blob-")
|
2018-04-13 08:45:34 +00:00
|
|
|
assert.NilError(t, err, "unable to get temporary blob file")
|
2015-08-01 06:27:19 +00:00
|
|
|
tempFile.Close()
|
|
|
|
|
2016-12-30 18:10:04 +00:00
|
|
|
blobFilename := r.getBlobFilename(blobDigest)
|
2015-08-01 06:27:19 +00:00
|
|
|
|
|
|
|
// 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 {
|
2018-04-13 08:45:34 +00:00
|
|
|
// FIXME(vdemeester) use a defer/clean func
|
2015-08-01 06:27:19 +00:00
|
|
|
os.Remove(tempFile.Name())
|
2016-12-30 18:10:04 +00:00
|
|
|
t.Fatalf("unable to move data blob: %s", err)
|
2015-08-01 06:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
os.Rename(tempFile.Name(), blobFilename)
|
|
|
|
os.Remove(tempFile.Name())
|
|
|
|
}
|
|
|
|
}
|
2016-12-30 18:10:04 +00:00
|
|
|
|
|
|
|
// Username returns the configured user name of the server
|
|
|
|
func (r *V2) Username() string {
|
|
|
|
return r.username
|
|
|
|
}
|
|
|
|
|
|
|
|
// Password returns the configured password of the server
|
|
|
|
func (r *V2) Password() string {
|
|
|
|
return r.password
|
|
|
|
}
|
|
|
|
|
2017-03-15 17:25:36 +00:00
|
|
|
// Email returns the configured email of the server
|
|
|
|
func (r *V2) Email() string {
|
|
|
|
return r.email
|
|
|
|
}
|
|
|
|
|
2016-12-30 18:10:04 +00:00
|
|
|
// Path returns the path where the registry write data
|
|
|
|
func (r *V2) Path() string {
|
|
|
|
return filepath.Join(r.dir, "docker", "registry", "v2")
|
|
|
|
}
|