osquery

package
v0.5.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func New(runner exec.Runner) (Querier, error)

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"])
	}
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL