Skip to content

Commit

Permalink
Merge branch 'master' into feat/telegram_feeder
Browse files Browse the repository at this point in the history
  • Loading branch information
Matrix86 authored Sep 2, 2024
2 parents 348d628 + 99ef094 commit 9924609
Show file tree
Hide file tree
Showing 11 changed files with 361 additions and 24 deletions.
4 changes: 1 addition & 3 deletions cmd/driplane/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,12 @@ func main() {
os.Exit(0)
}

go Signal(&o)
go Signal(o)

log.Debug("Trying to start orchestrator")
o.StartFeeders()
o.WaitFeeders()

log.Debug("Stopping")
o.StopFeeders()

return
}
4 changes: 2 additions & 2 deletions core/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type Orchestrator struct {
}

// NewOrchestrator create a new instance of the Orchestrator
func NewOrchestrator(config *Configuration) (Orchestrator, error) {
o := Orchestrator{
func NewOrchestrator(config *Configuration) (*Orchestrator, error) {
o := &Orchestrator{
config: config,
asts: make(map[string]*AST),
}
Expand Down
2 changes: 1 addition & 1 deletion core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package core
// Info on driplane
const (
Name = "driplane"
Version = "1.15.1"
Version = "1.20.0"
Author = "Gianluca Braga aka Matrix86"
)
71 changes: 71 additions & 0 deletions filters/ratelimit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package filters

import (
"context"
"strconv"

"github.com/Matrix86/driplane/data"
"github.com/evilsocket/islazy/log"
"golang.org/x/time/rate"
)

// RateLimit is a Filter to create a RateLimit number
type RateLimit struct {
Base

objects int64
seconds int64
limiter *rate.Limiter
context context.Context
cancelContext context.CancelFunc

params map[string]string
}

// NewRateLimitFilter is the registered method to instantiate a RateLimit Filter
func NewRateLimitFilter(p map[string]string) (Filter, error) {
ctx, cancel := context.WithCancel(context.Background())
f := &RateLimit{
params: p,
seconds: 1,
context: ctx,
cancelContext: cancel,
}
f.cbFilter = f.DoFilter

if v, ok := p["rate"]; ok {
i, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return nil, err
}
f.objects = i
}

f.limiter = rate.NewLimiter(rate.Limit(f.objects), int(f.seconds))

return f, nil
}

// DoFilter is the mandatory method used to "filter" the input data.Message
func (f *RateLimit) DoFilter(msg *data.Message) (bool, error) {
if f.objects > 0 {
if err := f.limiter.Wait(f.context); err != nil {
return false, nil
}
}

return true, nil
}

// OnEvent is called when an event occurs
func (f *RateLimit) OnEvent(event *data.Event) {
if event.Type == "shutdown" {
log.Debug("shutdown event received")
f.cancelContext()
}
}

// Set the name of the filter
func init() {
register("ratelimit", NewRateLimitFilter)
}
60 changes: 60 additions & 0 deletions filters/ratelimit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package filters

import (
"testing"

"github.com/Matrix86/driplane/data"
)

func TestNewRateLimitFilter(t *testing.T) {
filter, err := NewRateLimitFilter(map[string]string{"rate": "2", "min": "10", "max": "40"})
if err != nil {
t.Errorf("constructor returned '%s'", err)
}
if e, ok := filter.(*RateLimit); ok {
if e.objects != 2 {
t.Errorf("'output' parameter ignored")
}
} else {
t.Errorf("cannot cast to proper Filter...")
}

_, err = NewRateLimitFilter(map[string]string{"rate": "random text", "min": "x", "max": "40"})
if err == nil {
t.Errorf("constructor should return error")
}
}

func TestRateLimit_DoFilter(t *testing.T) {
filter, err := NewRateLimitFilter(map[string]string{"rate": "5"})
if err != nil {
t.Errorf("constructor returned '%s'", err)
}

if e, ok := filter.(*RateLimit); ok {
msg := "this is a test..."
m := data.NewMessage(msg)
_, err := e.DoFilter(m)
if err != nil {
t.Errorf("DoFilter returned an error '%s'", err)
}
if m.GetMessage() != msg {
t.Errorf("DoFilter changed the message")
}

filter.OnEvent(&data.Event{
Type: "shutdown",
Content: "shutdown",
})
v, err := e.DoFilter(m)
if err != nil {
t.Errorf("DoFilter returned an error '%s'", err)
}
if v {
t.Errorf("DoFilter has to return false after the shutdown")
}

} else {
t.Errorf("cannot cast to proper Filter...")
}
}
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
Expand Down
67 changes: 59 additions & 8 deletions plugins/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// UtilPackage contains useful generic methods
type UtilPackage struct {}
type UtilPackage struct{}

// GetUtil returns an UtilPackage
func GetUtil() *UtilPackage {
Expand All @@ -16,26 +16,26 @@ func GetUtil() *UtilPackage {

// UtilResponse contains the return values
type UtilResponse struct {
Error error
Status bool
Value string
Error error
Status bool
Value string
}

// Sleep call Sleep method for N seconds
func (c *UtilPackage) Sleep(seconds int) UtilResponse {
time.Sleep(time.Duration(seconds) * time.Second)
return UtilResponse{
Error: nil,
Error: nil,
Status: true,
}
}

// Getenv returns an environment variable if it exists
func (c *UtilPackage) Getenv(name string) UtilResponse {
return UtilResponse{
Error: nil,
Error: nil,
Status: true,
Value: os.Getenv(name),
Value: os.Getenv(name),
}
}

Expand All @@ -54,4 +54,55 @@ func (c *UtilPackage) Md5File(filename string) UtilResponse {
Status: true,
Value: hash,
}
}
}

// Sha1File returns the SHA1 hash of the file
func (c *UtilPackage) Sha1File(filename string) UtilResponse {
hash, err := utils.Sha1File(filename)
if err != nil {
return UtilResponse{
Error: err,
Status: false,
Value: "",
}
}
return UtilResponse{
Error: nil,
Status: true,
Value: hash,
}
}

// Sha256File returns the SHA256 hash of the file
func (c *UtilPackage) Sha256File(filename string) UtilResponse {
hash, err := utils.Sha256File(filename)
if err != nil {
return UtilResponse{
Error: err,
Status: false,
Value: "",
}
}
return UtilResponse{
Error: nil,
Status: true,
Value: hash,
}
}

// Sha512File returns the SHA512 hash of the file
func (c *UtilPackage) Sha512File(filename string) UtilResponse {
hash, err := utils.Sha512File(filename)
if err != nil {
return UtilResponse{
Error: err,
Status: false,
Value: "",
}
}
return UtilResponse{
Error: nil,
Status: true,
Value: hash,
}
}
84 changes: 79 additions & 5 deletions plugins/util_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package plugins

import (
"io/ioutil"
"os"
"testing"
)
Expand All @@ -11,7 +10,7 @@ func TestUtilPluginSleepMethod(t *testing.T) {

res := u.Sleep(0)
if res.Status == false {
t.Errorf("bad response: expected=%t had=%t", true, res.Status )
t.Errorf("bad response: expected=%t had=%t", true, res.Status)
}
}

Expand All @@ -34,9 +33,9 @@ func TestUtilPluginGetEnvMethod(t *testing.T) {
func TestUtilPluginMd5Method(t *testing.T) {
u := GetUtil()

file, err := ioutil.TempFile(os.TempDir(), "prefix")
file, err := os.CreateTemp(os.TempDir(), "prefix")
if err != nil {
t.Errorf("cannot create a temporary file" )
t.Errorf("cannot create a temporary file")
}
defer os.Remove(file.Name())

Expand All @@ -54,4 +53,79 @@ func TestUtilPluginMd5Method(t *testing.T) {
if res.Value != expected {
t.Errorf("Value has a bad value: expected=%s had=%s", expected, res.Value)
}
}
}

func TestUtilPluginSha1Method(t *testing.T) {
u := GetUtil()

file, err := os.CreateTemp(os.TempDir(), "prefix")
if err != nil {
t.Errorf("cannot create a temporary file")
}
defer os.Remove(file.Name())

res := u.Sha1File("/tmp/notexistentfile_")
if res.Status == true {
t.Errorf("Status should be false")
}

res = u.Sha1File(file.Name())
if res.Status == false {
t.Errorf("Status should be true")
}

expected := "da39a3ee5e6b4b0d3255bfef95601890afd80709"
if res.Value != expected {
t.Errorf("Value has a bad value: expected=%s had=%s", expected, res.Value)
}
}

func TestUtilPluginSha256Method(t *testing.T) {
u := GetUtil()

file, err := os.CreateTemp(os.TempDir(), "prefix")
if err != nil {
t.Errorf("cannot create a temporary file")
}
defer os.Remove(file.Name())

res := u.Sha256File("/tmp/notexistentfile_")
if res.Status == true {
t.Errorf("Status should be false")
}

res = u.Sha256File(file.Name())
if res.Status == false {
t.Errorf("Status should be true")
}

expected := "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
if res.Value != expected {
t.Errorf("Value has a bad value: expected=%s had=%s", expected, res.Value)
}
}

func TestUtilPluginSha512Method(t *testing.T) {
u := GetUtil()

file, err := os.CreateTemp(os.TempDir(), "prefix")
if err != nil {
t.Errorf("cannot create a temporary file")
}
defer os.Remove(file.Name())

res := u.Sha512File("/tmp/notexistentfile_")
if res.Status == true {
t.Errorf("Status should be false")
}

res = u.Sha512File(file.Name())
if res.Status == false {
t.Errorf("Status should be true")
}

expected := "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"
if res.Value != expected {
t.Errorf("Value has a bad value: expected=%s had=%s", expected, res.Value)
}
}
Loading

0 comments on commit 9924609

Please sign in to comment.