2014-08-13 20:28:28 +04: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
|
|
|
|
2015-08-26 21:15:40 +03:00
|
|
|
package mig /* import "mig.ninja/mig" */
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-08-26 00:54:52 +03:00
|
|
|
"mig.ninja/mig/modules"
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Command struct {
|
2014-09-30 04:01:15 +04:00
|
|
|
ID float64 `json:"id"`
|
|
|
|
Action Action `json:"action"`
|
|
|
|
Agent Agent `json:"agent"`
|
|
|
|
|
|
|
|
// Status can be one of:
|
|
|
|
// sent: the command has been sent by the scheduler to the agent
|
|
|
|
// success: the command has successfully ran on the agent and been returned to the scheduler
|
|
|
|
// cancelled: the command has been cancelled by the investigator
|
|
|
|
// expired: the command has been expired by the scheduler
|
|
|
|
// failed: the command has failed on the agent and been returned to the scheduler
|
|
|
|
// timeout: module execution has timed out, and the agent returned the command to the scheduler
|
|
|
|
Status string `json:"status"`
|
|
|
|
|
2015-04-19 19:07:13 +03:00
|
|
|
Results []modules.Result `json:"results"`
|
|
|
|
StartTime time.Time `json:"starttime"`
|
|
|
|
FinishTime time.Time `json:"finishtime"`
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
}
|
|
|
|
|
2014-09-30 04:01:15 +04:00
|
|
|
const (
|
|
|
|
StatusSent string = "sent"
|
|
|
|
StatusSuccess string = "success"
|
|
|
|
StatusCancelled string = "cancelled"
|
|
|
|
StatusExpired string = "expired"
|
|
|
|
StatusFailed string = "failed"
|
|
|
|
StatusTimeout string = "timeout"
|
|
|
|
)
|
|
|
|
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
// FromFile reads a command from a local file on the file system
|
|
|
|
// and return the mig.Command structure
|
|
|
|
func CmdFromFile(path string) (cmd Command, err error) {
|
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
err = fmt.Errorf("mig.CmdFromFile()-> %v", e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
jsonCmd, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(jsonCmd, &cmd)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// Syntax Check
|
|
|
|
err = checkCmd(cmd)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckCmd verifies that the Command received contained all the
|
|
|
|
// necessary fields, and returns an error when it doesn't.
|
|
|
|
func checkCmd(cmd Command) error {
|
2014-05-08 02:07:21 +04:00
|
|
|
if cmd.Agent.Name == "" {
|
|
|
|
return errors.New("cmd.Agent.Name is empty. Expecting string.")
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
}
|
2014-05-08 02:07:21 +04:00
|
|
|
if cmd.Agent.QueueLoc == "" {
|
|
|
|
return errors.New("cmd.Agent.QueueLoc is empty. Expecting string.")
|
Scheduler: Happy New Year, with a massive refactoring commit.
The logic is pretty much the same as before, but this commit is a massive code
cleanup. There is now:
* proper error handling: each function uses panic internally, with a defer block
that transform the panic into an error, and eventually logs
* syslog, log levels, asynchronous logging into LOG channel, and mig.LOG type
* configuration file, loaded into global Context, accessible to all functions
* Context initialization, with connection to database, broker, creation of channels
and other wonders is handled in context.go
* split of the scheduler code into several files, more work to do on that front,
particularly for the management of the Flow in flow.go
* Action and Command are split out in their own files, with specific methods
That's about it. Did I mention Happy New Year?
2014-01-07 00:59:27 +04:00
|
|
|
}
|
|
|
|
if cmd.Status == "" {
|
|
|
|
return errors.New("cmd.Status is empty. Expecting string.")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|