trust_server.go 3.7 KB

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