|
|
|
@@ -3,11 +3,10 @@ package image
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/gotestyourself/gotestyourself/golden"
|
|
|
|
|
"github.com/gotestyourself/gotestyourself/icmd"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const notaryURL = "https://notary-server:4443"
|
|
|
|
@@ -17,36 +16,29 @@ const alpineImage = "registry:5000/alpine:3.6"
|
|
|
|
|
const busyboxImage = "registry:5000/busybox:1.27.2"
|
|
|
|
|
|
|
|
|
|
func TestPullWithContentTrust(t *testing.T) {
|
|
|
|
|
image := createTrustedRemoteImage(t, "trust", "latest")
|
|
|
|
|
image := createMaskedTrustedRemoteImage(t, "trust", "latest")
|
|
|
|
|
|
|
|
|
|
// test that pulling without the tag defaults to latest
|
|
|
|
|
imageWithoutTag := strings.TrimSuffix(image, ":latest")
|
|
|
|
|
icmd.RunCmd(trustedCmdNoPassphrases(icmd.Command("docker", "pull", imageWithoutTag))).Assert(t, icmd.Success)
|
|
|
|
|
icmd.RunCommand("docker", "rmi", image).Assert(t, icmd.Success)
|
|
|
|
|
result := icmd.RunCmd(icmd.Command("docker", "pull", image), withTrustNoPassphrase)
|
|
|
|
|
result.Assert(t, icmd.Expected{Err: icmd.None})
|
|
|
|
|
golden.Assert(t, result.Stdout(), "pull-with-content-trust.golden")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// try pulling with the tag, record output for comparison later
|
|
|
|
|
result := icmd.RunCmd(trustedCmdNoPassphrases(icmd.Command("docker", "pull", image)))
|
|
|
|
|
result.Assert(t, icmd.Success)
|
|
|
|
|
firstPullOutput := result.String()
|
|
|
|
|
icmd.RunCommand("docker", "rmi", image).Assert(t, icmd.Success)
|
|
|
|
|
|
|
|
|
|
// push an unsigned image on the same reference name, but with different content (busybox)
|
|
|
|
|
// createMaskedTrustedRemoteImage creates a remote image that is signed with
|
|
|
|
|
// content trust, then pushes a different untrusted image at the same tag.
|
|
|
|
|
func createMaskedTrustedRemoteImage(t *testing.T, repo, tag string) string {
|
|
|
|
|
image := createTrustedRemoteImage(t, repo, tag)
|
|
|
|
|
createNamedUnsignedImageFromBusyBox(t, image)
|
|
|
|
|
|
|
|
|
|
// now pull with content trust
|
|
|
|
|
result = icmd.RunCmd(trustedCmdNoPassphrases(icmd.Command("docker", "pull", image)))
|
|
|
|
|
result.Assert(t, icmd.Success)
|
|
|
|
|
secondPullOutput := result.String()
|
|
|
|
|
|
|
|
|
|
// assert that the digest and other output is the same since we ignore the unsigned image
|
|
|
|
|
require.Equal(t, firstPullOutput, secondPullOutput)
|
|
|
|
|
return image
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func createTrustedRemoteImage(t *testing.T, repo, tag string) string {
|
|
|
|
|
image := fmt.Sprintf("%s/%s:%s", registryPrefix, repo, tag)
|
|
|
|
|
icmd.RunCommand("docker", "pull", alpineImage).Assert(t, icmd.Success)
|
|
|
|
|
icmd.RunCommand("docker", "tag", alpineImage, image).Assert(t, icmd.Success)
|
|
|
|
|
icmd.RunCmd(trustedCmdWithPassphrases(icmd.Command("docker", "push", image), "root_password", "repo_password")).Assert(t, icmd.Success)
|
|
|
|
|
result := icmd.RunCmd(
|
|
|
|
|
icmd.Command("docker", "push", image),
|
|
|
|
|
withTrustAndPassphrase("root_password", "repo_password"))
|
|
|
|
|
result.Assert(t, icmd.Success)
|
|
|
|
|
icmd.RunCommand("docker", "rmi", image).Assert(t, icmd.Success)
|
|
|
|
|
return image
|
|
|
|
|
}
|
|
|
|
@@ -58,22 +50,22 @@ func createNamedUnsignedImageFromBusyBox(t *testing.T, image string) {
|
|
|
|
|
icmd.RunCommand("docker", "rmi", image).Assert(t, icmd.Success)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func trustedCmdWithPassphrases(cmd icmd.Cmd, rootPwd, repositoryPwd string) icmd.Cmd {
|
|
|
|
|
env := append(os.Environ(), []string{
|
|
|
|
|
"DOCKER_CONTENT_TRUST=1",
|
|
|
|
|
"DOCKER_CONTENT_TRUST_SERVER=" + notaryURL,
|
|
|
|
|
"DOCKER_CONTENT_TRUST_ROOT_PASSPHRASE=" + rootPwd,
|
|
|
|
|
"DOCKER_CONTENT_TRUST_REPOSITORY_PASSPHRASE=" + repositoryPwd,
|
|
|
|
|
}...)
|
|
|
|
|
cmd.Env = append(cmd.Env, env...)
|
|
|
|
|
return cmd
|
|
|
|
|
func withTrustAndPassphrase(rootPwd, repositoryPwd string) func(cmd *icmd.Cmd) {
|
|
|
|
|
return func(cmd *icmd.Cmd) {
|
|
|
|
|
env := append(os.Environ(),
|
|
|
|
|
"DOCKER_CONTENT_TRUST=1",
|
|
|
|
|
"DOCKER_CONTENT_TRUST_SERVER="+notaryURL,
|
|
|
|
|
"DOCKER_CONTENT_TRUST_ROOT_PASSPHRASE="+rootPwd,
|
|
|
|
|
"DOCKER_CONTENT_TRUST_REPOSITORY_PASSPHRASE="+repositoryPwd,
|
|
|
|
|
)
|
|
|
|
|
cmd.Env = append(cmd.Env, env...)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func trustedCmdNoPassphrases(cmd icmd.Cmd) icmd.Cmd {
|
|
|
|
|
env := append(os.Environ(), []string{
|
|
|
|
|
func withTrustNoPassphrase(cmd *icmd.Cmd) {
|
|
|
|
|
env := append(os.Environ(),
|
|
|
|
|
"DOCKER_CONTENT_TRUST=1",
|
|
|
|
|
"DOCKER_CONTENT_TRUST_SERVER=" + notaryURL,
|
|
|
|
|
}...)
|
|
|
|
|
"DOCKER_CONTENT_TRUST_SERVER="+notaryURL,
|
|
|
|
|
)
|
|
|
|
|
cmd.Env = append(cmd.Env, env...)
|
|
|
|
|
return cmd
|
|
|
|
|
}
|
|
|
|
|