Skip to content

Commit

Permalink
vcsim: add SessionManager.ImpersonateUser method
Browse files Browse the repository at this point in the history
  • Loading branch information
dougm committed Aug 10, 2024
1 parent 0917e71 commit ee966a6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions govc/test/session.bats
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ load test_helper
run govc tags.ls -u "$user@$host"
assert_failure # logged out of persisted session

# ImpersonateUser

run govc session.login -as vcsim
assert_failure # user must have an existing session

run govc session.login -u vcsim:pass@"$host"
assert_success

run govc session.login -as vcsim
assert_success

rm -rf "$dir"
}

Expand Down
30 changes: 30 additions & 0 deletions simulator/session_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ func (m *SessionManager) getSession(id string) (Session, bool) {
return s, ok
}

func (m *SessionManager) findSession(user string) (Session, bool) {
sessionMutex.Lock()
defer sessionMutex.Unlock()
for _, session := range m.sessions {
if session.UserName == user {
return session, true
}
}
return Session{}, false
}

func (m *SessionManager) delSession(id string) {
sessionMutex.Lock()
defer sessionMutex.Unlock()
Expand Down Expand Up @@ -273,6 +284,25 @@ func (s *SessionManager) CloneSession(ctx *Context, ticket *types.CloneSession)
return body
}

func (s *SessionManager) ImpersonateUser(ctx *Context, req *types.ImpersonateUser) soap.HasFault {
body := new(methods.ImpersonateUserBody)

session, exists := s.findSession(req.UserName)

if exists {
session.Key = uuid.New().String()
ctx.SetSession(session, true)

body.Res = &types.ImpersonateUserResponse{
Returnval: session.UserSession,
}
} else {
body.Fault_ = invalidLogin
}

return body
}

func (s *SessionManager) AcquireGenericServiceTicket(ticket *types.AcquireGenericServiceTicket) soap.HasFault {
return &methods.AcquireGenericServiceTicketBody{
Res: &types.AcquireGenericServiceTicketResponse{
Expand Down

0 comments on commit ee966a6

Please sign in to comment.