From 9a6313ed3bddb3721916cccb15efcc037e8cad82 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 8 Sep 2025 17:40:39 +0200 Subject: [PATCH] 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 --- cli/command/image/push.go | 10 ++-------- cli/command/image/trust.go | 11 +++++++++-- cli/trust/trust.go | 5 ++++- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/cli/command/image/push.go b/cli/command/image/push.go index 1e3602b8712f..50ace2b20915 100644 --- a/cli/command/image/push.go +++ b/cli/command/image/push.go @@ -18,9 +18,7 @@ import ( "github.com/docker/cli/cli/streams" "github.com/docker/cli/cli/trust" "github.com/docker/cli/internal/jsonstream" - "github.com/docker/cli/internal/registry" "github.com/docker/cli/internal/tui" - "github.com/moby/moby/api/pkg/authconfig" "github.com/moby/moby/api/types/auxprogress" "github.com/moby/moby/client" "github.com/morikuni/aec" @@ -108,12 +106,8 @@ To push the complete multi-platform image, remove the --platform flag. } } - // Resolve the Repository name from fqn to RepositoryInfo - indexInfo := registry.NewIndexInfo(ref) - // Resolve the Auth config relevant for this server - authConfig := command.ResolveAuthConfig(dockerCli.ConfigFile(), indexInfo) - encodedAuth, err := authconfig.Encode(authConfig) + encodedAuth, err := command.RetrieveAuthTokenFromImage(dockerCli.ConfigFile(), ref.String()) if err != nil { return err } @@ -136,7 +130,7 @@ To push the complete multi-platform image, remove the --platform flag. }() if !opts.untrusted { - return pushTrustedReference(ctx, dockerCli, indexInfo, ref, authConfig, responseBody) + return pushTrustedReference(ctx, dockerCli, ref, responseBody) } if opts.quiet { diff --git a/cli/command/image/trust.go b/cli/command/image/trust.go index 80afa3252dbf..55b1ff4017f7 100644 --- a/cli/command/image/trust.go +++ b/cli/command/image/trust.go @@ -13,6 +13,7 @@ import ( "github.com/docker/cli/cli/streams" "github.com/docker/cli/cli/trust" "github.com/docker/cli/internal/jsonstream" + "github.com/docker/cli/internal/registry" "github.com/moby/moby/api/pkg/authconfig" registrytypes "github.com/moby/moby/api/types/registry" "github.com/moby/moby/client" @@ -43,12 +44,18 @@ func newNotaryClient(cli command.Streams, imgRefAndAuth trust.ImageRefAndAuth) ( } // pushTrustedReference pushes a canonical reference to the trust server. -func pushTrustedReference(ctx context.Context, ioStreams command.Streams, indexInfo *registrytypes.IndexInfo, ref reference.Named, authConfig registrytypes.AuthConfig, in io.Reader) error { +func pushTrustedReference(ctx context.Context, dockerCLI command.Cli, ref reference.Named, responseBody io.Reader) error { + // Resolve the Repository name from fqn to RepositoryInfo, and create an + // IndexInfo. Docker Content Trust uses the IndexInfo.Official field to + // select the right domain for Docker Hub's Notary server; + // https://github.com/docker/cli/blob/v28.4.0/cli/trust/trust.go#L65-L79 + indexInfo := registry.NewIndexInfo(ref) repoInfo := &trust.RepositoryInfo{ Name: reference.TrimNamed(ref), Index: indexInfo, } - return trust.PushTrustedReference(ctx, ioStreams, repoInfo, ref, authConfig, in, command.UserAgent()) + authConfig := command.ResolveAuthConfig(dockerCLI.ConfigFile(), indexInfo) + return trust.PushTrustedReference(ctx, dockerCLI, repoInfo, ref, authConfig, responseBody, command.UserAgent()) } // trustedPull handles content trust pulling of an image diff --git a/cli/trust/trust.go b/cli/trust/trust.go index b961de8ef423..fca9810a5d44 100644 --- a/cli/trust/trust.go +++ b/cli/trust/trust.go @@ -340,7 +340,10 @@ func GetImageReferencesAndAuth(ctx context.Context, return ImageRefAndAuth{}, err } - // Resolve the Repository name from fqn to RepositoryInfo + // Resolve the Repository name from fqn to RepositoryInfo, and create an + // IndexInfo. Docker Content Trust uses the IndexInfo.Official field to + // select the right domain for Docker Hub's Notary server; + // https://github.com/docker/cli/blob/v28.4.0/cli/trust/trust.go#L65-L79 indexInfo := registry.NewIndexInfo(ref) authConfig := authResolver(ctx, indexInfo) return ImageRefAndAuth{