|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/AbGuthrie/goquery/v2" |
| 8 | + gqconfig "github.com/AbGuthrie/goquery/v2/config" |
| 9 | + gqhosts "github.com/AbGuthrie/goquery/v2/hosts" |
| 10 | + gqmodels "github.com/AbGuthrie/goquery/v2/models" |
| 11 | + "github.com/kolide/fleet/server/kolide" |
| 12 | + "github.com/kolide/fleet/server/service" |
| 13 | + "github.com/pkg/errors" |
| 14 | + "github.com/urfave/cli" |
| 15 | +) |
| 16 | + |
| 17 | +type activeQuery struct { |
| 18 | + status string |
| 19 | + results []map[string]string |
| 20 | +} |
| 21 | + |
| 22 | +type goqueryClient struct { |
| 23 | + client *service.Client |
| 24 | + queryCounter int |
| 25 | + queries map[string]activeQuery |
| 26 | + // goquery passes the UUID, while we need the hostname (or ID) to |
| 27 | + // query against Fleet. Keep a mapping so that we know how to target |
| 28 | + // the host. |
| 29 | + hostnameByUUID map[string]string |
| 30 | +} |
| 31 | + |
| 32 | +func newGoqueryClient(fleetClient *service.Client) *goqueryClient { |
| 33 | + return &goqueryClient{ |
| 34 | + client: fleetClient, |
| 35 | + queryCounter: 0, |
| 36 | + queries: make(map[string]activeQuery), |
| 37 | + hostnameByUUID: make(map[string]string), |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func (c *goqueryClient) CheckHost(query string) (gqhosts.Host, error) { |
| 42 | + res, err := c.client.SearchTargets(query, nil, nil) |
| 43 | + if err != nil { |
| 44 | + return gqhosts.Host{}, err |
| 45 | + } |
| 46 | + |
| 47 | + var host *kolide.Host |
| 48 | + for _, h := range res.Hosts { |
| 49 | + // We allow hosts to be looked up by hostname in addition to UUID |
| 50 | + if query == h.UUID || query == h.HostName || query == h.ComputerName { |
| 51 | + host = &h |
| 52 | + break |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if host == nil { |
| 57 | + return gqhosts.Host{}, fmt.Errorf("host %s not found", query) |
| 58 | + } |
| 59 | + |
| 60 | + c.hostnameByUUID[host.UUID] = host.HostName |
| 61 | + |
| 62 | + return gqhosts.Host{ |
| 63 | + UUID: host.UUID, |
| 64 | + ComputerName: host.ComputerName, |
| 65 | + Platform: host.Platform, |
| 66 | + Version: host.OsqueryVersion, |
| 67 | + }, nil |
| 68 | +} |
| 69 | + |
| 70 | +func (c *goqueryClient) ScheduleQuery(uuid, query string) (string, error) { |
| 71 | + c.queryCounter++ |
| 72 | + queryName := strconv.Itoa(c.queryCounter) |
| 73 | + |
| 74 | + hostname, ok := c.hostnameByUUID[uuid] |
| 75 | + if !ok { |
| 76 | + return "", errors.New("could not lookup host") |
| 77 | + } |
| 78 | + |
| 79 | + res, err := c.client.LiveQuery(query, []string{}, []string{hostname}) |
| 80 | + if err != nil { |
| 81 | + return "", err |
| 82 | + } |
| 83 | + |
| 84 | + c.queries[queryName] = activeQuery{status: "Pending"} |
| 85 | + |
| 86 | + // We need to start a separate thread due to goquery expecting |
| 87 | + // scheduling a query and retrieving results to be separate |
| 88 | + // operations. |
| 89 | + go func() { |
| 90 | + select { |
| 91 | + case hostResult := <-res.Results(): |
| 92 | + c.queries[queryName] = activeQuery{status: "Completed", results: hostResult.Rows} |
| 93 | + |
| 94 | + // Print an error |
| 95 | + case err := <-res.Errors(): |
| 96 | + c.queries[queryName] = activeQuery{status: "error: " + err.Error()} |
| 97 | + } |
| 98 | + }() |
| 99 | + |
| 100 | + gqhosts.AddQueryToHost(uuid, gqhosts.Query{Name: queryName, SQL: query}) |
| 101 | + return queryName, nil |
| 102 | +} |
| 103 | + |
| 104 | +func (c *goqueryClient) FetchResults(queryName string) (gqmodels.Rows, string, error) { |
| 105 | + res, ok := c.queries[queryName] |
| 106 | + if !ok { |
| 107 | + return nil, "", fmt.Errorf("Unknown query %s", queryName) |
| 108 | + } |
| 109 | + |
| 110 | + return res.results, res.status, nil |
| 111 | +} |
| 112 | + |
| 113 | +func goqueryCommand() cli.Command { |
| 114 | + return cli.Command{ |
| 115 | + Name: "goquery", |
| 116 | + Usage: "Start the goquery interface", |
| 117 | + Flags: []cli.Flag{ |
| 118 | + configFlag(), |
| 119 | + contextFlag(), |
| 120 | + yamlFlag(), |
| 121 | + }, |
| 122 | + Action: func(c *cli.Context) error { |
| 123 | + fleet, err := clientFromCLI(c) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + goquery.Run(newGoqueryClient(fleet), gqconfig.Config{}) |
| 129 | + return nil |
| 130 | + }, |
| 131 | + } |
| 132 | +} |
0 commit comments