Skip to content

Commit 1b467f9

Browse files
Merge pull request #6512 from thaJeztah/less_trust
remove some uses of trust-specific types
2 parents 75f3c08 + 0dec83f commit 1b467f9

File tree

9 files changed

+63
-66
lines changed

9 files changed

+63
-66
lines changed

cli/command/image/pull.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"io"
78

89
"github.com/distribution/reference"
910
"github.com/docker/cli/cli"
1011
"github.com/docker/cli/cli/command"
1112
"github.com/docker/cli/cli/command/completion"
13+
"github.com/docker/cli/cli/streams"
1214
"github.com/docker/cli/cli/trust"
15+
"github.com/docker/cli/internal/jsonstream"
16+
"github.com/moby/moby/api/pkg/authconfig"
17+
registrytypes "github.com/moby/moby/api/types/registry"
18+
"github.com/moby/moby/client"
1319
"github.com/spf13/cobra"
1420
)
1521

@@ -84,10 +90,34 @@ func runPull(ctx context.Context, dockerCLI command.Cli, opts pullOptions) error
8490
return err
8591
}
8692
} else {
87-
if err := imagePullPrivileged(ctx, dockerCLI, imgRefAndAuth, opts); err != nil {
93+
if err := imagePullPrivileged(ctx, dockerCLI, imgRefAndAuth.Reference(), imgRefAndAuth.AuthConfig(), opts); err != nil {
8894
return err
8995
}
9096
}
9197
_, _ = fmt.Fprintln(dockerCLI.Out(), imgRefAndAuth.Reference().String())
9298
return nil
9399
}
100+
101+
// imagePullPrivileged pulls the image and displays it to the output
102+
func imagePullPrivileged(ctx context.Context, dockerCLI command.Cli, ref reference.Named, authConfig *registrytypes.AuthConfig, opts pullOptions) error {
103+
encodedAuth, err := authconfig.Encode(*authConfig)
104+
if err != nil {
105+
return err
106+
}
107+
responseBody, err := dockerCLI.Client().ImagePull(ctx, reference.FamiliarString(ref), client.ImagePullOptions{
108+
RegistryAuth: encodedAuth,
109+
PrivilegeFunc: nil,
110+
All: opts.all,
111+
Platform: opts.platform,
112+
})
113+
if err != nil {
114+
return err
115+
}
116+
defer responseBody.Close()
117+
118+
out := dockerCLI.Out()
119+
if opts.quiet {
120+
out = streams.NewOut(io.Discard)
121+
}
122+
return jsonstream.Display(ctx, responseBody, out)
123+
}

