Simplify the interface of parseSingleAuthHeader

Don't create a single-element map only for the only caller
to laboriously extract an element of that map; just return
a single entry.

Should not change behavior.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
This commit is contained in:
Miloslav Trmač
2021-09-11 22:20:26 +02:00
parent 2aeb690d37
commit 7674f2f76b
2 changed files with 10 additions and 20 deletions

View File

@ -121,17 +121,11 @@ func getAuthCredentials(r *http.Request) (*types.DockerAuthConfig, string, error
} }
// Fallback to looking for a single-auth header (i.e., one config). // Fallback to looking for a single-auth header (i.e., one config).
authConfigs, err = parseSingleAuthHeader(r) authConfig, err := parseSingleAuthHeader(r)
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }
var conf *types.DockerAuthConfig return &authConfig, "", nil
for k := range authConfigs {
c := authConfigs[k]
conf = &c
break
}
return conf, "", nil
} }
// Header builds the requested Authentication Header // Header builds the requested Authentication Header
@ -321,7 +315,7 @@ func imageAuthToDockerAuth(authConfig types.DockerAuthConfig) dockerAPITypes.Aut
// parseSingleAuthHeader extracts a DockerAuthConfig from the request's header. // parseSingleAuthHeader extracts a DockerAuthConfig from the request's header.
// The header content is a single DockerAuthConfig. // The header content is a single DockerAuthConfig.
func parseSingleAuthHeader(r *http.Request) (map[string]types.DockerAuthConfig, error) { func parseSingleAuthHeader(r *http.Request) (types.DockerAuthConfig, error) {
authHeader := r.Header.Get(string(XRegistryAuthHeader)) authHeader := r.Header.Get(string(XRegistryAuthHeader))
authConfig := dockerAPITypes.AuthConfig{} authConfig := dockerAPITypes.AuthConfig{}
// Accept "null" and handle it as empty value for compatibility reason with Docker. // Accept "null" and handle it as empty value for compatibility reason with Docker.
@ -329,12 +323,10 @@ func parseSingleAuthHeader(r *http.Request) (map[string]types.DockerAuthConfig,
if len(authHeader) > 0 && authHeader != "null" { if len(authHeader) > 0 && authHeader != "null" {
authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authHeader)) authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authHeader))
if err := json.NewDecoder(authJSON).Decode(&authConfig); err != nil { if err := json.NewDecoder(authJSON).Decode(&authConfig); err != nil {
return nil, err return types.DockerAuthConfig{}, err
} }
} }
authConfigs := make(map[string]types.DockerAuthConfig) return dockerAuthToImageAuth(authConfig), nil
authConfigs["0"] = dockerAuthToImageAuth(authConfig)
return authConfigs, nil
} }
// parseMultiAuthHeader extracts a DockerAuthConfig from the request's header. // parseMultiAuthHeader extracts a DockerAuthConfig from the request's header.

View File

@ -302,24 +302,22 @@ func TestParseSingleAuthHeader(t *testing.T) {
for _, tc := range []struct { for _, tc := range []struct {
input string input string
shouldErr bool shouldErr bool
expected map[string]types.DockerAuthConfig expected types.DockerAuthConfig
}{ }{
{ {
input: "", // An empty (or missing) header input: "", // An empty (or missing) header
expected: map[string]types.DockerAuthConfig{"0": {}}, expected: types.DockerAuthConfig{},
}, },
{ {
input: "null", input: "null",
expected: map[string]types.DockerAuthConfig{"0": {}}, expected: types.DockerAuthConfig{},
}, },
// Invalid JSON // Invalid JSON
{input: "@", shouldErr: true}, {input: "@", shouldErr: true},
// Success // Success
{ {
input: base64.URLEncoding.EncodeToString([]byte(`{"username":"u1","password":"p1"}`)), input: base64.URLEncoding.EncodeToString([]byte(`{"username":"u1","password":"p1"}`)),
expected: map[string]types.DockerAuthConfig{ expected: types.DockerAuthConfig{Username: "u1", Password: "p1"},
"0": {Username: "u1", Password: "p1"},
},
}, },
} { } {
req, err := http.NewRequest(http.MethodPost, "/", nil) req, err := http.NewRequest(http.MethodPost, "/", nil)