forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolden.go
587 lines (476 loc) · 14.8 KB
/
golden.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/*
Velociraptor - Dig Deeper
Copyright (C) 2019-2022 Rapid7 Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"runtime/pprof"
"sort"
"strings"
"sync"
"time"
"github.com/Velocidex/ordereddict"
"github.com/Velocidex/yaml/v2"
errors "github.com/go-errors/errors"
"github.com/sergi/go-diff/diffmatchpatch"
"www.velocidex.com/golang/velociraptor/actions"
actions_proto "www.velocidex.com/golang/velociraptor/actions/proto"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
"www.velocidex.com/golang/velociraptor/constants"
"www.velocidex.com/golang/velociraptor/crypto/storage"
crypto_utils "www.velocidex.com/golang/velociraptor/crypto/utils"
"www.velocidex.com/golang/velociraptor/file_store"
"www.velocidex.com/golang/velociraptor/json"
logging "www.velocidex.com/golang/velociraptor/logging"
"www.velocidex.com/golang/velociraptor/paths"
"www.velocidex.com/golang/velociraptor/reporting"
"www.velocidex.com/golang/velociraptor/services"
"www.velocidex.com/golang/velociraptor/services/writeback"
"www.velocidex.com/golang/velociraptor/startup"
"www.velocidex.com/golang/velociraptor/utils"
vql_subsystem "www.velocidex.com/golang/velociraptor/vql"
"www.velocidex.com/golang/velociraptor/vql/acl_managers"
"www.velocidex.com/golang/velociraptor/vql/psutils"
"www.velocidex.com/golang/velociraptor/vql/remapping"
vfilter "www.velocidex.com/golang/vfilter"
"www.velocidex.com/golang/vfilter/arg_parser"
)
var (
golden_command = app.Command(
"golden", "Run tests and compare against golden files.")
golden_command_directory = golden_command.Arg(
"directory", "Golden file directory path").Required().String()
golden_command_filter = golden_command.Flag("filter", "A regex to filter the test files").
String()
golden_env_map = golden_command.Flag("env", "Environment for the query.").
StringMap()
testonly = golden_command.Flag("testonly", "Do not update the fixture.").Bool()
disable_alarm = golden_command.Flag("disable_alarm", "Do not terminate when deadlocked.").Bool()
golden_update_datastore = golden_command.Flag("update_datastore",
"Normally golden tests run with the readonly datastore so as not to "+
"change the fixture. This flag allows updates to the fixtures.").
Bool()
// If the logs emit messages matching these then the test is
// considered failed. This helps us catch VQL errors.
fatalLogMessagesRegex = []string{
"(?i)Symbol .+ not found",
"(?i)Field .+ Expecting a .+ arg type, not",
"(?i)Artifact .+ not found",
"(?i)Order by column .+ not present in row",
"PANIC runtime error:",
"Extra unrecognized arg",
}
)
type testFixture struct {
Parameters map[string]string `json:"Parameters"`
Queries []string `json:"Queries"`
}
// We want to emulate as closely as possible the logic in the artifact
// collector client action. Therefore we build a vql_collector_args
// from the fixture.
func vqlCollectorArgsFromFixture(
config_obj *config_proto.Config,
fixture *testFixture) *actions_proto.VQLCollectorArgs {
vql_collector_args := &actions_proto.VQLCollectorArgs{}
for k, v := range fixture.Parameters {
vql_collector_args.Env = append(vql_collector_args.Env,
&actions_proto.VQLEnv{Key: k, Value: v})
}
return vql_collector_args
}
func makeCtxWithTimeout(
root_ctx context.Context, duration int) (context.Context, func()) {
ctx, cancel := context.WithCancel(root_ctx)
deadline := time.Now().Add(time.Second * time.Duration(duration))
fmt.Printf("Setting deadline to %v\n", deadline)
// Set an alarm for hard exit in 2 minutes. If we hit it then
// the code is deadlocked and we want to know what is
// happening.
go func() {
for {
select {
case <-ctx.Done():
fmt.Printf("Disarming alarm\n")
return
// If we get here we are deadlocked! Print all
// the goroutines and mutex and hard exit.
case <-time.After(time.Second):
if time.Now().Before(deadline) {
pid := int32(os.Getpid())
total_time, _ := psutils.TimesWithContext(ctx, pid)
memory, _ := psutils.MemoryInfoWithContext(ctx, pid)
fmt.Printf("Not time to fire yet %v %v %v\n",
time.Now(), json.MustMarshalString(total_time),
json.MustMarshalString(memory))
continue
}
p := pprof.Lookup("goroutine")
if p != nil {
_ = p.WriteTo(os.Stdout, 1)
}
p = pprof.Lookup("mutex")
if p != nil {
_ = p.WriteTo(os.Stdout, 1)
}
// Write the recent queries.
fmt.Println("Recent Queries.")
for _, q := range actions.QueryLog.Get() {
fmt.Println(q)
}
os.Stdout.Close()
// Hard exit with an error.
os.Exit(-1)
}
}
}()
return ctx, cancel
}
func runTest(fixture *testFixture, sm *services.Service,
config_obj *config_proto.Config) (string, error) {
// Freeze the time for consistent golden tests Monday, May 31, 2020 3:28:05 PM
closer := utils.MockTime(utils.NewMockClock(time.Unix(1590938885, 10)))
defer closer()
ctx := sm.Ctx
// Limit each test for maxmimum time
if !*disable_alarm {
sub_ctx, cancel := makeCtxWithTimeout(ctx, 30)
defer cancel()
ctx = sub_ctx
}
// Set this to emulate a working client.
storage.SetCurrentServerPem([]byte(config_obj.Frontend.Certificate))
writeback_service := writeback.GetWritebackService()
writeback_service.LoadWriteback(config_obj)
err := crypto_utils.VerifyConfig(config_obj)
if err != nil {
log.Fatal(err)
}
// Create an output container.
tmpfile, err := ioutil.TempFile("", "golden")
if err != nil {
log.Fatal(err)
}
defer os.Remove(tmpfile.Name())
container, err := reporting.NewContainer(
config_obj, tmpfile.Name(), "", 5, nil)
if err != nil {
return "", fmt.Errorf("Can not create output container: %w", err)
}
log_writer.Clear()
builder := services.ScopeBuilder{
Config: config_obj,
ACLManager: acl_managers.NewRoleACLManager(config_obj, "administrator", "org_admin"),
Logger: log.New(log_writer, "Velociraptor: ", 0),
Uploader: container,
Env: ordereddict.NewDict().
Set("GoldenOutput", tmpfile.Name()).
Set("config", config_obj.Client).
Set("_SessionId", "F.Golden").
Set(constants.SCOPE_MOCK, &remapping.MockingScopeContext{}),
}
if golden_env_map != nil {
for k, v := range *golden_env_map {
builder.Env.Set(k, v)
}
}
vql_collector_args := vqlCollectorArgsFromFixture(config_obj, fixture)
for _, env_spec := range vql_collector_args.Env {
builder.Env.Set(env_spec.Key, env_spec.Value)
}
// Cleanup after the query.
manager, err := services.GetRepositoryManager(config_obj)
if err != nil {
return "", err
}
scope := manager.BuildScopeFromScratch(builder)
defer scope.Close()
err = scope.AddDestructor(func() {
container.Close()
os.Remove(tmpfile.Name()) // clean up
})
if err != nil {
return "", err
}
result := ""
for _, query := range fixture.Queries {
result += query
scope.Log("Running query %v", query)
vql, err := vfilter.Parse(query)
if err != nil {
return "", err
}
result_chan := vfilter.GetResponseChannel(
vql, ctx, scope,
vql_subsystem.MarshalJsonIndent(scope),
1000, 1000)
for {
query_result, ok := <-result_chan
if !ok {
break
}
result += string(query_result.Payload)
}
}
for _, msg := range fatalLogMessagesRegex {
matches, err := log_writer.Matches(msg)
if matches || err != nil {
return "", fmt.Errorf("Log out matches %q", msg)
}
}
return result, nil
}
func doGolden() error {
logging.DisableLogging()
vql_subsystem.RegisterPlugin(&MemoryLogPlugin{})
vql_subsystem.RegisterFunction(&WriteFilestoreFunction{})
vql_subsystem.RegisterFunction(&MockTimeFunciton{})
config_obj, err := makeDefaultConfigLoader().LoadAndValidate()
if err != nil {
return err
}
// Do not update the datastore - this allows golden tests to avoid
// modifying the fixtures.
if !*golden_update_datastore {
config_obj.Datastore.Implementation = "ReadOnlyDataStore"
}
logger := logging.GetLogger(config_obj, &logging.ToolComponent)
logger.Info("Starting golden file test.")
log_writer = &MemoryLogWriter{config_obj: config_obj}
failures := []string{}
config_obj.Services = services.GoldenServicesSpec()
ctx, cancel := install_sig_handler()
defer cancel()
// Global timeout for the entire test
if !*disable_alarm {
timeout_ctx, cancel := makeCtxWithTimeout(ctx, 120)
defer cancel()
ctx = timeout_ctx
}
sm, err := startup.StartToolServices(ctx, config_obj)
defer sm.Close()
if err != nil {
return err
}
var file_paths []string
err = filepath.Walk(*golden_command_directory, func(file_path string, info os.FileInfo, err error) error {
if *golden_command_filter != "" &&
!strings.HasPrefix(filepath.Base(file_path), *golden_command_filter) {
return nil
}
if !strings.HasSuffix(file_path, ".in.yaml") {
return nil
}
file_paths = append(file_paths, file_path)
return nil
})
// Run the test cases in a predictable way
sort.Strings(file_paths)
logger.Info("<green>Testing %v test cases</>", len(file_paths))
for _, file_path := range file_paths {
select {
case <-sm.Ctx.Done():
return errors.New("Cancelled!")
default:
}
logger := log.New(os.Stderr, "golden: ", 0)
logger.Printf("Opening %v", file_path)
data, err := ioutil.ReadFile(file_path)
if err != nil {
return fmt.Errorf("Reading file: %w", err)
}
fixture := testFixture{}
err = yaml.Unmarshal(data, &fixture)
if err != nil {
return fmt.Errorf("Unmarshal input file: %w", err)
}
result, err := runTest(&fixture, sm, config_obj)
if err != nil {
return fmt.Errorf("Running test %v: %w", fixture, err)
}
outfile := strings.Replace(file_path, ".in.", ".out.", -1)
old_data, err := ioutil.ReadFile(outfile)
if err == nil {
if strings.TrimSpace(string(old_data)) != strings.TrimSpace(result) {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(
string(old_data), result, false)
fmt.Printf("Failed %v:\n", file_path)
fmt.Println(dmp.DiffPrettyText(diffs))
failures = append(failures, file_path)
}
} else {
fmt.Printf("New file for %v:\n", file_path)
fmt.Println(result)
failures = append(failures, file_path)
}
if !*testonly {
err = ioutil.WriteFile(
outfile,
[]byte(result), 0666)
if err != nil {
return fmt.Errorf("Unable to write golden file: %w", err)
}
}
}
if err != nil {
return fmt.Errorf("golden error FAIL: %w", err)
}
if len(failures) > 0 {
return fmt.Errorf(
"Failed! Some golden files did not match: %s\n", failures)
}
return nil
}
func init() {
command_handlers = append(command_handlers, func(command string) bool {
switch command {
case golden_command.FullCommand():
FatalIfError(golden_command, doGolden)
default:
return false
}
return true
})
}
var log_writer *MemoryLogWriter
type MemoryLogWriter struct {
mu sync.Mutex
config_obj *config_proto.Config
logs []string
}
func (self *MemoryLogWriter) Clear() {
self.mu.Lock()
defer self.mu.Unlock()
self.logs = nil
}
func (self *MemoryLogWriter) Write(b []byte) (int, error) {
self.mu.Lock()
defer self.mu.Unlock()
self.logs = append(self.logs, string(b))
logging.GetLogger(self.config_obj, &logging.ClientComponent).Info("%v", string(b))
return len(b), nil
}
func (self *MemoryLogWriter) Matches(pattern string) (bool, error) {
self.mu.Lock()
defer self.mu.Unlock()
re, err := regexp.Compile(pattern)
if err != nil {
return false, err
}
for _, line := range self.logs {
if re.FindString(line) != "" {
return true, nil
}
}
return false, nil
}
// Some tests need to inspect the logs
type MemoryLogPlugin struct{}
func (self MemoryLogPlugin) Call(
ctx context.Context,
scope vfilter.Scope,
args *ordereddict.Dict) <-chan vfilter.Row {
output_chan := make(chan vfilter.Row)
go func() {
defer close(output_chan)
if log_writer != nil {
for _, line := range log_writer.logs {
output_chan <- ordereddict.NewDict().
Set("Log", line)
}
log_writer.Clear()
}
}()
return output_chan
}
func (self MemoryLogPlugin) Info(scope vfilter.Scope, type_map *vfilter.TypeMap) *vfilter.PluginInfo {
return &vfilter.PluginInfo{
Name: "test_read_logs",
Doc: "Read logs in golden test.",
ArgType: type_map.AddType(scope, vfilter.Null{}),
}
}
type WriteFilestoreFunctionArgs struct {
Data string `vfilter:"optional,field=data"`
FSPath string `vfilter:"optional,field=path"`
}
type WriteFilestoreFunction struct{}
func (self WriteFilestoreFunction) Call(ctx context.Context,
scope vfilter.Scope,
args *ordereddict.Dict) vfilter.Any {
arg := &WriteFilestoreFunctionArgs{}
err := arg_parser.ExtractArgsWithContext(ctx, scope, args, arg)
if err != nil {
scope.Log("write_filestore: %s", err)
return &vfilter.Null{}
}
config_obj, ok := vql_subsystem.GetServerConfig(scope)
if !ok {
scope.Log("Command can only run on the server")
return &vfilter.Null{}
}
file_store_factory := file_store.GetFileStore(config_obj)
pathspec := paths.FSPathSpecFromClientPath(arg.FSPath)
writer, err := file_store_factory.WriteFile(pathspec)
if err != nil {
scope.Log("write_filestore: %s", err)
return &vfilter.Null{}
}
defer writer.Close()
writer.Write([]byte(arg.Data))
return true
}
func (self WriteFilestoreFunction) Info(
scope vfilter.Scope, type_map *vfilter.TypeMap) *vfilter.FunctionInfo {
return &vfilter.FunctionInfo{
Name: "write_filestore",
Doc: "Write a file on the filestore.",
ArgType: type_map.AddType(scope, &WriteFilestoreFunctionArgs{}),
}
}
type MockTimeFuncitonArgs struct {
Now int64 `vfilter:"required,field=now"`
}
type MockTimeFunciton struct{}
func (self MockTimeFunciton) Call(ctx context.Context,
scope vfilter.Scope,
args *ordereddict.Dict) vfilter.Any {
arg := &MockTimeFuncitonArgs{}
err := arg_parser.ExtractArgsWithContext(ctx, scope, args, arg)
if err != nil {
scope.Log("mock_time: %s", err)
return &vfilter.Null{}
}
clock := utils.NewMockClock(time.Unix(arg.Now, 0))
cancel := utils.MockTime(clock)
err = vql_subsystem.GetRootScope(scope).AddDestructor(cancel)
if err != nil {
scope.Log("mock_time: %s", err)
return &vfilter.Null{}
}
return true
}
func (self MockTimeFunciton) Info(
scope vfilter.Scope, type_map *vfilter.TypeMap) *vfilter.FunctionInfo {
return &vfilter.FunctionInfo{
Name: "mock_time",
ArgType: type_map.AddType(scope, &MockTimeFuncitonArgs{}),
}
}