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

libbeat/processors/add_process_metada: add capabilities to process me… #38252

Merged
merged 16 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 1 addition & 1 deletion CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ Setting environmental variable ELASTIC_NETINFO:false in Elastic Agent pod will d

*Libbeat*
- Add watcher that can be used to monitor Linux kernel events. {pull}37833[37833]

- Added support for ETW reader. {pull}36914[36914]
- Add support for linux capabilities in add_process_metadata. {pull}38252[38252]

*Heartbeat*
- Added status to monitor run log report.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type processMetadata struct {
env map[string]string
startTime time.Time
pid, ppid int
capEffective, capPermitted []string
//
fields mapstr.M
}
Expand Down Expand Up @@ -332,6 +333,12 @@ func (p *processMetadata) toMap() mapstr.M {
}
process["owner"] = user
}
if len(p.capEffective) > 0 {
process.Put("thread.capabilities.effective", p.capEffective)
}
if len(p.capPermitted) > 0 {
process.Put("thread.capabilities.permitted", p.capPermitted)
}

return mapstr.M{
"process": process,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common/capabilities"
conf "github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/mapstr"
Expand All @@ -40,6 +41,10 @@ import (
func TestAddProcessMetadata(t *testing.T) {
logp.TestingSetup(logp.WithSelectors(processorName))

capMock, err := capabilities.FromUint64(0xabacabb)
if err != nil {
t.Fatalf("could not instantiate capabilities: %s", err)
}
startTime := time.Now()
testProcs := testProvider{
1: {
Expand All @@ -53,11 +58,13 @@ func TestAddProcessMetadata(t *testing.T) {
"BOOT_IMAGE": "/boot/vmlinuz-4.11.8-300.fc26.x86_64",
"LANG": "en_US.UTF-8",
},
pid: 1,
ppid: 0,
startTime: startTime,
username: "root",
userid: "0",
pid: 1,
ppid: 0,
startTime: startTime,
username: "root",
userid: "0",
capEffective: capMock,
capPermitted: capMock,
},
3: {
name: "systemd",
Expand All @@ -70,11 +77,13 @@ func TestAddProcessMetadata(t *testing.T) {
"BOOT_IMAGE": "/boot/vmlinuz-4.11.8-300.fc26.x86_64",
"LANG": "en_US.UTF-8",
},
pid: 1,
ppid: 0,
startTime: startTime,
username: "user",
userid: "1001",
pid: 1,
ppid: 0,
startTime: startTime,
username: "user",
userid: "1001",
capEffective: capMock,
capPermitted: capMock,
},
}

Expand Down Expand Up @@ -162,6 +171,12 @@ func TestAddProcessMetadata(t *testing.T) {
"name": "root",
"id": "0",
},
"thread": mapstr.M{
"capabilities": mapstr.M{
"effective": capMock,
"permitted": capMock,
},
},
},
"container": mapstr.M{
"id": "b5285682fba7449c86452b89a800609440ecc88a7ba5f2d38bedfb85409b30b1",
Expand Down Expand Up @@ -247,6 +262,12 @@ func TestAddProcessMetadata(t *testing.T) {
"name": "root",
"id": "0",
},
"thread": mapstr.M{
"capabilities": mapstr.M{
"effective": capMock,
"permitted": capMock,
},
},
},
"container": mapstr.M{
"id": "b5285682fba7449c86452b89a800609440ecc88a7ba5f2d38bedfb85409b30b1",
Expand Down Expand Up @@ -287,6 +308,12 @@ func TestAddProcessMetadata(t *testing.T) {
"name": "root",
"id": "0",
},
"thread": mapstr.M{
"capabilities": mapstr.M{
"effective": capMock,
"permitted": capMock,
},
},
},
"container": mapstr.M{
"id": "b5285682fba7449c86452b89a800609440ecc88a7ba5f2d38bedfb85409b30b1",
Expand Down Expand Up @@ -328,6 +355,12 @@ func TestAddProcessMetadata(t *testing.T) {
"name": "root",
"id": "0",
},
"thread": mapstr.M{
"capabilities": mapstr.M{
"effective": capMock,
"permitted": capMock,
},
},
},
},
},
Expand Down Expand Up @@ -520,6 +553,12 @@ func TestAddProcessMetadata(t *testing.T) {
"name": "root",
"id": "0",
},
"thread": mapstr.M{
"capabilities": mapstr.M{
"effective": capMock,
"permitted": capMock,
},
},
},
"container": mapstr.M{
"id": "b5285682fba7449c86452b89a800609440ecc88a7ba5f2d38bedfb85409b30b1",
Expand Down Expand Up @@ -645,6 +684,12 @@ func TestAddProcessMetadata(t *testing.T) {
"name": "user",
"id": "1001",
},
"thread": mapstr.M{
"capabilities": mapstr.M{
"effective": capMock,
"permitted": capMock,
},
},
},
},
},
Expand Down
6 changes: 6 additions & 0 deletions libbeat/processors/add_process_metadata/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ var defaultFields = mapstr.M{
"name": nil,
"id": nil,
},
"thread": mapstr.M{
"capabilities": mapstr.M{
"effective": nil,
"permitted": nil,
},
},
},
"container": mapstr.M{
"id": nil,
Expand Down
29 changes: 19 additions & 10 deletions libbeat/processors/add_process_metadata/gosysinfo_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os/user"
"strings"

"github.com/elastic/beats/v7/libbeat/common/capabilities"
"github.com/elastic/go-sysinfo"
"github.com/elastic/go-sysinfo/types"
)
Expand Down Expand Up @@ -52,17 +53,25 @@ func (p gosysinfoProvider) GetProcessMetadata(pid int) (result *processMetadata,
}
}

// Capabilities are linux only and other systems will fail
// with ErrUnsupported. In the event of any errors, we simply
// don't report the capabilities.
capPermitted, _ := capabilities.FromPid(capabilities.Permitted, pid)
capEffective, _ := capabilities.FromPid(capabilities.Effective, pid)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason why we're not checking the errors here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If it fails, we don't include the capabilities, same outcome if the capabilities were empty. There are valid/normal reasons for it to fail, like the process being gone by the time we try to fetch it. Also on Windows/OS X i fails with ErrUnsupported.

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 add a quick comment to that effect?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


r := processMetadata{
name: info.Name,
args: info.Args,
env: env,
title: strings.Join(info.Args, " "),
exe: info.Exe,
pid: info.PID,
ppid: info.PPID,
startTime: info.StartTime,
username: username,
userid: userid,
name: info.Name,
args: info.Args,
env: env,
title: strings.Join(info.Args, " "),
exe: info.Exe,
pid: info.PID,
ppid: info.PPID,
capEffective: capEffective,
capPermitted: capPermitted,
startTime: info.StartTime,
username: username,
userid: userid,
}
r.fields = r.toMap()
return &r, nil
Expand Down
Loading