forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplication_test.go
240 lines (192 loc) · 6.6 KB
/
replication_test.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
package journal_test
import (
"context"
"errors"
"fmt"
"sync"
"testing"
"time"
"github.com/Velocidex/ordereddict"
"github.com/alecthomas/assert"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"
mock_proto "www.velocidex.com/golang/velociraptor/api/mock"
api_proto "www.velocidex.com/golang/velociraptor/api/proto"
"www.velocidex.com/golang/velociraptor/file_store/test_utils"
"www.velocidex.com/golang/velociraptor/services"
"www.velocidex.com/golang/velociraptor/services/journal"
"www.velocidex.com/golang/velociraptor/services/orgs"
"www.velocidex.com/golang/velociraptor/utils"
"www.velocidex.com/golang/velociraptor/vtesting"
_ "www.velocidex.com/golang/velociraptor/result_sets/timed"
)
type MockFrontendService struct {
mock *mock_proto.MockAPIClient
}
func (self MockFrontendService) GetMinionCount() int {
return 1
}
// The minion replicates to the master node.
func (self MockFrontendService) GetMasterAPIClient(ctx context.Context) (
api_proto.APIClient, func() error, error) {
return self.mock, func() error { return nil }, nil
}
type ReplicationTestSuite struct {
test_utils.TestSuite
ctrl *gomock.Controller
mock *mock_proto.MockAPIClient
}
func (self *ReplicationTestSuite) SetupTest() {
self.ConfigObj = self.TestSuite.LoadConfig()
self.ConfigObj.Frontend.IsMinion = true
self.ConfigObj.Services.FrontendServer = false
self.ConfigObj.Services.ReplicationService = true
self.LoadArtifactsIntoConfig([]string{`
name: Test.Artifact
type: CLIENT_EVENT
`})
self.Services = &orgs.ServiceContainer{}
self.Services.MockFrontendManager(&MockFrontendService{self.mock})
self.ctrl = gomock.NewController(self.T())
self.mock = mock_proto.NewMockAPIClient(self.ctrl)
}
func (self *ReplicationTestSuite) setupTest() {
self.TestSuite.SetupTest()
}
func (self *ReplicationTestSuite) TestReplicationServiceStandardWatchers() {
// The ReplicationService will call WatchEvents for both the
// Server.Internal.Ping and Server.Internal.Notifications
// queues.
stream := mock_proto.NewMockAPI_WatchEventClient(self.ctrl)
stream.EXPECT().Recv().AnyTimes().Return(nil, errors.New("Error"))
// Record the WatchEvents calls
var mu sync.Mutex
watched := []string{}
mock_watch_event_recorder := func(
ctx context.Context, in *api_proto.EventRequest, opts ...grpc.CallOption) (
api_proto.API_WatchEventClient, error) {
mu.Lock()
defer mu.Unlock()
// only record unique listeners.
if !utils.InString(watched, in.Queue) {
watched = append(watched, in.Queue)
}
// Return an error stream - this will cause the service to
// retry connections.
return stream, nil
}
self.mock.EXPECT().WatchEvent(gomock.Any(), gomock.Any()).
//gomock.AssignableToTypeOf(ctxInterface),
//gomock.AssignableToTypeOf(&api_proto.EventRequest{})).
DoAndReturn(mock_watch_event_recorder).AnyTimes()
self.Services.MockFrontendManager(&MockFrontendService{self.mock})
self.setupTest()
// Wait here until we call all the watchers.
vtesting.WaitUntil(5*time.Second, self.T(), func() bool {
mu.Lock()
defer mu.Unlock()
expected := []string{
"Server.Internal.ArtifactModification",
"Server.Internal.MasterRegistrations",
// The notifications service will watch for
// notifications through us.
"Server.Internal.Notifications",
// Watch for ping requests from the
// master. This is used to let the master know
// if a client is connected to us.
"Server.Internal.Ping",
"Server.Internal.Pong",
}
for _, e := range expected {
if !utils.InString(watched, e) {
return false
}
}
return true
})
}
func (self *ReplicationTestSuite) TestSendingEvents() {
self.TestReplicationServiceStandardWatchers()
var mu sync.Mutex
events := []*api_proto.PushEventRequest{}
var last_error error
// Sending some rows to an event queue
record_push_event := func(ctx context.Context,
in *api_proto.PushEventRequest,
opts ...grpc.CallOption) (*emptypb.Empty, error) {
mu.Lock()
defer mu.Unlock()
// On error do not capture the request
if last_error != nil {
return nil, last_error
}
events = append(events, in)
return &emptypb.Empty{}, last_error
}
// Push an event into the journal service on the minion. It
// will result in an RPC on the master to pass the event on.
self.mock.EXPECT().PushEvents(gomock.Any(), gomock.Any()).
DoAndReturn(record_push_event).AnyTimes()
my_event := []*ordereddict.Dict{
ordereddict.NewDict().Set("Foo", "Bar")}
journal_service, err := services.GetJournal(self.ConfigObj)
assert.NoError(self.T(), err)
replicator := journal_service.(*journal.ReplicationService)
replicator.SetRetryDuration(100 * time.Millisecond)
replicator.ProcessMasterRegistrations(ordereddict.NewDict().
Set("Events", []interface{}{"Test.Artifact"}))
events = nil
err = journal_service.PushRowsToArtifact(self.Ctx, self.ConfigObj,
my_event, "Test.Artifact", "C.1234", "F.123")
assert.NoError(self.T(), err)
// Wait to see if the first event was properly delivered.
vtesting.WaitUntil(time.Second, self.T(), func() bool {
mu.Lock()
defer mu.Unlock()
return len(events) > 0
})
assert.Equal(self.T(), len(events), 1)
// Now emulate an RPC server error.
mu.Lock()
last_error = errors.New("Master is down!")
mu.Unlock()
events = nil
// Pushing to the journal service will transparently queue the
// messages to a buffer file and will relay them later. NOTE:
// This does not block, callers can not be blocked since this
// is often on the critical path. We just dump 1000 messages
// into the queue - this should overflow into the file.
for i := 0; i < 1000; i++ {
err = journal_service.PushRowsToArtifact(self.Ctx, self.ConfigObj,
my_event, "Test.Artifact", "C.1234", "F.123")
assert.NoError(self.T(), err)
}
// Wait for events to move from the channel buffer in memory to
// the disk buffer.
time.Sleep(time.Second)
// Make sure we wrote something to the buffer file.
ptr := replicator.Buffer.GetHeader().WritePointer
assert.True(self.T(),
ptr > 2000, fmt.Sprintf("WritePointer %v", ptr))
// Wait a while to allow events to be delivered.
time.Sleep(time.Second)
// Still no event got through
assert.Equal(self.T(), len(events), 0)
// Now enable the server, it should just deliver all the
// messages from the buffer file after a while as the
// ReplicationService will retry.
mu.Lock()
last_error = nil
mu.Unlock()
vtesting.WaitUntil(5*time.Second, self.T(), func() bool {
mu.Lock()
defer mu.Unlock()
return len(events) == 1000
})
assert.Equal(self.T(), len(events), 1000)
}
func TestReplication(t *testing.T) {
suite.Run(t, &ReplicationTestSuite{})
}