2015-07-20 01:56:10 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-07-12 16:31:13 -04:00
|
|
|
"context"
|
2015-07-20 01:56:10 -04:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2015-07-23 08:12:36 -04:00
|
|
|
"strings"
|
2015-07-20 01:56:10 -04:00
|
|
|
"time"
|
|
|
|
|
2017-07-12 16:31:13 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-06-01 16:34:31 -04:00
|
|
|
cliconfig "github.com/docker/docker/cli/config"
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2017-04-19 08:04:39 -04:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2017-07-12 16:31:13 -04:00
|
|
|
"github.com/docker/docker/integration-cli/fixtures/plugin"
|
|
|
|
"github.com/docker/docker/integration-cli/request"
|
2017-01-05 13:08:24 -05:00
|
|
|
icmd "github.com/docker/docker/pkg/testutil/cmd"
|
2016-09-02 21:27:20 -04:00
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2015-07-20 01:56:10 -04:00
|
|
|
"github.com/go-check/check"
|
|
|
|
)
|
|
|
|
|
2016-03-11 17:11:35 -05:00
|
|
|
var notaryBinary = "notary"
|
|
|
|
var notaryServerBinary = "notary-server"
|
2015-07-20 01:56:10 -04:00
|
|
|
|
2016-03-07 14:48:11 -05:00
|
|
|
type keyPair struct {
|
|
|
|
Public string
|
|
|
|
Private string
|
|
|
|
}
|
|
|
|
|
2015-07-20 01:56:10 -04:00
|
|
|
type testNotary struct {
|
2016-03-07 14:48:11 -05:00
|
|
|
cmd *exec.Cmd
|
|
|
|
dir string
|
|
|
|
keys []keyPair
|
2015-07-20 01:56:10 -04:00
|
|
|
}
|
|
|
|
|
2015-10-08 14:10:38 -04:00
|
|
|
const notaryHost = "localhost:4443"
|
|
|
|
const notaryURL = "https://" + notaryHost
|
|
|
|
|
2017-01-05 13:08:24 -05:00
|
|
|
var SuccessTagging = icmd.Expected{
|
|
|
|
Out: "Tagging",
|
|
|
|
}
|
|
|
|
|
|
|
|
var SuccessSigningAndPushing = icmd.Expected{
|
|
|
|
Out: "Signing and pushing trust metadata",
|
|
|
|
}
|
|
|
|
|
|
|
|
var SuccessDownloaded = icmd.Expected{
|
|
|
|
Out: "Status: Downloaded",
|
|
|
|
}
|
|
|
|
|
|
|
|
var SuccessDownloadedOnStderr = icmd.Expected{
|
|
|
|
Err: "Status: Downloaded",
|
|
|
|
}
|
|
|
|
|
2015-07-20 01:56:10 -04:00
|
|
|
func newTestNotary(c *check.C) (*testNotary, error) {
|
2015-12-23 19:34:46 -05:00
|
|
|
// generate server config
|
2015-07-20 01:56:10 -04:00
|
|
|
template := `{
|
|
|
|
"server": {
|
2015-12-18 21:47:35 -05:00
|
|
|
"http_addr": "%s",
|
|
|
|
"tls_key_file": "%s",
|
|
|
|
"tls_cert_file": "%s"
|
2015-07-20 01:56:10 -04:00
|
|
|
},
|
|
|
|
"trust_service": {
|
|
|
|
"type": "local",
|
|
|
|
"hostname": "",
|
2015-07-31 18:01:50 -04:00
|
|
|
"port": "",
|
|
|
|
"key_algorithm": "ed25519"
|
2015-07-20 01:56:10 -04:00
|
|
|
},
|
|
|
|
"logging": {
|
2015-12-18 21:47:35 -05:00
|
|
|
"level": "debug"
|
|
|
|
},
|
|
|
|
"storage": {
|
|
|
|
"backend": "memory"
|
|
|
|
}
|
2015-07-20 01:56:10 -04:00
|
|
|
}`
|
|
|
|
tmp, err := ioutil.TempDir("", "notary-test-")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
confPath := filepath.Join(tmp, "config.json")
|
|
|
|
config, err := os.Create(confPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-24 23:57:21 -04:00
|
|
|
defer config.Close()
|
2015-12-18 21:47:35 -05:00
|
|
|
|
|
|
|
workingDir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := fmt.Fprintf(config, template, notaryHost, filepath.Join(workingDir, "fixtures/notary/localhost.key"), filepath.Join(workingDir, "fixtures/notary/localhost.cert")); err != nil {
|
2015-07-20 01:56:10 -04:00
|
|
|
os.RemoveAll(tmp)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-23 19:34:46 -05:00
|
|
|
// generate client config
|
|
|
|
clientConfPath := filepath.Join(tmp, "client-config.json")
|
|
|
|
clientConfig, err := os.Create(clientConfPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-24 23:57:21 -04:00
|
|
|
defer clientConfig.Close()
|
|
|
|
|
2015-12-23 19:34:46 -05:00
|
|
|
template = `{
|
|
|
|
"trust_dir" : "%s",
|
|
|
|
"remote_server": {
|
|
|
|
"url": "%s",
|
|
|
|
"skipTLSVerify": true
|
|
|
|
}
|
|
|
|
}`
|
2017-06-01 16:34:31 -04:00
|
|
|
if _, err = fmt.Fprintf(clientConfig, template, filepath.Join(cliconfig.Dir(), "trust"), notaryURL); err != nil {
|
2015-12-23 19:34:46 -05:00
|
|
|
os.RemoveAll(tmp)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-07 14:48:11 -05:00
|
|
|
// load key fixture filenames
|
|
|
|
var keys []keyPair
|
|
|
|
for i := 1; i < 5; i++ {
|
|
|
|
keys = append(keys, keyPair{
|
|
|
|
Public: filepath.Join(workingDir, fmt.Sprintf("fixtures/notary/delgkey%v.crt", i)),
|
|
|
|
Private: filepath.Join(workingDir, fmt.Sprintf("fixtures/notary/delgkey%v.key", i)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-23 19:34:46 -05:00
|
|
|
// run notary-server
|
2016-03-11 17:11:35 -05:00
|
|
|
cmd := exec.Command(notaryServerBinary, "-config", confPath)
|
2015-07-20 01:56:10 -04:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
os.RemoveAll(tmp)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
c.Skip(err.Error())
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-22 19:10:25 -04:00
|
|
|
|
|
|
|
testNotary := &testNotary{
|
2016-03-07 14:48:11 -05:00
|
|
|
cmd: cmd,
|
|
|
|
dir: tmp,
|
|
|
|
keys: keys,
|
2015-07-22 19:10:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for notary to be ready to serve requests.
|
2016-01-19 19:45:47 -05:00
|
|
|
for i := 1; i <= 20; i++ {
|
2015-07-22 19:10:25 -04:00
|
|
|
if err = testNotary.Ping(); err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond * time.Duration(i*i))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("Timeout waiting for test notary to become available: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return testNotary, nil
|
2015-07-20 01:56:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testNotary) Ping() error {
|
2016-09-02 21:27:20 -04:00
|
|
|
tlsConfig := tlsconfig.ClientDefault()
|
2015-07-20 01:56:10 -04:00
|
|
|
tlsConfig.InsecureSkipVerify = true
|
|
|
|
client := http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
2016-09-02 21:27:20 -04:00
|
|
|
TLSClientConfig: tlsConfig,
|
2015-07-20 01:56:10 -04:00
|
|
|
},
|
|
|
|
}
|
2015-10-08 14:10:38 -04:00
|
|
|
resp, err := client.Get(fmt.Sprintf("%s/v2/", notaryURL))
|
2015-07-20 01:56:10 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-19 03:40:20 -04:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2015-07-20 01:56:10 -04:00
|
|
|
return fmt.Errorf("notary ping replied with an unexpected status code %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testNotary) Close() {
|
|
|
|
t.cmd.Process.Kill()
|
2017-01-13 20:45:14 -05:00
|
|
|
t.cmd.Process.Wait()
|
2015-07-20 01:56:10 -04:00
|
|
|
os.RemoveAll(t.dir)
|
|
|
|
}
|
|
|
|
|
2017-04-19 08:04:39 -04:00
|
|
|
func trustedCmd(cmd *icmd.Cmd) func() {
|
2015-07-21 23:36:22 -04:00
|
|
|
pwd := "12345678"
|
2017-01-05 13:08:24 -05:00
|
|
|
cmd.Env = append(cmd.Env, trustEnv(notaryURL, pwd, pwd)...)
|
2017-04-19 08:04:39 -04:00
|
|
|
return nil
|
2015-07-21 23:36:22 -04:00
|
|
|
}
|
2015-07-23 08:12:36 -04:00
|
|
|
|
2017-04-19 08:04:39 -04:00
|
|
|
func trustedCmdWithServer(server string) func(*icmd.Cmd) func() {
|
|
|
|
return func(cmd *icmd.Cmd) func() {
|
2017-01-05 13:08:24 -05:00
|
|
|
pwd := "12345678"
|
|
|
|
cmd.Env = append(cmd.Env, trustEnv(server, pwd, pwd)...)
|
2017-04-19 08:04:39 -04:00
|
|
|
return nil
|
2017-01-05 13:08:24 -05:00
|
|
|
}
|
2015-07-21 23:36:22 -04:00
|
|
|
}
|
|
|
|
|
2017-04-19 08:04:39 -04:00
|
|
|
func trustedCmdWithPassphrases(rootPwd, repositoryPwd string) func(*icmd.Cmd) func() {
|
|
|
|
return func(cmd *icmd.Cmd) func() {
|
2017-01-05 13:08:24 -05:00
|
|
|
cmd.Env = append(cmd.Env, trustEnv(notaryURL, rootPwd, repositoryPwd)...)
|
2017-04-19 08:04:39 -04:00
|
|
|
return nil
|
2017-01-05 13:08:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func trustEnv(server, rootPwd, repositoryPwd string) []string {
|
|
|
|
env := append(os.Environ(), []string{
|
2015-10-09 15:14:34 -04:00
|
|
|
"DOCKER_CONTENT_TRUST=1",
|
|
|
|
fmt.Sprintf("DOCKER_CONTENT_TRUST_SERVER=%s", server),
|
|
|
|
fmt.Sprintf("DOCKER_CONTENT_TRUST_ROOT_PASSPHRASE=%s", rootPwd),
|
|
|
|
fmt.Sprintf("DOCKER_CONTENT_TRUST_REPOSITORY_PASSPHRASE=%s", repositoryPwd),
|
2017-01-05 13:08:24 -05:00
|
|
|
}...)
|
|
|
|
return env
|
2015-10-09 15:14:34 -04:00
|
|
|
}
|
|
|
|
|
2015-07-23 08:12:36 -04:00
|
|
|
func (s *DockerTrustSuite) setupTrustedImage(c *check.C, name string) string {
|
|
|
|
repoName := fmt.Sprintf("%v/dockercli/%s:latest", privateRegistryURL, name)
|
|
|
|
// tag the image and upload it to the private registry
|
2017-04-19 08:04:39 -04:00
|
|
|
cli.DockerCmd(c, "tag", "busybox", repoName)
|
|
|
|
cli.Docker(cli.Args("push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
|
|
|
|
cli.DockerCmd(c, "rmi", repoName)
|
2015-07-23 08:12:36 -04:00
|
|
|
return repoName
|
|
|
|
}
|
2015-12-23 19:34:46 -05:00
|
|
|
|
2016-12-27 15:51:00 -05:00
|
|
|
func (s *DockerTrustSuite) setupTrustedplugin(c *check.C, source, name string) string {
|
|
|
|
repoName := fmt.Sprintf("%v/dockercli/%s:latest", privateRegistryURL, name)
|
2017-07-12 16:31:13 -04:00
|
|
|
|
|
|
|
client, err := request.NewClient()
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("could not create test client"))
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
|
|
err = plugin.Create(ctx, client, repoName)
|
|
|
|
cancel()
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("could not create test plugin"))
|
|
|
|
|
2016-12-27 15:51:00 -05:00
|
|
|
// tag the image and upload it to the private registry
|
2017-07-12 16:31:13 -04:00
|
|
|
// TODO: shouldn't need to use the CLI to do trust
|
2017-04-19 08:04:39 -04:00
|
|
|
cli.Docker(cli.Args("plugin", "push", repoName), trustedCmd).Assert(c, SuccessSigningAndPushing)
|
2017-07-12 16:31:13 -04:00
|
|
|
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), 60*time.Second)
|
|
|
|
err = client.PluginRemove(ctx, repoName, types.PluginRemoveOptions{Force: true})
|
|
|
|
cancel()
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("failed to cleanup test plugin for trust suite"))
|
2016-12-27 15:51:00 -05:00
|
|
|
return repoName
|
|
|
|
}
|
|
|
|
|
2017-01-02 17:42:45 -05:00
|
|
|
func (s *DockerTrustSuite) notaryCmd(c *check.C, args ...string) string {
|
2016-03-16 22:59:18 -04:00
|
|
|
pwd := "12345678"
|
2015-12-23 19:34:46 -05:00
|
|
|
env := []string{
|
2016-03-16 22:59:18 -04:00
|
|
|
fmt.Sprintf("NOTARY_ROOT_PASSPHRASE=%s", pwd),
|
|
|
|
fmt.Sprintf("NOTARY_TARGETS_PASSPHRASE=%s", pwd),
|
|
|
|
fmt.Sprintf("NOTARY_SNAPSHOT_PASSPHRASE=%s", pwd),
|
2016-03-17 13:23:18 -04:00
|
|
|
fmt.Sprintf("NOTARY_DELEGATION_PASSPHRASE=%s", pwd),
|
2015-12-23 19:34:46 -05:00
|
|
|
}
|
2017-01-02 17:42:45 -05:00
|
|
|
result := icmd.RunCmd(icmd.Cmd{
|
|
|
|
Command: append([]string{notaryBinary, "-c", filepath.Join(s.not.dir, "client-config.json")}, args...),
|
|
|
|
Env: append(os.Environ(), env...),
|
|
|
|
})
|
|
|
|
result.Assert(c, icmd.Success)
|
|
|
|
return result.Combined()
|
2015-12-23 19:34:46 -05:00
|
|
|
}
|
|
|
|
|
2016-03-16 22:59:18 -04:00
|
|
|
func (s *DockerTrustSuite) notaryInitRepo(c *check.C, repoName string) {
|
2017-01-02 17:42:45 -05:00
|
|
|
s.notaryCmd(c, "init", repoName)
|
2016-03-07 14:48:11 -05:00
|
|
|
}
|
2015-12-23 19:34:46 -05:00
|
|
|
|
2016-03-16 22:59:18 -04:00
|
|
|
func (s *DockerTrustSuite) notaryCreateDelegation(c *check.C, repoName, role string, pubKey string, paths ...string) {
|
2016-03-07 14:48:11 -05:00
|
|
|
pathsArg := "--all-paths"
|
|
|
|
if len(paths) > 0 {
|
|
|
|
pathsArg = "--paths=" + strings.Join(paths, ",")
|
2015-12-23 19:34:46 -05:00
|
|
|
}
|
2016-03-07 14:48:11 -05:00
|
|
|
|
2017-01-02 17:42:45 -05:00
|
|
|
s.notaryCmd(c, "delegation", "add", repoName, role, pubKey, pathsArg)
|
2016-03-07 14:48:11 -05:00
|
|
|
}
|
2015-12-23 19:34:46 -05:00
|
|
|
|
2016-03-16 22:59:18 -04:00
|
|
|
func (s *DockerTrustSuite) notaryPublish(c *check.C, repoName string) {
|
2017-01-02 17:42:45 -05:00
|
|
|
s.notaryCmd(c, "publish", repoName)
|
2015-12-23 19:34:46 -05:00
|
|
|
}
|
2016-03-07 14:48:11 -05:00
|
|
|
|
|
|
|
func (s *DockerTrustSuite) notaryImportKey(c *check.C, repoName, role string, privKey string) {
|
2017-01-02 17:42:45 -05:00
|
|
|
s.notaryCmd(c, "key", "import", privKey, "-g", repoName, "-r", role)
|
2016-03-07 14:48:11 -05:00
|
|
|
}
|
|
|
|
|
2016-03-16 22:59:18 -04:00
|
|
|
func (s *DockerTrustSuite) notaryListTargetsInRole(c *check.C, repoName, role string) map[string]string {
|
2017-01-02 17:42:45 -05:00
|
|
|
out := s.notaryCmd(c, "list", repoName, "-r", role)
|
2016-03-07 14:48:11 -05:00
|
|
|
|
|
|
|
// should look something like:
|
|
|
|
// NAME DIGEST SIZE (BYTES) ROLE
|
|
|
|
// ------------------------------------------------------------------------------------------------------
|
|
|
|
// latest 24a36bbc059b1345b7e8be0df20f1b23caa3602e85d42fff7ecd9d0bd255de56 1377 targets
|
|
|
|
|
2016-03-16 22:59:18 -04:00
|
|
|
targets := make(map[string]string)
|
|
|
|
|
|
|
|
// no target
|
2016-03-07 14:48:11 -05:00
|
|
|
lines := strings.Split(strings.TrimSpace(out), "\n")
|
2016-03-16 22:59:18 -04:00
|
|
|
if len(lines) == 1 && strings.Contains(out, "No targets present in this repository.") {
|
|
|
|
return targets
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, there is at least one target
|
2016-03-07 14:48:11 -05:00
|
|
|
c.Assert(len(lines), checker.GreaterOrEqualThan, 3)
|
|
|
|
|
|
|
|
for _, line := range lines[2:] {
|
|
|
|
tokens := strings.Fields(line)
|
|
|
|
c.Assert(tokens, checker.HasLen, 4)
|
|
|
|
targets[tokens[0]] = tokens[3]
|
|
|
|
}
|
|
|
|
|
|
|
|
return targets
|
|
|
|
}
|
|
|
|
|
2016-03-16 22:59:18 -04:00
|
|
|
func (s *DockerTrustSuite) assertTargetInRoles(c *check.C, repoName, target string, roles ...string) {
|
2016-03-07 14:48:11 -05:00
|
|
|
// check all the roles
|
|
|
|
for _, role := range roles {
|
2016-03-16 22:59:18 -04:00
|
|
|
targets := s.notaryListTargetsInRole(c, repoName, role)
|
2016-03-07 14:48:11 -05:00
|
|
|
roleName, ok := targets[target]
|
|
|
|
c.Assert(ok, checker.True)
|
|
|
|
c.Assert(roleName, checker.Equals, role)
|
|
|
|
}
|
|
|
|
}
|
2016-03-16 22:59:18 -04:00
|
|
|
|
|
|
|
func (s *DockerTrustSuite) assertTargetNotInRoles(c *check.C, repoName, target string, roles ...string) {
|
|
|
|
targets := s.notaryListTargetsInRole(c, repoName, "targets")
|
|
|
|
|
|
|
|
roleName, ok := targets[target]
|
|
|
|
if ok {
|
|
|
|
for _, role := range roles {
|
|
|
|
c.Assert(roleName, checker.Not(checker.Equals), role)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|