Skip to content

Commit

Permalink
Fall back to os.Get[ug]id if user.Current() fails (#28696) (#28709)
Browse files Browse the repository at this point in the history
(cherry picked from commit 7af0c20)

Co-authored-by: David Kowalski <[email protected]>
  • Loading branch information
mergify[bot] and david-kow authored Oct 29, 2021
1 parent 79e45d6 commit f6b05fd
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions libbeat/cmd/instance/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"os/user"
"runtime"
"runtime/debug"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit f6b05fd

Please sign in to comment.