Skip to content

Commit

Permalink
extra metadata in creports
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Quest <[email protected]>
  • Loading branch information
kcq committed Jan 20, 2024
1 parent b04d388 commit 626e2ab
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 17 deletions.
14 changes: 9 additions & 5 deletions pkg/app/sensor/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ func Run() {
}

errutil.FailOn(configureLogger(*enableDebug, *logLevel, *logFormat, *logFile))

ctx := context.Background()

if len(os.Args) > 1 && os.Args[1] == "control" {
if err := runControlCommand(ctx); err != nil {
fmt.Fprintln(os.Stderr, "Control command failed: "+err.Error())
Expand All @@ -139,8 +137,14 @@ func Run() {

activeCaps, maxCaps, err := sysenv.Capabilities(0)
errutil.WarnOn(err)
log.Infof("sensor: ver=%v", version.Current())
log.Debugf("sensor: args => %#v", os.Args)

sr := &report.SensorReport{
Version: version.Current(),
Args: os.Args,
}

log.Infof("sensor: ver=%v", sr.Version)
log.Debugf("sensor: args => %#v", sr.Args)

log.Tracef("sensor: uid=%v euid=%v", os.Getuid(), os.Geteuid())
log.Tracef("sensor: privileged => %v", sysenv.IsPrivileged())
Expand All @@ -156,7 +160,7 @@ func Run() {
if len(*logFile) > 0 {
artifactsExtra = append(artifactsExtra, *logFile)
}
artifactor := artifact.NewProcessor(*artifactsDir, artifactsExtra)
artifactor := artifact.NewProcessor(sr, *artifactsDir, artifactsExtra)

exe, err := newExecution(
ctx,
Expand Down
37 changes: 29 additions & 8 deletions pkg/app/sensor/artifact/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/slimtoolkit/slim/pkg/sysidentity"
"github.com/slimtoolkit/slim/pkg/system"
"github.com/slimtoolkit/slim/pkg/util/fsutil"
"github.com/slimtoolkit/slim/pkg/version"
)

const (
Expand Down Expand Up @@ -282,16 +281,16 @@ type Processor interface {
}

type processor struct {
seReport *report.SensorReport
artifactsDirName string

// Extra files to put into the artifacts archive before exiting.
artifactsExtra []string

origPathMap map[string]struct{}
origPathMap map[string]struct{}
}

func NewProcessor(artifactsDirName string, artifactsExtra []string) Processor {
func NewProcessor(seReport *report.SensorReport, artifactsDirName string, artifactsExtra []string) Processor {
return &processor{
seReport: seReport,
artifactsDirName: artifactsDirName,
artifactsExtra: artifactsExtra,
}
Expand Down Expand Up @@ -458,7 +457,7 @@ func (a *processor) Process(

logger.Debugf("len(fanReport.ProcessFiles)=%v / fileCount=%v", len(fanReport.ProcessFiles), fileCount)
allFilesMap := findSymlinks(fileList, mountPoint, cmd.Excludes)
return saveResults(a.origPathMap, a.artifactsDirName, cmd, allFilesMap, fanReport, ptReport, peReport)
return saveResults(a.origPathMap, a.artifactsDirName, cmd, allFilesMap, fanReport, ptReport, peReport, a.seReport)
}

func (a *processor) Archive() error {
Expand Down Expand Up @@ -507,10 +506,19 @@ func saveResults(
fanMonReport *report.FanMonitorReport,
ptMonReport *report.PtMonitorReport,
peReport *report.PeMonitorReport,
seReport *report.SensorReport,
) error {
log.Debugf("saveResults(%v,...)", len(fileNames))

artifactStore := newStore(origPathMap, artifactsDirName, fileNames, fanMonReport, ptMonReport, peReport, cmd)
artifactStore := newStore(origPathMap,
artifactsDirName,
fileNames,
fanMonReport,
ptMonReport,
peReport,
seReport,
cmd)

artifactStore.prepareArtifacts()
artifactStore.saveArtifacts()
artifactStore.enumerateArtifacts()
Expand All @@ -529,6 +537,7 @@ type store struct {
fanMonReport *report.FanMonitorReport
ptMonReport *report.PtMonitorReport
peMonReport *report.PeMonitorReport
seReport *report.SensorReport
rawNames map[string]*report.ArtifactProps
nameList []string
resolve map[string]struct{}
Expand All @@ -546,13 +555,15 @@ func newStore(
fanMonReport *report.FanMonitorReport,
ptMonReport *report.PtMonitorReport,
peMonReport *report.PeMonitorReport,
seReport *report.SensorReport,
cmd *command.StartMonitor) *store {
store := &store{
origPathMap: origPathMap,
storeLocation: storeLocation,
fanMonReport: fanMonReport,
ptMonReport: ptMonReport,
peMonReport: peMonReport,
seReport: seReport,
rawNames: rawNames,
nameList: make([]string, 0, len(rawNames)),
resolve: map[string]struct{}{},
Expand Down Expand Up @@ -2382,13 +2393,23 @@ func (p *store) saveReport() error {
defer logger.Trace("exit")

creport := report.ContainerReport{
SensorVersion: version.Current(),
Sensor: p.seReport,
Monitors: report.MonitorReports{
Pt: p.ptMonReport,
Fan: p.fanMonReport,
},
}

if p.cmd != nil {
creport.StartCommand = &report.StartCommandReport{
AppName: p.cmd.AppName,
AppArgs: p.cmd.AppArgs,
AppUser: p.cmd.AppUser,
AppEntrypoint: p.cmd.AppEntrypoint,
AppCmd: p.cmd.AppCmd,
}
}

sinfo := system.GetSystemInfo()
creport.System = report.SystemReport{
Type: sinfo.Sysname,
Expand Down
24 changes: 20 additions & 4 deletions pkg/report/container_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,28 @@ type SystemReport struct {
Distro DistroInfo `json:"distro"`
}

// SensorReport provides a basic sensor report for the container environment
type SensorReport struct {
Version string `json:"version"`
Args []string `json:"args"`
}

// StartCommandReport provides a basic start command report for the container environment
type StartCommandReport struct {
AppName string `json:"app_name"`
AppArgs []string `json:"app_args,omitempty"`
AppEntrypoint []string `json:"app_entrypoint,omitempty"`
AppCmd []string `json:"app_cmd,omitempty"`
AppUser string `json:"app_user,omitempty"`
}

// ContainerReport contains container report fields
type ContainerReport struct {
SensorVersion string `json:"system"`
System SystemReport `json:"system"`
Monitors MonitorReports `json:"monitors"`
Image ImageReport `json:"image"`
StartCommand *StartCommandReport `json:"start_command"`
Sensor *SensorReport `json:"sensor"`
System SystemReport `json:"system"`
Monitors MonitorReports `json:"monitors"`
Image ImageReport `json:"image"`
}

// PermSetFromFlags maps artifact flags to permissions
Expand Down

0 comments on commit 626e2ab

Please sign in to comment.