This repository has been archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 235
/
investigator.go
384 lines (363 loc) · 9.13 KB
/
investigator.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: Julien Vehent jvehent@mozilla.com [:ulfr]
package mig /* import "github.com/mozilla/mig" */
import (
"fmt"
"time"
)
// Investigator describes a single MIG investigator
type Investigator struct {
ID float64 `json:"id,omitempty"`
Name string `json:"name"`
PGPFingerprint string `json:"pgpfingerprint"`
PublicKey []byte `json:"publickey,omitempty"`
PrivateKey []byte `json:"privatekey,omitempty"`
Status string `json:"status"`
CreatedAt time.Time `json:"createdat"`
LastModified time.Time `json:"lastmodified"`
APIKey string `json:"apikey,omitempty"`
Permissions InvestigatorPerms `json:"permissions"`
}
// CheckPermission validates if an investigator has given permission pv
func (i *Investigator) CheckPermission(pv int64) bool {
switch pv {
case PermSearch:
return i.Permissions.Search
case PermAction:
return i.Permissions.Action
case PermActionCreate:
return i.Permissions.ActionCreate
case PermCommand:
return i.Permissions.Command
case PermAgent:
return i.Permissions.Agent
case PermDashboard:
return i.Permissions.Dashboard
case PermLoader:
return i.Permissions.Loader
case PermLoaderStatus:
return i.Permissions.LoaderStatus
case PermLoaderExpect:
return i.Permissions.LoaderExpect
case PermLoaderKey:
return i.Permissions.LoaderKey
case PermLoaderNew:
return i.Permissions.LoaderNew
case PermManifest:
return i.Permissions.Manifest
case PermManifestSign:
return i.Permissions.ManifestSign
case PermManifestNew:
return i.Permissions.ManifestNew
case PermManifestLoaders:
return i.Permissions.ManifestLoaders
case PermInvestigator:
return i.Permissions.Investigator
case PermInvestigatorCreate:
return i.Permissions.InvestigatorCreate
case PermInvestigatorUpdate:
return i.Permissions.InvestigatorUpdate
}
return false
}
// InvestigatorPerms describes permissions assigned to an investigator
type InvestigatorPerms struct {
Search bool `json:"search"`
Action bool `json:"action"`
ActionCreate bool `json:"action_create"`
Command bool `json:"command"`
Agent bool `json:"agent"`
Dashboard bool `json:"dashboard"`
Loader bool `json:"loader"`
LoaderStatus bool `json:"loader_status"`
LoaderExpect bool `json:"loader_expect"`
LoaderKey bool `json:"loader_key"`
LoaderNew bool `json:"loader_new"`
Manifest bool `json:"manifest"`
ManifestSign bool `json:"manifest_sign"`
ManifestStatus bool `json:"manifest_status"`
ManifestNew bool `json:"manifest_new"`
ManifestLoaders bool `json:"manifest_loaders"`
Investigator bool `json:"investigator"`
InvestigatorCreate bool `json:"investigator_create"`
InvestigatorUpdate bool `json:"investigator_update"`
}
// FromMask converts a permission bit mask into a boolean permission set
func (ip *InvestigatorPerms) FromMask(mask int64) {
if (mask & PermSearch) != 0 {
ip.Search = true
}
if (mask & PermAction) != 0 {
ip.Action = true
}
if (mask & PermActionCreate) != 0 {
ip.ActionCreate = true
}
if (mask & PermCommand) != 0 {
ip.Command = true
}
if (mask & PermAgent) != 0 {
ip.Agent = true
}
if (mask & PermDashboard) != 0 {
ip.Dashboard = true
}
if (mask & PermLoader) != 0 {
ip.Loader = true
}
if (mask & PermLoaderStatus) != 0 {
ip.LoaderStatus = true
}
if (mask & PermLoaderExpect) != 0 {
ip.LoaderExpect = true
}
if (mask & PermLoaderKey) != 0 {
ip.LoaderKey = true
}
if (mask & PermLoaderNew) != 0 {
ip.LoaderNew = true
}
if (mask & PermManifest) != 0 {
ip.Manifest = true
}
if (mask & PermManifestSign) != 0 {
ip.ManifestSign = true
}
if (mask & PermManifestNew) != 0 {
ip.ManifestNew = true
}
if (mask & PermManifestStatus) != 0 {
ip.ManifestStatus = true
}
if (mask & PermManifestLoaders) != 0 {
ip.ManifestLoaders = true
}
if (mask & PermInvestigator) != 0 {
ip.Investigator = true
}
if (mask & PermInvestigatorCreate) != 0 {
ip.InvestigatorCreate = true
}
if (mask & PermInvestigatorUpdate) != 0 {
ip.InvestigatorUpdate = true
}
}
// ToMask converts a boolean permission set to a permission bit mask
func (ip *InvestigatorPerms) ToMask() (ret int64) {
if ip.Search {
ret |= PermSearch
}
if ip.Action {
ret |= PermAction
}
if ip.ActionCreate {
ret |= PermActionCreate
}
if ip.Command {
ret |= PermCommand
}
if ip.Agent {
ret |= PermAgent
}
if ip.Dashboard {
ret |= PermDashboard
}
if ip.Loader {
ret |= PermLoader
}
if ip.LoaderStatus {
ret |= PermLoaderStatus
}
if ip.LoaderExpect {
ret |= PermLoaderExpect
}
if ip.LoaderKey {
ret |= PermLoaderKey
}
if ip.LoaderNew {
ret |= PermLoaderNew
}
if ip.Manifest {
ret |= PermManifest
}
if ip.ManifestSign {
ret |= PermManifestSign
}
if ip.ManifestNew {
ret |= PermManifestNew
}
if ip.ManifestStatus {
ret |= PermManifestStatus
}
if ip.ManifestLoaders {
ret |= PermManifestLoaders
}
if ip.Investigator {
ret |= PermInvestigator
}
if ip.InvestigatorCreate {
ret |= PermInvestigatorCreate
}
if ip.InvestigatorUpdate {
ret |= PermInvestigatorUpdate
}
return ret
}
// ToDescriptive converts an existing boolean permission set to a descriptive string, used
// primarily in mig-console for summarizing permissions assigned to an investigator
func (ip *InvestigatorPerms) ToDescriptive() string {
cf := func(want int64, have int64) (bool, int64) {
var (
wantcnt, havecnt int64
n uint = 64
sb uint
)
for sb = 0; sb < n; sb++ {
wantcnt += (want >> sb) & 0x01
}
for sb = 0; sb < n; sb++ {
havecnt += ((have & want) >> sb) & 0x01
}
return (havecnt == wantcnt), havecnt
}
ret := ""
// Compare the permissions applied to the investigator to the various
// permission sets; if the user has the full set of permissions from the
// set we note that
tv := InvestigatorPerms{}
tv.DefaultSet()
fs, part := cf(tv.ToMask(), ip.ToMask())
if fs {
ret = "Default"
} else if part > 0 {
ret = "Default(partial)"
}
av := ""
tv = InvestigatorPerms{}
tv.AdminSet()
fs, part = cf(tv.ToMask(), ip.ToMask())
if fs {
av = "PermAdmin"
} else if part > 0 {
av = "PermAdmin(partial)"
}
if ret != "" && av != "" {
ret += ","
}
ret += av
av = ""
tv = InvestigatorPerms{}
tv.LoaderSet()
fs, part = cf(tv.ToMask(), ip.ToMask())
if fs {
av = "PermLoader"
} else if part > 0 {
av = "PermLoader(partial)"
}
if ret != "" && av != "" {
ret += ","
}
ret += av
av = ""
tv = InvestigatorPerms{}
tv.ManifestSet()
fs, part = cf(tv.ToMask(), ip.ToMask())
if fs {
av = "PermManifest"
} else if part > 0 {
av = "PermManifest(partial)"
}
if ret != "" && av != "" {
ret += ","
}
ret += av
return ret
}
// PermSets describes permission sets that can be applied; note default is omitted as this
// is currently always applied
var PermSets = []string{"PermManifest", "PermLoader", "PermAdmin"}
// FromSetList applies permission sets in slice sl to the investigator
func (ip *InvestigatorPerms) FromSetList(sl []string) error {
for _, x := range sl {
switch x {
case "PermManifest":
ip.ManifestSet()
case "PermLoader":
ip.LoaderSet()
case "PermAdmin":
ip.AdminSet()
default:
return fmt.Errorf("invalid permission %q", x)
}
}
return nil
}
// DefaultSet sets a default set of permissions on the investigator
func (ip *InvestigatorPerms) DefaultSet() {
ip.Search = true
ip.Action = true
ip.ActionCreate = true
ip.Command = true
ip.Agent = true
ip.Dashboard = true
}
// ManifestSet sets manifest related permissions on the investigator
func (ip *InvestigatorPerms) ManifestSet() {
ip.Manifest = true
ip.ManifestSign = true
ip.ManifestNew = true
ip.ManifestStatus = true
ip.ManifestLoaders = true
}
// LoaderSet sets loader related permissions on the investigator
func (ip *InvestigatorPerms) LoaderSet() {
ip.Loader = true
ip.LoaderStatus = true
ip.LoaderExpect = true
ip.LoaderKey = true
ip.LoaderNew = true
}
// AdminSet sets administrative permissions on the investigator
func (ip *InvestigatorPerms) AdminSet() {
ip.Investigator = true
ip.InvestigatorCreate = true
ip.InvestigatorUpdate = true
}
// Permissions that can be assigned to investigators
const (
PermSearch = 1 << iota
PermAction
PermActionCreate
PermCommand
PermAgent
PermDashboard
PermLoader
PermLoaderStatus
PermLoaderExpect
PermLoaderKey
PermLoaderNew
PermManifest
PermManifestSign
PermManifestNew
PermManifestStatus
PermManifestLoaders
PermInvestigator
PermInvestigatorCreate
PermInvestigatorUpdate
)
// Possible status values for an investigator
const (
StatusActiveInvestigator string = "active"
StatusDisabledInvestigator string = "disabled"
)
// InvestigatorAPIAuthHelper is a small struct used to pass information between
// the database and the API, and is used primarily for authorizing requests using
// API keys.
type InvestigatorAPIAuthHelper struct {
ID float64 // Investigator ID
APIKey []byte // Key hash
Salt []byte // Key salt
}