Move shell to separate file

This commit is contained in:
Zach Wasserman 2021-03-09 15:36:57 -08:00
parent ad9f9be411
commit f38206a927
2 changed files with 90 additions and 73 deletions

View file

@ -309,76 +309,3 @@ func main() {
log.Error().Err(err).Msg("")
}
}
var shellCommand = &cli.Command{
Name: "shell",
Aliases: []string{"osqueryi"},
Usage: "Run the osqueryi shell",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "osquery-channel",
Usage: "Channel of osquery version to use",
Value: "stable",
EnvVars: []string{"ORBIT_OSQUERY_CHANNEL"},
},
&cli.BoolFlag{
Name: "debug",
Usage: "Enable debug logging",
EnvVars: []string{"ORBIT_DEBUG"},
},
},
Action: func(c *cli.Context) error {
if c.Bool("debug") {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
if err := os.MkdirAll(c.String("root-dir"), constant.DefaultDirMode); err != nil {
return errors.Wrap(err, "initialize root dir")
}
localStore, err := filestore.New(filepath.Join(c.String("root-dir"), "tuf-metadata.json"))
if err != nil {
log.Fatal().Err(err).Msg("failed to create local metadata store")
}
// Initialize updater and get expected version
opt := update.DefaultOptions
opt.RootDirectory = c.String("root-dir")
opt.ServerURL = c.String("tuf-url")
opt.LocalStore = localStore
opt.InsecureTransport = c.Bool("insecure")
updater, err := update.New(opt)
if err != nil {
return err
}
if err := updater.UpdateMetadata(); err != nil {
log.Info().Err(err).Msg("failed to update metadata. using saved metadata.")
}
osquerydPath, err := updater.Get("osqueryd", c.String("osquery-channel"))
if err != nil {
return err
}
var g run.Group
// Create an osquery runner with the provided options
r, _ := osquery.NewRunner(
osquerydPath,
osquery.WithShell(),
// Handle additional args after --
osquery.WithFlags(c.Args().Slice()),
)
g.Add(r.Execute, r.Interrupt)
// Install a signal handler
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
g.Add(run.SignalHandler(ctx, os.Interrupt, os.Kill))
if err := g.Run(); err != nil {
log.Error().Err(err).Msg("unexpected exit")
}
return nil
},
}

90
cmd/orbit/shell.go Normal file
View file

@ -0,0 +1,90 @@
package main
import (
"context"
"os"
"path/filepath"
"github.com/fleetdm/orbit/pkg/constant"
"github.com/fleetdm/orbit/pkg/osquery"
"github.com/fleetdm/orbit/pkg/update"
"github.com/fleetdm/orbit/pkg/update/filestore"
"github.com/oklog/run"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
)
var shellCommand = &cli.Command{
Name: "shell",
Aliases: []string{"osqueryi"},
Usage: "Run the osqueryi shell",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "osqueryd-channel",
Usage: "Channel of osqueryd version to use",
Value: "stable",
EnvVars: []string{"ORBIT_OSQUERYD_CHANNEL"},
},
&cli.BoolFlag{
Name: "debug",
Usage: "Enable debug logging",
EnvVars: []string{"ORBIT_DEBUG"},
},
},
Action: func(c *cli.Context) error {
if c.Bool("debug") {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
if err := os.MkdirAll(c.String("root-dir"), constant.DefaultDirMode); err != nil {
return errors.Wrap(err, "initialize root dir")
}
localStore, err := filestore.New(filepath.Join(c.String("root-dir"), "tuf-metadata.json"))
if err != nil {
log.Fatal().Err(err).Msg("failed to create local metadata store")
}
// Initialize updater and get expected version
opt := update.DefaultOptions
opt.RootDirectory = c.String("root-dir")
opt.ServerURL = c.String("tuf-url")
opt.LocalStore = localStore
opt.InsecureTransport = c.Bool("insecure")
updater, err := update.New(opt)
if err != nil {
return err
}
if err := updater.UpdateMetadata(); err != nil {
log.Info().Err(err).Msg("failed to update metadata. using saved metadata.")
}
osquerydPath, err := updater.Get("osqueryd", c.String("osquery-channel"))
if err != nil {
return err
}
var g run.Group
// Create an osquery runner with the provided options
r, _ := osquery.NewRunner(
osquerydPath,
osquery.WithShell(),
// Handle additional args after --
osquery.WithFlags(c.Args().Slice()),
)
g.Add(r.Execute, r.Interrupt)
// Install a signal handler
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
g.Add(run.SignalHandler(ctx, os.Interrupt, os.Kill))
if err := g.Run(); err != nil {
log.Error().Err(err).Msg("unexpected exit")
}
return nil
},
}