mirror of
https://github.com/fleetdm/fleet
synced 2026-04-26 07:57:29 +00:00
> Related issue: #19886 # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://fleetdm.com/docs/contributing/committing-changes#changes-files) for more information. - [x] Added/updated tests - [x] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [x] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. - [x] Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).
89 lines
2.4 KiB
Go
89 lines
2.4 KiB
Go
// based on github.com/kolide/launcher/pkg/osquery/tables
|
|
package cryptsetup
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/fleetdm/fleet/v4/orbit/pkg/dataflatten"
|
|
"github.com/fleetdm/fleet/v4/orbit/pkg/table/dataflattentable"
|
|
"github.com/fleetdm/fleet/v4/orbit/pkg/table/tablehelpers"
|
|
"github.com/osquery/osquery-go/plugin/table"
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
var cryptsetupPaths = []string{
|
|
"/usr/sbin/cryptsetup",
|
|
"/sbin/cryptsetup",
|
|
}
|
|
|
|
const allowedNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-/_"
|
|
|
|
type Table struct {
|
|
logger zerolog.Logger
|
|
name string
|
|
}
|
|
|
|
func TablePlugin(logger zerolog.Logger) *table.Plugin {
|
|
columns := dataflattentable.Columns(
|
|
table.TextColumn("name"),
|
|
)
|
|
|
|
t := &Table{
|
|
logger: logger.With().Str("table", "cryptsetup_status").Logger(),
|
|
name: "cryptsetup_status",
|
|
}
|
|
|
|
return table.NewPlugin(t.name, columns, t.generate)
|
|
}
|
|
|
|
func (t *Table) generate(ctx context.Context, queryContext table.QueryContext) ([]map[string]string, error) {
|
|
var results []map[string]string
|
|
|
|
requestedNames := tablehelpers.GetConstraints(queryContext, "name",
|
|
tablehelpers.WithAllowedCharacters(allowedNameCharacters),
|
|
tablehelpers.WithLogger(t.logger),
|
|
)
|
|
|
|
if len(requestedNames) == 0 {
|
|
return results, fmt.Errorf("The %s table requires that you specify a constraint for name", t.name)
|
|
}
|
|
|
|
for _, name := range requestedNames {
|
|
output, err := tablehelpers.Exec(ctx, t.logger, 15, cryptsetupPaths, []string{"--readonly", "status", name}, false)
|
|
if err != nil {
|
|
t.logger.Debug().Err(err).Str("name", name).Msg("Error execing for status")
|
|
continue
|
|
}
|
|
|
|
status, err := parseStatus(output)
|
|
if err != nil {
|
|
t.logger.Info().Err(err).Str("name", name).Msg("Error parsing status")
|
|
continue
|
|
}
|
|
|
|
for _, dataQuery := range tablehelpers.GetConstraints(queryContext, "query", tablehelpers.WithDefaults("*")) {
|
|
flatData, err := t.flattenOutput(dataQuery, status)
|
|
if err != nil {
|
|
t.logger.Info().Err(err).Msg("flatten failed")
|
|
continue
|
|
}
|
|
|
|
rowData := map[string]string{"name": name}
|
|
|
|
results = append(results, dataflattentable.ToMap(flatData, dataQuery, rowData)...)
|
|
}
|
|
}
|
|
|
|
return results, nil
|
|
}
|
|
|
|
func (t *Table) flattenOutput(dataQuery string, status map[string]interface{}) ([]dataflatten.Row, error) {
|
|
flattenOpts := []dataflatten.FlattenOpts{
|
|
dataflatten.WithLogger(t.logger),
|
|
dataflatten.WithQuery(strings.Split(dataQuery, "/")),
|
|
}
|
|
|
|
return dataflatten.Flatten(status, flattenOpts...)
|
|
}
|