123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- package main
- import (
- "fmt"
- "io/ioutil"
- "os/exec"
- "strings"
- "time"
- "github.com/go-check/check"
- )
- func (s *DockerTrustSuite) TestTrustedPull(c *check.C) {
- repoName := s.setupTrustedImage(c, "trusted-pull")
- // Try pull
- pullCmd := exec.Command(dockerBinary, "pull", repoName)
- s.trustedCmd(pullCmd)
- out, _, err := runCommandWithOutput(pullCmd)
- if err != nil {
- c.Fatalf("Error running trusted pull: %s\n%s", err, out)
- }
- if !strings.Contains(string(out), "Tagging") {
- c.Fatalf("Missing expected output on trusted pull:\n%s", out)
- }
- dockerCmd(c, "rmi", repoName)
- // Try untrusted pull to ensure we pushed the tag to the registry
- pullCmd = exec.Command(dockerBinary, "pull", "--disable-content-trust=true", repoName)
- s.trustedCmd(pullCmd)
- out, _, err = runCommandWithOutput(pullCmd)
- if err != nil {
- c.Fatalf("Error running trusted pull: %s\n%s", err, out)
- }
- if !strings.Contains(string(out), "Status: Downloaded") {
- c.Fatalf("Missing expected output on trusted pull with --disable-content-trust:\n%s", out)
- }
- }
- func (s *DockerTrustSuite) TestTrustedIsolatedPull(c *check.C) {
- repoName := s.setupTrustedImage(c, "trusted-isolated-pull")
- // Try pull (run from isolated directory without trust information)
- pullCmd := exec.Command(dockerBinary, "--config", "/tmp/docker-isolated", "pull", repoName)
- s.trustedCmd(pullCmd)
- out, _, err := runCommandWithOutput(pullCmd)
- if err != nil {
- c.Fatalf("Error running trusted pull: %s\n%s", err, out)
- }
- if !strings.Contains(string(out), "Tagging") {
- c.Fatalf("Missing expected output on trusted pull:\n%s", out)
- }
- dockerCmd(c, "rmi", repoName)
- }
- func (s *DockerTrustSuite) TestUntrustedPull(c *check.C) {
- repoName := fmt.Sprintf("%v/dockercli/trusted:latest", privateRegistryURL)
- // 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
- pullCmd := exec.Command(dockerBinary, "pull", repoName)
- s.trustedCmd(pullCmd)
- out, _, err := runCommandWithOutput(pullCmd)
- if err == nil {
- c.Fatalf("Error expected when running trusted pull with:\n%s", out)
- }
- if !strings.Contains(string(out), "no trust data available") {
- c.Fatalf("Missing expected output on trusted pull:\n%s", out)
- }
- }
- 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)
- runAtDifferentDate(elevenYearsFromNow, func() {
- // Try pull
- pullCmd := exec.Command(dockerBinary, "pull", repoName)
- s.trustedCmd(pullCmd)
- out, _, err := runCommandWithOutput(pullCmd)
- if err == nil {
- c.Fatalf("Error running trusted pull in the distant future: %s\n%s", err, out)
- }
- if !strings.Contains(string(out), "could not validate the path to a trusted root") {
- c.Fatalf("Missing expected output on trusted pull in the distant future:\n%s", out)
- }
- })
- runAtDifferentDate(elevenYearsFromNow, func() {
- // Try pull
- pullCmd := exec.Command(dockerBinary, "pull", "--disable-content-trust", repoName)
- s.trustedCmd(pullCmd)
- out, _, err := runCommandWithOutput(pullCmd)
- if err != nil {
- c.Fatalf("Error running untrusted pull in the distant future: %s\n%s", err, out)
- }
- if !strings.Contains(string(out), "Status: Downloaded") {
- c.Fatalf("Missing expected output on untrusted pull in the distant future:\n%s", out)
- }
- })
- }
- 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)
- pushCmd := exec.Command(dockerBinary, "push", repoName)
- s.trustedCmd(pushCmd)
- out, _, err := runCommandWithOutput(pushCmd)
- if err != nil {
- c.Fatalf("Error running trusted push: %s\n%s", err, out)
- }
- if !strings.Contains(string(out), "Signing and pushing trust metadata") {
- c.Fatalf("Missing expected output on trusted push:\n%s", out)
- }
- dockerCmd(c, "rmi", repoName)
- // Try pull
- pullCmd := exec.Command(dockerBinary, "pull", repoName)
- s.trustedCmd(pullCmd)
- out, _, err = runCommandWithOutput(pullCmd)
- if err != nil {
- c.Fatalf("Error running trusted pull: %s\n%s", err, out)
- }
- if !strings.Contains(string(out), "Tagging") {
- c.Fatalf("Missing expected output on trusted pull:\n%s", out)
- }
- dockerCmd(c, "rmi", repoName)
- // Kill the notary server, start a new "evil" one.
- s.not.Close()
- s.not, err = newTestNotary(c)
- if err != nil {
- c.Fatalf("Restarting notary server failed.")
- }
- // 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
- pushCmd = exec.Command(dockerBinary, "--config", evilLocalConfigDir, "push", repoName)
- s.trustedCmd(pushCmd)
- out, _, err = runCommandWithOutput(pushCmd)
- if err != nil {
- c.Fatalf("Error running trusted push: %s\n%s", err, out)
- }
- if !strings.Contains(string(out), "Signing and pushing trust metadata") {
- c.Fatalf("Missing expected output on trusted push:\n%s", out)
- }
- // Now, try pulling with the original client from this new trust server. This should fail.
- pullCmd = exec.Command(dockerBinary, "pull", repoName)
- s.trustedCmd(pullCmd)
- out, _, err = runCommandWithOutput(pullCmd)
- if err == nil {
- c.Fatalf("Expected to fail on this pull due to different remote data: %s\n%s", err, out)
- }
- if !strings.Contains(string(out), "failed to validate data with current trusted certificates") {
- c.Fatalf("Missing expected output on trusted pull:\n%s", out)
- }
- }
- 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
- pushCmd := exec.Command(dockerBinary, "push", repoName)
- s.trustedCmd(pushCmd)
- out, _, err := runCommandWithOutput(pushCmd)
- if err != nil {
- c.Fatalf("trusted push failed: %s\n%s", err, out)
- }
- if !strings.Contains(string(out), "Signing and pushing trust metadata") {
- c.Fatalf("Missing expected output on trusted push:\n%s", out)
- }
- dockerCmd(c, "rmi", repoName)
- // Snapshots last for three years. This should be expired
- fourYearsLater := time.Now().Add(time.Hour * 24 * 365 * 4)
- runAtDifferentDate(fourYearsLater, func() {
- // Try pull
- pullCmd := exec.Command(dockerBinary, "pull", repoName)
- s.trustedCmd(pullCmd)
- out, _, err = runCommandWithOutput(pullCmd)
- if err == nil {
- c.Fatalf("Missing expected error running trusted pull with expired snapshots")
- }
- if !strings.Contains(string(out), "repository out-of-date") {
- c.Fatalf("Missing expected output on trusted pull with expired snapshot:\n%s", out)
- }
- })
- }
- func (s *DockerTrustSuite) TestTrustedOfflinePull(c *check.C) {
- repoName := s.setupTrustedImage(c, "trusted-offline-pull")
- pullCmd := exec.Command(dockerBinary, "pull", repoName)
- s.trustedCmdWithServer(pullCmd, "https://invalidnotaryserver")
- out, _, err := runCommandWithOutput(pullCmd)
- if err == nil {
- c.Fatalf("Expected error pulling with invalid notary server:\n%s", out)
- }
- if !strings.Contains(string(out), "error contacting notary server") {
- c.Fatalf("Missing expected output on trusted pull:\n%s", out)
- }
- // Do valid trusted pull to warm cache
- pullCmd = exec.Command(dockerBinary, "pull", repoName)
- s.trustedCmd(pullCmd)
- out, _, err = runCommandWithOutput(pullCmd)
- if err != nil {
- c.Fatalf("Error running trusted pull: %s\n%s", err, out)
- }
- if !strings.Contains(string(out), "Tagging") {
- c.Fatalf("Missing expected output on trusted pull:\n%s", out)
- }
- dockerCmd(c, "rmi", repoName)
- // Try pull again with invalid notary server, should use cache
- pullCmd = exec.Command(dockerBinary, "pull", repoName)
- s.trustedCmdWithServer(pullCmd, "https://invalidnotaryserver")
- out, _, err = runCommandWithOutput(pullCmd)
- if err != nil {
- c.Fatalf("Error running trusted pull: %s\n%s", err, out)
- }
- if !strings.Contains(string(out), "Tagging") {
- c.Fatalf("Missing expected output on trusted pull:\n%s", out)
- }
- }
|