Documentation
¶
Overview ¶
Package osquery integrates the osquery binary for system queries through an injected exec.Runner.
Build a Querier with a Runner and call its methods; every query is escalated through the Runner. Both the convenience table path AND the RawSql path refuse a curated deny-list of credential-bearing tables before running anything — a raw query that references a deny-listed table (in any clause) is rejected, so there is no path to read shadow/sudoers/… via osquery.
r, _ := exec.NewRunner(exec.Sudo)
q, err := osquery.New(r) // ErrNotInstalled if osqueryi is absent
if err != nil { ... }
rows, err := q.QueryTable(ctx, "os_version")
New is a single-implementation capability (design §3.8): it exposes the Querier interface for shape-uniformity with the backend-pattern packages even though osquery is the only implementation. There is no Backend argument — only the required Runner.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotInstalled is returned when osquery is not installed on the system. ErrNotInstalled = errors.New("osquery is not installed") // ErrQueryFailed is returned when an osquery query fails. ErrQueryFailed = errors.New("osquery query failed") )
Functions ¶
This section is empty.
Types ¶
type Querier ¶
type Querier interface {
// IsInstalled reports, live, whether an osqueryi binary is currently
// reachable. New already fails closed with ErrNotInstalled when the binary
// is absent at construction; IsInstalled re-probes so a caller can detect
// the binary being removed during the agent's lifetime. The ctx is accepted
// for shape-uniformity; the probe itself is a filesystem lookup.
IsInstalled(ctx context.Context) bool
// ListTables returns the names of the available osquery tables.
ListTables(ctx context.Context) ([]string, error)
// Query runs a structured query. BOTH the table path and the RawSql path are
// gated by the credential-bearing deny-list before any SQL is built or run —
// a raw query referencing a deny-listed table is refused. A query failure
// (or a policy refusal) is folded into the returned *pb.OSQueryResult
// (Success=false), not returned as a Go error.
Query(ctx context.Context, query *pb.OSQuery) (*pb.OSQueryResult, error)
// QueryTable runs SELECT * FROM <table> after the same validity + deny-list
// checks as Query's table path.
QueryTable(ctx context.Context, tableName string) ([]*pb.OSQueryRow, error)
// QuerySQL runs raw SQL and parses the JSON result rows.
QuerySQL(ctx context.Context, sql string) ([]*pb.OSQueryRow, error)
}
Querier is the osquery surface: a small, ctx-first interface over the osquery binary. It is single-implementation by nature (§3.8) — there is no second way to run osquery — but it is an interface so a consumer learns the same construct-a-handle shape as every other capability.
func New ¶
New creates an osquery Querier driven by runner. Returns ErrNotInstalled when the osqueryi binary is not found (eager fail-closed probe, so a caller learns at construction that osquery is unavailable), and an error when runner is nil.
Example ¶
ExampleNew shows the construct-a-handle flow: pick a Runner, build a Querier, and query a benign table. The credential-table deny-list and table-name validation are enforced inside QueryTable/Query.
package main
import (
"context"
"errors"
"fmt"
"log"
"github.com/manchtools/power-manage-sdk/sys/exec"
"github.com/manchtools/power-manage-sdk/sys/osquery"
)
func main() {
r, err := exec.NewRunner(exec.Sudo) // the consumer picks the escalation backend
if err != nil {
log.Fatal(err)
}
q, err := osquery.New(r)
if errors.Is(err, osquery.ErrNotInstalled) {
// osquery isn't installed on this host — skip osquery-dependent features.
return
}
if err != nil {
log.Fatal(err)
}
rows, err := q.QueryTable(context.Background(), "os_version")
if err != nil {
log.Fatal(err)
}
for _, row := range rows {
fmt.Println(row.Data["name"])
}
}
Output: