2015-09-02 20:56:01 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"time"
|
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
|
|
|
"github.com/docker/docker/pkg/testutil"
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd "github.com/docker/docker/pkg/testutil/cmd"
|
2015-09-02 20:56:01 -04:00
|
|
|
"github.com/go-check/check"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *DockerTrustSuite) TestTrustedPull(c *check.C) {
|
|
|
|
repoName := s.setupTrustedImage(c, "trusted-pull")
|
|
|
|
|
|
|
|
// Try pull
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "pull", repoName), trustedCmd).Assert(c, SuccessTagging)
|
2015-09-02 20:56:01 -04:00
|
|
|
|
|
|
|
dockerCmd(c, "rmi", repoName)
|
|
|
|
// Try untrusted pull to ensure we pushed the tag to the registry
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "pull", "--disable-content-trust=true", repoName), trustedCmd).Assert(c, SuccessDownloaded)
|
2015-09-02 20:56:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerTrustSuite) TestTrustedIsolatedPull(c *check.C) {
|
2015-09-29 09:26:04 -04:00
|
|
|
repoName := s.setupTrustedImage(c, "trusted-isolated-pull")
|
2015-09-02 20:56:01 -04:00
|
|
|
|
|
|
|
// Try pull (run from isolated directory without trust information)
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "--config", "/tmp/docker-isolated", "pull", repoName), trustedCmd).Assert(c, SuccessTagging)
|
2015-09-02 20:56:01 -04:00
|
|
|
|
|
|
|
dockerCmd(c, "rmi", repoName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerTrustSuite) TestUntrustedPull(c *check.C) {
|
2016-01-26 20:52:36 -05:00
|
|
|
repoName := fmt.Sprintf("%v/dockercliuntrusted/pulltest:latest", privateRegistryURL)
|
2015-09-02 20:56:01 -04:00
|
|
|
// tag the image and upload it to the private registry
|
|
|
|
dockerCmd(c, "tag", "busybox", repoName)
|
|
|
|
dockerCmd(c, "push", repoName)
|
|
|
|
dockerCmd(c, "rmi", repoName)
|
|
|
|
|
|
|
|
// Try trusted pull on untrusted tag
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
Err: "Error: remote trust data does not exist",
|
|
|
|
})
|
2015-09-02 20:56:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerTrustSuite) TestPullWhenCertExpired(c *check.C) {
|
|
|
|
c.Skip("Currently changes system time, causing instability")
|
|
|
|
repoName := s.setupTrustedImage(c, "trusted-cert-expired")
|
|
|
|
|
|
|
|
// Certificates have 10 years of expiration
|
|
|
|
elevenYearsFromNow := time.Now().Add(time.Hour * 24 * 365 * 11)
|
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
testutil.RunAtDifferentDate(elevenYearsFromNow, func() {
|
2015-09-02 20:56:01 -04:00
|
|
|
// Try pull
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
|
|
|
Command: []string{dockerBinary, "pull", repoName},
|
|
|
|
}, trustedCmd).Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
Err: "could not validate the path to a trusted root",
|
|
|
|
})
|
2015-09-02 20:56:01 -04:00
|
|
|
})
|
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
testutil.RunAtDifferentDate(elevenYearsFromNow, func() {
|
2015-09-02 20:56:01 -04:00
|
|
|
// Try pull
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
|
|
|
Command: []string{dockerBinary, "pull", "--disable-content-trust", repoName},
|
|
|
|
}, trustedCmd).Assert(c, SuccessDownloaded)
|
2015-09-02 20:56:01 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
dockerCmd(c, "tag", "busybox", repoName)
|
|
|
|
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
|
2015-09-02 20:56:01 -04:00
|
|
|
dockerCmd(c, "rmi", repoName)
|
|
|
|
|
|
|
|
// Try pull
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "pull", repoName), trustedCmd).Assert(c, SuccessTagging)
|
2015-09-02 20:56:01 -04:00
|
|
|
dockerCmd(c, "rmi", repoName)
|
|
|
|
|
|
|
|
// Kill the notary server, start a new "evil" one.
|
|
|
|
s.not.Close()
|
|
|
|
s.not, err = newTestNotary(c)
|
2015-11-16 03:24:11 -05:00
|
|
|
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf("Restarting notary server failed."))
|
2015-09-02 20:56:01 -04:00
|
|
|
|
|
|
|
// 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
|
|
|
|
dockerCmd(c, "--config", evilLocalConfigDir, "tag", "busybox", repoName)
|
|
|
|
|
|
|
|
// Push up to the new server
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "--config", evilLocalConfigDir, "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
|
2015-09-02 20:56:01 -04:00
|
|
|
|
2016-09-28 15:49:47 -04:00
|
|
|
// Now, try pulling with the original client from this new trust server. This should fail because the new root is invalid.
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
Err: "could not rotate trust to a new trusted root",
|
|
|
|
})
|
2015-09-02 20:56:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerTrustSuite) TestTrustedPullWithExpiredSnapshot(c *check.C) {
|
|
|
|
c.Skip("Currently changes system time, causing instability")
|
|
|
|
repoName := fmt.Sprintf("%v/dockercliexpiredtimestamppull/trusted:latest", privateRegistryURL)
|
|
|
|
// tag the image and upload it to the private registry
|
|
|
|
dockerCmd(c, "tag", "busybox", repoName)
|
|
|
|
|
|
|
|
// Push with default passphrases
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
|
2015-09-02 20:56:01 -04:00
|
|
|
dockerCmd(c, "rmi", repoName)
|
|
|
|
|
|
|
|
// Snapshots last for three years. This should be expired
|
|
|
|
fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
|
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
testutil.RunAtDifferentDate(fourYearsLater, func() {
|
2015-09-02 20:56:01 -04:00
|
|
|
// Try pull
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Cmd{
|
|
|
|
Command: []string{dockerBinary, "pull", repoName},
|
|
|
|
}, trustedCmd).Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
Err: "repository out-of-date",
|
|
|
|
})
|
2015-09-02 20:56:01 -04:00
|
|
|
})
|
|
|
|
}
|
2015-09-03 23:01:03 -04:00
|
|
|
|
|
|
|
func (s *DockerTrustSuite) TestTrustedOfflinePull(c *check.C) {
|
|
|
|
repoName := s.setupTrustedImage(c, "trusted-offline-pull")
|
|
|
|
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "pull", repoName), trustedCmdWithServer("https://invalidnotaryserver")).Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
Err: "error contacting notary server",
|
|
|
|
})
|
2015-09-03 23:01:03 -04:00
|
|
|
// Do valid trusted pull to warm cache
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "pull", repoName), trustedCmd).Assert(c, SuccessTagging)
|
2015-09-03 23:01:03 -04:00
|
|
|
dockerCmd(c, "rmi", repoName)
|
|
|
|
|
|
|
|
// Try pull again with invalid notary server, should use cache
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "pull", repoName), trustedCmdWithServer("https://invalidnotaryserver")).Assert(c, SuccessTagging)
|
2015-09-03 23:01:03 -04:00
|
|
|
}
|
2016-01-06 20:57:21 -05:00
|
|
|
|
|
|
|
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
|
2017-01-16 05:30:14 -05:00
|
|
|
buildImageSuccessfully(c, repoName, withDockerfile(`
|
2016-01-06 20:57:21 -05:00
|
|
|
FROM busybox
|
|
|
|
CMD echo trustedpulldelete
|
2017-01-16 05:30:14 -05:00
|
|
|
`))
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
|
2016-01-06 20:57:21 -05:00
|
|
|
|
2017-01-05 13:08:24 -05:00
|
|
|
dockerCmd(c, "rmi", repoName)
|
2016-01-06 20:57:21 -05:00
|
|
|
|
|
|
|
// Try pull
|
2017-01-05 13:08:24 -05:00
|
|
|
result := icmd.RunCmd(icmd.Command(dockerBinary, "pull", repoName), trustedCmd)
|
|
|
|
result.Assert(c, icmd.Success)
|
2016-01-06 20:57:21 -05:00
|
|
|
|
2017-01-05 13:08:24 -05:00
|
|
|
matches := digestRegex.FindStringSubmatch(result.Combined())
|
|
|
|
c.Assert(matches, checker.HasLen, 2, check.Commentf("unable to parse digest from pull output: %s", result.Combined()))
|
2016-01-06 20:57:21 -05:00
|
|
|
pullDigest := matches[1]
|
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
imageID := inspectField(c, repoName, "Id")
|
2016-01-06 20:57:21 -05:00
|
|
|
|
|
|
|
imageByDigest := repoName + "@" + pullDigest
|
2016-01-28 09:19:25 -05:00
|
|
|
byDigestID := inspectField(c, imageByDigest, "Id")
|
2016-01-06 20:57:21 -05:00
|
|
|
|
|
|
|
c.Assert(byDigestID, checker.Equals, imageID)
|
|
|
|
|
|
|
|
// rmi of tag should also remove the digest reference
|
|
|
|
dockerCmd(c, "rmi", repoName)
|
|
|
|
|
2017-01-16 05:30:14 -05:00
|
|
|
_, err := inspectFieldWithError(imageByDigest, "Id")
|
2016-01-06 20:57:21 -05:00
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("digest reference should have been removed"))
|
|
|
|
|
2016-01-28 09:19:25 -05:00
|
|
|
_, err = inspectFieldWithError(imageID, "Id")
|
2016-01-06 20:57:21 -05:00
|
|
|
c.Assert(err, checker.NotNil, check.Commentf("image should have been deleted"))
|
|
|
|
}
|
2016-03-09 03:18:30 -05:00
|
|
|
|
|
|
|
func (s *DockerTrustSuite) TestTrustedPullReadsFromReleasesRole(c *check.C) {
|
2016-03-16 22:59:18 -04:00
|
|
|
testRequires(c, NotaryHosting)
|
2016-03-09 03:18:30 -05:00
|
|
|
repoName := fmt.Sprintf("%v/dockerclireleasesdelegationpulling/trusted", privateRegistryURL)
|
|
|
|
targetName := fmt.Sprintf("%s:latest", repoName)
|
|
|
|
|
|
|
|
// Push with targets first, initializing the repo
|
|
|
|
dockerCmd(c, "tag", "busybox", targetName)
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "push", targetName), trustedCmd).Assert(c, icmd.Success)
|
2016-03-16 22:59:18 -04:00
|
|
|
s.assertTargetInRoles(c, repoName, "latest", "targets")
|
2016-03-09 03:18:30 -05:00
|
|
|
|
|
|
|
// Try pull, check we retrieve from targets role
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "-D", "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
|
|
|
|
Err: "retrieving target for targets role",
|
|
|
|
})
|
2016-03-09 03:18:30 -05:00
|
|
|
|
|
|
|
// Now we'll create the releases role, and try pushing and pulling
|
2016-03-16 22:59:18 -04:00
|
|
|
s.notaryCreateDelegation(c, repoName, "targets/releases", s.not.keys[0].Public)
|
2016-03-09 03:18:30 -05:00
|
|
|
s.notaryImportKey(c, repoName, "targets/releases", s.not.keys[0].Private)
|
2016-03-16 22:59:18 -04:00
|
|
|
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
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "-D", "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
|
|
|
|
Err: "retrieving target for targets role",
|
|
|
|
})
|
2016-03-16 22:59:18 -04:00
|
|
|
|
|
|
|
// try a pull -a, check that it succeeds because we can still pull from the
|
|
|
|
// targets role
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "-D", "pull", "-a", repoName), trustedCmd).Assert(c, icmd.Success)
|
2016-03-09 03:18:30 -05:00
|
|
|
|
|
|
|
// Push, should sign with targets/releases
|
|
|
|
dockerCmd(c, "tag", "busybox", targetName)
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "push", targetName), trustedCmd).Assert(c, icmd.Success)
|
2016-03-16 22:59:18 -04:00
|
|
|
s.assertTargetInRoles(c, repoName, "latest", "targets", "targets/releases")
|
2016-03-09 03:18:30 -05:00
|
|
|
|
|
|
|
// Try pull, check we retrieve from targets/releases role
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "-D", "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
|
|
|
|
Err: "retrieving target for targets/releases role",
|
|
|
|
})
|
2016-03-09 03:18:30 -05:00
|
|
|
|
|
|
|
// Create another delegation that we'll sign with
|
2016-03-16 22:59:18 -04:00
|
|
|
s.notaryCreateDelegation(c, repoName, "targets/other", s.not.keys[1].Public)
|
2016-03-09 03:18:30 -05:00
|
|
|
s.notaryImportKey(c, repoName, "targets/other", s.not.keys[1].Private)
|
2016-03-16 22:59:18 -04:00
|
|
|
s.notaryPublish(c, repoName)
|
2016-03-09 03:18:30 -05:00
|
|
|
|
|
|
|
dockerCmd(c, "tag", "busybox", targetName)
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "push", targetName), trustedCmd).Assert(c, icmd.Success)
|
2016-03-16 22:59:18 -04:00
|
|
|
s.assertTargetInRoles(c, repoName, "latest", "targets", "targets/releases", "targets/other")
|
2016-03-09 03:18:30 -05:00
|
|
|
|
|
|
|
// Try pull, check we retrieve from targets/releases role
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "-D", "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
|
|
|
|
Err: "retrieving target for targets/releases role",
|
|
|
|
})
|
2016-03-09 03:18:30 -05:00
|
|
|
}
|
2016-03-16 22:59:18 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
dockerCmd(c, "tag", "busybox", targetName)
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "push", targetName), trustedCmd).Assert(c, icmd.Success)
|
2016-03-16 22:59:18 -04:00
|
|
|
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
|
2017-01-05 13:08:24 -05:00
|
|
|
dockerCmd(c, "tag", "busybox", targetName)
|
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "-D", "pull", repoName), trustedCmd).Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
Err: "No trust data for",
|
|
|
|
})
|
2016-03-16 22:59:18 -04:00
|
|
|
|
|
|
|
// try a pull -a: we should fail since pull will only pull from the targets/releases
|
|
|
|
// role or the targets role
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd.RunCmd(icmd.Command(dockerBinary, "-D", "pull", "-a", repoName), trustedCmd).Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
Err: "No trusted tags for",
|
|
|
|
})
|
2016-03-16 22:59:18 -04:00
|
|
|
}
|