From edf6d82e5a2e1e851f632945b2ef2cd082675a67 Mon Sep 17 00:00:00 2001 From: RebeccaMahany Date: Wed, 23 Oct 2024 12:11:48 -0400 Subject: [PATCH] Remove unused commands --- cmd/launcher/main.go | 4 -- cmd/launcher/run_query.go | 98 -------------------------------------- cmd/launcher/run_socket.go | 84 -------------------------------- 3 files changed, 186 deletions(-) delete mode 100644 cmd/launcher/run_query.go delete mode 100644 cmd/launcher/run_socket.go diff --git a/cmd/launcher/main.go b/cmd/launcher/main.go index a8eaf0818..dd8342c7b 100644 --- a/cmd/launcher/main.go +++ b/cmd/launcher/main.go @@ -168,10 +168,6 @@ func runMain() int { func runSubcommands(systemMultiSlogger *multislogger.MultiSlogger) error { var run func(*multislogger.MultiSlogger, []string) error switch os.Args[1] { - case "socket": - run = runSocket - case "query": - run = runQuery case "doctor": run = runDoctor case "flare": diff --git a/cmd/launcher/run_query.go b/cmd/launcher/run_query.go deleted file mode 100644 index e64289aaf..000000000 --- a/cmd/launcher/run_query.go +++ /dev/null @@ -1,98 +0,0 @@ -package main - -import ( - "encoding/json" - "errors" - "flag" - "fmt" - "io" - "os" - "time" - - "github.com/kolide/kit/env" - "github.com/kolide/launcher/pkg/log/multislogger" - osquerygo "github.com/osquery/osquery-go" -) - -type queryFile struct { - Queries map[string]string `json:"queries"` -} - -func runQuery(_ *multislogger.MultiSlogger, args []string) error { - flagset := flag.NewFlagSet("launcher query", flag.ExitOnError) - var ( - flQueries = flagset.String( - "queries", - env.String("QUERIES", ""), - "A file containing queries to run", - ) - flSocket = flagset.String( - "socket", - env.String("SOCKET", ""), - "The path to the socket", - ) - ) - flagset.Usage = commandUsage(flagset, "launcher query") - if err := flagset.Parse(args); err != nil { - return err - } - - var queries queryFile - - if _, err := os.Stat(*flQueries); err == nil { - data, err := os.ReadFile(*flQueries) - if err != nil { - return fmt.Errorf("reading supplied queries file: %w", err) - } - if err := json.Unmarshal(data, &queries); err != nil { - return fmt.Errorf("unmarshalling queries file json: %w", err) - } - } - - if *flQueries == "" { - stdinQueries, err := io.ReadAll(os.Stdin) - if err != nil { - return fmt.Errorf("reading stdin: %w", err) - } - if err := json.Unmarshal(stdinQueries, &queries); err != nil { - return fmt.Errorf("unmarshalling stdin queries json: %w", err) - } - } - - if *flSocket == "" { - return errors.New("--socket must be defined") - } - - client, err := osquerygo.NewClient(*flSocket, 5*time.Second) - if err != nil { - return fmt.Errorf("opening osquery client connection on %s: %w", *flSocket, err) - } - defer client.Close() - - results := struct { - Results map[string]interface{} `json:"results"` - }{ - Results: map[string]interface{}{}, - } - - for name, query := range queries.Queries { - resp, err := client.Query(query) - if err != nil { - return fmt.Errorf("running query: %w", err) - } - - if resp.Status.Code != int32(0) { - fmt.Println("Error running query:", resp.Status.Message) - } - - results.Results[name] = resp.Response - } - - enc := json.NewEncoder(os.Stdout) - enc.SetIndent("", " ") - if err := enc.Encode(results); err != nil { - return fmt.Errorf("encoding JSON query results: %w", err) - } - - return nil -} diff --git a/cmd/launcher/run_socket.go b/cmd/launcher/run_socket.go deleted file mode 100644 index 959a0df4c..000000000 --- a/cmd/launcher/run_socket.go +++ /dev/null @@ -1,84 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "log/slog" - "os" - "os/signal" - "path/filepath" - "syscall" - - "github.com/kolide/kit/env" - "github.com/kolide/kit/fsutil" - "github.com/kolide/launcher/ee/agent" - "github.com/kolide/launcher/ee/agent/flags" - "github.com/kolide/launcher/ee/agent/knapsack" - "github.com/kolide/launcher/ee/agent/storage/inmemory" - "github.com/kolide/launcher/pkg/launcher" - "github.com/kolide/launcher/pkg/log/multislogger" - "github.com/kolide/launcher/pkg/osquery/runtime" - "github.com/kolide/launcher/pkg/osquery/table" -) - -func runSocket(systemMultiSlogger *multislogger.MultiSlogger, args []string) error { - flagset := flag.NewFlagSet("launcher socket", flag.ExitOnError) - var ( - flPath = flagset.String( - "path", - env.String("SOCKET_PATH", agent.TempPath("osquery.sock")), - "The path to the socket", - ) - flLauncherTables = flagset.Bool( - "launcher-tables", - false, - "Run with launcher specific tables", - ) - ) - flagset.Usage = commandUsage(flagset, "launcher socket") - if err := flagset.Parse(args); err != nil { - return err - } - - if _, err := os.Stat(filepath.Dir(*flPath)); os.IsNotExist(err) { - if err := os.Mkdir(filepath.Dir(*flPath), fsutil.DirMode); err != nil { - return fmt.Errorf("creating socket path base directory: %w", err) - } - } - - opts := []runtime.OsqueryInstanceOption{ - runtime.WithExtensionSocketPath(*flPath), - } - - if *flLauncherTables { - opts = append(opts, runtime.WithOsqueryExtensionPlugins(table.LauncherTables(nil)...)) - } - - // were passing an empty array here just to get the default options - cmdlineopts, err := launcher.ParseOptions("socket", make([]string, 0)) - if err != nil { - return err - } - // Add handler to write to stdout - systemMultiSlogger.AddHandler(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ - Level: slog.LevelDebug, - AddSource: true, - })) - fcOpts := []flags.Option{flags.WithCmdLineOpts(cmdlineopts)} - flagController := flags.NewFlagController(systemMultiSlogger.Logger, inmemory.NewStore(), fcOpts...) - k := knapsack.New(nil, flagController, nil, nil, nil) - runner := runtime.New(k, opts...) - go runner.Run() - - fmt.Println(*flPath) - - // Wait forever - sig := make(chan os.Signal, 1) - signal.Notify(sig, os.Interrupt, syscall.SIGTERM, syscall.Signal(15)) - <-sig - - // allow for graceful termination. - runner.Shutdown() - - return nil -}