Skip to content

Commit d5c181a

Browse files
committed
cli/command/image: pushTrustedReference: internalize constructing indexInfo
All information needed can be deducted from the image reference, which is used to create a indexInfo, repoInfo, and to resolve auth-config. In some situations this may result in resolving the auth-config twice after it already was resolved to an encoded auth-config. Signed-off-by: Sebastiaan van Stijn <github@gone.nl> (cherry picked from commit 9a6313e) Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent ec00b85 commit d5c181a

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

cli/command/image/push.go

Lines changed: 2 additions & 8 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"
@@ -114,12 +112,8 @@ To push the complete multi-platform image, remove the --platform flag.
114112
}
115113
}
116114

117-
// Resolve the Repository name from fqn to RepositoryInfo
118-
indexInfo := registry.NewIndexInfo(ref)
119-
120115
// Resolve the Auth config relevant for this server
121-
authConfig := command.ResolveAuthConfig(dockerCli.ConfigFile(), indexInfo)
122-
encodedAuth, err := registrytypes.EncodeAuthConfig(authConfig)
116+
encodedAuth, err := command.RetrieveAuthTokenFromImage(dockerCli.ConfigFile(), ref.String())
123117
if err != nil {
124118
return err
125119
}
@@ -142,7 +136,7 @@ To push the complete multi-platform image, remove the --platform flag.
142136
}()
143137

144138
if !opts.untrusted {
145-
return pushTrustedReference(ctx, dockerCli, indexInfo, ref, authConfig, responseBody)
139+
return pushTrustedReference(ctx, dockerCli, ref, responseBody)
146140
}
147141

148142
if opts.quiet {

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
@@ -342,7 +342,10 @@ func GetImageReferencesAndAuth(ctx context.Context,
342342
return ImageRefAndAuth{}, err
343343
}
344344

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

0 commit comments

Comments
 (0)