2017-08-24 18:45:20 -04:00
|
|
|
package trust
|
|
|
|
|
|
|
|
import (
|
2017-11-10 14:34:04 -05:00
|
|
|
"bytes"
|
2017-08-24 18:45:20 -04:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/config"
|
|
|
|
"github.com/docker/cli/cli/trust"
|
|
|
|
"github.com/docker/cli/internal/test"
|
|
|
|
"github.com/docker/cli/internal/test/testutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-09-26 12:33:35 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-10-30 12:21:41 -04:00
|
|
|
"github.com/theupdateframework/notary"
|
|
|
|
"github.com/theupdateframework/notary/client"
|
|
|
|
"github.com/theupdateframework/notary/client/changelist"
|
|
|
|
"github.com/theupdateframework/notary/passphrase"
|
|
|
|
"github.com/theupdateframework/notary/trustpinning"
|
|
|
|
"github.com/theupdateframework/notary/tuf/data"
|
2017-08-24 18:45:20 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const passwd = "password"
|
|
|
|
|
2017-08-25 17:49:40 -04:00
|
|
|
func TestTrustSignCommandErrors(t *testing.T) {
|
2017-08-24 18:45:20 -04:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "not-enough-args",
|
|
|
|
expectedError: "requires exactly 1 argument",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "too-many-args",
|
|
|
|
args: []string{"image", "tag"},
|
|
|
|
expectedError: "requires exactly 1 argument",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "sha-reference",
|
|
|
|
args: []string{"870d292919d01a0af7e7f056271dc78792c05f55f49b9b9012b6d89725bd9abd"},
|
|
|
|
expectedError: "invalid repository name",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid-img-reference",
|
|
|
|
args: []string{"ALPINE:latest"},
|
|
|
|
expectedError: "invalid reference format",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no-tag",
|
2017-09-13 12:50:37 -04:00
|
|
|
args: []string{"reg/img"},
|
|
|
|
expectedError: "No tag specified for reg/img",
|
2017-08-24 18:45:20 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "digest-reference",
|
|
|
|
args: []string{"ubuntu@sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2"},
|
|
|
|
expectedError: "cannot use a digest reference for IMAGE:TAG",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// change to a tmpdir
|
|
|
|
tmpDir, err := ioutil.TempDir("", "docker-sign-test-")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
config.SetDir(tmpDir)
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newSignCommand(
|
|
|
|
test.NewFakeCli(&fakeClient{}))
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 12:50:37 -04:00
|
|
|
func TestTrustSignCommandOfflineErrors(t *testing.T) {
|
2017-09-14 16:16:54 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
|
|
|
cli.SetNotaryClient(getOfflineNotaryRepository)
|
2017-09-13 12:50:37 -04:00
|
|
|
cmd := newSignCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"reg-name.io/image:tag"})
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
assert.Error(t, cmd.Execute())
|
|
|
|
testutil.ErrorContains(t, cmd.Execute(), "client is offline")
|
|
|
|
}
|
|
|
|
|
2017-08-24 18:45:20 -04:00
|
|
|
func TestGetOrGenerateNotaryKey(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "notary-test-")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2017-09-11 17:07:00 -04:00
|
|
|
notaryRepo, err := client.NewFileCachedRepository(tmpDir, "gun", "https://localhost", nil, passphrase.ConstantRetriever(passwd), trustpinning.TrustPinConfig{})
|
2017-08-24 18:45:20 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// repo is empty, try making a root key
|
|
|
|
rootKeyA, err := getOrGenerateNotaryKey(notaryRepo, data.CanonicalRootRole)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, rootKeyA)
|
|
|
|
|
|
|
|
// we should only have one newly generated key
|
2017-09-11 17:07:00 -04:00
|
|
|
allKeys := notaryRepo.GetCryptoService().ListAllKeys()
|
2017-08-24 18:45:20 -04:00
|
|
|
assert.Len(t, allKeys, 1)
|
2017-09-11 17:07:00 -04:00
|
|
|
assert.NotNil(t, notaryRepo.GetCryptoService().GetKey(rootKeyA.ID()))
|
2017-08-24 18:45:20 -04:00
|
|
|
|
|
|
|
// this time we should get back the same key if we ask for another root key
|
|
|
|
rootKeyB, err := getOrGenerateNotaryKey(notaryRepo, data.CanonicalRootRole)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, rootKeyB)
|
|
|
|
|
|
|
|
// we should only have one newly generated key
|
2017-09-11 17:07:00 -04:00
|
|
|
allKeys = notaryRepo.GetCryptoService().ListAllKeys()
|
2017-08-24 18:45:20 -04:00
|
|
|
assert.Len(t, allKeys, 1)
|
2017-09-11 17:07:00 -04:00
|
|
|
assert.NotNil(t, notaryRepo.GetCryptoService().GetKey(rootKeyB.ID()))
|
2017-08-24 18:45:20 -04:00
|
|
|
|
|
|
|
// The key we retrieved should be identical to the one we generated
|
|
|
|
assert.Equal(t, rootKeyA, rootKeyB)
|
|
|
|
|
|
|
|
// Now also try with a delegation key
|
|
|
|
releasesKey, err := getOrGenerateNotaryKey(notaryRepo, data.RoleName(trust.ReleasesRole))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, releasesKey)
|
|
|
|
|
|
|
|
// we should now have two keys
|
2017-09-11 17:07:00 -04:00
|
|
|
allKeys = notaryRepo.GetCryptoService().ListAllKeys()
|
2017-08-24 18:45:20 -04:00
|
|
|
assert.Len(t, allKeys, 2)
|
2017-09-11 17:07:00 -04:00
|
|
|
assert.NotNil(t, notaryRepo.GetCryptoService().GetKey(releasesKey.ID()))
|
2017-08-24 18:45:20 -04:00
|
|
|
// The key we retrieved should be identical to the one we generated
|
|
|
|
assert.NotEqual(t, releasesKey, rootKeyA)
|
|
|
|
assert.NotEqual(t, releasesKey, rootKeyB)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddStageSigners(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "notary-test-")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2017-09-11 17:07:00 -04:00
|
|
|
notaryRepo, err := client.NewFileCachedRepository(tmpDir, "gun", "https://localhost", nil, passphrase.ConstantRetriever(passwd), trustpinning.TrustPinConfig{})
|
2017-08-24 18:45:20 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// stage targets/user
|
|
|
|
userRole := data.RoleName("targets/user")
|
|
|
|
userKey := data.NewPublicKey("algoA", []byte("a"))
|
2017-10-10 13:16:01 -04:00
|
|
|
err = addStagedSigner(notaryRepo, userRole, []data.PublicKey{userKey})
|
|
|
|
assert.NoError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
// check the changelist for four total changes: two on targets/releases and two on targets/user
|
|
|
|
cl, err := notaryRepo.GetChangelist()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
changeList := cl.List()
|
|
|
|
assert.Len(t, changeList, 4)
|
|
|
|
// ordering is determinstic:
|
|
|
|
|
|
|
|
// first change is for targets/user key creation
|
|
|
|
newSignerKeyChange := changeList[0]
|
|
|
|
expectedJSON, err := json.Marshal(&changelist.TUFDelegation{
|
|
|
|
NewThreshold: notary.MinThreshold,
|
|
|
|
AddKeys: data.KeyList([]data.PublicKey{userKey}),
|
|
|
|
})
|
2017-09-26 12:33:35 -04:00
|
|
|
require.NoError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
expectedChange := changelist.NewTUFChange(
|
|
|
|
changelist.ActionCreate,
|
|
|
|
userRole,
|
|
|
|
changelist.TypeTargetsDelegation,
|
|
|
|
"", // no path for delegations
|
|
|
|
expectedJSON,
|
|
|
|
)
|
|
|
|
assert.Equal(t, expectedChange, newSignerKeyChange)
|
|
|
|
|
|
|
|
// second change is for targets/user getting all paths
|
|
|
|
newSignerPathsChange := changeList[1]
|
|
|
|
expectedJSON, err = json.Marshal(&changelist.TUFDelegation{
|
|
|
|
AddPaths: []string{""},
|
|
|
|
})
|
2017-09-26 12:33:35 -04:00
|
|
|
require.NoError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
expectedChange = changelist.NewTUFChange(
|
|
|
|
changelist.ActionCreate,
|
|
|
|
userRole,
|
|
|
|
changelist.TypeTargetsDelegation,
|
|
|
|
"", // no path for delegations
|
|
|
|
expectedJSON,
|
|
|
|
)
|
|
|
|
assert.Equal(t, expectedChange, newSignerPathsChange)
|
|
|
|
|
|
|
|
releasesRole := data.RoleName("targets/releases")
|
|
|
|
|
|
|
|
// third change is for targets/releases key creation
|
|
|
|
releasesKeyChange := changeList[2]
|
|
|
|
expectedJSON, err = json.Marshal(&changelist.TUFDelegation{
|
|
|
|
NewThreshold: notary.MinThreshold,
|
|
|
|
AddKeys: data.KeyList([]data.PublicKey{userKey}),
|
|
|
|
})
|
2017-09-26 12:33:35 -04:00
|
|
|
require.NoError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
expectedChange = changelist.NewTUFChange(
|
|
|
|
changelist.ActionCreate,
|
|
|
|
releasesRole,
|
|
|
|
changelist.TypeTargetsDelegation,
|
|
|
|
"", // no path for delegations
|
|
|
|
expectedJSON,
|
|
|
|
)
|
|
|
|
assert.Equal(t, expectedChange, releasesKeyChange)
|
|
|
|
|
|
|
|
// fourth change is for targets/releases getting all paths
|
|
|
|
releasesPathsChange := changeList[3]
|
|
|
|
expectedJSON, err = json.Marshal(&changelist.TUFDelegation{
|
|
|
|
AddPaths: []string{""},
|
|
|
|
})
|
2017-09-26 12:33:35 -04:00
|
|
|
require.NoError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
expectedChange = changelist.NewTUFChange(
|
|
|
|
changelist.ActionCreate,
|
|
|
|
releasesRole,
|
|
|
|
changelist.TypeTargetsDelegation,
|
|
|
|
"", // no path for delegations
|
|
|
|
expectedJSON,
|
|
|
|
)
|
|
|
|
assert.Equal(t, expectedChange, releasesPathsChange)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSignedManifestHashAndSize(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "notary-test-")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2017-09-11 17:07:00 -04:00
|
|
|
notaryRepo, err := client.NewFileCachedRepository(tmpDir, "gun", "https://localhost", nil, passphrase.ConstantRetriever(passwd), trustpinning.TrustPinConfig{})
|
2017-08-24 18:45:20 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
target := &client.Target{}
|
|
|
|
target.Hashes, target.Length, err = getSignedManifestHashAndSize(notaryRepo, "test")
|
|
|
|
assert.EqualError(t, err, "client is offline")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetReleasedTargetHashAndSize(t *testing.T) {
|
|
|
|
oneReleasedTgt := []client.TargetSignedStruct{}
|
|
|
|
// make and append 3 non-released signatures on the "unreleased" target
|
|
|
|
unreleasedTgt := client.Target{Name: "unreleased", Hashes: data.Hashes{notary.SHA256: []byte("hash")}}
|
|
|
|
for _, unreleasedRole := range []string{"targets/a", "targets/b", "targets/c"} {
|
|
|
|
oneReleasedTgt = append(oneReleasedTgt, client.TargetSignedStruct{Role: mockDelegationRoleWithName(unreleasedRole), Target: unreleasedTgt})
|
|
|
|
}
|
|
|
|
_, _, err := getReleasedTargetHashAndSize(oneReleasedTgt, "unreleased")
|
|
|
|
assert.EqualError(t, err, "No valid trust data for unreleased")
|
|
|
|
releasedTgt := client.Target{Name: "released", Hashes: data.Hashes{notary.SHA256: []byte("released-hash")}}
|
|
|
|
oneReleasedTgt = append(oneReleasedTgt, client.TargetSignedStruct{Role: mockDelegationRoleWithName("targets/releases"), Target: releasedTgt})
|
|
|
|
hash, _, _ := getReleasedTargetHashAndSize(oneReleasedTgt, "unreleased")
|
|
|
|
assert.Equal(t, data.Hashes{notary.SHA256: []byte("released-hash")}, hash)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateTarget(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "notary-test-")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2017-09-11 17:07:00 -04:00
|
|
|
notaryRepo, err := client.NewFileCachedRepository(tmpDir, "gun", "https://localhost", nil, passphrase.ConstantRetriever(passwd), trustpinning.TrustPinConfig{})
|
2017-08-24 18:45:20 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = createTarget(notaryRepo, "")
|
|
|
|
assert.EqualError(t, err, "No tag specified")
|
|
|
|
_, err = createTarget(notaryRepo, "1")
|
|
|
|
assert.EqualError(t, err, "client is offline")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetExistingSignatureInfoForReleasedTag(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "notary-test-")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2017-09-11 17:07:00 -04:00
|
|
|
notaryRepo, err := client.NewFileCachedRepository(tmpDir, "gun", "https://localhost", nil, passphrase.ConstantRetriever(passwd), trustpinning.TrustPinConfig{})
|
2017-08-24 18:45:20 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = getExistingSignatureInfoForReleasedTag(notaryRepo, "test")
|
|
|
|
assert.EqualError(t, err, "client is offline")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrettyPrintExistingSignatureInfo(t *testing.T) {
|
2017-09-26 12:53:21 -04:00
|
|
|
buf := bytes.NewBuffer(nil)
|
2017-08-24 18:45:20 -04:00
|
|
|
signers := []string{"Bob", "Alice", "Carol"}
|
|
|
|
existingSig := trustTagRow{trustTagKey{"tagName", "abc123"}, signers}
|
2017-09-26 12:53:21 -04:00
|
|
|
prettyPrintExistingSignatureInfo(buf, existingSig)
|
2017-08-24 18:45:20 -04:00
|
|
|
|
2017-09-26 12:53:21 -04:00
|
|
|
assert.Contains(t, buf.String(), "Existing signatures for tag tagName digest abc123 from:\nAlice, Bob, Carol")
|
2017-08-24 18:45:20 -04:00
|
|
|
}
|
|
|
|
|
2017-09-26 12:33:35 -04:00
|
|
|
func TestSignCommandChangeListIsCleanedOnError(t *testing.T) {
|
2017-08-24 18:45:20 -04:00
|
|
|
tmpDir, err := ioutil.TempDir("", "docker-sign-test-")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpDir)
|
2017-09-26 12:33:35 -04:00
|
|
|
|
2017-08-24 18:45:20 -04:00
|
|
|
config.SetDir(tmpDir)
|
2017-09-26 12:33:35 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
|
|
|
cli.SetNotaryClient(getLoadedNotaryRepository)
|
|
|
|
cmd := newSignCommand(cli)
|
2017-08-24 18:45:20 -04:00
|
|
|
cmd.SetArgs([]string{"ubuntu:latest"})
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2017-09-26 12:33:35 -04:00
|
|
|
|
2017-08-24 18:45:20 -04:00
|
|
|
err = cmd.Execute()
|
2017-09-26 12:33:35 -04:00
|
|
|
require.Error(t, err)
|
|
|
|
|
2017-09-11 17:07:00 -04:00
|
|
|
notaryRepo, err := client.NewFileCachedRepository(tmpDir, "docker.io/library/ubuntu", "https://localhost", nil, passphrase.ConstantRetriever(passwd), trustpinning.TrustPinConfig{})
|
2017-08-24 18:45:20 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
cl, err := notaryRepo.GetChangelist()
|
2017-09-26 12:33:35 -04:00
|
|
|
require.NoError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
assert.Equal(t, len(cl.List()), 0)
|
|
|
|
}
|
2017-09-29 12:51:45 -04:00
|
|
|
|
|
|
|
func TestSignCommandLocalFlag(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
|
|
|
cli.SetNotaryClient(getEmptyTargetsNotaryRepository)
|
|
|
|
cmd := newSignCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"--local", "reg-name.io/image:red"})
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
testutil.ErrorContains(t, cmd.Execute(), "error during connect: Get /images/reg-name.io/image:red/json: unsupported protocol scheme")
|
|
|
|
|
|
|
|
}
|