/
filterhashfile.go
123 lines (110 loc) · 3.1 KB
/
filterhashfile.go
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
package hashfile
import (
"bufio"
"context"
"errors"
"fmt"
"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/goglog"
"github.com/tsaikd/gogstash/config/logevent"
"io"
"os"
)
// ModuleName is the name used in config file
const ModuleName = "hashfile"
const defaultBufSize = 20000 // default buffer size
// ErrorTag tag added to event when process module failed
const ErrorTag = "gogstash_filter_hashfile_error"
// FilterConfig holds the configuration json fields and internal objects
type FilterConfig struct {
config.FilterConfig
Field string `json:"field" yaml:"field"` // field name of file to hash
Output string `json:"output" yaml:"output"` // field name to store output to
Algos []string `json:"algos" yaml:"algos"` // hash algos to hash on
BufSize int `json:"buf_size" yaml:"buf_size"` // buffer size for copy
}
// DefaultFilterConfig returns an FilterConfig struct with default values
func DefaultFilterConfig() FilterConfig {
var allHash []string
for k, _ := range SupportedHashes {
allHash = append(allHash, k)
}
return FilterConfig{
FilterConfig: config.FilterConfig{
CommonConfig: config.CommonConfig{
Type: ModuleName,
},
},
Field: "file_name",
Algos: allHash,
BufSize: defaultBufSize,
Output: "hash",
}
}
// Hash is our interface to a hash. Each hash that implements this interface will work with the module.
type Hash interface {
io.Writer
Sum() []byte
}
// SupportedHashes the list of supported hashes and their init functions
var SupportedHashes map[string]func(interface{}) Hash = make(map[string]func(interface{}) Hash)
// InitHandler initialize the filter plugin
func InitHandler(ctx context.Context, raw config.ConfigRaw, control config.Control) (config.TypeFilterConfig, error) {
conf := DefaultFilterConfig()
err := config.ReflectConfig(raw, &conf)
if err != nil {
return nil, err
}
// check that all hashes are supported
for _, v := range conf.Algos {
if _, ok := SupportedHashes[v]; !ok {
return &conf, fmt.Errorf("%s not supported", v)
}
}
if conf.BufSize < 1 {
conf.BufSize = defaultBufSize
}
return &conf, nil
}
// Event the main filter event
func (f *FilterConfig) Event(ctx context.Context, event logevent.LogEvent) (logevent.LogEvent, bool) {
// init hashers
hashers := []Hash{}
for _, v := range f.Algos {
hashers = append(hashers, SupportedHashes[v](nil))
}
// read file and hash
fn := event.GetString(f.Field)
fi, err := os.Open(fn)
if err != nil {
goglog.Logger.Errorf("%s: %s", ModuleName, err.Error())
event.AddTag(ErrorTag)
return event, false
}
defer fi.Close()
input := bufio.NewReader(fi)
buf := make([]byte, f.BufSize)
for {
n, err := input.Read(buf)
if n > 0 {
for x := 0; x < len(hashers); x++ {
hashers[x].Write(buf[:n])
}
continue
}
if errors.Is(err, io.EOF) {
break
}
if err != nil {
goglog.Logger.Errorf("%s: %s", ModuleName, err.Error())
return event, false
}
}
// add result to event
result := map[string][]byte{}
for k, v := range f.Algos {
result[v] = hashers[k].Sum()
}
event.SetValue(f.Output, result)
return event, true
}