trust_server.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/cliconfig"
  13. "github.com/docker/docker/pkg/integration/checker"
  14. "github.com/docker/docker/pkg/tlsconfig"
  15. "github.com/go-check/check"
  16. )
  17. var notaryBinary = "notary"
  18. var notaryServerBinary = "notary-server"
  19. type keyPair struct {
  20. Public string
  21. Private string
  22. }
  23. type testNotary struct {
  24. cmd *exec.Cmd
  25. dir string
  26. keys []keyPair
  27. }
  28. const notaryHost = "localhost:4443"
  29. const notaryURL = "https://" + notaryHost
  30. func newTestNotary(c *check.C) (*testNotary, error) {
  31. // generate server config
  32. template := `{
  33. "server": {
  34. "http_addr": "%s",
  35. "tls_key_file": "%s",
  36. "tls_cert_file": "%s"
  37. },
  38. "trust_service": {
  39. "type": "local",
  40. "hostname": "",
  41. "port": "",
  42. "key_algorithm": "ed25519"
  43. },
  44. "logging": {
  45. "level": "debug"
  46. },
  47. "storage": {
  48. "backend": "memory"
  49. }
  50. }`
  51. tmp, err := ioutil.TempDir("", "notary-test-")
  52. if err != nil {
  53. return nil, err
  54. }
  55. confPath := filepath.Join(tmp, "config.json")
  56. config, err := os.Create(confPath)
  57. defer config.Close()
  58. if err != nil {
  59. return nil, err
  60. }
  61. workingDir, err := os.Getwd()
  62. if err != nil {
  63. return nil, err
  64. }
  65. if _, err := fmt.Fprintf(config, template, notaryHost, filepath.Join(workingDir, "fixtures/notary/localhost.key"), filepath.Join(workingDir, "fixtures/notary/localhost.cert")); err != nil {
  66. os.RemoveAll(tmp)
  67. return nil, err
  68. }
  69. // generate client config
  70. clientConfPath := filepath.Join(tmp, "client-config.json")
  71. clientConfig, err := os.Create(clientConfPath)
  72. defer clientConfig.Close()
  73. if err != nil {
  74. return nil, err
  75. }
  76. template = `{
  77. "trust_dir" : "%s",
  78. "remote_server": {
  79. "url": "%s",
  80. "skipTLSVerify": true
  81. }
  82. }`
  83. if _, err = fmt.Fprintf(clientConfig, template, filepath.Join(cliconfig.ConfigDir(), "trust"), notaryURL); err != nil {
  84. os.RemoveAll(tmp)
  85. return nil, err
  86. }
  87. // load key fixture filenames
  88. var keys []keyPair
  89. for i := 1; i < 5; i++ {
  90. keys = append(keys, keyPair{
  91. Public: filepath.Join(workingDir, fmt.Sprintf("fixtures/notary/delgkey%v.crt", i)),
  92. Private: filepath.Join(workingDir, fmt.Sprintf("fixtures/notary/delgkey%v.key", i)),
  93. })
  94. }
  95. // run notary-server
  96. cmd := exec.Command(notaryServerBinary, "-config", confPath)
  97. if err := cmd.Start(); err != nil {
  98. os.RemoveAll(tmp)
  99. if os.IsNotExist(err) {
  100. c.Skip(err.Error())
  101. }
  102. return nil, err
  103. }
  104. testNotary := &testNotary{
  105. cmd: cmd,
  106. dir: tmp,
  107. keys: keys,
  108. }
  109. // Wait for notary to be ready to serve requests.
  110. for i := 1; i <= 20; i++ {
  111. if err = testNotary.Ping(); err == nil {
  112. break
  113. }
  114. time.Sleep(10 * time.Millisecond * time.Duration(i*i))
  115. }
  116. if err != nil {
  117. c.Fatalf("Timeout waiting for test notary to become available: %s", err)
  118. }
  119. return testNotary, nil
  120. }
  121. func (t *testNotary) Ping() error {
  122. tlsConfig := tlsconfig.ClientDefault
  123. tlsConfig.InsecureSkipVerify = true
  124. client := http.Client{
  125. Transport: &http.Transport{
  126. Proxy: http.ProxyFromEnvironment,
  127. Dial: (&net.Dialer{
  128. Timeout: 30 * time.Second,
  129. KeepAlive: 30 * time.Second,
  130. }).Dial,
  131. TLSHandshakeTimeout: 10 * time.Second,
  132. TLSClientConfig: &tlsConfig,
  133. },
  134. }
  135. resp, err := client.Get(fmt.Sprintf("%s/v2/", notaryURL))
  136. if err != nil {
  137. return err
  138. }
  139. if resp.StatusCode != http.StatusOK {
  140. return fmt.Errorf("notary ping replied with an unexpected status code %d", resp.StatusCode)
  141. }
  142. return nil
  143. }
  144. func (t *testNotary) Close() {
  145. t.cmd.Process.Kill()
  146. os.RemoveAll(t.dir)
  147. }
  148. func (s *DockerTrustSuite) trustedCmd(cmd *exec.Cmd) {
  149. pwd := "12345678"
  150. trustCmdEnv(cmd, notaryURL, pwd, pwd)
  151. }
  152. func (s *DockerTrustSuite) trustedCmdWithServer(cmd *exec.Cmd, server string) {
  153. pwd := "12345678"
  154. trustCmdEnv(cmd, server, pwd, pwd)
  155. }
  156. func (s *DockerTrustSuite) trustedCmdWithPassphrases(cmd *exec.Cmd, rootPwd, repositoryPwd string) {
  157. trustCmdEnv(cmd, notaryURL, rootPwd, repositoryPwd)
  158. }
  159. func trustCmdEnv(cmd *exec.Cmd, server, rootPwd, repositoryPwd string) {
  160. env := []string{
  161. "DOCKER_CONTENT_TRUST=1",
  162. fmt.Sprintf("DOCKER_CONTENT_TRUST_SERVER=%s", server),
  163. fmt.Sprintf("DOCKER_CONTENT_TRUST_ROOT_PASSPHRASE=%s", rootPwd),
  164. fmt.Sprintf("DOCKER_CONTENT_TRUST_REPOSITORY_PASSPHRASE=%s", repositoryPwd),
  165. }
  166. cmd.Env = append(os.Environ(), env...)
  167. }
  168. func (s *DockerTrustSuite) setupTrustedImage(c *check.C, name string) string {
  169. repoName := fmt.Sprintf("%v/dockercli/%s:latest", privateRegistryURL, name)
  170. // tag the image and upload it to the private registry
  171. dockerCmd(c, "tag", "busybox", repoName)
  172. pushCmd := exec.Command(dockerBinary, "push", repoName)
  173. s.trustedCmd(pushCmd)
  174. out, _, err := runCommandWithOutput(pushCmd)
  175. if err != nil {
  176. c.Fatalf("Error running trusted push: %s\n%s", err, out)
  177. }
  178. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  179. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  180. }
  181. if out, status := dockerCmd(c, "rmi", repoName); status != 0 {
  182. c.Fatalf("Error removing image %q\n%s", repoName, out)
  183. }
  184. return repoName
  185. }
  186. func notaryClientEnv(cmd *exec.Cmd) {
  187. pwd := "12345678"
  188. env := []string{
  189. fmt.Sprintf("NOTARY_ROOT_PASSPHRASE=%s", pwd),
  190. fmt.Sprintf("NOTARY_TARGETS_PASSPHRASE=%s", pwd),
  191. fmt.Sprintf("NOTARY_SNAPSHOT_PASSPHRASE=%s", pwd),
  192. fmt.Sprintf("NOTARY_DELEGATION_PASSPHRASE=%s", pwd),
  193. }
  194. cmd.Env = append(os.Environ(), env...)
  195. }
  196. func (s *DockerTrustSuite) notaryInitRepo(c *check.C, repoName string) {
  197. initCmd := exec.Command(notaryBinary, "-c", filepath.Join(s.not.dir, "client-config.json"), "init", repoName)
  198. notaryClientEnv(initCmd)
  199. out, _, err := runCommandWithOutput(initCmd)
  200. if err != nil {
  201. c.Fatalf("Error initializing notary repository: %s\n", out)
  202. }
  203. }
  204. func (s *DockerTrustSuite) notaryCreateDelegation(c *check.C, repoName, role string, pubKey string, paths ...string) {
  205. pathsArg := "--all-paths"
  206. if len(paths) > 0 {
  207. pathsArg = "--paths=" + strings.Join(paths, ",")
  208. }
  209. delgCmd := exec.Command(notaryBinary, "-c", filepath.Join(s.not.dir, "client-config.json"),
  210. "delegation", "add", repoName, role, pubKey, pathsArg)
  211. notaryClientEnv(delgCmd)
  212. out, _, err := runCommandWithOutput(delgCmd)
  213. if err != nil {
  214. c.Fatalf("Error adding %s role to notary repository: %s\n", role, out)
  215. }
  216. }
  217. func (s *DockerTrustSuite) notaryPublish(c *check.C, repoName string) {
  218. pubCmd := exec.Command(notaryBinary, "-c", filepath.Join(s.not.dir, "client-config.json"), "publish", repoName)
  219. notaryClientEnv(pubCmd)
  220. out, _, err := runCommandWithOutput(pubCmd)
  221. if err != nil {
  222. c.Fatalf("Error publishing notary repository: %s\n", out)
  223. }
  224. }
  225. func (s *DockerTrustSuite) notaryImportKey(c *check.C, repoName, role string, privKey string) {
  226. impCmd := exec.Command(notaryBinary, "-c", filepath.Join(s.not.dir, "client-config.json"), "key",
  227. "import", privKey, "-g", repoName, "-r", role)
  228. notaryClientEnv(impCmd)
  229. out, _, err := runCommandWithOutput(impCmd)
  230. if err != nil {
  231. c.Fatalf("Error importing key to notary repository: %s\n", out)
  232. }
  233. }
  234. func (s *DockerTrustSuite) notaryListTargetsInRole(c *check.C, repoName, role string) map[string]string {
  235. listCmd := exec.Command(notaryBinary, "-c", filepath.Join(s.not.dir, "client-config.json"), "list",
  236. repoName, "-r", role)
  237. notaryClientEnv(listCmd)
  238. out, _, err := runCommandWithOutput(listCmd)
  239. if err != nil {
  240. c.Fatalf("Error listing targets in notary repository: %s\n", out)
  241. }
  242. // should look something like:
  243. // NAME DIGEST SIZE (BYTES) ROLE
  244. // ------------------------------------------------------------------------------------------------------
  245. // latest 24a36bbc059b1345b7e8be0df20f1b23caa3602e85d42fff7ecd9d0bd255de56 1377 targets
  246. targets := make(map[string]string)
  247. // no target
  248. lines := strings.Split(strings.TrimSpace(out), "\n")
  249. if len(lines) == 1 && strings.Contains(out, "No targets present in this repository.") {
  250. return targets
  251. }
  252. // otherwise, there is at least one target
  253. c.Assert(len(lines), checker.GreaterOrEqualThan, 3)
  254. for _, line := range lines[2:] {
  255. tokens := strings.Fields(line)
  256. c.Assert(tokens, checker.HasLen, 4)
  257. targets[tokens[0]] = tokens[3]
  258. }
  259. return targets
  260. }
  261. func (s *DockerTrustSuite) assertTargetInRoles(c *check.C, repoName, target string, roles ...string) {
  262. // check all the roles
  263. for _, role := range roles {
  264. targets := s.notaryListTargetsInRole(c, repoName, role)
  265. roleName, ok := targets[target]
  266. c.Assert(ok, checker.True)
  267. c.Assert(roleName, checker.Equals, role)
  268. }
  269. }
  270. func (s *DockerTrustSuite) assertTargetNotInRoles(c *check.C, repoName, target string, roles ...string) {
  271. targets := s.notaryListTargetsInRole(c, repoName, "targets")
  272. roleName, ok := targets[target]
  273. if ok {
  274. for _, role := range roles {
  275. c.Assert(roleName, checker.Not(checker.Equals), role)
  276. }
  277. }
  278. }