From 3725a34cbf592697d5fca3089d14045c66975fda Mon Sep 17 00:00:00 2001 From: Miloslav Trmač Date: Sat, 11 Sep 2021 23:15:49 +0200 Subject: Call MakeXRegistryAuthHeader instead of Header(..., XRegistryAuthHeader) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit All callers hard-code a header value, so this is actually shorter. Should not change behavior. Signed-off-by: Miloslav Trmač --- pkg/bindings/play/play.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pkg/bindings/play') diff --git a/pkg/bindings/play/play.go b/pkg/bindings/play/play.go index 2cd7c3997..64a2ae6ae 100644 --- a/pkg/bindings/play/play.go +++ b/pkg/bindings/play/play.go @@ -40,7 +40,7 @@ func Kube(ctx context.Context, path string, options *KubeOptions) (*entities.Pla } // TODO: have a global system context we can pass around (1st argument) - header, err := auth.Header(nil, auth.XRegistryAuthHeader, options.GetAuthfile(), options.GetUsername(), options.GetPassword()) + header, err := auth.MakeXRegistryAuthHeader(nil, options.GetAuthfile(), options.GetUsername(), options.GetPassword()) if err != nil { return nil, err } -- cgit v1.2.3-54-g00ecf From 3cfefa1248feb9de8041b9fc67987b508d4c3fcb Mon Sep 17 00:00:00 2001 From: Miloslav Trmač Date: Thu, 21 Oct 2021 21:31:22 +0200 Subject: Remove the authfile parameter of MakeXRegistryAuthHeader MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Having a parameter that modifies the provides types.SystemContext seems rather unexpected and risky to have around - and the only user of that is actually a no-op, others only provide a nil SystemContext; so, remove that option and simplify (well, somewhat; many callers now have extra &types.SystemContext{AuthFilePath} boilerplate; at least that's consistent with that code carrying a TODO to create a larger-scope SystemContext). Should not change behavior. Signed-off-by: Miloslav Trmač --- pkg/auth/auth.go | 5 +---- pkg/auth/auth_test.go | 28 ++++++++++++++-------------- pkg/bindings/images/build.go | 2 +- pkg/bindings/images/images.go | 5 +++-- pkg/bindings/images/pull.go | 3 ++- pkg/bindings/play/play.go | 3 ++- 6 files changed, 23 insertions(+), 23 deletions(-) (limited to 'pkg/bindings/play') diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index 006572b09..2124b5302 100644 --- a/pkg/auth/auth.go +++ b/pkg/auth/auth.go @@ -172,7 +172,7 @@ func MakeXRegistryConfigHeader(sys *types.SystemContext, username, password stri // MakeXRegistryAuthHeader returns a map with the XRegistryAuthHeader set which can // conveniently be used in the http stack. -func MakeXRegistryAuthHeader(sys *types.SystemContext, authfile, username, password string) (map[string]string, error) { +func MakeXRegistryAuthHeader(sys *types.SystemContext, username, password string) (map[string]string, error) { if username != "" { content, err := encodeSingleAuthConfig(types.DockerAuthConfig{Username: username, Password: password}) if err != nil { @@ -184,9 +184,6 @@ func MakeXRegistryAuthHeader(sys *types.SystemContext, authfile, username, passw if sys == nil { sys = &types.SystemContext{} } - if authfile != "" { - sys.AuthFilePath = authfile - } authConfigs, err := imageAuth.GetAllCredentials(sys) if err != nil { return nil, err diff --git a/pkg/auth/auth_test.go b/pkg/auth/auth_test.go index a727a9d50..bce488a91 100644 --- a/pkg/auth/auth_test.go +++ b/pkg/auth/auth_test.go @@ -29,12 +29,12 @@ var largeAuthFileValues = map[string]types.DockerAuthConfig{ "quay.io": {Username: "quay", Password: "top"}, } -// tempAuthFilePath returns a non-empty path pointing -// to a temporary file with fileContents, or "" if fileContents is empty; and a cleanup -// function the caller must arrange to call. -func tempAuthFilePath(t *testing.T, fileContents string) (string, func()) { +// systemContextForAuthFile returns a types.SystemContext with AuthFilePath pointing +// to a temporary file with fileContents, or nil if fileContents is empty; and a cleanup +// function the calle rmust arrange to call. +func systemContextForAuthFile(t *testing.T, fileContents string) (*types.SystemContext, func()) { if fileContents == "" { - return "", func() {} + return nil, func() {} } f, err := ioutil.TempFile("", "auth.json") @@ -42,7 +42,7 @@ func tempAuthFilePath(t *testing.T, fileContents string) (string, func()) { path := f.Name() err = ioutil.WriteFile(path, []byte(fileContents), 0700) require.NoError(t, err) - return path, func() { os.Remove(path) } + return &types.SystemContext{AuthFilePath: path}, func() { os.Remove(path) } } // Test that GetCredentials() correctly parses what MakeXRegistryConfigHeader() produces @@ -79,9 +79,9 @@ func TestMakeXRegistryConfigHeaderGetCredentialsRoundtrip(t *testing.T) { expectedFileValues: largeAuthFileValues, }, } { - inputAuthFile, cleanup := tempAuthFilePath(t, tc.fileContents) + sys, cleanup := systemContextForAuthFile(t, tc.fileContents) defer cleanup() - headers, err := MakeXRegistryConfigHeader(&types.SystemContext{AuthFilePath: inputAuthFile}, tc.username, tc.password) + headers, err := MakeXRegistryConfigHeader(sys, tc.username, tc.password) require.NoError(t, err) req, err := http.NewRequest(http.MethodPost, "/", nil) require.NoError(t, err, tc.name) @@ -131,9 +131,9 @@ func TestMakeXRegistryAuthHeaderGetCredentialsRoundtrip(t *testing.T) { expectedFileValues: largeAuthFileValues, }, } { - inputAuthFile, cleanup := tempAuthFilePath(t, tc.fileContents) + sys, cleanup := systemContextForAuthFile(t, tc.fileContents) defer cleanup() - headers, err := MakeXRegistryAuthHeader(nil, inputAuthFile, tc.username, tc.password) + headers, err := MakeXRegistryAuthHeader(sys, tc.username, tc.password) require.NoError(t, err) req, err := http.NewRequest(http.MethodPost, "/", nil) require.NoError(t, err, tc.name) @@ -206,9 +206,9 @@ func TestMakeXRegistryConfigHeader(t *testing.T) { }`, }, } { - authFile, cleanup := tempAuthFilePath(t, tc.fileContents) + sys, cleanup := systemContextForAuthFile(t, tc.fileContents) defer cleanup() - res, err := MakeXRegistryConfigHeader(&types.SystemContext{AuthFilePath: authFile}, tc.username, tc.password) + res, err := MakeXRegistryConfigHeader(sys, tc.username, tc.password) if tc.shouldErr { assert.Error(t, err, tc.name) } else { @@ -269,9 +269,9 @@ func TestMakeXRegistryAuthHeader(t *testing.T) { }`, }, } { - authFile, cleanup := tempAuthFilePath(t, tc.fileContents) + sys, cleanup := systemContextForAuthFile(t, tc.fileContents) defer cleanup() - res, err := MakeXRegistryAuthHeader(nil, authFile, tc.username, tc.password) + res, err := MakeXRegistryAuthHeader(sys, tc.username, tc.password) if tc.shouldErr { assert.Error(t, err, tc.name) } else { diff --git a/pkg/bindings/images/build.go b/pkg/bindings/images/build.go index fece5e9d0..7bca43132 100644 --- a/pkg/bindings/images/build.go +++ b/pkg/bindings/images/build.go @@ -294,7 +294,7 @@ func Build(ctx context.Context, containerFiles []string, options entities.BuildO err error ) if options.SystemContext != nil && options.SystemContext.DockerAuthConfig != nil { - headers, err = auth.MakeXRegistryAuthHeader(options.SystemContext, options.SystemContext.AuthFilePath, options.SystemContext.DockerAuthConfig.Username, options.SystemContext.DockerAuthConfig.Password) + headers, err = auth.MakeXRegistryAuthHeader(options.SystemContext, options.SystemContext.DockerAuthConfig.Username, options.SystemContext.DockerAuthConfig.Password) } else { headers, err = auth.MakeXRegistryConfigHeader(options.SystemContext, "", "") } diff --git a/pkg/bindings/images/images.go b/pkg/bindings/images/images.go index 74603015c..152ff0cde 100644 --- a/pkg/bindings/images/images.go +++ b/pkg/bindings/images/images.go @@ -8,6 +8,7 @@ import ( "net/url" "strconv" + imageTypes "github.com/containers/image/v5/types" "github.com/containers/podman/v3/pkg/api/handlers/types" "github.com/containers/podman/v3/pkg/auth" "github.com/containers/podman/v3/pkg/bindings" @@ -280,7 +281,7 @@ func Push(ctx context.Context, source string, destination string, options *PushO return err } // TODO: have a global system context we can pass around (1st argument) - header, err := auth.MakeXRegistryAuthHeader(nil, options.GetAuthfile(), options.GetUsername(), options.GetPassword()) + header, err := auth.MakeXRegistryAuthHeader(&imageTypes.SystemContext{AuthFilePath: options.GetAuthfile()}, options.GetUsername(), options.GetPassword()) if err != nil { return err } @@ -329,7 +330,7 @@ func Search(ctx context.Context, term string, options *SearchOptions) ([]entitie } // TODO: have a global system context we can pass around (1st argument) - header, err := auth.MakeXRegistryAuthHeader(nil, options.GetAuthfile(), "", "") + header, err := auth.MakeXRegistryAuthHeader(&imageTypes.SystemContext{AuthFilePath: options.GetAuthfile()}, "", "") if err != nil { return nil, err } diff --git a/pkg/bindings/images/pull.go b/pkg/bindings/images/pull.go index c6f20e3e1..ac583973f 100644 --- a/pkg/bindings/images/pull.go +++ b/pkg/bindings/images/pull.go @@ -10,6 +10,7 @@ import ( "os" "strconv" + "github.com/containers/image/v5/types" "github.com/containers/podman/v3/pkg/auth" "github.com/containers/podman/v3/pkg/bindings" "github.com/containers/podman/v3/pkg/domain/entities" @@ -42,7 +43,7 @@ func Pull(ctx context.Context, rawImage string, options *PullOptions) ([]string, } // TODO: have a global system context we can pass around (1st argument) - header, err := auth.MakeXRegistryAuthHeader(nil, options.GetAuthfile(), options.GetUsername(), options.GetPassword()) + header, err := auth.MakeXRegistryAuthHeader(&types.SystemContext{AuthFilePath: options.GetAuthfile()}, options.GetUsername(), options.GetPassword()) if err != nil { return nil, err } diff --git a/pkg/bindings/play/play.go b/pkg/bindings/play/play.go index 64a2ae6ae..111a25cac 100644 --- a/pkg/bindings/play/play.go +++ b/pkg/bindings/play/play.go @@ -6,6 +6,7 @@ import ( "os" "strconv" + "github.com/containers/image/v5/types" "github.com/containers/podman/v3/pkg/auth" "github.com/containers/podman/v3/pkg/bindings" "github.com/containers/podman/v3/pkg/domain/entities" @@ -40,7 +41,7 @@ func Kube(ctx context.Context, path string, options *KubeOptions) (*entities.Pla } // TODO: have a global system context we can pass around (1st argument) - header, err := auth.MakeXRegistryAuthHeader(nil, options.GetAuthfile(), options.GetUsername(), options.GetPassword()) + header, err := auth.MakeXRegistryAuthHeader(&types.SystemContext{AuthFilePath: options.GetAuthfile()}, options.GetUsername(), options.GetPassword()) if err != nil { return nil, err } -- cgit v1.2.3-54-g00ecf