registry.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "github.com/docker/distribution/digest"
  10. "github.com/go-check/check"
  11. )
  12. const (
  13. v2binary = "registry-v2"
  14. v2binarySchema1 = "registry-v2-schema1"
  15. )
  16. type testRegistryV2 struct {
  17. cmd *exec.Cmd
  18. dir string
  19. username string
  20. password string
  21. email string
  22. }
  23. func newTestRegistryV2(c *check.C, schema1, auth bool) (*testRegistryV2, error) {
  24. tmp, err := ioutil.TempDir("", "registry-test-")
  25. if err != nil {
  26. return nil, err
  27. }
  28. template := `version: 0.1
  29. loglevel: debug
  30. storage:
  31. filesystem:
  32. rootdirectory: %s
  33. http:
  34. addr: %s
  35. %s`
  36. var (
  37. htpasswd string
  38. username string
  39. password string
  40. email string
  41. )
  42. if auth {
  43. htpasswdPath := filepath.Join(tmp, "htpasswd")
  44. // generated with: htpasswd -Bbn testuser testpassword
  45. userpasswd := "testuser:$2y$05$sBsSqk0OpSD1uTZkHXc4FeJ0Z70wLQdAX/82UiHuQOKbNbBrzs63m"
  46. username = "testuser"
  47. password = "testpassword"
  48. email = "test@test.org"
  49. if err := ioutil.WriteFile(htpasswdPath, []byte(userpasswd), os.FileMode(0644)); err != nil {
  50. return nil, err
  51. }
  52. htpasswd = fmt.Sprintf(`auth:
  53. htpasswd:
  54. realm: basic-realm
  55. path: %s
  56. `, htpasswdPath)
  57. }
  58. confPath := filepath.Join(tmp, "config.yaml")
  59. config, err := os.Create(confPath)
  60. if err != nil {
  61. return nil, err
  62. }
  63. if _, err := fmt.Fprintf(config, template, tmp, privateRegistryURL, htpasswd); err != nil {
  64. os.RemoveAll(tmp)
  65. return nil, err
  66. }
  67. binary := v2binary
  68. if schema1 {
  69. binary = v2binarySchema1
  70. }
  71. cmd := exec.Command(binary, confPath)
  72. if err := cmd.Start(); err != nil {
  73. os.RemoveAll(tmp)
  74. if os.IsNotExist(err) {
  75. c.Skip(err.Error())
  76. }
  77. return nil, err
  78. }
  79. return &testRegistryV2{
  80. cmd: cmd,
  81. dir: tmp,
  82. username: username,
  83. password: password,
  84. email: email,
  85. }, nil
  86. }
  87. func (t *testRegistryV2) Ping() error {
  88. // We always ping through HTTP for our test registry.
  89. resp, err := http.Get(fmt.Sprintf("http://%s/v2/", privateRegistryURL))
  90. if err != nil {
  91. return err
  92. }
  93. resp.Body.Close()
  94. fail := resp.StatusCode != http.StatusOK
  95. if t.username != "" {
  96. // unauthorized is a _good_ status when pinging v2/ and it needs auth
  97. fail = fail && resp.StatusCode != http.StatusUnauthorized
  98. }
  99. if fail {
  100. return fmt.Errorf("registry ping replied with an unexpected status code %d", resp.StatusCode)
  101. }
  102. return nil
  103. }
  104. func (t *testRegistryV2) Close() {
  105. t.cmd.Process.Kill()
  106. os.RemoveAll(t.dir)
  107. }
  108. func (t *testRegistryV2) getBlobFilename(blobDigest digest.Digest) string {
  109. // Split the digest into it's algorithm and hex components.
  110. dgstAlg, dgstHex := blobDigest.Algorithm(), blobDigest.Hex()
  111. // The path to the target blob data looks something like:
  112. // baseDir + "docker/registry/v2/blobs/sha256/a3/a3ed...46d4/data"
  113. return fmt.Sprintf("%s/docker/registry/v2/blobs/%s/%s/%s/data", t.dir, dgstAlg, dgstHex[:2], dgstHex)
  114. }
  115. func (t *testRegistryV2) readBlobContents(c *check.C, blobDigest digest.Digest) []byte {
  116. // Load the target manifest blob.
  117. manifestBlob, err := ioutil.ReadFile(t.getBlobFilename(blobDigest))
  118. if err != nil {
  119. c.Fatalf("unable to read blob: %s", err)
  120. }
  121. return manifestBlob
  122. }
  123. func (t *testRegistryV2) writeBlobContents(c *check.C, blobDigest digest.Digest, data []byte) {
  124. if err := ioutil.WriteFile(t.getBlobFilename(blobDigest), data, os.FileMode(0644)); err != nil {
  125. c.Fatalf("unable to write malicious data blob: %s", err)
  126. }
  127. }
  128. func (t *testRegistryV2) tempMoveBlobData(c *check.C, blobDigest digest.Digest) (undo func()) {
  129. tempFile, err := ioutil.TempFile("", "registry-temp-blob-")
  130. if err != nil {
  131. c.Fatalf("unable to get temporary blob file: %s", err)
  132. }
  133. tempFile.Close()
  134. blobFilename := t.getBlobFilename(blobDigest)
  135. // Move the existing data file aside, so that we can replace it with a
  136. // another blob of data.
  137. if err := os.Rename(blobFilename, tempFile.Name()); err != nil {
  138. os.Remove(tempFile.Name())
  139. c.Fatalf("unable to move data blob: %s", err)
  140. }
  141. return func() {
  142. os.Rename(tempFile.Name(), blobFilename)
  143. os.Remove(tempFile.Name())
  144. }
  145. }