This repository was archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathactions.go
372 lines (348 loc) · 11.5 KB
/
actions.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
// 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 [email protected] [:ulfr]
package database /* import "github.com/mozilla/mig/database" */
import (
"database/sql"
"encoding/json"
"fmt"
"time"
"github.com/mozilla/mig"
_ "github.com/lib/pq"
)
// A container for information about an action loaded directly from Postgres.
// The `deserializeActionFromDB` function attempts to process this into a proper `Action`.
type actionFromDB struct {
ID float64
Name string
Target string
ValidFrom time.Time
ExpireAfter time.Time
Status string
SyntaxVersion uint16
DescriptionJSON []byte
ThreatJSON []byte
OperationsJSON []byte
SignaturesJSON []byte
}
func deserializeActionFromDB(retrieved actionFromDB) (mig.Action, error) {
action := mig.Action{
ID: retrieved.ID,
Name: retrieved.Name,
Target: retrieved.Target,
ValidFrom: retrieved.ValidFrom,
ExpireAfter: retrieved.ExpireAfter,
Status: retrieved.Status,
SyntaxVersion: retrieved.SyntaxVersion,
}
deserializeErrors := map[string]error{
"description": json.Unmarshal(retrieved.DescriptionJSON, &action.Description),
"threat": json.Unmarshal(retrieved.ThreatJSON, &action.Threat),
"operations": json.Unmarshal(retrieved.OperationsJSON, &action.Operations),
"signatures": json.Unmarshal(retrieved.SignaturesJSON, &action.PGPSignatures),
}
for attribute, err := range deserializeErrors {
if err != nil {
err = fmt.Errorf("Failed to unmarshal action %s: '%s'", attribute, err.Error())
return mig.Action{}, err
}
}
return action, nil
}
// LastActions retrieves the last X actions by time from the database
func (db *DB) LastActions(limit int) (actions []mig.Action, err error) {
rows, err := db.c.Query(`SELECT id, name, target, description, threat, operations,
validfrom, expireafter, starttime, finishtime, lastupdatetime,
status, pgpsignatures, syntaxversion
FROM actions ORDER BY starttime DESC LIMIT $1`, limit)
if rows != nil {
defer rows.Close()
}
if err != nil && err != sql.ErrNoRows {
err = fmt.Errorf("Error while listing actions: '%v'", err)
return
}
for rows.Next() {
var jDesc, jThreat, jOps, jSig []byte
var a mig.Action
err = rows.Scan(&a.ID, &a.Name, &a.Target,
&jDesc, &jThreat, &jOps, &a.ValidFrom, &a.ExpireAfter,
&a.StartTime, &a.FinishTime, &a.LastUpdateTime, &a.Status, &jSig, &a.SyntaxVersion)
if err != nil {
err = fmt.Errorf("Error while retrieving action: '%v'", err)
return
}
err = json.Unmarshal(jDesc, &a.Description)
if err != nil {
err = fmt.Errorf("Failed to unmarshal action description: '%v'", err)
return
}
err = json.Unmarshal(jThreat, &a.Threat)
if err != nil {
err = fmt.Errorf("Failed to unmarshal action threat: '%v'", err)
return
}
err = json.Unmarshal(jOps, &a.Operations)
if err != nil {
err = fmt.Errorf("Failed to unmarshal action operations: '%v'", err)
return
}
err = json.Unmarshal(jSig, &a.PGPSignatures)
if err != nil {
err = fmt.Errorf("Failed to unmarshal action signatures: '%v'", err)
return
}
a.Counters, err = db.GetActionCounters(a.ID)
if err != nil {
return
}
actions = append(actions, a)
}
if err := rows.Err(); err != nil {
err = fmt.Errorf("Failed to complete database query: '%v'", err)
}
return
}
// ActionByID retrieves an action from the database using its ID
// If the query fails, the returned action will have ID -1
func (db *DB) ActionByID(id float64) (a mig.Action, err error) {
a.ID = -1
var jDesc, jThreat, jOps, jSig []byte
err = db.c.QueryRow(`SELECT id, name, target, description, threat, operations,
validfrom, expireafter, starttime, finishtime, lastupdatetime,
status, pgpsignatures, syntaxversion
FROM actions WHERE id=$1`, id).Scan(&a.ID, &a.Name, &a.Target,
&jDesc, &jThreat, &jOps, &a.ValidFrom, &a.ExpireAfter,
&a.StartTime, &a.FinishTime, &a.LastUpdateTime, &a.Status, &jSig, &a.SyntaxVersion)
if err != nil {
err = fmt.Errorf("Error while retrieving action: '%v'", err)
return
}
err = json.Unmarshal(jDesc, &a.Description)
if err != nil {
err = fmt.Errorf("Failed to unmarshal action description: '%v'", err)
return
}
err = json.Unmarshal(jThreat, &a.Threat)
if err != nil {
err = fmt.Errorf("Failed to unmarshal action threat: '%v'", err)
return
}
err = json.Unmarshal(jOps, &a.Operations)
if err != nil {
err = fmt.Errorf("Failed to unmarshal action operations: '%v'", err)
return
}
err = json.Unmarshal(jSig, &a.PGPSignatures)
if err != nil {
err = fmt.Errorf("Failed to unmarshal action signatures: '%v'", err)
return
}
a.Counters, err = db.GetActionCounters(a.ID)
if err != nil {
return
}
return
}
// ActionMetaByID retrieves the metadata fields of an action from the database using its ID
func (db *DB) ActionMetaByID(id float64) (a mig.Action, err error) {
err = db.c.QueryRow(`SELECT id, name, validfrom, expireafter, starttime, finishtime, lastupdatetime,
status FROM actions WHERE id=$1`, id).Scan(&a.ID, &a.Name, &a.ValidFrom, &a.ExpireAfter,
&a.StartTime, &a.FinishTime, &a.LastUpdateTime, &a.Status)
if err != nil {
err = fmt.Errorf("Error while retrieving action: '%v'", err)
return
}
if err == sql.ErrNoRows {
return
}
return
}
// InsertAction writes an action into the database.
func (db *DB) InsertAction(a mig.Action) (err error) {
jDesc, err := json.Marshal(a.Description)
if err != nil {
return fmt.Errorf("Failed to marshal description: '%v'", err)
}
jThreat, err := json.Marshal(a.Threat)
if err != nil {
return fmt.Errorf("Failed to marshal threat: '%v'", err)
}
jOperations, err := json.Marshal(a.Operations)
if err != nil {
return fmt.Errorf("Failed to marshal operations: '%v'", err)
}
aPGPSignatures, err := json.Marshal(a.PGPSignatures)
if err != nil {
return fmt.Errorf("Failed to marshal pgp signatures: '%v'", err)
}
_, err = db.c.Exec(`INSERT INTO actions
(id, name, target, description, threat, operations,
validfrom, expireafter, starttime, finishtime, lastupdatetime,
status, pgpsignatures, syntaxversion)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)`,
a.ID, a.Name, a.Target, jDesc, jThreat, jOperations,
a.ValidFrom, a.ExpireAfter, a.StartTime, a.FinishTime, a.LastUpdateTime,
a.Status, aPGPSignatures, a.SyntaxVersion)
if err != nil {
return fmt.Errorf("Failed to store action: '%v'", err)
}
return
}
// UpdateAction stores updated action fields into the database.
func (db *DB) UpdateAction(a mig.Action) (err error) {
_, err = db.c.Exec(`UPDATE actions SET (starttime, lastupdatetime, status) = ($2, $3, $4) WHERE id=$1`,
a.ID, a.StartTime, a.LastUpdateTime, a.Status)
if err != nil {
return fmt.Errorf("Failed to update action: '%v'", err)
}
return
}
// InsertOrUpdateAction looks for an existing action in DB and update it,
// or insert a new one if none is found
func (db *DB) InsertOrUpdateAction(a mig.Action) (inserted bool, err error) {
var id float64
err = db.c.QueryRow(`SELECT id FROM actions WHERE id=$1`, a.ID).Scan(&id)
if err != nil && err != sql.ErrNoRows {
return inserted, fmt.Errorf("Error while retrieving action: '%v'", err)
}
if err == sql.ErrNoRows {
inserted = true
return inserted, db.InsertAction(a)
} else {
return inserted, db.UpdateAction(a)
}
}
// UpdateActionStatus updates the status of an action
func (db *DB) UpdateActionStatus(a mig.Action) (err error) {
_, err = db.c.Exec(`UPDATE actions SET (status) = ($2) WHERE id=$1`,
a.ID, a.Status)
if err != nil {
return fmt.Errorf("Failed to update action status: '%v'", err)
}
return
}
// UpdateRunningAction stores updated time and counters on a running action
func (db *DB) UpdateRunningAction(a mig.Action) (err error) {
_, err = db.c.Exec(`UPDATE actions SET (lastupdatetime) = ($2) WHERE id=$1`,
a.ID, a.LastUpdateTime)
if err != nil {
return fmt.Errorf("Failed to update action: '%v'", err)
}
return
}
// FinishAction updates the action fields to mark it as done
func (db *DB) FinishAction(a mig.Action) (err error) {
a.FinishTime = time.Now()
a.Status = "completed"
_, err = db.c.Exec(`UPDATE actions SET (finishtime, lastupdatetime, status) = ($1, $2, $3) WHERE id=$4`,
a.FinishTime, a.LastUpdateTime, a.Status, a.ID)
if err != nil {
return fmt.Errorf("Failed to update action: '%v'", err)
}
return
}
// InsertSignature create an entry in the signatures tables that map an investigator
// to an action and a signature
func (db *DB) InsertSignature(aid, iid float64, sig string) (err error) {
_, err = db.c.Exec(`INSERT INTO signatures(actionid, investigatorid, pgpsignature)
VALUES($1, $2, $3)`, aid, iid, sig)
if err != nil {
return fmt.Errorf("Failed to store signature: '%v'", err)
}
return
}
func (db *DB) GetActionCounters(aid float64) (counters mig.ActionCounters, err error) {
rows, err := db.c.Query(`SELECT DISTINCT(status), COUNT(id) FROM commands
WHERE actionid = $1 GROUP BY status`, aid)
if rows != nil {
defer rows.Close()
}
if err != nil && err != sql.ErrNoRows {
err = fmt.Errorf("Error while retrieving counters: '%v'", err)
return
}
for rows.Next() {
var count int
var status string
err = rows.Scan(&status, &count)
if err != nil {
err = fmt.Errorf("Error while retrieving counter: '%v'", err)
}
switch status {
case mig.StatusSent:
counters.InFlight = count
counters.Sent += count
case mig.StatusSuccess:
counters.Success = count
counters.Done += count
counters.Sent += count
case mig.StatusCancelled:
counters.Cancelled = count
counters.Done += count
counters.Sent += count
case mig.StatusExpired:
counters.Expired = count
counters.Done += count
counters.Sent += count
case mig.StatusFailed:
counters.Failed = count
counters.Done += count
counters.Sent += count
case mig.StatusTimeout:
counters.TimeOut = count
counters.Done += count
counters.Sent += count
}
}
if err := rows.Err(); err != nil {
err = fmt.Errorf("Failed to complete database query: '%v'", err)
}
return
}
// SetupRunnableActions retrieves actions that are ready to run. This function is designed
// to run concurrently across multiple schedulers, by update the status of the action at
// the same time as retrieving it. It returns an array of actions rady to be run.
func (db *DB) SetupRunnableActions() (actions []mig.Action, err error) {
rows, err := db.c.Query(`UPDATE actions SET status='scheduled'
WHERE status='pending' AND validfrom < NOW() AND expireafter > NOW()
RETURNING id, name, target, description, threat, operations,
validfrom, expireafter, status, pgpsignatures, syntaxversion`)
if rows != nil {
defer rows.Close()
}
if err != nil && err != sql.ErrNoRows {
err = fmt.Errorf("Error while setting up runnable actions: '%v'", err)
return
}
for rows.Next() {
retrieved := actionFromDB{}
err = rows.Scan(
&retrieved.ID,
&retrieved.Name,
&retrieved.Target,
&retrieved.DescriptionJSON,
&retrieved.ThreatJSON,
&retrieved.OperationsJSON,
&retrieved.ValidFrom,
&retrieved.ExpireAfter,
&retrieved.Status,
&retrieved.SignaturesJSON,
&retrieved.SyntaxVersion)
if err != nil {
err = fmt.Errorf("Error while retrieving action: '%s'", err.Error())
return
}
action, err := deserializeActionFromDB(retrieved)
if err != nil {
return []mig.Action{}, err
}
actions = append(actions, action)
}
if err := rows.Err(); err != nil {
err = fmt.Errorf("Failed to complete database query: '%v'", err)
}
return
}