forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
event.go
181 lines (156 loc) · 3.77 KB
/
event.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package file
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"hash"
"io"
"os"
"runtime"
"strconv"
"time"
"github.com/pkg/errors"
"github.com/elastic/beats/libbeat/common"
)
// Action is a description of the change that occurred.
type Action uint8
func (a Action) String() string {
if name, found := actionNames[a]; found {
return name
}
return "unknown"
}
// List of possible Actions.
const (
Unknown Action = iota << 1
AttributesModified
Created
Deleted
Updated
Moved
)
var actionNames = map[Action]string{
AttributesModified: "attributes_modified",
Created: "created",
Deleted: "deleted",
Updated: "updated",
Moved: "moved",
}
// Event describe the filesystem change and includes metadata about the file.
type Event struct {
Timestamp time.Time // Time of event.
Path string // The path associated with the event.
TargetPath string // Target path for symlinks.
Action string // Action (like created, updated).
Info *Metadata // File metadata (if the file exists).
Hashes map[string]string // File hashes.
errors []error
}
// Metadata contains file metadata.
type Metadata struct {
Inode uint64
UID uint32
GID uint32
SID string
Owner string
Group string
Mode os.FileMode
Size int64
ATime time.Time // Last access time.
MTime time.Time // Last modification time.
CTime time.Time // Last status change time.
Type string // File type (dir, file, symlink).
}
func buildMapStr(e *Event) common.MapStr {
m := common.MapStr{
"@timestamp": e.Timestamp,
"path": e.Path,
"action": e.Action,
"hashed": len(e.Hashes) > 0,
}
if e.TargetPath != "" {
m["target_path"] = e.TargetPath
}
if e.Info != nil {
info := e.Info
m["inode"] = strconv.FormatUint(info.Inode, 10)
m["size"] = info.Size
m["atime"] = info.ATime
m["mtime"] = info.MTime
m["ctime"] = info.CTime
if info.Type != "" {
m["type"] = info.Type
}
if runtime.GOOS == "windows" {
if info.SID != "" {
m["sid"] = info.SID
}
} else {
m["uid"] = info.UID
m["gid"] = info.GID
m["mode"] = fmt.Sprintf("%#04o", uint32(info.Mode))
}
if info.Owner != "" {
m["owner"] = info.Owner
}
if info.Group != "" {
m["group"] = info.Group
}
}
for name, hash := range e.Hashes {
m[name] = hash
}
return m
}
func hashFile(name string, hashType ...string) (map[string]string, error) {
if len(hashType) == 0 {
return nil, nil
}
var hashes []hash.Hash
for _, name := range hashType {
switch name {
case "md5":
hashes = append(hashes, md5.New())
case "sha1":
hashes = append(hashes, sha1.New())
case "sha224":
hashes = append(hashes, sha256.New224())
case "sha256":
hashes = append(hashes, sha256.New())
case "sha384":
hashes = append(hashes, sha512.New384())
case "sha512":
hashes = append(hashes, sha512.New())
case "sha512_224":
hashes = append(hashes, sha512.New512_224())
case "sha512_256":
hashes = append(hashes, sha512.New512_256())
default:
return nil, errors.Errorf("unknown hash type '%v'", name)
}
}
f, err := os.Open(name)
if err != nil {
return nil, errors.Wrap(err, "failed to open file for hashing")
}
defer f.Close()
hashWriter := multiWriter(hashes)
if _, err := io.Copy(hashWriter, f); err != nil {
return nil, errors.Wrap(err, "failed to calculate file hashes")
}
nameToHash := make(map[string]string, len(hashes))
for i, h := range hashes {
nameToHash[hashType[i]] = hex.EncodeToString(h.Sum(nil))
}
return nameToHash, nil
}
func multiWriter(hash []hash.Hash) io.Writer {
writers := make([]io.Writer, 0, len(hash))
for _, h := range hash {
writers = append(writers, h)
}
return io.MultiWriter(writers...)
}