Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean up drop users within a single lease #49096

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions lib/srv/usermgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@ func (u *HostUserManagement) UpsertUser(name string, ui services.HostUsersInfo)
return closer, nil
}

const userLeaseDuration = time.Second * 20

func (u *HostUserManagement) doWithUserLock(f func(types.SemaphoreLease) error) error {
lock, err := services.AcquireSemaphoreWithRetry(u.ctx,
services.AcquireSemaphoreWithRetryConfig{
Expand All @@ -493,7 +495,7 @@ func (u *HostUserManagement) doWithUserLock(f func(types.SemaphoreLease) error)
SemaphoreKind: types.SemaphoreKindHostUserModification,
SemaphoreName: "host_user_modification",
MaxLeases: 1,
Expires: time.Now().Add(time.Second * 20),
Expires: time.Now().Add(userLeaseDuration),
},
Retry: retryutils.LinearConfig{
Step: time.Second * 5,
Expand Down Expand Up @@ -556,26 +558,35 @@ func (u *HostUserManagement) DeleteAllUsers() error {
return trace.Wrap(err)
}
var errs []error
for _, name := range users {
lt, err := u.storage.GetHostUserInteractionTime(u.ctx, name)
if err != nil {
u.log.DebugContext(u.ctx, "Failed to find user login time", "host_username", name, "error", err)
continue
}
u.doWithUserLock(func(l types.SemaphoreLease) error {
u.doWithUserLock(func(l types.SemaphoreLease) error {
for _, name := range users {
if time.Until(l.Expires) < userLeaseDuration/2 {
l.Expires = time.Now().Add(userLeaseDuration / 2)
if err := u.storage.KeepAliveSemaphoreLease(u.ctx, l); err != nil {
u.log.DebugContext(u.ctx, "Failed to keep alive host user lease", "error", err)
}
}

lt, err := u.storage.GetHostUserInteractionTime(u.ctx, name)
if err != nil {
u.log.DebugContext(u.ctx, "Failed to find user login time", "host_username", name, "error", err)
continue
}

if time.Since(lt) < u.userGrace {
eriktate marked this conversation as resolved.
Show resolved Hide resolved
// small grace period in order to avoid deleting users
// in-between them starting their SSH session and
// entering the shell
return nil
continue
}
errs = append(errs, u.DeleteUser(name, teleportGroup.Gid))

l.Expires = time.Now().Add(time.Second * 10)
u.storage.KeepAliveSemaphoreLease(u.ctx, l)
return nil
})
}
if err := u.DeleteUser(name, teleportGroup.Gid); err != nil {
errs = append(errs, err)
}
}

return nil
})
return trace.NewAggregate(errs...)
}

Expand Down
Loading