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"
|
2018-02-27 10:54:36 -05:00
|
|
|
"runtime"
|
2017-08-24 18:45:20 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/config"
|
|
|
|
"github.com/docker/cli/cli/trust"
|
|
|
|
"github.com/docker/cli/internal/test"
|
2018-03-08 08:35:17 -05:00
|
|
|
notaryfake "github.com/docker/cli/internal/test/notary"
|
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"
|
2018-06-08 12:24:26 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
|
|
|
"gotest.tools/skip"
|
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-")
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
config.SetDir(tmpDir)
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newSignCommand(
|
|
|
|
test.NewFakeCli(&fakeClient{}))
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-08-24 18:45:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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{})
|
2018-03-08 08:35:17 -05:00
|
|
|
cli.SetNotaryClient(notaryfake.GetOfflineNotaryRepository)
|
2017-09-13 12:50:37 -04:00
|
|
|
cmd := newSignCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"reg-name.io/image:tag"})
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2017-12-21 16:27:57 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), "client is offline")
|
2017-09-13 12:50:37 -04:00
|
|
|
}
|
|
|
|
|
2017-08-24 18:45:20 -04:00
|
|
|
func TestGetOrGenerateNotaryKey(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "notary-test-")
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
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{})
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
|
|
|
|
// repo is empty, try making a root key
|
|
|
|
rootKeyA, err := getOrGenerateNotaryKey(notaryRepo, data.CanonicalRootRole)
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, rootKeyA != nil)
|
2017-08-24 18:45:20 -04:00
|
|
|
|
|
|
|
// we should only have one newly generated key
|
2017-09-11 17:07:00 -04:00
|
|
|
allKeys := notaryRepo.GetCryptoService().ListAllKeys()
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Len(allKeys, 1))
|
|
|
|
assert.Check(t, notaryRepo.GetCryptoService().GetKey(rootKeyA.ID()) != nil)
|
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)
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, rootKeyB != nil)
|
2017-08-24 18:45:20 -04:00
|
|
|
|
|
|
|
// we should only have one newly generated key
|
2017-09-11 17:07:00 -04:00
|
|
|
allKeys = notaryRepo.GetCryptoService().ListAllKeys()
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Len(allKeys, 1))
|
|
|
|
assert.Check(t, notaryRepo.GetCryptoService().GetKey(rootKeyB.ID()) != nil)
|
2017-08-24 18:45:20 -04:00
|
|
|
|
|
|
|
// The key we retrieved should be identical to the one we generated
|
2017-12-21 16:27:57 -05:00
|
|
|
assert.Check(t, is.DeepEqual(rootKeyA.Public(), rootKeyB.Public()))
|
2017-08-24 18:45:20 -04:00
|
|
|
|
|
|
|
// Now also try with a delegation key
|
|
|
|
releasesKey, err := getOrGenerateNotaryKey(notaryRepo, data.RoleName(trust.ReleasesRole))
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, releasesKey != nil)
|
2017-08-24 18:45:20 -04:00
|
|
|
|
|
|
|
// we should now have two keys
|
2017-09-11 17:07:00 -04:00
|
|
|
allKeys = notaryRepo.GetCryptoService().ListAllKeys()
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Len(allKeys, 2))
|
|
|
|
assert.Check(t, notaryRepo.GetCryptoService().GetKey(releasesKey.ID()) != nil)
|
2017-08-24 18:45:20 -04:00
|
|
|
// The key we retrieved should be identical to the one we generated
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, releasesKey != rootKeyA)
|
|
|
|
assert.Check(t, releasesKey != rootKeyB)
|
2017-08-24 18:45:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddStageSigners(t *testing.T) {
|
2018-02-27 10:54:36 -05:00
|
|
|
skip.If(t, runtime.GOOS == "windows", "FIXME: not supported currently")
|
2017-08-24 18:45:20 -04:00
|
|
|
tmpDir, err := ioutil.TempDir("", "notary-test-")
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
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{})
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
|
|
|
|
// 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})
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(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()
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
changeList := cl.List()
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Len(changeList, 4))
|
2018-09-07 01:25:58 -04:00
|
|
|
// ordering is deterministic:
|
2017-08-24 18:45:20 -04:00
|
|
|
|
|
|
|
// 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}),
|
|
|
|
})
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
expectedChange := changelist.NewTUFChange(
|
|
|
|
changelist.ActionCreate,
|
|
|
|
userRole,
|
|
|
|
changelist.TypeTargetsDelegation,
|
|
|
|
"", // no path for delegations
|
|
|
|
expectedJSON,
|
|
|
|
)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(expectedChange, newSignerKeyChange))
|
2017-08-24 18:45:20 -04:00
|
|
|
|
|
|
|
// second change is for targets/user getting all paths
|
|
|
|
newSignerPathsChange := changeList[1]
|
|
|
|
expectedJSON, err = json.Marshal(&changelist.TUFDelegation{
|
|
|
|
AddPaths: []string{""},
|
|
|
|
})
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
expectedChange = changelist.NewTUFChange(
|
|
|
|
changelist.ActionCreate,
|
|
|
|
userRole,
|
|
|
|
changelist.TypeTargetsDelegation,
|
|
|
|
"", // no path for delegations
|
|
|
|
expectedJSON,
|
|
|
|
)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(expectedChange, newSignerPathsChange))
|
2017-08-24 18:45:20 -04:00
|
|
|
|
|
|
|
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}),
|
|
|
|
})
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
expectedChange = changelist.NewTUFChange(
|
|
|
|
changelist.ActionCreate,
|
|
|
|
releasesRole,
|
|
|
|
changelist.TypeTargetsDelegation,
|
|
|
|
"", // no path for delegations
|
|
|
|
expectedJSON,
|
|
|
|
)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(expectedChange, releasesKeyChange))
|
2017-08-24 18:45:20 -04:00
|
|
|
|
|
|
|
// fourth change is for targets/releases getting all paths
|
|
|
|
releasesPathsChange := changeList[3]
|
|
|
|
expectedJSON, err = json.Marshal(&changelist.TUFDelegation{
|
|
|
|
AddPaths: []string{""},
|
|
|
|
})
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
expectedChange = changelist.NewTUFChange(
|
|
|
|
changelist.ActionCreate,
|
|
|
|
releasesRole,
|
|
|
|
changelist.TypeTargetsDelegation,
|
|
|
|
"", // no path for delegations
|
|
|
|
expectedJSON,
|
|
|
|
)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(expectedChange, releasesPathsChange))
|
2017-08-24 18:45:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSignedManifestHashAndSize(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "notary-test-")
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
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{})
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
target := &client.Target{}
|
|
|
|
target.Hashes, target.Length, err = getSignedManifestHashAndSize(notaryRepo, "test")
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "client is offline")
|
2017-08-24 18:45:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "No valid trust data for unreleased")
|
2017-08-24 18:45:20 -04:00
|
|
|
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")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.DeepEqual(data.Hashes{notary.SHA256: []byte("released-hash")}, hash))
|
2017-08-24 18:45:20 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateTarget(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "notary-test-")
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
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{})
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
_, err = createTarget(notaryRepo, "")
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "No tag specified")
|
2017-08-24 18:45:20 -04:00
|
|
|
_, err = createTarget(notaryRepo, "1")
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "client is offline")
|
2017-08-24 18:45:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetExistingSignatureInfoForReleasedTag(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "notary-test-")
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
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{})
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
_, err = getExistingSignatureInfoForReleasedTag(notaryRepo, "test")
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "client is offline")
|
2017-08-24 18:45:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Contains(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-")
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
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{})
|
2018-03-08 08:35:17 -05:00
|
|
|
cli.SetNotaryClient(notaryfake.GetLoadedNotaryRepository)
|
2017-09-26 12:33:35 -04:00
|
|
|
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()
|
2018-03-06 15:49:00 -05:00
|
|
|
assert.Assert(t, err != nil)
|
2017-09-26 12:33:35 -04:00
|
|
|
|
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{})
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-24 18:45:20 -04:00
|
|
|
cl, err := notaryRepo.GetChangelist()
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(len(cl.List()), 0))
|
2017-08-24 18:45:20 -04:00
|
|
|
}
|
2017-09-29 12:51:45 -04:00
|
|
|
|
|
|
|
func TestSignCommandLocalFlag(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
2018-03-08 08:35:17 -05:00
|
|
|
cli.SetNotaryClient(notaryfake.GetEmptyTargetsNotaryRepository)
|
2017-09-29 12:51:45 -04:00
|
|
|
cmd := newSignCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"--local", "reg-name.io/image:red"})
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), "error during connect: Get /images/reg-name.io/image:red/json: unsupported protocol scheme")
|
2017-09-29 12:51:45 -04:00
|
|
|
|
|
|
|
}
|