trust_server.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net"
  6. "net/http"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. "github.com/docker/docker/pkg/tlsconfig"
  13. "github.com/go-check/check"
  14. )
  15. var notaryBinary = "notary-server"
  16. type testNotary struct {
  17. cmd *exec.Cmd
  18. dir string
  19. }
  20. const notaryHost = "localhost:4443"
  21. const notaryURL = "https://" + notaryHost
  22. func newTestNotary(c *check.C) (*testNotary, error) {
  23. template := `{
  24. "server": {
  25. "addr": "%s",
  26. "tls_key_file": "fixtures/notary/localhost.key",
  27. "tls_cert_file": "fixtures/notary/localhost.cert"
  28. },
  29. "trust_service": {
  30. "type": "local",
  31. "hostname": "",
  32. "port": "",
  33. "key_algorithm": "ed25519"
  34. },
  35. "logging": {
  36. "level": 5
  37. }
  38. }`
  39. tmp, err := ioutil.TempDir("", "notary-test-")
  40. if err != nil {
  41. return nil, err
  42. }
  43. confPath := filepath.Join(tmp, "config.json")
  44. config, err := os.Create(confPath)
  45. if err != nil {
  46. return nil, err
  47. }
  48. if _, err := fmt.Fprintf(config, template, notaryHost); err != nil {
  49. os.RemoveAll(tmp)
  50. return nil, err
  51. }
  52. cmd := exec.Command(notaryBinary, "-config", confPath)
  53. if err := cmd.Start(); err != nil {
  54. os.RemoveAll(tmp)
  55. if os.IsNotExist(err) {
  56. c.Skip(err.Error())
  57. }
  58. return nil, err
  59. }
  60. testNotary := &testNotary{
  61. cmd: cmd,
  62. dir: tmp,
  63. }
  64. // Wait for notary to be ready to serve requests.
  65. for i := 1; i <= 5; i++ {
  66. if err = testNotary.Ping(); err == nil {
  67. break
  68. }
  69. time.Sleep(10 * time.Millisecond * time.Duration(i*i))
  70. }
  71. if err != nil {
  72. c.Fatalf("Timeout waiting for test notary to become available: %s", err)
  73. }
  74. return testNotary, nil
  75. }
  76. func (t *testNotary) Ping() error {
  77. tlsConfig := tlsconfig.ClientDefault
  78. tlsConfig.InsecureSkipVerify = true
  79. client := http.Client{
  80. Transport: &http.Transport{
  81. Proxy: http.ProxyFromEnvironment,
  82. Dial: (&net.Dialer{
  83. Timeout: 30 * time.Second,
  84. KeepAlive: 30 * time.Second,
  85. }).Dial,
  86. TLSHandshakeTimeout: 10 * time.Second,
  87. TLSClientConfig: &tlsConfig,
  88. },
  89. }
  90. resp, err := client.Get(fmt.Sprintf("%s/v2/", notaryURL))
  91. if err != nil {
  92. return err
  93. }
  94. if resp.StatusCode != 200 {
  95. return fmt.Errorf("notary ping replied with an unexpected status code %d", resp.StatusCode)
  96. }
  97. return nil
  98. }
  99. func (t *testNotary) Close() {
  100. t.cmd.Process.Kill()
  101. os.RemoveAll(t.dir)
  102. }
  103. func (s *DockerTrustSuite) trustedCmd(cmd *exec.Cmd) {
  104. pwd := "12345678"
  105. trustCmdEnv(cmd, notaryURL, pwd, pwd)
  106. }
  107. func (s *DockerTrustSuite) trustedCmdWithServer(cmd *exec.Cmd, server string) {
  108. pwd := "12345678"
  109. trustCmdEnv(cmd, server, pwd, pwd)
  110. }
  111. func (s *DockerTrustSuite) trustedCmdWithPassphrases(cmd *exec.Cmd, rootPwd, repositoryPwd string) {
  112. trustCmdEnv(cmd, notaryURL, rootPwd, repositoryPwd)
  113. }
  114. func (s *DockerTrustSuite) trustedCmdWithDeprecatedEnvPassphrases(cmd *exec.Cmd, offlinePwd, taggingPwd string) {
  115. trustCmdDeprecatedEnv(cmd, notaryURL, offlinePwd, taggingPwd)
  116. }
  117. func trustCmdEnv(cmd *exec.Cmd, server, rootPwd, repositoryPwd string) {
  118. env := []string{
  119. "DOCKER_CONTENT_TRUST=1",
  120. fmt.Sprintf("DOCKER_CONTENT_TRUST_SERVER=%s", server),
  121. fmt.Sprintf("DOCKER_CONTENT_TRUST_ROOT_PASSPHRASE=%s", rootPwd),
  122. fmt.Sprintf("DOCKER_CONTENT_TRUST_REPOSITORY_PASSPHRASE=%s", repositoryPwd),
  123. }
  124. cmd.Env = append(os.Environ(), env...)
  125. }
  126. // Helper method to test the old env variables OFFLINE and TAGGING that will
  127. // be deprecated by 1.10
  128. func trustCmdDeprecatedEnv(cmd *exec.Cmd, server, offlinePwd, taggingPwd string) {
  129. env := []string{
  130. "DOCKER_CONTENT_TRUST=1",
  131. fmt.Sprintf("DOCKER_CONTENT_TRUST_SERVER=%s", server),
  132. fmt.Sprintf("DOCKER_CONTENT_TRUST_OFFLINE_PASSPHRASE=%s", offlinePwd),
  133. fmt.Sprintf("DOCKER_CONTENT_TRUST_TAGGING_PASSPHRASE=%s", taggingPwd),
  134. }
  135. cmd.Env = append(os.Environ(), env...)
  136. }
  137. func (s *DockerTrustSuite) setupTrustedImage(c *check.C, name string) string {
  138. repoName := fmt.Sprintf("%v/dockercli/%s:latest", privateRegistryURL, name)
  139. // tag the image and upload it to the private registry
  140. dockerCmd(c, "tag", "busybox", repoName)
  141. pushCmd := exec.Command(dockerBinary, "push", repoName)
  142. s.trustedCmd(pushCmd)
  143. out, _, err := runCommandWithOutput(pushCmd)
  144. if err != nil {
  145. c.Fatalf("Error running trusted push: %s\n%s", err, out)
  146. }
  147. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  148. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  149. }
  150. if out, status := dockerCmd(c, "rmi", repoName); status != 0 {
  151. c.Fatalf("Error removing image %q\n%s", repoName, out)
  152. }
  153. return repoName
  154. }