cli/command/image/trust.go

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@ import (
1010
"github.com/distribution/reference"
1111
"github.com/docker/cli/cli/command"
1212
"github.com/docker/cli/cli/config"
13-
"github.com/docker/cli/cli/streams"
1413
"github.com/docker/cli/cli/trust"
15-
"github.com/docker/cli/internal/jsonstream"
1614
"github.com/docker/cli/internal/registry"
17-
"github.com/moby/moby/api/pkg/authconfig"
1815
registrytypes "github.com/moby/moby/api/types/registry"
19-
"github.com/moby/moby/client"
2016
"github.com/opencontainers/go-digest"
2117
"github.com/sirupsen/logrus"
2218
notaryclient "github.com/theupdateframework/notary/client"
@@ -31,16 +27,16 @@ type target struct {
3127

3228
// notaryClientProvider is used in tests to provide a dummy notary client.
3329
type notaryClientProvider interface {
34-
NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error)
30+
NotaryClient() (notaryclient.Repository, error)
3531
}
3632

3733
// newNotaryClient provides a Notary Repository to interact with signed metadata for an image.
38-
func newNotaryClient(cli command.Streams, imgRefAndAuth trust.ImageRefAndAuth) (notaryclient.Repository, error) {
34+
func newNotaryClient(cli command.Streams, repoInfo *trust.RepositoryInfo, authConfig *registrytypes.AuthConfig) (notaryclient.Repository, error) {
3935
if ncp, ok := cli.(notaryClientProvider); ok {
4036
// notaryClientProvider is used in tests to provide a dummy notary client.
41-
return ncp.NotaryClient(imgRefAndAuth, []string{"pull"})
37+
return ncp.NotaryClient()
4238
}
43-
return trust.GetNotaryRepository(cli.In(), cli.Out(), command.UserAgent(), imgRefAndAuth.RepoInfo(), imgRefAndAuth.AuthConfig(), "pull")
39+
return trust.GetNotaryRepository(cli.In(), cli.Out(), command.UserAgent(), repoInfo, authConfig, "pull")
4440
}
4541

4642
// pushTrustedReference pushes a canonical reference to the trust server.
@@ -81,7 +77,7 @@ func trustedPull(ctx context.Context, cli command.Cli, imgRefAndAuth trust.Image
8177
if err != nil {
8278
return err
8379
}
84-
if err := imagePullPrivileged(ctx, cli, updatedImgRefAndAuth, pullOptions{
80+
if err := imagePullPrivileged(ctx, cli, updatedImgRefAndAuth.Reference(), updatedImgRefAndAuth.AuthConfig(), pullOptions{
8581
all: false,
8682
platform: opts.platform,
8783
quiet: opts.quiet,
@@ -107,7 +103,7 @@ func trustedPull(ctx context.Context, cli command.Cli, imgRefAndAuth trust.Image
107103
}
108104

109105
func getTrustedPullTargets(cli command.Cli, imgRefAndAuth trust.ImageRefAndAuth) ([]target, error) {
110-
notaryRepo, err := newNotaryClient(cli, imgRefAndAuth)
106+
notaryRepo, err := newNotaryClient(cli, imgRefAndAuth.RepoInfo(), imgRefAndAuth.AuthConfig())
111107
if err != nil {
112108
return nil, fmt.Errorf("error establishing connection to trust repository: %w", err)
113109
}
@@ -155,38 +151,14 @@ func getTrustedPullTargets(cli command.Cli, imgRefAndAuth trust.ImageRefAndAuth)
155151
return []target{r}, err
156152
}
157153

158-
// imagePullPrivileged pulls the image and displays it to the output
159-
func imagePullPrivileged(ctx context.Context, cli command.Cli, imgRefAndAuth trust.ImageRefAndAuth, opts pullOptions) error {
160-
encodedAuth, err := authconfig.Encode(*imgRefAndAuth.AuthConfig())
161-
if err != nil {
162-
return err
163-
}
164-
responseBody, err := cli.Client().ImagePull(ctx, reference.FamiliarString(imgRefAndAuth.Reference()), client.ImagePullOptions{
165-
RegistryAuth: encodedAuth,
166-
PrivilegeFunc: nil,
167-
All: opts.all,
168-
Platform: opts.platform,
169-
})
170-
if err != nil {
171-
return err
172-
}
173-
defer responseBody.Close()
174-
175-
out := cli.Out()
176-
if opts.quiet {
177-
out = streams.NewOut(io.Discard)
178-
}
179-
return jsonstream.Display(ctx, responseBody, out)
180-
}
181-
182154
// TrustedReference returns the canonical trusted reference for an image reference
183155
func TrustedReference(ctx context.Context, cli command.Cli, ref reference.NamedTagged) (reference.Canonical, error) {
184156
imgRefAndAuth, err := trust.GetImageReferencesAndAuth(ctx, authResolver(cli), ref.String())
185157
if err != nil {
186158
return nil, err
187159
}
188160

189-
notaryRepo, err := newNotaryClient(cli, imgRefAndAuth)
161+
notaryRepo, err := newNotaryClient(cli, imgRefAndAuth.RepoInfo(), imgRefAndAuth.AuthConfig())
190162
if err != nil {
191163
return nil, fmt.Errorf("error establishing connection to trust repository: %w", err)
192164
}

cli/command/trust/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ type trustKey struct {
5252

5353
// notaryClientProvider is used in tests to provide a dummy notary client.
5454
type notaryClientProvider interface {
55-
NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (client.Repository, error)
55+
NotaryClient() (client.Repository, error)
5656
}
5757

5858
// newNotaryClient provides a Notary Repository to interact with signed metadata for an image.
5959
func newNotaryClient(cli command.Streams, imgRefAndAuth trust.ImageRefAndAuth, actions []string) (client.Repository, error) {
6060
if ncp, ok := cli.(notaryClientProvider); ok {
6161
// notaryClientProvider is used in tests to provide a dummy notary client.
62-
return ncp.NotaryClient(imgRefAndAuth, actions)
62+
return ncp.NotaryClient()
6363
}
6464
return trust.GetNotaryRepository(cli.In(), cli.Out(), command.UserAgent(), imgRefAndAuth.RepoInfo(), imgRefAndAuth.AuthConfig(), actions...)
6565
}

cli/command/trust/inspect_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"io"
55
"testing"
66

7-
"github.com/docker/cli/cli/trust"
87
"github.com/docker/cli/internal/test"
98
"github.com/docker/cli/internal/test/notary"
109
"github.com/theupdateframework/notary/client"
@@ -48,7 +47,7 @@ func TestTrustInspectCommandRepositoryErrors(t *testing.T) {
4847
testCases := []struct {
4948
doc string
5049
args []string
51-
notaryRepository func(trust.ImageRefAndAuth, []string) (client.Repository, error)
50+
notaryRepository func() (client.Repository, error)
5251
err string
5352
golden string
5453
}{
@@ -100,7 +99,7 @@ func TestTrustInspectCommand(t *testing.T) {
10099
testCases := []struct {
101100
doc string
102101
args []string
103-
notaryRepository func(trust.ImageRefAndAuth, []string) (client.Repository, error)
102+
notaryRepository func() (client.Repository, error)
104103
golden string
105104
}{
106105
{

cli/command/trust/revoke_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"io"
66
"testing"
77

8-
"github.com/docker/cli/cli/trust"
98
"github.com/docker/cli/internal/test"
109
"github.com/docker/cli/internal/test/notary"
1110
"github.com/theupdateframework/notary/client"
@@ -60,7 +59,7 @@ func TestTrustRevokeCommand(t *testing.T) {
6059

6160
testCases := []struct {
6261
doc string
63-
notaryRepository func(trust.ImageRefAndAuth, []string) (client.Repository, error)
62+
notaryRepository func() (client.Repository, error)
6463
args []string
6564
expectedErr string
6665
expectedMessage string

cli/trust/trust.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func certificateDirectory(server string) (string, error) {
7979
}
8080

8181
// Server returns the base URL for the trust server.
82-
func Server(index *registrytypes.IndexInfo) (string, error) {
82+
func Server(indexName string) (string, error) {
8383
if s := os.Getenv("DOCKER_CONTENT_TRUST_SERVER"); s != "" {
8484
urlObj, err := url.Parse(s)
8585
if err != nil || urlObj.Scheme != "https" {
@@ -88,10 +88,10 @@ func Server(index *registrytypes.IndexInfo) (string, error) {
8888

8989
return s, nil
9090
}
91-
if index.Official {
91+
if indexName == "docker.io" || indexName == "index.docker.io" {
9292
return NotaryServer, nil
9393
}
94-
return "https://" + index.Name, nil
94+
return "https://" + indexName, nil
9595
}
9696

9797
type simpleCredentialStore struct {
@@ -117,7 +117,7 @@ const dctDeprecation = `WARNING: Docker is retiring DCT for Docker Official Imag
117117
// information needed to operate on a notary repository.
118118
// It creates an HTTP transport providing authentication support.
119119
func GetNotaryRepository(in io.Reader, out io.Writer, userAgent string, repoInfo *RepositoryInfo, authConfig *registrytypes.AuthConfig, actions ...string) (client.Repository, error) {
120-
server, err := Server(repoInfo.Index)
120+
server, err := Server(repoInfo.Index.Name)
121121
if err != nil {
122122
return nil, err
123123
}

cli/trust/trust_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"testing"
55

66
"github.com/distribution/reference"
7-
registrytypes "github.com/moby/moby/api/types/registry"
87
"github.com/opencontainers/go-digest"
98
"github.com/theupdateframework/notary/client"
109
"github.com/theupdateframework/notary/trustpinning"
@@ -56,32 +55,32 @@ func TestGetSignableRolesError(t *testing.T) {
5655

5756
func TestENVTrustServer(t *testing.T) {
5857
t.Setenv("DOCKER_CONTENT_TRUST_SERVER", "https://notary-test.example.com:5000")
59-
indexInfo := &registrytypes.IndexInfo{Name: "testserver"}
60-
output, err := Server(indexInfo)
58+
output, err := Server("testserver")
6159
const expected = "https://notary-test.example.com:5000"
6260
assert.NilError(t, err)
6361
assert.Equal(t, output, expected)
6462
}
6563

6664
func TestHTTPENVTrustServer(t *testing.T) {
6765
t.Setenv("DOCKER_CONTENT_TRUST_SERVER", "http://notary-test.example.com:5000")
68-
indexInfo := &registrytypes.IndexInfo{Name: "testserver"}
69-
_, err := Server(indexInfo)
66+
_, err := Server("testserver")
7067
const expected = "valid https URL required for trust server"
7168
assert.ErrorContains(t, err, expected, "Expected error with invalid scheme")
7269
}
7370

7471
func TestOfficialTrustServer(t *testing.T) {
75-
indexInfo := &registrytypes.IndexInfo{Name: "testserver", Official: true}
76-
output, err := Server(indexInfo)
72+
output, err := Server("docker.io")
7773
const expected = NotaryServer
7874
assert.NilError(t, err)
7975
assert.Equal(t, output, expected)
76+
77+
output, err = Server("index.docker.io")
78+
assert.NilError(t, err)
79+
assert.Equal(t, output, expected)
8080
}
8181

8282
func TestNonOfficialTrustServer(t *testing.T) {
83-
indexInfo := &registrytypes.IndexInfo{Name: "testserver", Official: false}
84-
output, err := Server(indexInfo)
83+
output, err := Server("testserver")
8584
const expected = "https://testserver"
8685
assert.NilError(t, err)
8786
assert.Equal(t, output, expected)

internal/test/cli.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ import (
1212
"github.com/docker/cli/cli/context/store"
1313
manifeststore "github.com/docker/cli/cli/manifest/store"
1414
"github.com/docker/cli/cli/streams"
15-
"github.com/docker/cli/cli/trust"
1615
"github.com/docker/cli/internal/registryclient"
1716
"github.com/moby/moby/client"
1817
notaryclient "github.com/theupdateframework/notary/client"
1918
)
2019

2120
// NotaryClientFuncType defines a function that returns a fake notary client
22-
type NotaryClientFuncType func(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error)
21+
type NotaryClientFuncType func() (notaryclient.Repository, error)
2322

2423
// FakeCli emulates the default DockerCli
2524
type FakeCli struct {
@@ -169,9 +168,9 @@ func (c *FakeCli) SetNotaryClient(notaryClientFunc NotaryClientFuncType) {
169168
}
170169

171170
// NotaryClient returns an err for testing unless defined
172-
func (c *FakeCli) NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error) {
171+
func (c *FakeCli) NotaryClient() (notaryclient.Repository, error) {
173172
if c.notaryClientFunc != nil {
174-
return c.notaryClientFunc(imgRefAndAuth, actions)
173+
return c.notaryClientFunc()
175174
}
176175
return nil, errors.New("no notary client available unless defined")
177176
}

internal/test/notary/client.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package notary
22

33
import (
4-
"github.com/docker/cli/cli/trust"
54
"github.com/theupdateframework/notary/client"
65
"github.com/theupdateframework/notary/client/changelist"
76
"github.com/theupdateframework/notary/cryptoservice"
@@ -12,7 +11,7 @@ import (
1211
)
1312

1413
// GetOfflineNotaryRepository returns a OfflineNotaryRepository
15-
func GetOfflineNotaryRepository(trust.ImageRefAndAuth, []string) (client.Repository, error) {
14+
func GetOfflineNotaryRepository() (client.Repository, error) {
1615
return OfflineNotaryRepository{}, nil
1716
}
1817

@@ -146,7 +145,7 @@ func (OfflineNotaryRepository) GetGUN() data.GUN {
146145
}
147146

148147
// GetUninitializedNotaryRepository returns an UninitializedNotaryRepository
149-
func GetUninitializedNotaryRepository(trust.ImageRefAndAuth, []string) (client.Repository, error) {
148+
func GetUninitializedNotaryRepository() (client.Repository, error) {
150149
return UninitializedNotaryRepository{}, nil
151150
}
152151

@@ -207,7 +206,7 @@ func (UninitializedNotaryRepository) RotateKey(data.RoleName, bool, []string) er
207206
}
208207

209208
// GetEmptyTargetsNotaryRepository returns an EmptyTargetsNotaryRepository
210-
func GetEmptyTargetsNotaryRepository(trust.ImageRefAndAuth, []string) (client.Repository, error) {
209+
func GetEmptyTargetsNotaryRepository() (client.Repository, error) {
211210
return EmptyTargetsNotaryRepository{}, nil
212211
}
213212

@@ -285,7 +284,7 @@ func (EmptyTargetsNotaryRepository) RotateKey(data.RoleName, bool, []string) err
285284
}
286285

287286
// GetLoadedNotaryRepository returns a LoadedNotaryRepository
288-
func GetLoadedNotaryRepository(trust.ImageRefAndAuth, []string) (client.Repository, error) {
287+
func GetLoadedNotaryRepository() (client.Repository, error) {
289288
return LoadedNotaryRepository{}, nil
290289
}
291290

@@ -511,7 +510,7 @@ func (l LoadedNotaryRepository) GetCryptoService() signed.CryptoService {
511510
}
512511

513512
// GetLoadedWithNoSignersNotaryRepository returns a LoadedWithNoSignersNotaryRepository
514-
func GetLoadedWithNoSignersNotaryRepository(trust.ImageRefAndAuth, []string) (client.Repository, error) {
513+
func GetLoadedWithNoSignersNotaryRepository() (client.Repository, error) {
515514
return LoadedWithNoSignersNotaryRepository{}, nil
516515
}
517516

0 commit comments

Comments
 (0)