Skip to content

Commit

Permalink
Force logout of sessions in case of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rikatz committed Dec 6, 2023
1 parent 2510f71 commit f947576
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions pkg/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ func GetOrCreate(ctx context.Context, params *Params) (*Session, error) {
return s, nil
}

logger.V(2).Info("logout the rest session because it is inactive")
if err := s.TagManager.Logout(ctx); err != nil {
logger.Error(err, "unable to logout rest session")
} else {
logger.Info("logout rest session succeed")
}

logger.V(2).Info("logout the session because it is inactive")
if err := s.Client.Logout(ctx); err != nil {
logger.Error(err, "unable to logout session")
Expand Down Expand Up @@ -198,6 +205,11 @@ func GetOrCreate(ctx context.Context, params *Params) (*Session, error) {
// Assign tag manager to the session.
manager, err := newManager(ctx, logger, sessionKey, client.Client, soapURL.User, params.feature)
if err != nil {
logger.Error(err, "unable to get new Manager, will logout")
// Logout of previously logged session to not leak
if errLogout := client.Logout(ctx); errLogout != nil {
logger.Error(errLogout, "error logging out of leading client session")
}

Check warning on line 212 in pkg/session/session.go

View check run for this annotation

Codecov / codecov/patch

pkg/session/session.go#L208-L212

Added lines #L208 - L212 were not covered by tests
return nil, errors.Wrap(err, "unable to create tags manager")
}
session.TagManager = manager
Expand All @@ -206,6 +218,14 @@ func GetOrCreate(ctx context.Context, params *Params) (*Session, error) {
if params.datacenter != "" {
dc, err := session.Finder.Datacenter(ctx, params.datacenter)
if err != nil {
logger.Error(err, "unable to get datacenter, will logout")
// Logout of previously logged session to not leak
if errLogout := manager.Logout(ctx); errLogout != nil {
logger.Error(errLogout, "error logging out of leading rest session")
}
if errLogout := client.Logout(ctx); errLogout != nil {
logger.Error(errLogout, "error logging out of leading client session")
}

Check warning on line 228 in pkg/session/session.go

View check run for this annotation

Codecov / codecov/patch

pkg/session/session.go#L221-L228

Added lines #L221 - L228 were not covered by tests
return nil, errors.Wrapf(err, "unable to find datacenter %q", params.datacenter)
}
session.datacenter = dc
Expand Down Expand Up @@ -243,6 +263,9 @@ func newClient(ctx context.Context, logger logr.Logger, sessionKey string, url *
if err != nil {
logger.Error(err, "failed to keep alive govmomi client")
logger.Info("clearing the session")
if errLogout := c.Logout(ctx); errLogout != nil {
logger.Error(err, "failed to logout keepalive failed session")
}

Check warning on line 268 in pkg/session/session.go

View check run for this annotation

Codecov / codecov/patch

pkg/session/session.go#L266-L268

Added lines #L266 - L268 were not covered by tests
sessionCache.Delete(sessionKey)
}
return err
Expand All @@ -262,14 +285,18 @@ func newManager(ctx context.Context, logger logr.Logger, sessionKey string, clie
if feature.EnableKeepAlive {
rc.Transport = keepalive.NewHandlerREST(rc, feature.KeepAliveDuration, func() error {
s, err := rc.Session(ctx)
if err != nil {
return err
}
if s != nil {
if s != nil && err == nil {

Check warning on line 288 in pkg/session/session.go

View check run for this annotation

Codecov / codecov/patch

pkg/session/session.go#L288

Added line #L288 was not covered by tests
return nil
}

if err != nil {
logger.Error(err, "failed to keep alive rest client")
}

Check warning on line 294 in pkg/session/session.go

View check run for this annotation

Codecov / codecov/patch

pkg/session/session.go#L292-L294

Added lines #L292 - L294 were not covered by tests

logger.Info("rest client session expired, clearing session")
if errLogout := rc.Logout(ctx); errLogout != nil {
logger.Error(err, "failed to logout keepalive failed rest session")
}

Check warning on line 299 in pkg/session/session.go

View check run for this annotation

Codecov / codecov/patch

pkg/session/session.go#L297-L299

Added lines #L297 - L299 were not covered by tests
sessionCache.Delete(sessionKey)
return errors.New("rest client session expired")
})
Expand Down

0 comments on commit f947576

Please sign in to comment.