2015-04-19 19:07:13 +03:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
//
|
|
|
|
// Contributor: Julien Vehent jvehent@mozilla.com [:ulfr]
|
2015-08-27 17:41:13 +03:00
|
|
|
|
|
|
|
/* This package implements interfaces to write modules for MIG.
|
|
|
|
For a guide on how to write modules, head over to
|
|
|
|
http://mig.mozilla.org/doc/modules.rst.html
|
|
|
|
*/
|
2018-07-11 20:11:22 +03:00
|
|
|
package modules /* import "github.com/mozilla/mig/modules" */
|
2015-04-19 19:07:13 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2016-01-22 18:27:33 +03:00
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"encoding/base64"
|
2015-04-19 19:07:13 +03:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-05-07 23:14:50 +03:00
|
|
|
"io"
|
2016-01-22 18:27:33 +03:00
|
|
|
"io/ioutil"
|
2016-10-09 07:29:48 +03:00
|
|
|
"net"
|
2016-10-14 21:15:00 +03:00
|
|
|
"os"
|
2016-10-09 07:29:48 +03:00
|
|
|
"path"
|
2016-10-17 23:47:30 +03:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2016-10-14 21:15:00 +03:00
|
|
|
"time"
|
2015-04-19 19:07:13 +03:00
|
|
|
)
|
|
|
|
|
2016-10-09 07:29:48 +03:00
|
|
|
var ModuleRunDir string
|
|
|
|
|
2015-04-19 19:07:13 +03:00
|
|
|
// Message defines the input messages received by modules.
|
2016-10-18 19:48:39 +03:00
|
|
|
//
|
|
|
|
// All messages will have Class and Parameters set. PersistSock is used in a case
|
|
|
|
// where a parameters message is being sent for a persistent module. In this case,
|
|
|
|
// PersistSock will contain the socket specification the module has registered as
|
|
|
|
// listening on.
|
2015-04-19 19:07:13 +03:00
|
|
|
type Message struct {
|
2016-10-18 19:48:39 +03:00
|
|
|
Class MessageClass `json:"class"` // represent the type of message being passed to the module
|
|
|
|
Parameters interface{} `json:"parameters,omitempty"` // for `parameters` class, this interface contains the module parameters
|
|
|
|
PersistSock string `json:"persistsock,omitempty"` // Persistent module socket path if required
|
2015-04-19 19:07:13 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 00:05:26 +03:00
|
|
|
type MessageClass string
|
|
|
|
|
2015-04-19 19:07:13 +03:00
|
|
|
const (
|
2015-04-29 00:05:26 +03:00
|
|
|
MsgClassParameters MessageClass = "parameters"
|
|
|
|
MsgClassStop MessageClass = "stop"
|
2016-10-09 07:29:48 +03:00
|
|
|
MsgClassPing MessageClass = "ping"
|
|
|
|
MsgClassLog MessageClass = "log"
|
2016-10-17 23:47:30 +03:00
|
|
|
MsgClassRegister MessageClass = "register"
|
2016-12-08 04:02:26 +03:00
|
|
|
MsgClassConfig MessageClass = "config"
|
2017-02-28 20:12:30 +03:00
|
|
|
MsgClassAlert MessageClass = "alert"
|
2015-04-19 19:07:13 +03:00
|
|
|
)
|
|
|
|
|
2016-10-18 19:48:39 +03:00
|
|
|
// Parameter format expected for a log message
|
2016-10-09 07:29:48 +03:00
|
|
|
type LogParams struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
2016-10-18 19:48:39 +03:00
|
|
|
// Parameter format expected for a register message
|
2016-10-17 23:47:30 +03:00
|
|
|
type RegParams struct {
|
|
|
|
SockPath string `json:"sockpath"`
|
|
|
|
}
|
|
|
|
|
2016-12-08 04:02:26 +03:00
|
|
|
type ConfigParams struct {
|
2017-09-06 21:57:18 +03:00
|
|
|
Config interface{} `json:"config"`
|
|
|
|
Hostname string `json:"hostname"`
|
|
|
|
Env interface{} `json:"environment"`
|
|
|
|
Tags map[string]string `json:"tags"`
|
2016-12-08 04:02:26 +03:00
|
|
|
}
|
|
|
|
|
2017-02-28 20:12:30 +03:00
|
|
|
// AlertParams describes the parameters used in an alert message
|
|
|
|
type AlertParams struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
2015-04-19 19:07:13 +03:00
|
|
|
// Result implement the base type for results returned by modules.
|
|
|
|
// All modules must return this type of result. The fields are:
|
|
|
|
//
|
|
|
|
// - FoundAnything: a boolean that must be set to true if the module ran
|
|
|
|
// a search that returned at least one positive result
|
|
|
|
//
|
|
|
|
// - Success: a boolean that must be set to true if the module ran without
|
|
|
|
// fatal errors. soft errors are reported in Errors
|
|
|
|
//
|
|
|
|
// - Elements: an undefined type that can be customized by the module to
|
|
|
|
// contain the detailled results
|
|
|
|
//
|
|
|
|
// - Statistics: an undefined type that can be customized by the module to
|
|
|
|
// contain some information about how it ran
|
|
|
|
//
|
|
|
|
// - Errors: an array of strings that contain non-fatal errors encountered
|
|
|
|
// by the module
|
|
|
|
type Result struct {
|
|
|
|
FoundAnything bool `json:"foundanything"`
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Elements interface{} `json:"elements"`
|
|
|
|
Statistics interface{} `json:"statistics"`
|
|
|
|
Errors []string `json:"errors"`
|
|
|
|
}
|
|
|
|
|
2015-05-22 04:40:46 +03:00
|
|
|
// Runner provides the interface to an execution of a module
|
|
|
|
type Runner interface {
|
2016-12-16 18:55:21 +03:00
|
|
|
Run(ModuleReader) string
|
2015-04-19 19:07:13 +03:00
|
|
|
ValidateParameters() error
|
|
|
|
}
|
|
|
|
|
2016-10-09 07:29:48 +03:00
|
|
|
// PersistRunner provides the interface to execution of a persistent module. All
|
|
|
|
// modules will satisfy Runner. Persistent modules will satisfy both Runner and
|
|
|
|
// PersistRunner.
|
|
|
|
type PersistRunner interface {
|
2016-12-16 18:55:21 +03:00
|
|
|
RunPersist(ModuleReader, ModuleWriter)
|
2016-12-08 04:02:26 +03:00
|
|
|
PersistModConfig() interface{}
|
2016-12-16 18:55:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ModuleReader is used to read module communications. It's intent is to
|
|
|
|
// wrap the initial reader (e.g., stdin) with a buffered reader that will exist for
|
|
|
|
// the lifetime of execution of the module. When the module reads input, it will
|
|
|
|
// read from BufferReader inside ModuleReader since our communication is line delimited,
|
|
|
|
// so we want to make sure we allocate this buffer only once.
|
|
|
|
type ModuleReader struct {
|
|
|
|
Reader io.Reader
|
|
|
|
BufferReader *bufio.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new ModuleReader wrapping reader r.
|
|
|
|
func NewModuleReader(r io.Reader) (ret ModuleReader) {
|
|
|
|
ret.Reader = r
|
|
|
|
ret.BufferReader = bufio.NewReader(ret.Reader)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModuleWriter is used to write module communications. We don't require bufio on
|
|
|
|
// writes, but this type exists just to provide consistency with ModuleReader.
|
|
|
|
type ModuleWriter struct {
|
|
|
|
Writer io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new ModuleWriter wrapping writer w.
|
|
|
|
func NewModuleWriter(w io.Writer) (ret ModuleWriter) {
|
|
|
|
ret.Writer = w
|
|
|
|
return
|
2016-10-09 07:29:48 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 00:05:26 +03:00
|
|
|
// MakeMessage creates a new modules.Message with a given class and parameters and
|
|
|
|
// return the byte slice of the json marshalled message
|
2016-01-22 18:27:33 +03:00
|
|
|
func MakeMessage(class MessageClass, params interface{}, comp bool) (rawMsg []byte, err error) {
|
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
err = fmt.Errorf("Failed to make modules.Message: %v", e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-04-29 00:05:26 +03:00
|
|
|
var msg Message
|
|
|
|
msg.Class = class
|
|
|
|
msg.Parameters = params
|
2016-01-22 18:27:33 +03:00
|
|
|
// If the compression flag is set, treat Parameters as a compressed
|
|
|
|
// byte string.
|
|
|
|
if comp {
|
|
|
|
pstr, ok := msg.Parameters.(string)
|
|
|
|
if !ok {
|
|
|
|
panic("Compressed parameter was not a string")
|
|
|
|
}
|
|
|
|
b := bytes.NewBuffer([]byte(pstr))
|
|
|
|
rb64 := base64.NewDecoder(base64.StdEncoding, b)
|
|
|
|
r, err := gzip.NewReader(rb64)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
rb, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(rb, &msg.Parameters)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2015-04-29 00:05:26 +03:00
|
|
|
rawMsg, err = json.Marshal(msg)
|
|
|
|
if err != nil {
|
2016-01-22 18:27:33 +03:00
|
|
|
panic(err)
|
2015-04-29 00:05:26 +03:00
|
|
|
}
|
2015-04-20 21:36:03 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-18 19:48:39 +03:00
|
|
|
// MakeMessageLog creates a new message of class log.
|
2016-10-09 07:29:48 +03:00
|
|
|
func MakeMessageLog(f string, args ...interface{}) (rawMsg []byte, err error) {
|
|
|
|
param := LogParams{Message: fmt.Sprintf(f, args...)}
|
|
|
|
msg := Message{Class: MsgClassLog, Parameters: param}
|
|
|
|
rawMsg, err = json.Marshal(&msg)
|
|
|
|
if err != nil {
|
2016-10-14 21:15:00 +03:00
|
|
|
err = fmt.Errorf("Failed to make module log message: %v", err)
|
|
|
|
return
|
2016-10-09 07:29:48 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-18 19:48:39 +03:00
|
|
|
// Creates a new message of class register.
|
2016-10-17 23:47:30 +03:00
|
|
|
func MakeMessageRegister(spec string) (rawMsg []byte, err error) {
|
|
|
|
param := RegParams{SockPath: spec}
|
|
|
|
msg := Message{Class: MsgClassRegister, Parameters: param}
|
|
|
|
rawMsg, err = json.Marshal(&msg)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Failed to make module register message: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-08 04:02:26 +03:00
|
|
|
// Creates a new message of class config.
|
2017-09-06 21:57:18 +03:00
|
|
|
func MakeMessageConfig(cfgdata interface{}, hostname string, env interface{},
|
|
|
|
tags map[string]string) (rawMsg []byte, err error) {
|
|
|
|
param := ConfigParams{
|
|
|
|
Config: cfgdata,
|
|
|
|
Hostname: hostname,
|
|
|
|
Env: env,
|
|
|
|
Tags: tags,
|
|
|
|
}
|
2016-12-08 04:02:26 +03:00
|
|
|
msg := Message{Class: MsgClassConfig, Parameters: param}
|
|
|
|
rawMsg, err = json.Marshal(&msg)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Failed to make module config message: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-28 20:12:30 +03:00
|
|
|
// MakeMessageAlert creates a new message of class alert
|
|
|
|
func MakeMessageAlert(f string, args ...interface{}) (rawMsg []byte, err error) {
|
|
|
|
param := AlertParams{Message: fmt.Sprintf(f, args...)}
|
|
|
|
msg := Message{Class: MsgClassAlert, Parameters: param}
|
|
|
|
rawMsg, err = json.Marshal(&msg)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Failed to make module alert message: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-08 00:12:17 +03:00
|
|
|
// Keep reading until we get a full line or an error, and return
|
|
|
|
func readInputLine(rdr *bufio.Reader) ([]byte, error) {
|
|
|
|
var ret []byte
|
|
|
|
for {
|
|
|
|
lb, isPrefix, err := rdr.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
ret = append(ret, lb...)
|
|
|
|
if !isPrefix {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2016-12-16 18:55:21 +03:00
|
|
|
// ReadInput reads one line of input from ModuleReader r, unmarshal it into a modules.Message
|
2015-04-19 19:07:13 +03:00
|
|
|
// and returns the message to the caller
|
2016-12-16 18:55:21 +03:00
|
|
|
func ReadInput(r ModuleReader) (msg Message, err error) {
|
2015-04-19 19:07:13 +03:00
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
err = fmt.Errorf("ReadInput() -> %v", e)
|
|
|
|
}
|
|
|
|
}()
|
2016-12-16 18:55:21 +03:00
|
|
|
linebuffer, err := readInputLine(r.BufferReader)
|
2015-05-08 00:12:17 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2015-04-19 19:07:13 +03:00
|
|
|
}
|
2015-05-08 00:12:17 +03:00
|
|
|
err = json.Unmarshal(linebuffer, &msg)
|
2015-04-19 19:07:13 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-16 18:55:21 +03:00
|
|
|
// ReadInputParameters reads the first line from ModuleReader r and expects to find a
|
2015-04-19 19:07:13 +03:00
|
|
|
// modules.Message of class `parameters`. This function uses ReadInput and will
|
|
|
|
// block waiting for data on stdin
|
2016-12-16 18:55:21 +03:00
|
|
|
func ReadInputParameters(r ModuleReader, p interface{}) (err error) {
|
2015-04-19 19:07:13 +03:00
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
err = fmt.Errorf("ReadInputParameters() -> %v", e)
|
|
|
|
}
|
|
|
|
}()
|
2015-05-07 23:14:50 +03:00
|
|
|
msg, err := ReadInput(r)
|
2015-04-19 19:07:13 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if msg.Class != MsgClassParameters {
|
|
|
|
panic("unexpected input is not module parameters")
|
|
|
|
}
|
|
|
|
rawParams, err := json.Marshal(msg.Parameters)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(rawParams, p)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-17 23:47:30 +03:00
|
|
|
// ReadPersistInputParameters performs the same function as ReadInputParameters
|
|
|
|
// however it also validates a socket path has been specified to query the
|
|
|
|
// persistent module, returning an error if this is not present. Populates
|
|
|
|
// p and also returns the socket path.
|
2016-12-16 18:55:21 +03:00
|
|
|
func ReadPersistInputParameters(r ModuleReader, p interface{}) (spath string, err error) {
|
2016-10-17 23:47:30 +03:00
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
err = fmt.Errorf("ReadPersistInputParameters() -> %v", e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
msg, err := ReadInput(r)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if msg.Class != MsgClassParameters {
|
|
|
|
panic("unexpected input is not module parameters")
|
|
|
|
}
|
|
|
|
if msg.PersistSock == "" {
|
|
|
|
panic("no persistsock set in message")
|
|
|
|
}
|
|
|
|
spath = msg.PersistSock
|
|
|
|
rawParams, err := json.Marshal(msg.Parameters)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(rawParams, p)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-18 19:48:39 +03:00
|
|
|
// Write output in buf to writer w. buf is expected to contain a single line
|
|
|
|
// of data, and a line feed is appended to terminate the line as module IO is
|
|
|
|
// line delimited.
|
2016-12-16 18:55:21 +03:00
|
|
|
func WriteOutput(buf []byte, w ModuleWriter) (err error) {
|
2016-10-09 07:29:48 +03:00
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
err = fmt.Errorf("WriteOutput() -> %v", e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
// Since our output is line delimited, add a newline
|
|
|
|
buf = append(buf, '\n')
|
|
|
|
left := len(buf)
|
|
|
|
for left > 0 {
|
2016-12-16 18:55:21 +03:00
|
|
|
nb, err := w.Writer.Write(buf)
|
2016-10-09 07:29:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
left -= nb
|
|
|
|
buf = buf[nb:]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-19 19:07:13 +03:00
|
|
|
// WatchForStop continuously reads stdin for a stop message. When one is received,
|
|
|
|
// `true` is sent into the stop channel.
|
2016-12-16 18:55:21 +03:00
|
|
|
func WatchForStop(r ModuleReader, stopChan *chan bool) error {
|
2015-04-19 19:07:13 +03:00
|
|
|
for {
|
2015-05-07 23:14:50 +03:00
|
|
|
msg, err := ReadInput(r)
|
2015-04-19 19:07:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if msg.Class == MsgClassStop {
|
|
|
|
*stopChan <- true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-28 20:12:30 +03:00
|
|
|
// dispatchFunc can be set by a module using RegisterDispatchFunction, and is called
|
|
|
|
// inside DefaultPersistHandlers if the module recieves an alert message from the master
|
|
|
|
// agent process.
|
|
|
|
var dispatchFunc func(string)
|
|
|
|
|
|
|
|
// RegisterDispatchFunction can be called by a module to register a function that will be
|
|
|
|
// used to process incoming alert messages from the master agent process. It is primarily
|
|
|
|
// used by the dispatch module.
|
|
|
|
func RegisterDispatchFunction(f func(string)) {
|
|
|
|
dispatchFunc = f
|
|
|
|
}
|
|
|
|
|
2016-10-18 19:48:39 +03:00
|
|
|
// A general management function that can be called by persistent modules from the
|
|
|
|
// RunPersist function. Looks after replying to ping messages, writing logs, and other
|
|
|
|
// communication between the agent and the running persistent module.
|
2016-12-16 18:55:21 +03:00
|
|
|
func DefaultPersistHandlers(in ModuleReader, out ModuleWriter, logch chan string,
|
2017-09-06 21:57:18 +03:00
|
|
|
errch chan error, regch chan string, alertch chan string, confch chan ConfigParams) {
|
2016-10-09 07:29:48 +03:00
|
|
|
inChan := make(chan Message, 0)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
msg, err := ReadInput(in)
|
|
|
|
if err != nil {
|
|
|
|
close(inChan)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
inChan <- msg
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
failed := false
|
|
|
|
|
|
|
|
select {
|
2016-10-14 21:15:00 +03:00
|
|
|
case em := <-errch:
|
|
|
|
failed = true
|
|
|
|
// An error occurred somewhere in the persistent module and
|
|
|
|
// we want to exit. Try to write the log message, and also
|
|
|
|
// schedule a hard exit to ensure we do not run into a blocking
|
|
|
|
// scenario during the write.
|
|
|
|
go func() {
|
|
|
|
time.Sleep(time.Second * 5)
|
|
|
|
os.Exit(1)
|
|
|
|
}()
|
|
|
|
logmsg, err := MakeMessageLog("%v", em)
|
|
|
|
if err == nil {
|
|
|
|
WriteOutput(logmsg, out)
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
2017-02-28 20:12:30 +03:00
|
|
|
case s := <-alertch:
|
|
|
|
almsg, err := MakeMessageAlert("%v", s)
|
|
|
|
if err != nil {
|
|
|
|
failed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = WriteOutput(almsg, out)
|
|
|
|
if err != nil {
|
|
|
|
failed = true
|
|
|
|
break
|
|
|
|
}
|
2016-10-09 07:29:48 +03:00
|
|
|
case s := <-logch:
|
|
|
|
logmsg, err := MakeMessageLog("%v", s)
|
|
|
|
if err != nil {
|
|
|
|
failed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = WriteOutput(logmsg, out)
|
|
|
|
if err != nil {
|
|
|
|
failed = true
|
|
|
|
break
|
|
|
|
}
|
2016-10-17 23:47:30 +03:00
|
|
|
case r := <-regch:
|
|
|
|
regmsg, err := MakeMessageRegister(r)
|
|
|
|
if err != nil {
|
|
|
|
failed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = WriteOutput(regmsg, out)
|
|
|
|
if err != nil {
|
|
|
|
failed = true
|
|
|
|
break
|
|
|
|
}
|
2016-10-09 07:29:48 +03:00
|
|
|
case msg, ok := <-inChan:
|
|
|
|
if !ok {
|
|
|
|
failed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch msg.Class {
|
|
|
|
case "ping":
|
|
|
|
buf, err := json.Marshal(&msg)
|
|
|
|
if err != nil {
|
|
|
|
failed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = WriteOutput(buf, out)
|
|
|
|
if err != nil {
|
|
|
|
failed = true
|
|
|
|
break
|
|
|
|
}
|
2016-12-08 04:02:26 +03:00
|
|
|
case "config":
|
|
|
|
var confparam ConfigParams
|
|
|
|
buf, err := json.Marshal(msg.Parameters)
|
|
|
|
if err != nil {
|
|
|
|
failed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(buf, &confparam)
|
|
|
|
if err != nil {
|
|
|
|
failed = true
|
|
|
|
break
|
|
|
|
}
|
2017-09-06 21:57:18 +03:00
|
|
|
confch <- confparam
|
2017-02-28 20:12:30 +03:00
|
|
|
case "alert":
|
|
|
|
if dispatchFunc != nil {
|
|
|
|
var alparam AlertParams
|
|
|
|
buf, err := json.Marshal(msg.Parameters)
|
|
|
|
if err != nil {
|
|
|
|
failed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(buf, &alparam)
|
|
|
|
if err != nil {
|
|
|
|
failed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
dispatchFunc(alparam.Message)
|
|
|
|
}
|
2016-10-09 07:29:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if failed {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-18 19:48:39 +03:00
|
|
|
// Request handler that can be called from RunPersist in a persistent module. Looks
|
|
|
|
// after accepting incoming requests to listener l, and routing the requests to the
|
|
|
|
// registered incoming request handler f.
|
2016-10-14 21:15:00 +03:00
|
|
|
func HandlePersistRequest(l net.Listener, f func(interface{}) string, errch chan error) {
|
2016-10-09 07:29:48 +03:00
|
|
|
for {
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
2016-10-14 21:15:00 +03:00
|
|
|
errch <- err
|
|
|
|
return
|
2016-10-09 07:29:48 +03:00
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
var p interface{}
|
2016-12-16 18:55:21 +03:00
|
|
|
mw := NewModuleWriter(conn)
|
|
|
|
mr := NewModuleReader(conn)
|
|
|
|
err = ReadInputParameters(mr, &p)
|
2016-10-09 07:29:48 +03:00
|
|
|
if err != nil {
|
2016-10-14 21:15:00 +03:00
|
|
|
errch <- err
|
|
|
|
return
|
2016-10-09 07:29:48 +03:00
|
|
|
}
|
|
|
|
resp := f(p)
|
2016-12-16 18:55:21 +03:00
|
|
|
WriteOutput([]byte(resp), mw)
|
2016-10-09 07:29:48 +03:00
|
|
|
err = conn.Close()
|
|
|
|
if err != nil {
|
2016-10-14 21:15:00 +03:00
|
|
|
errch <- err
|
|
|
|
return
|
2016-10-09 07:29:48 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-18 19:48:39 +03:00
|
|
|
// Sends the parameters in p as a request to a persistent module listening at socket
|
|
|
|
// specification sockspec; would typically be used in the Run() function of a
|
|
|
|
// persistent module.
|
2016-10-17 23:47:30 +03:00
|
|
|
func SendPersistRequest(p interface{}, sockspec string) (res string) {
|
2016-10-14 21:15:00 +03:00
|
|
|
defer func() {
|
|
|
|
// If something goes wrong here we will want to format and
|
|
|
|
// return the result an a Result type, with the error
|
|
|
|
// message set.
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
var r Result
|
|
|
|
r.Errors = append(r.Errors, fmt.Sprintf("%v", e))
|
|
|
|
r.Success = false
|
|
|
|
resbuf, _ := json.Marshal(&r)
|
|
|
|
res = string(resbuf)
|
|
|
|
}
|
|
|
|
}()
|
2016-10-17 23:47:30 +03:00
|
|
|
var (
|
|
|
|
fam string
|
|
|
|
address string
|
|
|
|
)
|
|
|
|
args := strings.Split(sockspec, ":")
|
|
|
|
if len(args) < 1 || len(args) > 3 {
|
|
|
|
panic("invalid socket specification for request")
|
|
|
|
}
|
|
|
|
switch args[0] {
|
|
|
|
case "unix":
|
|
|
|
if len(args) != 2 {
|
|
|
|
panic("invalid socket specification for unix connection")
|
|
|
|
}
|
|
|
|
fam = "unix"
|
|
|
|
address = args[1]
|
|
|
|
case "tcp":
|
|
|
|
if len(args) != 3 {
|
|
|
|
panic("invalid socket specification for tcp connection")
|
|
|
|
}
|
|
|
|
fam = "tcp"
|
|
|
|
address = strings.Join(args[1:], ":")
|
|
|
|
default:
|
|
|
|
panic("socket specification had invalid address family")
|
|
|
|
}
|
|
|
|
conn, err := net.Dial(fam, address)
|
2016-10-09 07:29:48 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-12-16 18:55:21 +03:00
|
|
|
nw := NewModuleWriter(conn)
|
2016-10-09 07:29:48 +03:00
|
|
|
buf, err := MakeMessage(MsgClassParameters, p, false)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-12-16 18:55:21 +03:00
|
|
|
err = WriteOutput(buf, nw)
|
2016-10-09 07:29:48 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
rb, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-10-14 21:15:00 +03:00
|
|
|
res = string(rb)
|
|
|
|
return
|
2016-10-09 07:29:48 +03:00
|
|
|
}
|
|
|
|
|
2016-10-18 19:48:39 +03:00
|
|
|
// Get a listener for a persistent module that is appropriate for the platform type, returns
|
|
|
|
// the listener itself in addition to the socket specification that should be registered.
|
2016-10-17 23:47:30 +03:00
|
|
|
func GetPersistListener(modname string) (l net.Listener, specname string, err error) {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "darwin", "linux":
|
|
|
|
sname := fmt.Sprintf("persist-%v.sock", modname)
|
|
|
|
spath := path.Join(ModuleRunDir, sname)
|
|
|
|
specname = "unix:" + spath
|
|
|
|
_ = os.Remove(spath)
|
|
|
|
l, err = net.Listen("unix", spath)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = fmt.Errorf("persistent listener not available for this platform")
|
|
|
|
return
|
2016-10-09 07:29:48 +03:00
|
|
|
}
|
|
|
|
|
2015-04-19 19:07:13 +03:00
|
|
|
// HasResultsPrinter implements functions used by module to print information
|
|
|
|
type HasResultsPrinter interface {
|
|
|
|
PrintResults(Result, bool) ([]string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetElements reads the elements from a struct of results into the el interface
|
|
|
|
func (r Result) GetElements(el interface{}) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
err = fmt.Errorf("GetElements() -> %v", e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
buf, err := json.Marshal(r.Elements)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(buf, el)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStatistics reads the statistics from a struct of results into the stats interface
|
|
|
|
func (r Result) GetStatistics(stats interface{}) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
err = fmt.Errorf("GetStatistics() -> %v", e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
buf, err := json.Marshal(r.Statistics)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(buf, stats)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-27 22:14:46 +03:00
|
|
|
type HasEnhancedPrivacy interface {
|
|
|
|
EnhancePrivacy(Result) (Result, error)
|
|
|
|
}
|
|
|
|
|
2015-04-19 19:07:13 +03:00
|
|
|
// HasParamsCreator implements a function that creates module parameters
|
|
|
|
type HasParamsCreator interface {
|
|
|
|
ParamsCreator() (interface{}, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasParamsParser implements a function that parses command line parameters
|
|
|
|
type HasParamsParser interface {
|
|
|
|
ParamsParser([]string) (interface{}, error)
|
|
|
|
}
|