1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
package selector
import (
"errors"
"io/ioutil"
"github.com/DCSO/balboa/observation"
lua "github.com/yuin/gopher-lua"
)
const (
luaInputObservationTypeName = "observation"
luaProcessObservationFunc = "process"
)
func init() {
err := RegisterSelector("lua", MakeLuaSelector)
if err != nil {
panic(err)
}
}
type LuaSelector struct {
luaScript string
ingest []string
L *lua.LState
}
func MakeLuaSelector(config interface{}) (selector Selector, err error) {
l := &LuaSelector{}
if configKV, ok := config.(map[interface{}]interface{}); ok {
if scriptOption, ok := configKV["script"]; ok {
scriptFile := scriptOption.(string)
scriptContent, err := ioutil.ReadFile(scriptFile)
if err != nil {
return nil, err
}
l.luaScript = string(scriptContent)
} else {
return nil, errors.New("type assertion failed")
}
if ingestListInterface, ok := configKV["ingest"]; ok {
if ingestList, ok := ingestListInterface.([]interface{}); ok {
for _, tag := range ingestList {
l.ingest = append(l.ingest, tag.(string))
}
} else {
return nil, errors.New("type assertion failed")
}
}
} else {
return nil, errors.New("type assertion failed")
}
if err = l.InitializeLua(); err != nil {
return nil, err
}
return l, nil
}
func checkObservation(L *lua.LState) *observation.InputObservation {
o := L.CheckUserData(1)
if v, ok := o.Value.(*observation.InputObservation); ok {
return v
}
L.ArgError(1, "observation expected")
return nil
}
func observationGetRcode(L *lua.LState) int {
o := checkObservation(L)
L.Push(lua.LString(o.Rcode))
return 1
}
func observationGetRdata(L *lua.LState) int {
o := checkObservation(L)
L.Push(lua.LString(o.Rdata))
return 1
}
func observationGetRrtype(L *lua.LState) int {
o := checkObservation(L)
L.Push(lua.LString(o.Rrtype))
return 1
}
func observationGetRrname(L *lua.LState) int {
o := checkObservation(L)
L.Push(lua.LString(o.Rrname))
return 1
}
func observationGetSensorId(L *lua.LState) int {
o := checkObservation(L)
L.Push(lua.LString(o.SensorID))
return 1
}
func observationGetTags(L *lua.LState) int {
o := checkObservation(L)
t := L.NewTable()
for tag, _ := range o.Tags {
t.Append(lua.LString(tag))
}
L.Push(t)
return 1
}
func observationAddTag(L *lua.LState) int {
o := checkObservation(L)
tag := L.ToString(2)
if tag != "" {
o.Tags[tag] = struct{}{}
}
return 0
}
var observationMethods = map[string]lua.LGFunction{
"rcode": observationGetRcode,
"rdata": observationGetRdata,
"rrtype": observationGetRrtype,
"rrname": observationGetRrname,
"sensor_id": observationGetSensorId,
"tags": observationGetTags,
"add_tag": observationAddTag,
}
func (l *LuaSelector) registerInputObservation() {
mt := l.L.NewTypeMetatable(luaInputObservationTypeName)
l.L.SetGlobal(luaInputObservationTypeName, mt)
l.L.SetField(mt, "__index", l.L.SetFuncs(l.L.NewTable(), observationMethods))
}
func (l *LuaSelector) InitializeLua() (err error) {
l.L = lua.NewState()
l.registerInputObservation()
err = l.L.DoString(l.luaScript)
return err
}
func (l *LuaSelector) Reinitialize() (err error) {
panic("implement me")
}
func (l *LuaSelector) ProcessObservation(observation *observation.InputObservation) (err error) {
fn := l.L.GetGlobal(luaProcessObservationFunc)
if err := l.L.CallByParam(lua.P{
Fn: fn,
NRet: 0,
Protect: true,
}, &lua.LUserData{
Value: observation,
Metatable: l.L.GetTypeMetatable(luaInputObservationTypeName),
}); err != nil {
return err
}
return err
}
func (l *LuaSelector) IngestionList() []string {
return l.ingest
}
|