From 7612334cbcedb7e0a273cc307343a19949b50f29 Mon Sep 17 00:00:00 2001 From: Ondra Machacek Date: Thu, 10 Oct 2024 15:53:23 +0200 Subject: [PATCH] Modify the testing of vmware credentials This PR modify the testing of vmware credentials so it follows the code of the forklift project. Also we return proper error return codes in case something is wrong. Signed-off-by: Ondra Machacek --- internal/agent/rest.go | 45 ++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/internal/agent/rest.go b/internal/agent/rest.go index fe8f72b..8c628b7 100644 --- a/internal/agent/rest.go +++ b/internal/agent/rest.go @@ -7,13 +7,16 @@ import ( "net/http" "net/url" "path/filepath" + "strings" "time" "github.com/go-chi/chi" "github.com/go-chi/render" + liberr "github.com/konveyor/forklift-controller/pkg/lib/error" "github.com/kubev2v/migration-planner/internal/agent/fileio" "github.com/kubev2v/migration-planner/pkg/log" - "github.com/vmware/govmomi/session/cache" + "github.com/vmware/govmomi" + "github.com/vmware/govmomi/session" "github.com/vmware/govmomi/vim25" "github.com/vmware/govmomi/vim25/soap" ) @@ -68,9 +71,9 @@ func credentialHandler(log *log.PrefixLogger, dataDir string, w http.ResponseWri } log.Info("received credentials") - err = testVmwareConnection(log, credentials) + status, err := testVmwareConnection(r.Context(), log, credentials) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + http.Error(w, err.Error(), status) return } @@ -88,20 +91,36 @@ func credentialHandler(log *log.PrefixLogger, dataDir string, w http.ResponseWri w.WriteHeader(204) } -func testVmwareConnection(log *log.PrefixLogger, credentials Credentials) error { - u, err := soap.ParseURL(credentials.URL) +func testVmwareConnection(requestCtx context.Context, log *log.PrefixLogger, credentials Credentials) (status int, err error) { + ctx, cancel := context.WithTimeout(requestCtx, 10*time.Second) + defer cancel() + + u, err := url.Parse(credentials.URL) if err != nil { - return err + return http.StatusInternalServerError, liberr.Wrap(err) } u.User = url.UserPassword(credentials.Username, credentials.Password) - s := &cache.Session{URL: u, Reauth: true, Insecure: true} - c := new(vim25.Client) + soapClient := soap.NewClient(u, true) // FIXME: CA cert + vimClient, err := vim25.NewClient(ctx, soapClient) + if err != nil { + return http.StatusInternalServerError, liberr.Wrap(err) + } + client := &govmomi.Client{ + SessionManager: session.NewManager(vimClient), + Client: vimClient, + } log.Info("logging into vmware using received credentials") - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - err = s.Login(ctx, c, nil) + err = client.Login(ctx, u.User) if err != nil { - return err + err = liberr.Wrap(err) + if strings.Contains(err.Error(), "incorrect") && strings.Contains(err.Error(), "password") { + return http.StatusUnauthorized, err + } + return http.StatusBadRequest, err } - return s.Logout(ctx, c) + + client.Logout(ctx) + client.CloseIdleConnections() + + return http.StatusAccepted, nil }