forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplication.go
247 lines (206 loc) · 5.9 KB
/
replication.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
package api
import (
"fmt"
"sort"
"strings"
"sync"
"time"
"github.com/Velocidex/ordereddict"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/sirupsen/logrus"
context "golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
"www.velocidex.com/golang/velociraptor/acls"
api_proto "www.velocidex.com/golang/velociraptor/api/proto"
utils "www.velocidex.com/golang/velociraptor/api/utils"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
"www.velocidex.com/golang/velociraptor/json"
"www.velocidex.com/golang/velociraptor/logging"
"www.velocidex.com/golang/velociraptor/services"
"www.velocidex.com/golang/velociraptor/services/debug"
"www.velocidex.com/golang/vfilter"
)
var (
replicationReceiveHistorgram = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "replication_master_send_latency",
Help: "Latency for the master to send replication messages to the minion.",
Buckets: prometheus.LinearBuckets(0.1, 1, 10),
},
[]string{"status"},
)
gReplicationTracker = &replicationTracker{
currentReplications: make(map[string]*replicatedStats),
}
)
func streamEvents(
ctx context.Context,
config_obj *config_proto.Config,
in *api_proto.EventRequest,
stream api_proto.API_WatchEventServer,
peer_name string,
stats *replicatedStats) (err error) {
logger := logging.GetLogger(config_obj, &logging.APICmponent)
logger.WithFields(logrus.Fields{
"arg": in,
"user": peer_name,
}).Info("Replicating Events")
journal, err := services.GetJournal(config_obj)
if err != nil {
return err
}
// Special case this so the caller can immediately initialize the
// watchers.
if in.Queue == "Server.Internal.MasterRegistrations" {
result := ordereddict.NewDict().Set("Events", journal.GetWatchers())
serialized, _ := result.MarshalJSON()
stream.Send(&api_proto.EventResponse{
Jsonl: serialized,
})
stats.Sent++
}
// The API service is running on the master only! This means
// the journal service is local.
output_chan, cancel := journal.Watch(
ctx, in.Queue, "replication-"+in.WatcherName)
defer cancel()
for {
select {
case <-ctx.Done():
return
case event, ok := <-output_chan:
if !ok {
return
}
serialized, err := json.Marshal(event)
if err != nil {
continue
}
response := &api_proto.EventResponse{
Jsonl: serialized,
}
timer := prometheus.NewTimer(
prometheus.ObserverFunc(func(v float64) {
replicationReceiveHistorgram.WithLabelValues("").Observe(v)
}))
// If we are not able to send within the sepecified 5
// seconds we must abort the connection.
err = utils.DoWithTimeout(func() error {
return stream.Send(response)
}, 5*time.Second)
if err != nil {
return err
}
timer.ObserveDuration()
stats.Sent++
if err != nil {
continue
}
}
}
return nil
}
// NOTE: The API server is only running on the master node.
func (self *ApiServer) WatchEvent(
in *api_proto.EventRequest,
stream api_proto.API_WatchEventServer) error {
// Get the TLS context from the peer and verify its
// certificate.
ctx := stream.Context()
users := services.GetUserManager()
user_record, config_obj, err := users.GetUserFromContext(ctx)
if err != nil {
return err
}
// This name is taken from the certificate usually
// VelociraptorServer.
peer_name := user_record.Name
// Check that the principal is allowed to issue queries.
permissions := acls.ANY_QUERY
ok, err := services.CheckAccess(config_obj, peer_name, permissions)
if err != nil {
return status.Error(codes.PermissionDenied,
fmt.Sprintf("User %v is not allowed to run queries.",
peer_name))
}
if !ok {
return status.Error(codes.PermissionDenied, fmt.Sprintf(
"Permission denied: User %v requires permission %v to run queries",
peer_name, permissions))
}
// Update the peer name to make it unique
peer_addr, ok := peer.FromContext(ctx)
if ok {
peer_name = strings.Split(peer_addr.Addr.String(), ":")[0]
}
// Wait here for orderly shutdown of event streams.
self.wg.Add(1)
defer self.wg.Done()
// The call can access the datastore from any org becuase it is a
// server->server call.
org_manager, err := services.GetOrgManager()
if err != nil {
return err
}
org_config_obj, err := org_manager.GetOrgConfig(in.OrgId)
if err != nil {
return err
}
// Cert is good enough for us, run the query.
stats, closer := gReplicationTracker.Add(in.Queue, peer_name, in.OrgId)
defer closer()
return streamEvents(
ctx, org_config_obj, in, stream, peer_name, stats)
}
type replicatedStats struct {
Sent int
}
type replicationTracker struct {
mu sync.Mutex
currentReplications map[string]*replicatedStats
}
func (self *replicationTracker) Debug() []*ordereddict.Dict {
self.mu.Lock()
defer self.mu.Unlock()
result := []*ordereddict.Dict{}
keys := []string{}
for k := range self.currentReplications {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v, _ := self.currentReplications[k]
result = append(result, ordereddict.NewDict().
Set("Type", "Replication").
Set("Name", k).
Set("Stats", v))
}
return result
}
func (self *replicationTracker) Add(queue, peer, org_id string) (*replicatedStats, func()) {
key := queue + "->" + peer + " " + org_id
self.mu.Lock()
defer self.mu.Unlock()
stats := &replicatedStats{}
self.currentReplications[key] = stats
return stats, func() {
self.mu.Lock()
defer self.mu.Unlock()
delete(self.currentReplications, key)
}
}
func init() {
debug.RegisterProfileWriter(debug.ProfileWriterInfo{
Name: "Replication",
Description: "Report current replication connections between master and minion",
ProfileWriter: func(ctx context.Context,
scope vfilter.Scope, output_chan chan vfilter.Row) {
for _, i := range gReplicationTracker.Debug() {
output_chan <- i
}
},
})
}