From f6b05fd873d60dd3426ded8ce96741cc385154b7 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 29 Oct 2021 11:23:41 +0800 Subject: [PATCH] Fall back to os.Get[ug]id if user.Current() fails (#28696) (#28709) (cherry picked from commit 7af0c206b20cb71299f099627971788d65a4931f) Co-authored-by: David Kowalski <50632861+david-kow@users.noreply.github.com> --- libbeat/cmd/instance/beat.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/libbeat/cmd/instance/beat.go b/libbeat/cmd/instance/beat.go index ed7448f49914..af4fdc6a79de 100644 --- a/libbeat/cmd/instance/beat.go +++ b/libbeat/cmd/instance/beat.go @@ -32,6 +32,7 @@ import ( "os/user" "runtime" "runtime/debug" + "strconv" "strings" "time" @@ -191,13 +192,20 @@ func Run(settings Settings, bt beat.Creator) error { monitoring.NewTimestamp(registry, "build_time").Set(version.BuildTime()) monitoring.NewBool(registry, "elastic_licensed").Set(b.Info.ElasticLicensed) - u, err := user.Current() - if err != nil { - return err + if u, err := user.Current(); err != nil { + if _, ok := err.(user.UnknownUserIdError); ok { + // This usually happens if the user UID does not exist in /etc/passwd. It might be the case on K8S + // if the user set securityContext.runAsUser to an arbitrary value. + monitoring.NewString(registry, "uid").Set(strconv.Itoa(os.Getuid())) + monitoring.NewString(registry, "gid").Set(strconv.Itoa(os.Getgid())) + } else { + return err + } + } else { + monitoring.NewString(registry, "username").Set(u.Username) + monitoring.NewString(registry, "uid").Set(u.Uid) + monitoring.NewString(registry, "gid").Set(u.Gid) } - monitoring.NewString(registry, "username").Set(u.Username) - monitoring.NewString(registry, "uid").Set(u.Uid) - monitoring.NewString(registry, "gid").Set(u.Gid) // Add additional info to state registry. This is also reported to monitoring stateRegistry := monitoring.GetNamespace("state").GetRegistry()