trust_server_test.go 8.8 KB

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