Skip to content

Commit 6dc0a08

Browse files
committed
cli/command: remove NotaryClient from CLI interface
This method was a shallow wrapper around trust.GetNotaryRepository, but due to its signature resulted in the trust package, and notary dependencies to become a dependency of the CLI. Consequence of this was that cli-plugins, which need the cli/command package, would also get notary and its dependencies as a dependency. Thie patch: - Removes the NotaryClient method from the interface - Inlines the code where needed, skipping the wrapper - Define a local interface for some tests where a dummy notary client was used. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent e038e15 commit 6dc0a08

File tree

7 files changed

+35
-15
lines changed

7 files changed

+35
-15
lines changed

cli/command/cli.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
manifeststore "github.com/docker/cli/cli/manifest/store"
2626
registryclient "github.com/docker/cli/cli/registry/client"
2727
"github.com/docker/cli/cli/streams"
28-
"github.com/docker/cli/cli/trust"
2928
"github.com/docker/cli/cli/version"
3029
dopts "github.com/docker/cli/opts"
3130
"github.com/docker/docker/api"
@@ -36,7 +35,6 @@ import (
3635
"github.com/docker/go-connections/tlsconfig"
3736
"github.com/pkg/errors"
3837
"github.com/spf13/cobra"
39-
notaryclient "github.com/theupdateframework/notary/client"
4038
)
4139

4240
const defaultInitTimeout = 2 * time.Second
@@ -56,7 +54,6 @@ type Cli interface {
5654
Apply(ops ...CLIOption) error
5755
ConfigFile() *configfile.ConfigFile
5856
ServerInfo() ServerInfo
59-
NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error)
6057
DefaultVersion() string
6158
CurrentVersion() string
6259
ManifestStore() manifeststore.Store
@@ -405,11 +402,6 @@ func (cli *DockerCli) initializeFromClient() {
405402
cli.client.NegotiateAPIVersionPing(ping)
406403
}
407404

