Skip to content

Commit c775585

Browse files
authored
Merge pull request #5885 from thaJeztah/internalize_notaryclient
cli/command: internalize and deprecate Cli.NotaryClient
2 parents 539f6de + 9bc16bb commit c775585

File tree

8 files changed

+54
-15
lines changed

8 files changed

+54
-15
lines changed

cli/command/cli.go

Lines changed: 1 addition & 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
@@ -67,6 +64,7 @@ type Cli interface {
6764
CurrentContext() string
6865
DockerEndpoint() docker.Endpoint
6966
TelemetryClient
67+
DeprecatedNotaryClient
7068
}
7169

7270
// DockerCli is an instance the docker command line client.
@@ -405,11 +403,6 @@ func (cli *DockerCli) initializeFromClient() {
405403
cli.client.NegotiateAPIVersionPing(ping)
406404
}
407405

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-
413406
// ContextStore returns the ContextStore
414407
func (cli *DockerCli) ContextStore() store.Store {
415408
return cli.contextStore

cli/command/cli_deprecated.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package command
2+
3+
import (
4+
"github.com/docker/cli/cli/trust"
5+
notaryclient "github.com/theupdateframework/notary/client"
6+
)
7+
8+
type DeprecatedNotaryClient interface {
9+
// NotaryClient provides a Notary Repository to interact with signed metadata for an image
10+
//
11+
// Deprecated: use [trust.GetNotaryRepository] instead. This method is no longer used and will be removed in the next release.
12+
NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error)
13+
}
14+
15+
// NotaryClient provides a Notary Repository to interact with signed metadata for an image
16+
func (cli *DockerCli) NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error) {
17+
return trust.GetNotaryRepository(cli.In(), cli.Out(), UserAgent(), imgRefAndAuth.RepoInfo(), imgRefAndAuth.AuthConfig(), actions...)
18+
}

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)
@@ -200,7 +214,7 @@ func trustedPull(ctx context.Context, cli command.Cli, imgRefAndAuth trust.Image
200214
}
201215

202216
func getTrustedPullTargets(cli command.Cli, imgRefAndAuth trust.ImageRefAndAuth) ([]target, error) {
203-
notaryRepo, err := cli.NotaryClient(imgRefAndAuth, trust.ActionsPullOnly)
217+
notaryRepo, err := newNotaryClient(cli, imgRefAndAuth)
204218
if err != nil {
205219
return nil, errors.Wrap(err, "error establishing connection to trust repository")
206220
}
@@ -280,7 +294,7 @@ func TrustedReference(ctx context.Context, cli command.Cli, ref reference.NamedT
280294
return nil, err
281295
}
282296

283-
notaryRepo, err := cli.NotaryClient(imgRefAndAuth, []string{"pull"})
297+
notaryRepo, err := newNotaryClient(cli, imgRefAndAuth)
284298
if err != nil {
285299
return nil, errors.Wrap(err, "error establishing connection to trust repository")
286300
}

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)