Skip to content

Commit

Permalink
[NPM-3662] Add sestatus to agent flare (#32068)
Browse files Browse the repository at this point in the history
  • Loading branch information
pimlu authored Dec 13, 2024
1 parent e87eaec commit 37fbbc8
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 3 deletions.
41 changes: 41 additions & 0 deletions cmd/system-probe/api/debug/handlers_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024-present Datadog, Inc.

//go:build linux

// Package debug contains handlers for debug information global to all of system-probe
package debug

import (
"context"
"errors"
"fmt"
"net/http"
"os/exec"
"time"
)

// HandleSelinuxSestatus reports the output of sestatus as an http result
func HandleSelinuxSestatus(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()

cmd := exec.CommandContext(ctx, "sestatus")
output, err := cmd.CombinedOutput()

var execError *exec.Error
var exitErr *exec.ExitError

if err != nil {
// don't 500 for ExitErrors etc, to report "normal" failures to the selinux_sestatus.log file
if !errors.As(err, &execError) && !errors.As(err, &exitErr) {
w.WriteHeader(500)
}
fmt.Fprintf(w, "command failed: %s\n%s", err, output)
return
}

w.Write(output)
}
20 changes: 20 additions & 0 deletions cmd/system-probe/api/debug/handlers_nolinux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024-present Datadog, Inc.

//go:build !linux

// Package debug contains handlers for debug information global to all of system-probe
package debug

import (
"io"
"net/http"
)

// HandleSelinuxSestatus is not supported
func HandleSelinuxSestatus(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(500)
io.WriteString(w, "HandleSelinuxSestatus is not supported on this platform")
}
2 changes: 2 additions & 0 deletions cmd/system-probe/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

gorilla "github.com/gorilla/mux"

"github.com/DataDog/datadog-agent/cmd/system-probe/api/debug"
"github.com/DataDog/datadog-agent/cmd/system-probe/api/module"
"github.com/DataDog/datadog-agent/cmd/system-probe/api/server"
sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
Expand Down Expand Up @@ -58,6 +59,7 @@ func StartServer(cfg *sysconfigtypes.Config, telemetry telemetry.Component, wmet

if runtime.GOOS == "linux" {
mux.HandleFunc("/debug/ebpf_btf_loader_info", ebpf.HandleBTFLoaderInfo)
mux.HandleFunc("/debug/selinux_sestatus", debug.HandleSelinuxSestatus)
}

go func() {
Expand Down
5 changes: 2 additions & 3 deletions pkg/ebpf/debug_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
package ebpf

import (
"fmt"
"io"
"net/http"

"github.com/DataDog/datadog-agent/pkg/util/log"
)

// HandleBTFLoaderInfo responds with where the system-probe found BTF data (and
// if it was in a pre-bundled tarball, where within that tarball it came from)
func HandleBTFLoaderInfo(w http.ResponseWriter, _ *http.Request) {
info, err := GetBTFLoaderInfo()
if err != nil {
log.Errorf("unable to get ebpf_btf_loader info: %s", err)
fmt.Fprintf(w, "unable to get ebpf_btf_loader info: %s", err)
w.WriteHeader(500)
return
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/flare/archive_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func addSystemProbePlatformSpecificEntries(fb flaretypes.FlareBuilder) {
_ = fb.AddFileFromFunc(filepath.Join("system-probe", "conntrack_cached.log"), getSystemProbeConntrackCached)
_ = fb.AddFileFromFunc(filepath.Join("system-probe", "conntrack_host.log"), getSystemProbeConntrackHost)
_ = fb.AddFileFromFunc(filepath.Join("system-probe", "ebpf_btf_loader.log"), getSystemProbeBTFLoaderInfo)
_ = fb.AddFileFromFunc(filepath.Join("system-probe", "selinux_sestatus.log"), getSystemProbeSelinuxSestatus)
}
}

Expand Down Expand Up @@ -148,3 +149,9 @@ func getSystemProbeBTFLoaderInfo() ([]byte, error) {
url := sysprobeclient.DebugURL("/ebpf_btf_loader_info")
return getHTTPData(sysProbeClient, url)
}

func getSystemProbeSelinuxSestatus() ([]byte, error) {
sysProbeClient := sysprobeclient.Get(getSystemProbeSocketPath())
url := sysprobeclient.DebugURL("/selinux_sestatus")
return getHTTPData(sysProbeClient, url)
}
11 changes: 11 additions & 0 deletions releasenotes/notes/agent-flare-sestatus-5820cfc79ec91d1f.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Each section from every release note are combined when the
# CHANGELOG.rst is rendered. So the text needs to be worded so that
# it does not depend on any information only available in another
# section. This may mean repeating some details, but each section
# must be readable independently of the other.
#
# Each section note must be formatted as reStructuredText.
---
enhancements:
- |
Added the output of ``sestatus`` into the Agent flare. This information will appear in ``system-probe/selinux_sestatus.log``.

0 comments on commit 37fbbc8

Please sign in to comment.