integration-cli/docker_cli_pull_trusted_test.go
f324f485
 package main
 
 import (
 	"fmt"
 	"io/ioutil"
 
33968e6c
 	"github.com/docker/docker/integration-cli/checker"
b0ba39d4
 	"github.com/docker/docker/integration-cli/cli"
50c4475d
 	"github.com/docker/docker/integration-cli/cli/build"
f324f485
 	"github.com/go-check/check"
92427b3a
 	"github.com/gotestyourself/gotestyourself/icmd"
f324f485
 )
 
 func (s *DockerTrustSuite) TestTrustedPull(c *check.C) {
 	repoName := s.setupTrustedImage(c, "trusted-pull")
 
 	// Try pull
b0ba39d4
 	cli.Docker(cli.Args("pull", repoName), trustedCmd).Assert(c, SuccessTagging)
f324f485
 
b0ba39d4
 	cli.DockerCmd(c, "rmi", repoName)
f324f485
 	// Try untrusted pull to ensure we pushed the tag to the registry
b0ba39d4
 	cli.Docker(cli.Args("pull", "--disable-content-trust=true", repoName), trustedCmd).Assert(c, SuccessDownloaded)
f324f485
 }
 
 func (s *DockerTrustSuite) TestTrustedIsolatedPull(c *check.C) {
8781367c
 	repoName := s.setupTrustedImage(c, "trusted-isolated-pull")
f324f485
 
 	// Try pull (run from isolated directory without trust information)
b0ba39d4
 	cli.Docker(cli.Args("--config", "/tmp/docker-isolated", "pull", repoName), trustedCmd).Assert(c, SuccessTagging)
f324f485
 
b0ba39d4
 	cli.DockerCmd(c, "rmi", repoName)
f324f485
 }
 
 func (s *DockerTrustSuite) TestUntrustedPull(c *check.C) {
0617521b
 	repoName := fmt.Sprintf("%v/dockercliuntrusted/pulltest:latest", privateRegistryURL)
f324f485
 	// tag the image and upload it to the private registry
b0ba39d4
 	cli.DockerCmd(c, "tag", "busybox", repoName)
 	cli.DockerCmd(c, "push", repoName)
 	cli.DockerCmd(c, "rmi", repoName)
f324f485
 
 	// Try trusted pull on untrusted tag
b0ba39d4
 	cli.Docker(cli.Args("pull", repoName), trustedCmd).Assert(c, icmd.Expected{
303b1d20
 		ExitCode: 1,
 		Err:      "Error: remote trust data does not exist",
 	})
f324f485
 }
 
 func (s *DockerTrustSuite) TestTrustedPullFromBadTrustServer(c *check.C) {
 	repoName := fmt.Sprintf("%v/dockerclievilpull/trusted:latest", privateRegistryURL)
 	evilLocalConfigDir, err := ioutil.TempDir("", "evil-local-config-dir")
 	if err != nil {
 		c.Fatalf("Failed to create local temp dir")
 	}
 
 	// tag the image and upload it to the private registry
b0ba39d4
 	cli.DockerCmd(c, "tag", "busybox", repoName)
f324f485
 
b0ba39d4
 	cli.Docker(cli.Args("push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
 	cli.DockerCmd(c, "rmi", repoName)
f324f485
 
 	// Try pull
b0ba39d4
 	cli.Docker(cli.Args("pull", repoName), trustedCmd).Assert(c, SuccessTagging)
 	cli.DockerCmd(c, "rmi", repoName)
f324f485
 
 	// Kill the notary server, start a new "evil" one.
 	s.not.Close()
 	s.not, err = newTestNotary(c)
c16dd88c
 
 	c.Assert(err, check.IsNil, check.Commentf("Restarting notary server failed."))
f324f485
 
 	// In order to make an evil server, lets re-init a client (with a different trust dir) and push new data.
 	// tag an image and upload it to the private registry
b0ba39d4
 	cli.DockerCmd(c, "--config", evilLocalConfigDir, "tag", "busybox", repoName)
f324f485
 
 	// Push up to the new server
b0ba39d4
 	cli.Docker(cli.Args("--config", evilLocalConfigDir, "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
f324f485
 
0797af39
 	// Now, try pulling with the original client from this new trust server. This should fail because the new root is invalid.
b0ba39d4
 	cli.Docker(cli.Args("pull", repoName), trustedCmd).Assert(c, icmd.Expected{
303b1d20
 		ExitCode: 1,
 		Err:      "could not rotate trust to a new trusted root",
 	})
f324f485
 }
 
5e11cd43
 func (s *DockerTrustSuite) TestTrustedOfflinePull(c *check.C) {
 	repoName := s.setupTrustedImage(c, "trusted-offline-pull")
 
b0ba39d4
 	cli.Docker(cli.Args("pull", repoName), trustedCmdWithServer("https://invalidnotaryserver")).Assert(c, icmd.Expected{
303b1d20
 		ExitCode: 1,
 		Err:      "error contacting notary server",
 	})
5e11cd43
 	// Do valid trusted pull to warm cache
b0ba39d4
 	cli.Docker(cli.Args("pull", repoName), trustedCmd).Assert(c, SuccessTagging)
 	cli.DockerCmd(c, "rmi", repoName)
5e11cd43
 
 	// Try pull again with invalid notary server, should use cache
b0ba39d4
 	cli.Docker(cli.Args("pull", repoName), trustedCmdWithServer("https://invalidnotaryserver")).Assert(c, SuccessTagging)
5e11cd43
 }
2f048f73
 
 func (s *DockerTrustSuite) TestTrustedPullDelete(c *check.C) {
 	repoName := fmt.Sprintf("%v/dockercli/%s:latest", privateRegistryURL, "trusted-pull-delete")
 	// tag the image and upload it to the private registry
b0ba39d4
 	cli.BuildCmd(c, repoName, build.WithDockerfile(`
2f048f73
                     FROM busybox
                     CMD echo trustedpulldelete
c10f6ef4
                 `))
b0ba39d4
 	cli.Docker(cli.Args("push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
2f048f73
 
b0ba39d4
 	cli.DockerCmd(c, "rmi", repoName)
2f048f73
 
 	// Try pull
b0ba39d4
 	result := cli.Docker(cli.Args("pull", repoName), trustedCmd).Assert(c, icmd.Success)
2f048f73
 
303b1d20
 	matches := digestRegex.FindStringSubmatch(result.Combined())
 	c.Assert(matches, checker.HasLen, 2, check.Commentf("unable to parse digest from pull output: %s", result.Combined()))
2f048f73
 	pullDigest := matches[1]
 
62a856e9
 	imageID := inspectField(c, repoName, "Id")
2f048f73
 
 	imageByDigest := repoName + "@" + pullDigest
62a856e9
 	byDigestID := inspectField(c, imageByDigest, "Id")
2f048f73
 
 	c.Assert(byDigestID, checker.Equals, imageID)
 
 	// rmi of tag should also remove the digest reference
b0ba39d4
 	cli.DockerCmd(c, "rmi", repoName)
2f048f73
 
c10f6ef4
 	_, err := inspectFieldWithError(imageByDigest, "Id")
2f048f73
 	c.Assert(err, checker.NotNil, check.Commentf("digest reference should have been removed"))
 
62a856e9
 	_, err = inspectFieldWithError(imageID, "Id")
2f048f73
 	c.Assert(err, checker.NotNil, check.Commentf("image should have been deleted"))
 }
ca57f4e6
 
 func (s *DockerTrustSuite) TestTrustedPullReadsFromReleasesRole(c *check.C) {
623ccc2f
 	testRequires(c, NotaryHosting)
ca57f4e6
 	repoName := fmt.Sprintf("%v/dockerclireleasesdelegationpulling/trusted", privateRegistryURL)
 	targetName := fmt.Sprintf("%s:latest", repoName)
 
 	// Push with targets first, initializing the repo
b0ba39d4
 	cli.DockerCmd(c, "tag", "busybox", targetName)
 	cli.Docker(cli.Args("push", targetName), trustedCmd).Assert(c, icmd.Success)
623ccc2f
 	s.assertTargetInRoles(c, repoName, "latest", "targets")
ca57f4e6
 
 	// Try pull, check we retrieve from targets role
b0ba39d4
 	cli.Docker(cli.Args("-D", "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
303b1d20
 		Err: "retrieving target for targets role",
 	})
ca57f4e6
 
 	// Now we'll create the releases role, and try pushing and pulling
623ccc2f
 	s.notaryCreateDelegation(c, repoName, "targets/releases", s.not.keys[0].Public)
ca57f4e6
 	s.notaryImportKey(c, repoName, "targets/releases", s.not.keys[0].Private)
623ccc2f
 	s.notaryPublish(c, repoName)
 
 	// try a pull, check that we can still pull because we can still read the
 	// old tag in the targets role
b0ba39d4
 	cli.Docker(cli.Args("-D", "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
303b1d20
 		Err: "retrieving target for targets role",
 	})
623ccc2f
 
 	// try a pull -a, check that it succeeds because we can still pull from the
 	// targets role
b0ba39d4
 	cli.Docker(cli.Args("-D", "pull", "-a", repoName), trustedCmd).Assert(c, icmd.Success)
ca57f4e6
 
 	// Push, should sign with targets/releases
b0ba39d4
 	cli.DockerCmd(c, "tag", "busybox", targetName)
 	cli.Docker(cli.Args("push", targetName), trustedCmd).Assert(c, icmd.Success)
623ccc2f
 	s.assertTargetInRoles(c, repoName, "latest", "targets", "targets/releases")
ca57f4e6
 
 	// Try pull, check we retrieve from targets/releases role
b0ba39d4
 	cli.Docker(cli.Args("-D", "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
303b1d20
 		Err: "retrieving target for targets/releases role",
 	})
ca57f4e6
 
 	// Create another delegation that we'll sign with
623ccc2f
 	s.notaryCreateDelegation(c, repoName, "targets/other", s.not.keys[1].Public)
ca57f4e6
 	s.notaryImportKey(c, repoName, "targets/other", s.not.keys[1].Private)
623ccc2f
 	s.notaryPublish(c, repoName)
ca57f4e6
 
b0ba39d4
 	cli.DockerCmd(c, "tag", "busybox", targetName)
 	cli.Docker(cli.Args("push", targetName), trustedCmd).Assert(c, icmd.Success)
623ccc2f
 	s.assertTargetInRoles(c, repoName, "latest", "targets", "targets/releases", "targets/other")
ca57f4e6
 
 	// Try pull, check we retrieve from targets/releases role
b0ba39d4
 	cli.Docker(cli.Args("-D", "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
303b1d20
 		Err: "retrieving target for targets/releases role",
 	})
ca57f4e6
 }
623ccc2f
 
 func (s *DockerTrustSuite) TestTrustedPullIgnoresOtherDelegationRoles(c *check.C) {
 	testRequires(c, NotaryHosting)
 	repoName := fmt.Sprintf("%v/dockerclipullotherdelegation/trusted", privateRegistryURL)
 	targetName := fmt.Sprintf("%s:latest", repoName)
 
 	// We'll create a repo first with a non-release delegation role, so that when we
 	// push we'll sign it into the delegation role
 	s.notaryInitRepo(c, repoName)
 	s.notaryCreateDelegation(c, repoName, "targets/other", s.not.keys[0].Public)
 	s.notaryImportKey(c, repoName, "targets/other", s.not.keys[0].Private)
 	s.notaryPublish(c, repoName)
 
 	// Push should write to the delegation role, not targets
b0ba39d4
 	cli.DockerCmd(c, "tag", "busybox", targetName)
 	cli.Docker(cli.Args("push", targetName), trustedCmd).Assert(c, icmd.Success)
623ccc2f
 	s.assertTargetInRoles(c, repoName, "latest", "targets/other")
 	s.assertTargetNotInRoles(c, repoName, "latest", "targets")
 
 	// Try pull - we should fail, since pull will only pull from the targets/releases
 	// role or the targets role
b0ba39d4
 	cli.DockerCmd(c, "tag", "busybox", targetName)
 	cli.Docker(cli.Args("-D", "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
303b1d20
 		ExitCode: 1,
 		Err:      "No trust data for",
 	})
623ccc2f
 
 	// try a pull -a: we should fail since pull will only pull from the targets/releases
 	// role or the targets role
b0ba39d4
 	cli.Docker(cli.Args("-D", "pull", "-a", repoName), trustedCmd).Assert(c, icmd.Expected{
303b1d20
 		ExitCode: 1,
 		Err:      "No trusted tags for",
 	})
623ccc2f
 }