|
| 1 | +package table |
| 2 | + |
| 3 | +// ColumnDefinition defines the relevant information for a column in a table |
| 4 | +// plugin. Both values are mandatory. Prefer using the *Column helpers to |
| 5 | +// create ColumnDefinition structs. |
| 6 | +type ColumnDefinition struct { |
| 7 | + Name string `json:"name,omitempty"` |
| 8 | + Type ColumnType `json:"type,omitempty"` |
| 9 | + Description string `json:"description,omitempty"` |
| 10 | + |
| 11 | + // Options from https://github.com/osquery/osquery/blob/master/osquery/core/sql/column.h#L37 |
| 12 | + Index bool `json:"index"` |
| 13 | + Required bool `json:"required"` |
| 14 | + Additional bool `json:"additional"` |
| 15 | + Optimized bool `json:"optimized"` |
| 16 | + Hidden bool `json:"hidden"` |
| 17 | +} |
| 18 | + |
| 19 | +// ColumnType is a strongly typed representation of the data type string for a |
| 20 | +// column definition. The named constants should be used. |
| 21 | +type ColumnType string |
| 22 | + |
| 23 | +// The following column types are defined in osquery tables.h. |
| 24 | +const ( |
| 25 | + ColumnTypeUnknown ColumnType = "UNKNOWN" |
| 26 | + ColumnTypeText = "TEXT" |
| 27 | + ColumnTypeInteger = "INTEGER" |
| 28 | + ColumnTypeBigInt = "BIGINT" |
| 29 | + ColumnTypeUnsignedBigInt = "UNSIGNED BIGINT" |
| 30 | + ColumnTypeDouble = "DOUBLE" |
| 31 | + ColumnTypeBlob = "BLOB" |
| 32 | +) |
| 33 | + |
| 34 | +type ColumnOpt func(*ColumnDefinition) |
| 35 | + |
| 36 | +// TextColumn is a helper for defining columns containing strings. |
| 37 | +func TextColumn(name string, opts ...ColumnOpt) ColumnDefinition { |
| 38 | + return NewColumn(name, ColumnTypeText, opts...) |
| 39 | +} |
| 40 | + |
| 41 | +// IntegerColumn is a helper for defining columns containing integers. |
| 42 | +func IntegerColumn(name string, opts ...ColumnOpt) ColumnDefinition { |
| 43 | + return NewColumn(name, ColumnTypeInteger, opts...) |
| 44 | +} |
| 45 | + |
| 46 | +// BigIntColumn is a helper for defining columns containing big integers. |
| 47 | +func BigIntColumn(name string, opts ...ColumnOpt) ColumnDefinition { |
| 48 | + return NewColumn(name, ColumnTypeBigInt, opts...) |
| 49 | +} |
| 50 | + |
| 51 | +// DoubleColumn is a helper for defining columns containing floating point |
| 52 | +// values. |
| 53 | +func DoubleColumn(name string, opts ...ColumnOpt) ColumnDefinition { |
| 54 | + return NewColumn(name, ColumnTypeDouble, opts...) |
| 55 | +} |
| 56 | + |
| 57 | +// NewColumn returns a ColumnDefinition for the specified column. |
| 58 | +func NewColumn(name string, ctype ColumnType, opts ...ColumnOpt) ColumnDefinition { |
| 59 | + cd := ColumnDefinition{ |
| 60 | + Name: name, |
| 61 | + Type: ctype, |
| 62 | + } |
| 63 | + |
| 64 | + for _, opt := range opts { |
| 65 | + opt(&cd) |
| 66 | + } |
| 67 | + |
| 68 | + return cd |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | +// IndexColumn is a functional argument to declare this as an indexed |
| 73 | +// column. Depending on impmelentation, this can significantly change |
| 74 | +// performance. See osquery source code for more information. |
| 75 | +func IndexColumn() ColumnOpt { |
| 76 | + return func(cd *ColumnDefinition) { |
| 77 | + cd.Index = true |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// RequiredColumn is a functional argument that sets this as a |
| 82 | +// required column. sqlite will not process queries, if a required |
| 83 | +// column is missing. See osquery source code for more information. |
| 84 | +func RequiredColumn() ColumnOpt { |
| 85 | + return func(cd *ColumnDefinition) { |
| 86 | + cd.Required = true |
| 87 | + } |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +// AdditionalColumn is a functional argument that sets this as an |
| 92 | +// additional column. See osquery source code for more information. |
| 93 | +func AdditionalColumn() ColumnOpt { |
| 94 | + return func(cd *ColumnDefinition) { |
| 95 | + cd.Additional = true |
| 96 | + } |
| 97 | + |
| 98 | +} |
| 99 | + |
| 100 | +// OptimizedColumn is a functional argument that sets this as an |
| 101 | +// optimized column. See osquery source code for more information. |
| 102 | +func OptimizedColumn() ColumnOpt { |
| 103 | + return func(cd *ColumnDefinition) { |
| 104 | + cd.Optimized = true |
| 105 | + } |
| 106 | + |
| 107 | +} |
| 108 | + |
| 109 | +// HiddenColumn is a functional argument that sets this as an |
| 110 | +// hidden column. This omits it from `select *` queries. See osquery source code for more information. |
| 111 | +func HiddenColumn() ColumnOpt { |
| 112 | + return func(cd *ColumnDefinition) { |
| 113 | + cd.Hidden = true |
| 114 | + } |
| 115 | + |
| 116 | +} |
| 117 | + |
| 118 | +// ColumnDescription sets the column description. This is not |
| 119 | +// currently part of the underlying osquery api, it is here for human |
| 120 | +// consumption. It may become part of osquery spec generation. |
| 121 | +func ColumnDescription(d string) ColumnOpt { |
| 122 | + return func(cd *ColumnDefinition) { |
| 123 | + cd.Description = d |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// Options returns the bitmask representation of the boolean column |
| 128 | +// options. This uses the values as encoded in |
| 129 | +// https://github.com/osquery/osquery/blob/master/osquery/core/sql/column.h#L37 |
| 130 | +func (c *ColumnDefinition) Options() uint8 { |
| 131 | + optionsBitmask := uint8(0) |
| 132 | + |
| 133 | + optionValues := map[uint8]bool{ |
| 134 | + 1: c.Index, |
| 135 | + 2: c.Required, |
| 136 | + 4: c.Additional, |
| 137 | + 8: c.Optimized, |
| 138 | + 16: c.Hidden, |
| 139 | + } |
| 140 | + |
| 141 | + for v, b := range optionValues { |
| 142 | + if b { |
| 143 | + optionsBitmask = optionsBitmask | v |
| 144 | + } |
| 145 | + } |
| 146 | + return optionsBitmask |
| 147 | +} |
0 commit comments