408-
// NotaryClient provides a Notary Repository to interact with signed metadata for an image
409-
func (cli *DockerCli) NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error) {
410-
return trust.GetNotaryRepository(cli.In(), cli.Out(), UserAgent(), imgRefAndAuth.RepoInfo(), imgRefAndAuth.AuthConfig(), actions...)
411-
}
412-
413405
// ContextStore returns the ContextStore
414406
func (cli *DockerCli) ContextStore() store.Store {
415407
return cli.contextStore

cli/command/image/trust.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ type target struct {
3030
size int64
3131
}
3232

33+
// notaryClientProvider is used in tests to provide a dummy notary client.
34+
type notaryClientProvider interface {
35+
NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (client.Repository, error)
36+
}
37+
38+
// newNotaryClient provides a Notary Repository to interact with signed metadata for an image.
39+
func newNotaryClient(cli command.Streams, imgRefAndAuth trust.ImageRefAndAuth) (client.Repository, error) {
40+
if ncp, ok := cli.(notaryClientProvider); ok {
41+
// notaryClientProvider is used in tests to provide a dummy notary client.
42+
return ncp.NotaryClient(imgRefAndAuth, []string{"pull"})
43+
}
44+
return trust.GetNotaryRepository(cli.In(), cli.Out(), command.UserAgent(), imgRefAndAuth.RepoInfo(), imgRefAndAuth.AuthConfig(), "pull")
45+
}
46+
3347
// TrustedPush handles content trust pushing of an image
3448
func TrustedPush(ctx context.Context, cli command.Cli, repoInfo *registry.RepositoryInfo, ref reference.Named, authConfig registrytypes.AuthConfig, options image.PushOptions) error {
3549
responseBody, err := cli.Client().ImagePush(ctx, reference.FamiliarString(ref), options)
@@ -213,7 +227,7 @@ func trustedPull(ctx context.Context, cli command.Cli, imgRefAndAuth trust.Image
213227
}
214228

215229
func getTrustedPullTargets(cli command.Cli, imgRefAndAuth trust.ImageRefAndAuth) ([]target, error) {
216-
notaryRepo, err := cli.NotaryClient(imgRefAndAuth, trust.ActionsPullOnly)
230+
notaryRepo, err := newNotaryClient(cli, imgRefAndAuth)
217231
if err != nil {
218232
return nil, errors.Wrap(err, "error establishing connection to trust repository")
219233
}
@@ -293,7 +307,7 @@ func TrustedReference(ctx context.Context, cli command.Cli, ref reference.NamedT
293307
return nil, err
294308
}
295309

296-
notaryRepo, err := cli.NotaryClient(imgRefAndAuth, []string{"pull"})
310+
notaryRepo, err := newNotaryClient(cli, imgRefAndAuth)
297311
if err != nil {
298312
return nil, errors.Wrap(err, "error establishing connection to trust repository")
299313
}

cli/command/trust/common.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ type trustKey struct {
4949
ID string `json:",omitempty"`
5050
}
5151

52+
// notaryClientProvider is used in tests to provide a dummy notary client.
53+
type notaryClientProvider interface {
54+
NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (client.Repository, error)
55+
}
56+
57+
// newNotaryClient provides a Notary Repository to interact with signed metadata for an image.
58+
func newNotaryClient(cli command.Streams, imgRefAndAuth trust.ImageRefAndAuth, actions []string) (client.Repository, error) {
59+
if ncp, ok := cli.(notaryClientProvider); ok {
60+
// notaryClientProvider is used in tests to provide a dummy notary client.
61+
return ncp.NotaryClient(imgRefAndAuth, actions)
62+
}
63+
return trust.GetNotaryRepository(cli.In(), cli.Out(), command.UserAgent(), imgRefAndAuth.RepoInfo(), imgRefAndAuth.AuthConfig(), actions...)
64+
}
65+
5266
// lookupTrustInfo returns processed signature and role information about a notary repository.
5367
// This information is to be pretty printed or serialized into a machine-readable format.
5468
func lookupTrustInfo(ctx context.Context, cli command.Cli, remote string) ([]trustTagRow, []client.RoleWithSignatures, []data.Role, error) {
@@ -57,7 +71,7 @@ func lookupTrustInfo(ctx context.Context, cli command.Cli, remote string) ([]tru
5771
return []trustTagRow{}, []client.RoleWithSignatures{}, []data.Role{}, err
5872
}
5973
tag := imgRefAndAuth.Tag()
60-
notaryRepo, err := cli.NotaryClient(imgRefAndAuth, trust.ActionsPullOnly)
74+
notaryRepo, err := newNotaryClient(cli, imgRefAndAuth, trust.ActionsPullOnly)
6175
if err != nil {
6276
return []trustTagRow{}, []client.RoleWithSignatures{}, []data.Role{}, trust.NotaryError(imgRefAndAuth.Reference().Name(), err)
6377
}

cli/command/trust/revoke.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func revokeTrust(ctx context.Context, dockerCLI command.Cli, remote string, opti
5353
}
5454
}
5555

56-
notaryRepo, err := dockerCLI.NotaryClient(imgRefAndAuth, trust.ActionsPushAndPull)
56+
notaryRepo, err := newNotaryClient(dockerCLI, imgRefAndAuth, trust.ActionsPushAndPull)
5757
if err != nil {
5858
return err
5959
}

cli/command/trust/sign.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func runSignImage(ctx context.Context, dockerCLI command.Cli, options signOption
5252
return err
5353
}
5454

55-
notaryRepo, err := dockerCLI.NotaryClient(imgRefAndAuth, trust.ActionsPushAndPull)
55+
notaryRepo, err := newNotaryClient(dockerCLI, imgRefAndAuth, trust.ActionsPushAndPull)
5656
if err != nil {
5757
return trust.NotaryError(imgRefAndAuth.Reference().Name(), err)
5858
}

cli/command/trust/signer_add.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func addSignerToRepo(ctx context.Context, dockerCLI command.Cli, signerName stri
8585
return err
8686
}
8787

88-
notaryRepo, err := dockerCLI.NotaryClient(imgRefAndAuth, trust.ActionsPushAndPull)
88+
notaryRepo, err := newNotaryClient(dockerCLI, imgRefAndAuth, trust.ActionsPushAndPull)
8989
if err != nil {
9090
return trust.NotaryError(imgRefAndAuth.Reference().Name(), err)
9191
}

cli/command/trust/signer_remove.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func removeSingleSigner(ctx context.Context, dockerCLI command.Cli, repoName, si
103103
if signerDelegation == releasesRoleTUFName {
104104
return false, errors.Errorf("releases is a reserved keyword and cannot be removed")
105105
}
106-
notaryRepo, err := dockerCLI.NotaryClient(imgRefAndAuth, trust.ActionsPushAndPull)
106+
notaryRepo, err := newNotaryClient(dockerCLI, imgRefAndAuth, trust.ActionsPushAndPull)
107107
if err != nil {
108108
return false, trust.NotaryError(imgRefAndAuth.Reference().Name(), err)
109109
}

0 commit comments

Comments
 (0)