compat: /auth: parse server address correctly

Use `auth.Login` as `podman login` does which parses and normalizes the
input addresses correctly, especially for docker.io.

[NO NEW TESTS NEEDED] as we do not have means to test logging into
docker.io in CI.

Fixes: #17571
Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
This commit is contained in:
Valentin Rothberg
2023-02-20 13:47:35 +01:00
parent 5cb18a9f47
commit bad41f67e7

View File

@ -4,9 +4,11 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io"
"net/http" "net/http"
"strings" "strings"
"github.com/containers/common/pkg/auth"
DockerClient "github.com/containers/image/v5/docker" DockerClient "github.com/containers/image/v5/docker"
"github.com/containers/image/v5/types" "github.com/containers/image/v5/types"
"github.com/containers/podman/v4/libpod" "github.com/containers/podman/v4/libpod"
@ -16,13 +18,6 @@ import (
docker "github.com/docker/docker/api/types" docker "github.com/docker/docker/api/types"
) )
func stripAddressOfScheme(address string) string {
for _, s := range []string{"https", "http"} {
address = strings.TrimPrefix(address, s+"://")
}
return address
}
func Auth(w http.ResponseWriter, r *http.Request) { func Auth(w http.ResponseWriter, r *http.Request) {
var authConfig docker.AuthConfig var authConfig docker.AuthConfig
err := json.NewDecoder(r.Body).Decode(&authConfig) err := json.NewDecoder(r.Body).Decode(&authConfig)
@ -41,9 +36,13 @@ func Auth(w http.ResponseWriter, r *http.Request) {
sysCtx := runtime.SystemContext() sysCtx := runtime.SystemContext()
sysCtx.DockerInsecureSkipTLSVerify = skipTLS sysCtx.DockerInsecureSkipTLSVerify = skipTLS
fmt.Println("Authenticating with existing credentials...") loginOpts := &auth.LoginOptions{
registry := stripAddressOfScheme(authConfig.ServerAddress) Username: authConfig.Username,
if err := DockerClient.CheckAuth(r.Context(), sysCtx, authConfig.Username, authConfig.Password, registry); err == nil { Password: authConfig.Password,
Stdout: io.Discard,
NoWriteBack: true, // to prevent credentials to be written on disk
}
if err := auth.Login(r.Context(), sysCtx, loginOpts, []string{authConfig.ServerAddress}); err == nil {
utils.WriteResponse(w, http.StatusOK, entities.AuthReport{ utils.WriteResponse(w, http.StatusOK, entities.AuthReport{
IdentityToken: "", IdentityToken: "",
Status: "Login Succeeded", Status: "Login Succeeded",