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

Better debug logging for host user creation #46789

Merged
merged 1 commit into from
Oct 1, 2024

Conversation

eriktate
Copy link
Contributor

Host user creation doesn't produce many useful logs, even in debug mode. This PR adds some new logs and updates some existing ones to help make more sense of what's happening at runtime.

@rosstimothy rosstimothy added the no-changelog Indicates that a PR does not require a changelog entry label Sep 19, 2024
@gravitational gravitational deleted a comment from github-actions bot Sep 19, 2024
Copy link
Contributor

@rosstimothy rosstimothy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mean to pile more work on you here, but we've been undergoing a slow migration from logrus to log/slog. Could I trouble you to use slog instead of logrus for any new loggers being constructed or added as members to a struct?

@eriktate
Copy link
Contributor Author

I don't mean to pile more work on you here, but we've been undergoing a slow migration from logrus to log/slog. Could I trouble you to use slog instead of logrus for any new loggers being constructed or added as members to a struct?

Yeah definitely! I should've asked, I noticed there were a few loggers in play and wondered if there was a reason for that haha. I'll get all of these updated 👍

@eriktate eriktate force-pushed the eriktate/improved-host-user-logging branch 4 times, most recently from ea53d75 to 23b356c Compare September 20, 2024 18:37
lib/srv/sess.go Outdated Show resolved Hide resolved
lib/srv/sess.go Outdated Show resolved Hide resolved
lib/srv/sess.go Outdated Show resolved Hide resolved
lib/srv/sess.go Outdated Show resolved Hide resolved
lib/srv/usermgmt.go Outdated Show resolved Hide resolved
lib/srv/usermgmt.go Outdated Show resolved Hide resolved
lib/srv/usermgmt.go Outdated Show resolved Hide resolved
lib/srv/usermgmt.go Outdated Show resolved Hide resolved
lib/srv/sess.go Outdated Show resolved Hide resolved
@eriktate eriktate force-pushed the eriktate/improved-host-user-logging branch 3 times, most recently from 1ebe439 to 5204fca Compare September 20, 2024 20:25
lib/srv/usermgmt.go Outdated Show resolved Hide resolved
@eriktate eriktate force-pushed the eriktate/improved-host-user-logging branch 2 times, most recently from c29cc2a to 950f236 Compare September 26, 2024 23:13
lib/srv/sess.go Outdated
@@ -289,15 +294,19 @@ func (s *SessionRegistry) WriteSudoersFile(identityContext IdentityContext) (io.
// If the returned closer is not nil, it must be called at the end of the session to
// clean up the local user.
func (s *SessionRegistry) UpsertHostUser(identityContext IdentityContext) (bool, io.Closer, error) {
ctx := context.TODO()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have s.Srv.Context(), can we use it instead?

@@ -259,6 +260,7 @@ func TestSession_newRecorder(t *testing.T) {
id: "test",
log: logger,
registry: &SessionRegistry{
logger: slog.Default(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have github.com/gravitational/teleport/lib/utils.NewSlogLoggerForTests() which is usually used for this type of initializations.
It does a little more that just calling slog.Default()

@@ -68,10 +70,10 @@ func NewHostSudoers(uuid string) HostSudoers {
backend, err := newHostSudoersBackend(uuid)
switch {
case trace.IsNotImplemented(err):
log.Debugf("Skipping host sudoers management: %v", err)
slog.DebugContext(context.Background(), "Skipping host sudoers management", "err", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
slog.DebugContext(context.Background(), "Skipping host sudoers management", "err", err)
slog.DebugContext(context.Background(), "Skipping host sudoers management", "error", err)

return nil
case err != nil: //nolint:staticcheck // linter fails on non-linux system as only linux implementation returns useful values.
log.Warnf("Error making new HostSudoersBackend: %s", err)
slog.DebugContext(context.Background(), "Error making new HostSudoersBackend", "err", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
slog.DebugContext(context.Background(), "Error making new HostSudoersBackend", "err", err)
slog.DebugContext(context.Background(), "Error making new HostSudoersBackend", "error", err)

@@ -232,6 +236,15 @@ func (u *HostSudoersManagement) RemoveSudoers(name string) error {
var unmanagedUserErr = errors.New("user not managed by teleport")

func (u *HostUserManagement) updateUser(name string, ui services.HostUsersInfo) (io.Closer, error) {
ctx := context.TODO()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use u.ctx ?

final = append(final, group)
}

log.DebugContext(ctx, "Setting user groups", "before", current, "after", final)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💅 If we sort them, it would make human comparison easier

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great call 👍

"uid", ui.UID,
)

log.DebugContext(context.Background(), "Attempting to create host user", "gid", ui.GID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use s.ctx instead of context.Backgroun()?

Comment on lines 445 to 447
if _, err := os.Stat(userOpts.Home); os.IsNotExist(err) {
log.InfoContext(context.Background(), "Creating home directory", "home", userOpts.Home, "gid", userOpts.GID)
if err := u.backend.CreateHomeDirectory(userOpts.Home, user.Uid, user.Gid); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a race here. The Home might be created between the os.Stat and the CreateHomeDirectory.
Should we just try to create it anyway and then handle the error gracefully?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's essentially what CreateHomeDirectory does, but it swallows the error context instead of returning it to the caller. I suppose we could surface the error when the directory already exists and handle it here instead 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dropped the os.Stat in favor of making CreateHomeDirectory return the os.ErrExists so that callers can handle the cases as they see fit 👍

lib/srv/usermgmt.go Show resolved Hide resolved
@@ -533,14 +590,15 @@ func isUnknownGroupError(err error, groupName string) bool {

// DeleteAllUsers deletes all host users in the teleport service group.
func (u *HostUserManagement) DeleteAllUsers() error {
u.log.DebugContext(context.Background(), "Attempting to delete all temporary host users")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move this to info?

@eriktate eriktate force-pushed the eriktate/improved-host-user-logging branch 2 times, most recently from 9d7cac3 to cb8b316 Compare October 1, 2024 19:20
Copy link
Contributor

@rosstimothy rosstimothy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Erik!

@eriktate eriktate force-pushed the eriktate/improved-host-user-logging branch from cb8b316 to 0259e86 Compare October 1, 2024 19:39
lib/srv/usermgmt_test.go Outdated Show resolved Hide resolved
…d updating existing logs to structured logging
@eriktate eriktate force-pushed the eriktate/improved-host-user-logging branch from 0259e86 to ac1c622 Compare October 1, 2024 20:02
@eriktate eriktate added this pull request to the merge queue Oct 1, 2024
Merged via the queue into master with commit 685dd44 Oct 1, 2024
39 checks passed
@eriktate eriktate deleted the eriktate/improved-host-user-logging branch October 1, 2024 20:50
@public-teleport-github-review-bot

@eriktate See the table below for backport results.

Branch Result
branch/v14 Failed
branch/v15 Failed
branch/v16 Failed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants