forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatastore.go
247 lines (205 loc) · 6.4 KB
/
datastore.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 (
"sync"
context "golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"www.velocidex.com/golang/velociraptor/acls"
api_proto "www.velocidex.com/golang/velociraptor/api/proto"
"www.velocidex.com/golang/velociraptor/datastore"
"www.velocidex.com/golang/velociraptor/file_store/api"
"www.velocidex.com/golang/velociraptor/file_store/path_specs"
"www.velocidex.com/golang/velociraptor/services"
)
// Raw Datastore access requires the DATASTORE_ACCESS permission. This
// is not provided by any role so the only callers allowed are
// server-server gRPC calls (e.g. minion -> master)
func (self *ApiServer) GetSubject(
ctx context.Context,
in *api_proto.DataRequest) (*api_proto.DataResponse, error) {
users := services.GetUserManager()
user_record, org_config_obj, err := users.GetUserFromContext(ctx)
if err != nil {
return nil, Status(self.verbose, err)
}
user_name := user_record.Name
token, err := services.GetEffectivePolicy(org_config_obj, user_name)
if err != nil {
return nil, Status(self.verbose, err)
}
perm, err := services.CheckAccessWithToken(token, acls.DATASTORE_ACCESS)
if !perm || err != nil {
return nil, PermissionDenied(err,
"User is not allowed to access datastore.")
}
// Only the superuser is allowed to switch orgs.
if token.SuperUser && org_config_obj.OrgId != in.OrgId {
org_manager, err := services.GetOrgManager()
if err != nil {
return nil, Status(self.verbose, err)
}
org_config_obj, err = org_manager.GetOrgConfig(in.OrgId)
if err != nil {
return nil, Status(self.verbose, err)
}
}
db, err := datastore.GetDB(org_config_obj)
if err != nil {
return nil, Status(self.verbose, err)
}
raw_db, ok := db.(datastore.RawDataStore)
if !ok {
return nil, status.Error(codes.Internal,
"Datastore has no raw access.")
}
data, err := raw_db.GetBuffer(org_config_obj, getURN(in))
return &api_proto.DataResponse{
Data: data,
}, err
}
func (self *ApiServer) SetSubject(
ctx context.Context,
in *api_proto.DataRequest) (*api_proto.DataResponse, error) {
users := services.GetUserManager()
user_record, org_config_obj, err := users.GetUserFromContext(ctx)
if err != nil {
return nil, Status(self.verbose, err)
}
user_name := user_record.Name
token, err := services.GetEffectivePolicy(org_config_obj, user_name)
if err != nil {
return nil, Status(self.verbose, err)
}
perm, err := services.CheckAccessWithToken(token, acls.DATASTORE_ACCESS)
if !perm || err != nil {
return nil, PermissionDenied(err,
"User is not allowed to access datastore.")
}
// Only the superuser is allowed to switch orgs.
if token.SuperUser && org_config_obj.OrgId != in.OrgId {
org_manager, err := services.GetOrgManager()
if err != nil {
return nil, Status(self.verbose, err)
}
org_config_obj, err = org_manager.GetOrgConfig(in.OrgId)
if err != nil {
return nil, Status(self.verbose, err)
}
}
db, err := datastore.GetDB(org_config_obj)
if err != nil {
return nil, Status(self.verbose, err)
}
raw_db, ok := db.(datastore.RawDataStore)
if !ok {
return nil, status.Error(codes.Internal,
"Datastore has no raw access.")
}
if in.Sync {
var wg sync.WaitGroup
// Wait for the data to hit the disk.
wg.Add(1)
err = raw_db.SetBuffer(org_config_obj, getURN(in), in.Data, func() {
wg.Done()
})
wg.Wait()
} else {
// Just write quickly.
err = raw_db.SetBuffer(org_config_obj, getURN(in), in.Data, nil)
}
return &api_proto.DataResponse{}, err
}
func (self *ApiServer) ListChildren(
ctx context.Context,
in *api_proto.DataRequest) (*api_proto.ListChildrenResponse, error) {
users := services.GetUserManager()
user_record, org_config_obj, err := users.GetUserFromContext(ctx)
if err != nil {
return nil, Status(self.verbose, err)
}
user_name := user_record.Name
token, err := services.GetEffectivePolicy(org_config_obj, user_name)
if err != nil {
return nil, Status(self.verbose, err)
}
perm, err := services.CheckAccessWithToken(token, acls.DATASTORE_ACCESS)
if !perm || err != nil {
return nil, PermissionDenied(err,
"User is not allowed to access datastore.")
}
// The call can access the datastore from any org becuase it is a
// server->server call.
if token.SuperUser && org_config_obj.OrgId != in.OrgId {
org_manager, err := services.GetOrgManager()
if err != nil {
return nil, Status(self.verbose, err)
}
org_config_obj, err = org_manager.GetOrgConfig(in.OrgId)
if err != nil {
return nil, Status(self.verbose, err)
}
}
db, err := datastore.GetDB(org_config_obj)
if err != nil {
return nil, Status(self.verbose, err)
}
children, err := db.ListChildren(org_config_obj, getURN(in))
if err != nil {
return nil, Status(self.verbose, err)
}
result := &api_proto.ListChildrenResponse{}
for _, child := range children {
result.Children = append(result.Children, &api_proto.DSPathSpec{
Components: child.Components(),
PathType: int64(child.Type()),
Tag: child.Tag(),
IsDir: child.IsDir(),
})
}
return result, nil
}
func (self *ApiServer) DeleteSubject(
ctx context.Context,
in *api_proto.DataRequest) (*emptypb.Empty, error) {
users := services.GetUserManager()
user_record, org_config_obj, err := users.GetUserFromContext(ctx)
if err != nil {
return nil, Status(self.verbose, err)
}
user_name := user_record.Name
token, err := services.GetEffectivePolicy(org_config_obj, user_name)
if err != nil {
return nil, Status(self.verbose, err)
}
perm, err := services.CheckAccessWithToken(token, acls.DATASTORE_ACCESS)
if !perm || err != nil {
return nil, PermissionDenied(err,
"User is not allowed to access datastore.")
}
if token.SuperUser && org_config_obj.OrgId != in.OrgId {
org_manager, err := services.GetOrgManager()
if err != nil {
return nil, Status(self.verbose, err)
}
org_config_obj, err = org_manager.GetOrgConfig(in.OrgId)
if err != nil {
return nil, Status(self.verbose, err)
}
}
db, err := datastore.GetDB(org_config_obj)
if err != nil {
return nil, Status(self.verbose, err)
}
return &emptypb.Empty{}, db.DeleteSubject(org_config_obj, getURN(in))
}
func getURN(in *api_proto.DataRequest) api.DSPathSpec {
path_spec := in.Pathspec
if path_spec == nil {
path_spec = &api_proto.DSPathSpec{}
}
return path_specs.NewUnsafeDatastorePath(
path_spec.Components...).
SetType(api.PathType(path_spec.PathType)).
SetTag(path_spec.Tag)
}