Skip to content

Commit

Permalink
Modify the testing of vmware credentials
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
machacekondra committed Oct 10, 2024
1 parent 10ea4ca commit 85f0009
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions internal/agent/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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(log, credentials)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, err.Error(), status)
return
}

Expand All @@ -88,20 +91,37 @@ 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(log *log.PrefixLogger, credentials Credentials) (status int, err error) {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 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
}

0 comments on commit 85f0009

Please sign in to comment.