github.com/Uptycs/basequery-go@v0.8.0/cmd/examples/table/main.go (about) 1 package main 2 3 import ( 4 "context" 5 "flag" 6 "fmt" 7 "log" 8 "sync" 9 "time" 10 11 osquery "github.com/Uptycs/basequery-go" 12 "github.com/Uptycs/basequery-go/plugin/table" 13 ) 14 15 var ( 16 socket = flag.String("socket", "", "Path to the extensions UNIX domain socket") 17 timeout = flag.Int("timeout", 3, "Seconds to wait for autoloaded extensions") 18 interval = flag.Int("interval", 3, "Seconds delay between connectivity checks") 19 mutableData []map[string]string 20 lock sync.RWMutex 21 ) 22 23 func main() { 24 flag.Parse() 25 if *socket == "" { 26 log.Fatalln("Missing required --socket argument") 27 } 28 serverTimeout := osquery.ServerTimeout( 29 time.Second * time.Duration(*timeout), 30 ) 31 serverPingInterval := osquery.ServerPingInterval( 32 time.Second * time.Duration(*interval), 33 ) 34 serverPromPort := osquery.ServerPrometheusPort(3000) 35 36 mutableData = []map[string]string{ 37 { 38 "i": "1234", 39 "b": "12345678900", 40 "d": "1.2345", 41 "t": "hello", 42 }, 43 { 44 "i": "-1234", 45 "b": "-12345678900", 46 "d": "-1.2345", 47 "t": "world", 48 }, 49 } 50 51 server, err := osquery.NewExtensionManagerServer( 52 "example_extension", 53 *socket, 54 serverTimeout, 55 serverPingInterval, 56 serverPromPort, 57 ) 58 59 if err != nil { 60 log.Fatalf("Error creating extension: %s\n", err) 61 } 62 server.RegisterPlugin(table.NewPlugin("example_table", ExampleColumns(), ExampleGenerate)) 63 server.RegisterPlugin(table.NewMutablePlugin("mutable_table", MutableColumns(), MutableGenerate, MutableInsert, MutableUpdate, MutableDelete)) 64 if err := server.Run(); err != nil { 65 log.Fatal(err) 66 } 67 } 68 69 // ExampleColumns returns the example table columns. 70 func ExampleColumns() []table.ColumnDefinition { 71 return []table.ColumnDefinition{ 72 table.TextColumn("text"), 73 table.IntegerColumn("integer"), 74 table.BigIntColumn("big_int"), 75 table.DoubleColumn("double"), 76 } 77 } 78 79 // ExampleGenerate is called when select is run on example table. It returns static one row data. 80 func ExampleGenerate(ctx context.Context, queryContext table.QueryContext) ([]map[string]string, error) { 81 return []map[string]string{ 82 { 83 "text": "hello world", 84 "integer": "123", 85 "big_int": "-1234567890", 86 "double": "3.14159", 87 }, 88 }, nil 89 } 90 91 // MutableColumns returns the mutable table columns. 92 func MutableColumns() []table.ColumnDefinition { 93 return []table.ColumnDefinition{ 94 table.IntegerColumn("i"), 95 table.BigIntColumn("b"), 96 table.DoubleColumn("d"), 97 table.TextColumn("t"), 98 } 99 } 100 101 // MutableGenerate is called when mutable table is queried. It returns cached data. 102 func MutableGenerate(ctx context.Context, queryContext table.QueryContext) ([]map[string]string, error) { 103 lock.RLock() 104 defer lock.RUnlock() 105 return mutableData, nil 106 } 107 108 // MutableInsert is called when mutable table is inserted into 109 func MutableInsert(ctx context.Context, autoRowID bool, row []interface{}) ([]map[string]string, error) { 110 id := fmt.Sprintf("%d", int(row[0].(float64))) 111 lock.Lock() 112 mutableData = append(mutableData, map[string]string{ 113 "i": id, 114 "b": fmt.Sprintf("%v", row[1]), 115 "d": fmt.Sprintf("%f", row[2]), 116 "t": fmt.Sprintf("%s", row[3]), 117 }) 118 lock.Unlock() 119 120 return []map[string]string{{"id": id, "status": "success"}}, nil 121 } 122 123 // MutableUpdate is called when mutable tale is updated 124 func MutableUpdate(ctx context.Context, rowID int64, row []interface{}) error { 125 id := fmt.Sprintf("%d", int(row[0].(float64))) 126 lock.Lock() 127 mutableData[rowID] = map[string]string{ 128 "i": id, 129 "b": fmt.Sprintf("%v", row[1]), 130 "d": fmt.Sprintf("%f", row[2]), 131 "t": fmt.Sprintf("%s", row[3]), 132 } 133 lock.Unlock() 134 135 return nil 136 } 137 138 // MutableDelete is called when mutable table rows are deleted 139 func MutableDelete(ctx context.Context, rowID int64) error { 140 lock.Lock() 141 mutableData = append(mutableData[:rowID], mutableData[rowID+1:]...) 142 lock.Unlock() 143 144 return nil 145 }