Skip to content

Commit 076ec3b

Browse files
authored
Merge pull request #5878 from thaJeztah/trust_cleans
Assorted cleanups to reduce trust / notary imports
2 parents 124716b + c7072a8 commit 076ec3b

File tree

12 files changed

+87
-137
lines changed

12 files changed

+87
-137
lines changed

cli/command/image/trust.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TrustedPush(ctx context.Context, cli command.Cli, repoInfo *registry.Reposi
4848
func PushTrustedReference(ctx context.Context, ioStreams command.Streams, repoInfo *registry.RepositoryInfo, ref reference.Named, authConfig registrytypes.AuthConfig, in io.Reader) error {
4949
// If it is a trusted push we would like to find the target entry which match the
5050
// tag provided in the function and then do an AddTarget later.
51-
target := &client.Target{}
51+
notaryTarget := &client.Target{}
5252
// Count the times of calling for handleTarget,
5353
// if it is called more that once, that should be considered an error in a trusted push.
5454
cnt := 0
@@ -65,12 +65,12 @@ func PushTrustedReference(ctx context.Context, ioStreams command.Streams, repoIn
6565
if dgst, err := digest.Parse(pushResult.Digest); err == nil {
6666
h, err := hex.DecodeString(dgst.Hex())
6767
if err != nil {
68-
target = nil
68+
notaryTarget = nil
6969
return
7070
}
71-
target.Name = pushResult.Tag
72-
target.Hashes = data.Hashes{string(dgst.Algorithm()): h}
73-
target.Length = int64(pushResult.Size)
71+
notaryTarget.Name = pushResult.Tag
72+
notaryTarget.Hashes = data.Hashes{string(dgst.Algorithm()): h}
73+
notaryTarget.Length = int64(pushResult.Size)
7474
}
7575
}
7676
}
@@ -99,7 +99,7 @@ func PushTrustedReference(ctx context.Context, ioStreams command.Streams, repoIn
9999
return errors.Errorf("internal error: only one call to handleTarget expected")
100100
}
101101

102-
if target == nil {
102+
if notaryTarget == nil {
103103
return errors.Errorf("no targets found, provide a specific tag in order to sign it")
104104
}
105105

@@ -134,10 +134,10 @@ func PushTrustedReference(ctx context.Context, ioStreams command.Streams, repoIn
134134
return trust.NotaryError(repoInfo.Name.Name(), err)
135135
}
136136
_, _ = fmt.Fprintf(ioStreams.Out(), "Finished initializing %q\n", repoInfo.Name.Name())
137-
err = repo.AddTarget(target, data.CanonicalTargetsRole)
137+
err = repo.AddTarget(notaryTarget, data.CanonicalTargetsRole)
138138
case nil:
139139
// already initialized and we have successfully downloaded the latest metadata
140-
err = AddTargetToAllSignableRoles(repo, target)
140+
err = trust.AddToAllSignableRoles(repo, notaryTarget)
141141
default:
142142
return trust.NotaryError(repoInfo.Name.Name(), err)
143143
}
@@ -155,19 +155,6 @@ func PushTrustedReference(ctx context.Context, ioStreams command.Streams, repoIn
155155
return nil
156156
}
157157

158-
// AddTargetToAllSignableRoles attempts to add the image target to all the top level delegation roles we can
159-
// (based on whether we have the signing key and whether the role's path allows
160-
// us to).
161-
// If there are no delegation roles, we add to the targets role.
162-
func AddTargetToAllSignableRoles(repo client.Repository, target *client.Target) error {
163-
signableRoles, err := trust.GetSignableRoles(repo, target)
164-
if err != nil {
165-
return err
166-
}
167-
168-
return repo.AddTarget(target, signableRoles...)
169-
}
170-
171158
// trustedPull handles content trust pulling of an image
172159
func trustedPull(ctx context.Context, cli command.Cli, imgRefAndAuth trust.ImageRefAndAuth, opts PullOptions) error {
173160
refs, err := getTrustedPullTargets(cli, imgRefAndAuth)

cli/command/image/trust_test.go

Lines changed: 0 additions & 57 deletions
This file was deleted.

cli/command/trust/helpers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,9 @@ func getOrGenerateRootKeyAndInitRepo(notaryRepo client.Repository) error {
4747
}
4848
return notaryRepo.Initialize([]string{rootKey.ID()}, data.CanonicalSnapshotRole)
4949
}
50+
51+
const testPass = "password"
52+
53+
func testPassRetriever(string, string, bool, int) (string, bool, error) {
54+
return testPass, false, nil
55+
}

cli/command/trust/helpers_test.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

cli/command/trust/key_generate_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/docker/cli/cli/config"
1212
"github.com/docker/cli/internal/test"
1313
"github.com/theupdateframework/notary"
14-
"github.com/theupdateframework/notary/passphrase"
1514
"github.com/theupdateframework/notary/trustmanager"
1615
tufutils "github.com/theupdateframework/notary/tuf/utils"
1716
"gotest.tools/v3/assert"
@@ -51,11 +50,9 @@ func TestGenerateKeySuccess(t *testing.T) {
5150
pubKeyCWD := t.TempDir()
5251
privKeyStorageDir := t.TempDir()
5352

54-
const testPass = "password"
55-
cannedPasswordRetriever := passphrase.ConstantRetriever(testPass)
5653
// generate a single key
5754
keyName := "alice"
58-
privKeyFileStore, err := trustmanager.NewKeyFileStore(privKeyStorageDir, cannedPasswordRetriever)
55+
privKeyFileStore, err := trustmanager.NewKeyFileStore(privKeyStorageDir, testPassRetriever)
5956
assert.NilError(t, err)
6057

6158
pubKeyPEM, err := generateKeyAndOutputPubPEM(keyName, privKeyFileStore)

cli/command/trust/key_load_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/docker/cli/cli/config"
1313
"github.com/docker/cli/internal/test"
1414
"github.com/theupdateframework/notary"
15-
"github.com/theupdateframework/notary/passphrase"
1615
"github.com/theupdateframework/notary/storage"
1716
"github.com/theupdateframework/notary/trustmanager"
1817
tufutils "github.com/theupdateframework/notary/tuf/utils"
@@ -122,8 +121,6 @@ func TestLoadKeyFromPath(t *testing.T) {
122121

123122
keyStorageDir := t.TempDir()
124123

125-
const passwd = "password"
126-
cannedPasswordRetriever := passphrase.ConstantRetriever(passwd)
127124
keyFileStore, err := storage.NewPrivateKeyFileStorage(keyStorageDir, notary.KeyExtension)
128125
assert.NilError(t, err)
129126
privKeyImporters := []trustmanager.Importer{keyFileStore}
@@ -133,7 +130,7 @@ func TestLoadKeyFromPath(t *testing.T) {
133130
assert.NilError(t, err)
134131

135132
// import the key to our keyStorageDir
136-
assert.Check(t, loadPrivKeyBytesToStore(privKeyBytes, privKeyImporters, privKeyFilepath, "signer-name", cannedPasswordRetriever))
133+
assert.Check(t, loadPrivKeyBytesToStore(privKeyBytes, privKeyImporters, privKeyFilepath, "signer-name", testPassRetriever))
137134

138135
// check that the appropriate ~/<trust_dir>/private/<key_id>.key file exists
139136
expectedImportKeyPath := filepath.Join(keyStorageDir, notary.PrivDir, keyID+"."+notary.KeyExtension)
@@ -151,7 +148,7 @@ func TestLoadKeyFromPath(t *testing.T) {
151148
// assert encrypted header
152149
assert.Check(t, is.Equal("ENCRYPTED PRIVATE KEY", keyPEM.Type))
153150

154-
decryptedKey, err := tufutils.ParsePKCS8ToTufKey(keyPEM.Bytes, []byte(passwd))
151+
decryptedKey, err := tufutils.ParsePKCS8ToTufKey(keyPEM.Bytes, []byte(testPass))
155152
assert.NilError(t, err)
156153
fixturePEM, _ := pem.Decode(keyBytes)
157154
assert.Check(t, is.DeepEqual(fixturePEM.Bytes, decryptedKey.Private()))
@@ -213,8 +210,6 @@ func TestLoadPubKeyFailure(t *testing.T) {
213210
assert.NilError(t, os.WriteFile(pubKeyFilepath, pubKeyFixture, notary.PrivNoExecPerms))
214211
keyStorageDir := t.TempDir()
215212

216-
const passwd = "password"
217-
cannedPasswordRetriever := passphrase.ConstantRetriever(passwd)
218213
keyFileStore, err := storage.NewPrivateKeyFileStorage(keyStorageDir, notary.KeyExtension)
219214
assert.NilError(t, err)
220215
privKeyImporters := []trustmanager.Importer{keyFileStore}
@@ -223,7 +218,7 @@ func TestLoadPubKeyFailure(t *testing.T) {
223218
assert.NilError(t, err)
224219

225220
// import the key to our keyStorageDir - it should fail
226-
err = loadPrivKeyBytesToStore(pubKeyBytes, privKeyImporters, pubKeyFilepath, "signer-name", cannedPasswordRetriever)
221+
err = loadPrivKeyBytesToStore(pubKeyBytes, privKeyImporters, pubKeyFilepath, "signer-name", testPassRetriever)
227222
expected := fmt.Sprintf("provided file %s is not a supported private key - to add a signer's public key use docker trust signer add", pubKeyFilepath)
228223
assert.Error(t, err, expected)
229224
}

cli/command/trust/revoke_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"github.com/docker/cli/internal/test"
1010
"github.com/docker/cli/internal/test/notary"
1111
"github.com/theupdateframework/notary/client"
12-
"github.com/theupdateframework/notary/passphrase"
13-
"github.com/theupdateframework/notary/trustpinning"
1412
"gotest.tools/v3/assert"
1513
is "gotest.tools/v3/assert/cmp"
1614
"gotest.tools/v3/golden"
@@ -151,14 +149,6 @@ func TestTrustRevokeCommand(t *testing.T) {
151149
}
152150
}
153151

154-
func TestGetSignableRolesForTargetAndRemoveError(t *testing.T) {
155-
notaryRepo, err := client.NewFileCachedRepository(t.TempDir(), "gun", "https://localhost", nil, passphrase.ConstantRetriever("password"), trustpinning.TrustPinConfig{})
156-
assert.NilError(t, err)
157-
target := client.Target{}
158-
err = getSignableRolesForTargetAndRemove(target, notaryRepo)
159-
assert.Error(t, err, "client is offline")
160-
}
161-
162152
func TestRevokeTrustPromptTermination(t *testing.T) {
163153
ctx, cancel := context.WithCancel(context.Background())
164154
t.Cleanup(cancel)

cli/command/trust/sign.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func signAndPublishToTarget(out io.Writer, imgRefAndAuth trust.ImageRefAndAuth,
116116
if err != nil {
117117
return err
118118
}
119-
err = image.AddTargetToAllSignableRoles(notaryRepo, &target)
119+
err = trust.AddToAllSignableRoles(notaryRepo, &target)
120120
if err == nil {
121121
prettyPrintExistingSignatureInfo(out, existingSigInfo)
122122
err = notaryRepo.Publish()

cli/command/trust/sign_test.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@ import (
1414
"github.com/theupdateframework/notary"
1515
"github.com/theupdateframework/notary/client"
1616
"github.com/theupdateframework/notary/client/changelist"
17-
"github.com/theupdateframework/notary/passphrase"
1817
"github.com/theupdateframework/notary/trustpinning"
1918
"github.com/theupdateframework/notary/tuf/data"
2019
"gotest.tools/v3/assert"
2120
is "gotest.tools/v3/assert/cmp"
2221
"gotest.tools/v3/skip"
2322
)
2423

25-
const passwd = "password"
26-
2724
func TestTrustSignCommandErrors(t *testing.T) {
2825
testCases := []struct {
2926
name string
@@ -83,7 +80,7 @@ func TestTrustSignCommandOfflineErrors(t *testing.T) {
8380
}
8481

8582
func TestGetOrGenerateNotaryKey(t *testing.T) {
86-
notaryRepo, err := client.NewFileCachedRepository(t.TempDir(), "gun", "https://localhost", nil, passphrase.ConstantRetriever(passwd), trustpinning.TrustPinConfig{})
83+
notaryRepo, err := client.NewFileCachedRepository(t.TempDir(), "gun", "https://localhost", nil, testPassRetriever, trustpinning.TrustPinConfig{})
8784
assert.NilError(t, err)
8885

8986
// repo is empty, try making a root key
@@ -126,7 +123,7 @@ func TestGetOrGenerateNotaryKey(t *testing.T) {
126123
func TestAddStageSigners(t *testing.T) {
127124
skip.If(t, runtime.GOOS == "windows", "FIXME: not supported currently")
128125

129-
notaryRepo, err := client.NewFileCachedRepository(t.TempDir(), "gun", "https://localhost", nil, passphrase.ConstantRetriever(passwd), trustpinning.TrustPinConfig{})
126+
notaryRepo, err := client.NewFileCachedRepository(t.TempDir(), "gun", "https://localhost", nil, testPassRetriever, trustpinning.TrustPinConfig{})
130127
assert.NilError(t, err)
131128

132129
// stage targets/user
@@ -207,7 +204,7 @@ func TestAddStageSigners(t *testing.T) {
207204
}
208205

209206
func TestGetSignedManifestHashAndSize(t *testing.T) {
210-
notaryRepo, err := client.NewFileCachedRepository(t.TempDir(), "gun", "https://localhost", nil, passphrase.ConstantRetriever(passwd), trustpinning.TrustPinConfig{})
207+
notaryRepo, err := client.NewFileCachedRepository(t.TempDir(), "gun", "https://localhost", nil, testPassRetriever, trustpinning.TrustPinConfig{})
211208
assert.NilError(t, err)
212209
_, _, err = getSignedManifestHashAndSize(notaryRepo, "test")
213210
assert.Error(t, err, "client is offline")
@@ -229,7 +226,7 @@ func TestGetReleasedTargetHashAndSize(t *testing.T) {
229226
}
230227

231228
func TestCreateTarget(t *testing.T) {
232-
notaryRepo, err := client.NewFileCachedRepository(t.TempDir(), "gun", "https://localhost", nil, passphrase.ConstantRetriever(passwd), trustpinning.TrustPinConfig{})
229+
notaryRepo, err := client.NewFileCachedRepository(t.TempDir(), "gun", "https://localhost", nil, testPassRetriever, trustpinning.TrustPinConfig{})
233230
assert.NilError(t, err)
234231
_, err = createTarget(notaryRepo, "")
235232
assert.Error(t, err, "no tag specified")
@@ -238,7 +235,7 @@ func TestCreateTarget(t *testing.T) {
238235
}
239236

240237
func TestGetExistingSignatureInfoForReleasedTag(t *testing.T) {
241-
notaryRepo, err := client.NewFileCachedRepository(t.TempDir(), "gun", "https://localhost", nil, passphrase.ConstantRetriever(passwd), trustpinning.TrustPinConfig{})
238+
notaryRepo, err := client.NewFileCachedRepository(t.TempDir(), "gun", "https://localhost", nil, testPassRetriever, trustpinning.TrustPinConfig{})
242239
assert.NilError(t, err)
243240
_, err = getExistingSignatureInfoForReleasedTag(notaryRepo, "test")
244241
assert.Error(t, err, "client is offline")
@@ -267,7 +264,7 @@ func TestSignCommandChangeListIsCleanedOnError(t *testing.T) {
267264
err := cmd.Execute()
268265
assert.Assert(t, err != nil)
269266

270-
notaryRepo, err := client.NewFileCachedRepository(tmpDir, "docker.io/library/ubuntu", "https://localhost", nil, passphrase.ConstantRetriever(passwd), trustpinning.TrustPinConfig{})
267+
notaryRepo, err := client.NewFileCachedRepository(tmpDir, "docker.io/library/ubuntu", "https://localhost", nil, testPassRetriever, trustpinning.TrustPinConfig{})
271268
assert.NilError(t, err)
272269
cl, err := notaryRepo.GetChangelist()
273270
assert.NilError(t, err)

cli/trust/trust.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ var (
4040
ActionsPullOnly = []string{"pull"}
4141
// ActionsPushAndPull defines the actions for read-write interactions with a Notary Repository
4242
ActionsPushAndPull = []string{"pull", "push"}
43-
// NotaryServer is the endpoint serving the Notary trust server
44-
NotaryServer = "https://notary.docker.io"
4543
)
4644

45+
// NotaryServer is the endpoint serving the Notary trust server
46+
const NotaryServer = "https://notary.docker.io"
47+
4748
// GetTrustDirectory returns the base trust directory name
4849
func GetTrustDirectory() string {
4950
return filepath.Join(config.Dir(), "trust")
@@ -238,6 +239,20 @@ func NotaryError(repoName string, err error) error {
238239
return err
239240
}
240241

242+
// AddToAllSignableRoles attempts to add the image target to all the top level
243+
// delegation roles we can (based on whether we have the signing key and whether
244+
// the role's path allows us to).
245+
//
246+
// If there are no delegation roles, we add to the targets role.
247+
func AddToAllSignableRoles(repo client.Repository, target *client.Target) error {
248+
signableRoles, err := GetSignableRoles(repo, target)
249+
if err != nil {
250+
return err
251+
}
252+
253+
return repo.AddTarget(target, signableRoles...)
254+
}
255+
241256
// GetSignableRoles returns a list of roles for which we have valid signing
242257
// keys, given a notary repository and a target
243258
func GetSignableRoles(repo client.Repository, target *client.Target) ([]data.RoleName, error) {

0 commit comments

Comments
 (0)