2020-12-17 03:24:23 +00:00
|
|
|
package osquery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"time"
|
|
|
|
|
2020-12-21 23:57:38 +00:00
|
|
|
"github.com/fleetdm/orbit/pkg/process"
|
2020-12-17 03:24:23 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-01-14 02:21:25 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-12-17 03:24:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Runner struct {
|
|
|
|
proc *process.Process
|
|
|
|
cmd *exec.Cmd
|
|
|
|
cancel func()
|
|
|
|
}
|
|
|
|
|
2020-12-17 22:48:12 +00:00
|
|
|
func NewRunner(options ...func(*Runner) error) (*Runner, error) {
|
2020-12-17 03:24:23 +00:00
|
|
|
r := &Runner{}
|
|
|
|
|
|
|
|
// TODO set path and flags appropriately
|
|
|
|
cmd := exec.Command(
|
|
|
|
"osqueryd",
|
|
|
|
"--pidfile=/tmp/osquery.pid",
|
|
|
|
"--database_path=/tmp/osquery.test.db",
|
|
|
|
"--extensions_socket=/tmp/osquery.em",
|
|
|
|
"--config_path=/tmp/osquery.conf",
|
|
|
|
"--logger_path=/tmp",
|
|
|
|
)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
r.cmd = cmd
|
|
|
|
r.proc = process.NewWithCmd(cmd)
|
|
|
|
|
2020-12-17 22:48:12 +00:00
|
|
|
for _, option := range options {
|
|
|
|
err := option(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "apply option")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 03:24:23 +00:00
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2020-12-17 22:48:12 +00:00
|
|
|
func WithFlags(flags []string) func(*Runner) error {
|
|
|
|
return func(r *Runner) error {
|
|
|
|
r.cmd.Args = append(r.cmd.Args, flags...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 17:36:06 +00:00
|
|
|
func WithEnv(env []string) func(*Runner) error {
|
|
|
|
return func(r *Runner) error {
|
|
|
|
r.cmd.Env = append(r.cmd.Env, env...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 03:00:16 +00:00
|
|
|
func WithPath(path string) func(*Runner) error {
|
|
|
|
return func(r *Runner) error {
|
|
|
|
r.cmd.Path = path
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 01:00:46 +00:00
|
|
|
// WithShell adds the -S flag to run an osqueryi shell.
|
|
|
|
func WithShell() func(*Runner) error {
|
|
|
|
return func(r *Runner) error {
|
|
|
|
r.cmd.Args = append(r.cmd.Args, "-S")
|
|
|
|
r.cmd.Stdout = os.Stdout
|
|
|
|
r.cmd.Stderr = os.Stderr
|
|
|
|
r.cmd.Stdin = os.Stdin
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 03:24:23 +00:00
|
|
|
func (r *Runner) Execute() error {
|
2021-01-14 02:21:25 +00:00
|
|
|
log.Debug().Str("cmd", r.cmd.String()).Msg("Run osquery")
|
|
|
|
|
2021-01-13 03:00:16 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
r.cancel = cancel
|
|
|
|
|
2020-12-17 03:24:23 +00:00
|
|
|
if err := r.proc.Start(); err != nil {
|
|
|
|
return errors.Wrap(err, "start osquery")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.proc.StopOrKill(ctx, 10*time.Second); err != nil {
|
|
|
|
return errors.Wrap(err, "osquery exited with error")
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("osquery exited unexpectedly")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) Interrupt(err error) {
|
2021-01-14 02:21:25 +00:00
|
|
|
log.Debug().Msg("interrupt osquery")
|
2020-12-17 03:24:23 +00:00
|
|
|
r.cancel()
|
|
|
|
}
|