Skip to content

Commit 394991e

Browse files
Merge pull request #6510 from thaJeztah/28.x_backport_auth
[28.x backport] cli/command/image: pushTrustedReference: internalize constructing indexInfo
2 parents e5bce5c + d5c181a commit 394991e

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

cli/command/image/push.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ import (
1717
"github.com/docker/cli/cli/streams"
1818
"github.com/docker/cli/cli/trust"
1919
"github.com/docker/cli/internal/jsonstream"
20-
"github.com/docker/cli/internal/registry"
2120
"github.com/docker/cli/internal/tui"
2221
"github.com/docker/docker/api/types/auxprogress"
2322
"github.com/docker/docker/api/types/image"
24-
registrytypes "github.com/docker/docker/api/types/registry"
2523
"github.com/morikuni/aec"
2624
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2725
"github.com/pkg/errors"
@@ -100,9 +98,11 @@ To push the complete multi-platform image, remove the --platform flag.
10098
}
10199

102100
ref, err := reference.ParseNormalizedNamed(opts.remote)
103-
switch {
104-
case err != nil:
101+
if err != nil {
105102
return err
103+
}
104+
105+
switch {
106106
case opts.all && !reference.IsNameOnly(ref):
107107
return errors.New("tag can't be used with --all-tags/-a")
108108
case !opts.all && reference.IsNameOnly(ref):
@@ -112,43 +112,37 @@ To push the complete multi-platform image, remove the --platform flag.
112112
}
113113
}
114114

115-
// Resolve the Repository name from fqn to RepositoryInfo
116-
indexInfo := registry.NewIndexInfo(ref)
117-
118115
// Resolve the Auth config relevant for this server
119-
authConfig := command.ResolveAuthConfig(dockerCli.ConfigFile(), indexInfo)
120-
encodedAuth, err := registrytypes.EncodeAuthConfig(authConfig)
116+
encodedAuth, err := command.RetrieveAuthTokenFromImage(dockerCli.ConfigFile(), ref.String())
121117
if err != nil {
122118
return err
123119
}
124-
options := image.PushOptions{
120+
121+
responseBody, err := dockerCli.Client().ImagePush(ctx, reference.FamiliarString(ref), image.PushOptions{
125122
All: opts.all,
126123
RegistryAuth: encodedAuth,
127124
PrivilegeFunc: nil,
128125
Platform: platform,
129-
}
130-
131-
responseBody, err := dockerCli.Client().ImagePush(ctx, reference.FamiliarString(ref), options)
126+
})
132127
if err != nil {
133128
return err
134129
}
135130

136131
defer func() {
132+
_ = responseBody.Close()
137133
for _, note := range notes {
138134
out.PrintNote(note)
139135
}
140136
}()
141137

142-
defer responseBody.Close()
143138
if !opts.untrusted {
144-
// TODO pushTrustedReference currently doesn't respect `--quiet`
145-
return pushTrustedReference(ctx, dockerCli, indexInfo, ref, authConfig, responseBody)
139+
return pushTrustedReference(ctx, dockerCli, ref, responseBody)
146140
}
147141

148142
if opts.quiet {
149143
err = jsonstream.Display(ctx, responseBody, streams.NewOut(io.Discard), jsonstream.WithAuxCallback(handleAux()))
150144
if err == nil {
151-
fmt.Fprintln(dockerCli.Out(), ref.String())
145+
_, _ = fmt.Fprintln(dockerCli.Out(), ref.String())
152146
}
153147
return err
154148
}

cli/command/image/trust.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/docker/cli/cli/streams"
1313
"github.com/docker/cli/cli/trust"
1414
"github.com/docker/cli/internal/jsonstream"
15+
"github.com/docker/cli/internal/registry"
1516
"github.com/docker/docker/api/types/image"
1617
registrytypes "github.com/docker/docker/api/types/registry"
1718
"github.com/opencontainers/go-digest"
@@ -42,12 +43,18 @@ func newNotaryClient(cli command.Streams, imgRefAndAuth trust.ImageRefAndAuth) (
4243
}
4344

4445
// pushTrustedReference pushes a canonical reference to the trust server.
45-
func pushTrustedReference(ctx context.Context, ioStreams command.Streams, indexInfo *registrytypes.IndexInfo, ref reference.Named, authConfig registrytypes.AuthConfig, in io.Reader) error {
46+
func pushTrustedReference(ctx context.Context, dockerCLI command.Cli, ref reference.Named, responseBody io.Reader) error {
47+
// Resolve the Repository name from fqn to RepositoryInfo, and create an
48+
// IndexInfo. Docker Content Trust uses the IndexInfo.Official field to
49+
// select the right domain for Docker Hub's Notary server;
50+
// https://github.com/docker/cli/blob/v28.4.0/cli/trust/trust.go#L65-L79
51+
indexInfo := registry.NewIndexInfo(ref)
4652
repoInfo := &trust.RepositoryInfo{
4753
Name: reference.TrimNamed(ref),
4854
Index: indexInfo,
4955
}
50-
return trust.PushTrustedReference(ctx, ioStreams, repoInfo, ref, authConfig, in, command.UserAgent())
56+
authConfig := command.ResolveAuthConfig(dockerCLI.ConfigFile(), indexInfo)
57+
return trust.PushTrustedReference(ctx, dockerCLI, repoInfo, ref, authConfig, responseBody, command.UserAgent())
5158
}
5259

5360
// trustedPull handles content trust pulling of an image

cli/trust/trust.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,10 @@ func GetImageReferencesAndAuth(ctx context.Context,
351351
return ImageRefAndAuth{}, err
352352
}
353353

354-
// Resolve the Repository name from fqn to RepositoryInfo
354+
// Resolve the Repository name from fqn to RepositoryInfo, and create an
355+
// IndexInfo. Docker Content Trust uses the IndexInfo.Official field to
356+
// select the right domain for Docker Hub's Notary server;
357+
// https://github.com/docker/cli/blob/v28.4.0/cli/trust/trust.go#L65-L79
355358
indexInfo := registry.NewIndexInfo(ref)
356359
authConfig := authResolver(ctx, indexInfo)
357360
return ImageRefAndAuth{

0 commit comments

Comments
 (0)