trust_server_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. cliconfig "github.com/docker/docker/cli/config"
  13. "github.com/docker/docker/pkg/integration/checker"
  14. "github.com/docker/go-connections/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. if err != nil {
  58. return nil, err
  59. }
  60. defer config.Close()
  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. if err != nil {
  73. return nil, err
  74. }
  75. defer clientConfig.Close()
  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.Dir(), "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 (s *DockerTrustSuite) setupTrustedplugin(c *check.C, source, name string) string {
  187. repoName := fmt.Sprintf("%v/dockercli/%s:latest", privateRegistryURL, name)
  188. // tag the image and upload it to the private registry
  189. dockerCmd(c, "plugin", "install", "--grant-all-permissions", "--alias", repoName, source)
  190. pushCmd := exec.Command(dockerBinary, "plugin", "push", repoName)
  191. s.trustedCmd(pushCmd)
  192. out, _, err := runCommandWithOutput(pushCmd)
  193. if err != nil {
  194. c.Fatalf("Error running trusted plugin push: %s\n%s", err, out)
  195. }
  196. if !strings.Contains(string(out), "Signing and pushing trust metadata") {
  197. c.Fatalf("Missing expected output on trusted push:\n%s", out)
  198. }
  199. if out, status := dockerCmd(c, "plugin", "rm", "-f", repoName); status != 0 {
  200. c.Fatalf("Error removing plugin %q\n%s", repoName, out)
  201. }
  202. return repoName
  203. }
  204. func notaryClientEnv(cmd *exec.Cmd) {
  205. pwd := "12345678"
  206. env := []string{
  207. fmt.Sprintf("NOTARY_ROOT_PASSPHRASE=%s", pwd),
  208. fmt.Sprintf("NOTARY_TARGETS_PASSPHRASE=%s", pwd),
  209. fmt.Sprintf("NOTARY_SNAPSHOT_PASSPHRASE=%s", pwd),
  210. fmt.Sprintf("NOTARY_DELEGATION_PASSPHRASE=%s", pwd),
  211. }
  212. cmd.Env = append(os.Environ(), env...)
  213. }
  214. func (s *DockerTrustSuite) notaryInitRepo(c *check.C, repoName string) {
  215. initCmd := exec.Command(notaryBinary, "-c", filepath.Join(s.not.dir, "client-config.json"), "init", repoName)
  216. notaryClientEnv(initCmd)
  217. out, _, err := runCommandWithOutput(initCmd)
  218. if err != nil {
  219. c.Fatalf("Error initializing notary repository: %s\n", out)
  220. }
  221. }
  222. func (s *DockerTrustSuite) notaryCreateDelegation(c *check.C, repoName, role string, pubKey string, paths ...string) {
  223. pathsArg := "--all-paths"
  224. if len(paths) > 0 {
  225. pathsArg = "--paths=" + strings.Join(paths, ",")
  226. }
  227. delgCmd := exec.Command(notaryBinary, "-c", filepath.Join(s.not.dir, "client-config.json"),
  228. "delegation", "add", repoName, role, pubKey, pathsArg)
  229. notaryClientEnv(delgCmd)
  230. out, _, err := runCommandWithOutput(delgCmd)
  231. if err != nil {
  232. c.Fatalf("Error adding %s role to notary repository: %s\n", role, out)
  233. }
  234. }
  235. func (s *DockerTrustSuite) notaryPublish(c *check.C, repoName string) {
  236. pubCmd := exec.Command(notaryBinary, "-c", filepath.Join(s.not.dir, "client-config.json"), "publish", repoName)
  237. notaryClientEnv(pubCmd)
  238. out, _, err := runCommandWithOutput(pubCmd)
  239. if err != nil {
  240. c.Fatalf("Error publishing notary repository: %s\n", out)
  241. }
  242. }
  243. func (s *DockerTrustSuite) notaryImportKey(c *check.C, repoName, role string, privKey string) {
  244. impCmd := exec.Command(notaryBinary, "-c", filepath.Join(s.not.dir, "client-config.json"), "key",
  245. "import", privKey, "-g", repoName, "-r", role)
  246. notaryClientEnv(impCmd)
  247. out, _, err := runCommandWithOutput(impCmd)
  248. if err != nil {
  249. c.Fatalf("Error importing key to notary repository: %s\n", out)
  250. }
  251. }
  252. func (s *DockerTrustSuite) notaryListTargetsInRole(c *check.C, repoName, role string) map[string]string {
  253. listCmd := exec.Command(notaryBinary, "-c", filepath.Join(s.not.dir, "client-config.json"), "list",
  254. repoName, "-r", role)
  255. notaryClientEnv(listCmd)
  256. out, _, err := runCommandWithOutput(listCmd)
  257. if err != nil {
  258. c.Fatalf("Error listing targets in notary repository: %s\n", out)
  259. }
  260. // should look something like:
  261. // NAME DIGEST SIZE (BYTES) ROLE
  262. // ------------------------------------------------------------------------------------------------------
  263. // latest 24a36bbc059b1345b7e8be0df20f1b23caa3602e85d42fff7ecd9d0bd255de56 1377 targets
  264. targets := make(map[string]string)
  265. // no target
  266. lines := strings.Split(strings.TrimSpace(out), "\n")
  267. if len(lines) == 1 && strings.Contains(out, "No targets present in this repository.") {
  268. return targets
  269. }
  270. // otherwise, there is at least one target
  271. c.Assert(len(lines), checker.GreaterOrEqualThan, 3)
  272. for _, line := range lines[2:] {
  273. tokens := strings.Fields(line)
  274. c.Assert(tokens, checker.HasLen, 4)
  275. targets[tokens[0]] = tokens[3]
  276. }
  277. return targets
  278. }
  279. func (s *DockerTrustSuite) assertTargetInRoles(c *check.C, repoName, target string, roles ...string) {
  280. // check all the roles
  281. for _, role := range roles {
  282. targets := s.notaryListTargetsInRole(c, repoName, role)
  283. roleName, ok := targets[target]
  284. c.Assert(ok, checker.True)
  285. c.Assert(roleName, checker.Equals, role)
  286. }
  287. }
  288. func (s *DockerTrustSuite) assertTargetNotInRoles(c *check.C, repoName, target string, roles ...string) {
  289. targets := s.notaryListTargetsInRole(c, repoName, "targets")
  290. roleName, ok := targets[target]
  291. if ok {
  292. for _, role := range roles {
  293. c.Assert(roleName, checker.Not(checker.Equals), role)
  294. }
  295. }
  296. }