forked from moonstream-to/waggle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bugout.go
506 lines (425 loc) · 15.6 KB
/
bugout.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
package main
// Much of this code is copied from waggle: https://github.com/bugout-dev/waggle/blob/main/main.go
import (
"bytes"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
bugout "github.com/bugout-dev/bugout-go/pkg"
spire "github.com/bugout-dev/bugout-go/pkg/spire"
)
type BugoutAPIClient struct {
BroodBaseURL string
SpireBaseURL string
HTTPClient *http.Client
BugoutSpireClient spire.SpireClient
}
func InitBugoutAPIClient() (*BugoutAPIClient, error) {
if BROOD_API_URL == "" {
BROOD_API_URL = "https://auth.bugout.dev"
}
if SPIRE_API_URL == "" {
SPIRE_API_URL = "https://spire.bugout.dev"
}
if BUGOUT_API_TIMEOUT_SECONDS == "" {
BUGOUT_API_TIMEOUT_SECONDS = "10"
}
timeoutSeconds, conversionErr := strconv.Atoi(BUGOUT_API_TIMEOUT_SECONDS)
if conversionErr != nil {
return nil, conversionErr
}
timeout := time.Duration(timeoutSeconds) * time.Second
httpClient := http.Client{Timeout: timeout}
spire.ClientFromEnv()
return &BugoutAPIClient{
BroodBaseURL: BROOD_API_URL,
SpireBaseURL: SPIRE_API_URL,
HTTPClient: &httpClient,
BugoutSpireClient: spire.SpireClient{SpireURL: SPIRE_API_URL, Routes: spire.RoutesFromURL(SPIRE_API_URL), HTTPClient: &httpClient},
}, nil
}
func CleanTimestamp(rawTimestamp string) string {
return strings.ReplaceAll(rawTimestamp, " ", "T")
}
func GetCursorFromJournal(client *bugout.BugoutClient, token, journalID, cursorName string) (string, error) {
query := fmt.Sprintf("context_type:waggle tag:type:cursor tag:cursor:%s", cursorName)
parameters := map[string]string{
"order": "desc",
"content": "true", // We may use the content in the future, even though we are simply using context_url right now
}
results, err := client.Spire.SearchEntries(token, journalID, query, 1, 0, parameters)
if err != nil {
return "", err
}
if results.TotalResults == 0 {
return "", nil
}
return results.Results[0].ContextUrl, nil
}
func WriteCursorToJournal(client *bugout.BugoutClient, token, journalID, cursorName, cursor, queryTerms string) error {
title := fmt.Sprintf("waggle cursor: %s", cursorName)
entryContext := spire.EntryContext{
ContextType: "waggle",
ContextID: cursor,
ContextURL: cursor,
}
tags := []string{
"type:cursor",
fmt.Sprintf("cursor:%s", cursorName),
fmt.Sprintf("waggle_version:%s", WAGGLE_VERSION),
}
content := fmt.Sprintf("Cursor: %s at %s\nQuery: %s", cursorName, cursor, queryTerms)
_, err := client.Spire.CreateEntry(token, journalID, title, content, tags, entryContext)
return err
}
func ReportsIterator(client *bugout.BugoutClient, token, journalID, cursor, queryTerms string, limit, offset int) (spire.EntryResultsPage, error) {
var query string = fmt.Sprintf("!tag:type:cursor %s", queryTerms)
if cursor != "" {
cleanedCursor := CleanTimestamp(cursor)
query = fmt.Sprintf("%s created_at:>%s", query, cleanedCursor)
fmt.Fprintln(os.Stderr, "query:", query)
}
parameters := map[string]string{
"order": "asc",
"content": "false",
}
return client.Spire.SearchEntries(token, journalID, query, limit, offset, parameters)
}
func LoadDropperReports(searchResults spire.EntryResultsPage) ([]DropperClaimMessage, error) {
reports := make([]DropperClaimMessage, len(searchResults.Results))
for i, result := range searchResults.Results {
parseErr := json.Unmarshal([]byte(result.Content), &reports[i])
if parseErr != nil {
return reports, parseErr
}
}
return reports, nil
}
func DropperReportsToCSV(reports []DropperClaimMessage, header bool, w io.Writer) error {
numRecords := len(reports)
startIndex := 0
if header {
numRecords++
startIndex++
}
records := make([][]string, numRecords)
if header {
records[0] = []string{
"dropId", "requestID", "claimant", "blockDeadline", "amount", "signer", "signature",
}
}
for i, report := range reports {
records[i+startIndex] = []string{
report.DropId,
report.RequestID,
report.Claimant,
report.BlockDeadline,
report.Amount,
report.Signer,
report.Signature,
}
}
csvWriter := csv.NewWriter(w)
return csvWriter.WriteAll(records)
}
func ProcessDropperClaims(client *bugout.BugoutClient, bugoutToken, journalID, cursorName, query string, batchSize int, header bool, w io.Writer) error {
cursor, cursorErr := GetCursorFromJournal(client, bugoutToken, journalID, cursorName)
if cursorErr != nil {
return cursorErr
}
searchResults, searchErr := ReportsIterator(client, bugoutToken, journalID, cursor, query, batchSize, 0)
if searchErr != nil {
return searchErr
}
reports, loadErr := LoadDropperReports(searchResults)
if loadErr != nil {
return loadErr
}
writeErr := DropperReportsToCSV(reports, header, w)
if writeErr != nil {
return writeErr
}
var processedErr error
if len(searchResults.Results) > 0 {
processedErr = WriteCursorToJournal(client, bugoutToken, journalID, cursorName, searchResults.Results[len(searchResults.Results)-1].CreatedAt, query)
}
return processedErr
}
type RequestResourceHolder struct {
HolderId string `json:"holder_id"`
HolderType string `json:"holder_type"`
Permissions []string `json:"permissions"`
}
type ResourceHolder struct {
Id string `json:"id"`
HolderType string `json:"holder_type"`
Permissions []string `json:"permissions"`
}
type ResourceHolderUser struct {
Id string `json:"id"`
Username string `json:"username"`
ApplicationId string `json:"application_id"`
HolderType string `json:"holder_type"`
Permissions []string `json:"permissions"`
}
type ResourceHolderGroup struct {
Id string `json:"id"`
Name string `json:"name"`
Autogenerated bool `json:"autogenerated"`
HolderType string `json:"holder_type"`
Permissions []string `json:"permissions"`
}
type ResourceHolders struct {
ResourceId string `json:"resource_id"`
Holders []ResourceHolder `json:"holders"`
}
func (c *BugoutAPIClient) CheckAccessToResource(token, resourceId string) (ResourceHolders, int, error) {
var resourceHolders ResourceHolders
var requestBodyBytes []byte
request, requestErr := http.NewRequest("GET", fmt.Sprintf("%s/resources/%s/holders", c.BroodBaseURL, resourceId), bytes.NewBuffer(requestBodyBytes))
if requestErr != nil {
return resourceHolders, 0, requestErr
}
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
request.Header.Add("Accept", "application/json")
request.Header.Add("Content-Type", "application/json")
response, responseErr := c.HTTPClient.Do(request)
if responseErr != nil {
return resourceHolders, 0, responseErr
}
defer response.Body.Close()
responseBody, responseBodyErr := io.ReadAll(response.Body)
if responseBodyErr != nil {
return resourceHolders, response.StatusCode, fmt.Errorf("could not read response body: %s", responseBodyErr.Error())
}
if response.StatusCode != 200 {
return resourceHolders, response.StatusCode, fmt.Errorf("unexpected status code: %d -- response status: %s", response.StatusCode, response.Status)
}
unmarshalErr := json.Unmarshal(responseBody, &resourceHolders)
if unmarshalErr != nil {
return resourceHolders, response.StatusCode, fmt.Errorf("could not parse response body: %s", unmarshalErr.Error())
}
return resourceHolders, response.StatusCode, nil
}
type User struct {
Id string `json:"user_id"`
Username string `json:"username"`
ApplicationId string `json:"application_id"`
}
func (c *BugoutAPIClient) FindUser(token, userId string) (User, int, error) {
var user User
var requestBodyBytes []byte
request, requestErr := http.NewRequest("GET", fmt.Sprintf("%s/user/find?user_id=%s", c.BroodBaseURL, userId), bytes.NewBuffer(requestBodyBytes))
if requestErr != nil {
return user, 0, requestErr
}
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
request.Header.Add("Accept", "application/json")
request.Header.Add("Content-Type", "application/json")
response, responseErr := c.HTTPClient.Do(request)
if responseErr != nil {
return user, 0, requestErr
}
defer response.Body.Close()
responseBody, responseBodyErr := io.ReadAll(response.Body)
if responseBodyErr != nil {
return user, response.StatusCode, fmt.Errorf("could not read response body: %s", responseBodyErr.Error())
}
if response.StatusCode != 200 {
return user, response.StatusCode, fmt.Errorf("unexpected status code: %d -- response status: %s", response.StatusCode, response.Status)
}
unmarshalErr := json.Unmarshal(responseBody, &user)
if unmarshalErr != nil {
return user, response.StatusCode, fmt.Errorf("could not parse response body: %s", unmarshalErr.Error())
}
return user, response.StatusCode, nil
}
type Group struct {
Id string `json:"user_id"`
Name string `json:"name"`
Autogenerated bool `json:"autogenerated"`
}
func (c *BugoutAPIClient) FindGroup(token, groupId string) (Group, int, error) {
var group Group
var requestBodyBytes []byte
request, requestErr := http.NewRequest("GET", fmt.Sprintf("%s/group/find?group_id=%s", c.BroodBaseURL, groupId), bytes.NewBuffer(requestBodyBytes))
if requestErr != nil {
return group, 0, requestErr
}
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
request.Header.Add("Accept", "application/json")
request.Header.Add("Content-Type", "application/json")
response, responseErr := c.HTTPClient.Do(request)
if responseErr != nil {
return group, 0, requestErr
}
defer response.Body.Close()
responseBody, responseBodyErr := io.ReadAll(response.Body)
if responseBodyErr != nil {
return group, response.StatusCode, fmt.Errorf("could not read response body: %s", responseBodyErr.Error())
}
if response.StatusCode != 200 {
return group, response.StatusCode, fmt.Errorf("unexpected status code: %d -- response status: %s", response.StatusCode, response.Status)
}
unmarshalErr := json.Unmarshal(responseBody, &group)
if unmarshalErr != nil {
return group, response.StatusCode, fmt.Errorf("could not parse response body: %s", unmarshalErr.Error())
}
return group, response.StatusCode, nil
}
func (c *BugoutAPIClient) ModifyAccessToResource(token, resourceId, method string, requestResourceHolder *RequestResourceHolder) (ResourceHolders, int, error) {
var resourceHolders ResourceHolders
requestBody := RequestResourceHolder{
HolderId: requestResourceHolder.HolderId,
HolderType: requestResourceHolder.HolderType,
Permissions: requestResourceHolder.Permissions,
}
reqBodyBytes := new(bytes.Buffer)
json.NewEncoder(reqBodyBytes).Encode(requestBody)
request, requestErr := http.NewRequest(method, fmt.Sprintf("%s/resources/%s/holders", c.BroodBaseURL, resourceId), reqBodyBytes)
if requestErr != nil {
return resourceHolders, 0, requestErr
}
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
request.Header.Add("Accept", "application/json")
request.Header.Add("Content-Type", "application/json")
response, responseErr := c.HTTPClient.Do(request)
if responseErr != nil {
return resourceHolders, 0, responseErr
}
defer response.Body.Close()
responseBody, responseBodyErr := io.ReadAll(response.Body)
if responseBodyErr != nil {
return resourceHolders, response.StatusCode, fmt.Errorf("could not read response body: %s", responseBodyErr.Error())
}
if response.StatusCode != 200 {
return resourceHolders, response.StatusCode, fmt.Errorf("unexpected status code: %d -- response status: %s", response.StatusCode, response.Status)
}
unmarshalErr := json.Unmarshal(responseBody, &resourceHolders)
if unmarshalErr != nil {
return resourceHolders, response.StatusCode, fmt.Errorf("could not parse response body: %s", unmarshalErr.Error())
}
return resourceHolders, response.StatusCode, nil
}
type JobEntryContent struct {
FailedCallRequests []string `json:"failed_call_requests"`
PushedCallRequestIds []string `json:"pushed_call_request_ids"`
ContentParseError bool `json:"content_parse_error,omitempty"`
ContentRaw string `json:"content_raw,omitempty"`
}
type RequestJobEntry struct {
Title string `json:"title"`
Content string `json:"content"`
Tags []string `json:"tags"`
}
type JobResponse struct {
JobEntryUrl string `json:"job_entry_url,omitempty"`
Title string `json:"title"`
Content JobEntryContent `json:"content"`
Tags []string `json:"tags"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type JobsResponse struct {
Signer string `json:"signer"`
TotalResults int `json:"total_results"`
Offset int `json:"offset,omitempty"`
NextOffset int `json:"next_offset,omitempty"`
Jobs []JobResponse `json:"jobs"`
}
func SearchJobsInJournal(client *spire.SpireClient, signer string, limit, offset int) (*JobsResponse, error) {
searchQuery := fmt.Sprintf("tag:signer:%s", signer)
parameters := map[string]string{
"content": "true",
}
entryResultsPage, searchErr := client.SearchEntries(BUGOUT_WAGGLE_ADMIN_ACCESS_TOKEN, BUGOUT_METATX_JOBS_JOURNAL_ID, searchQuery, limit, offset, parameters)
if searchErr != nil {
return nil, searchErr
}
jobsResponse := JobsResponse{
Signer: signer,
TotalResults: entryResultsPage.TotalResults,
Offset: entryResultsPage.Offset,
NextOffset: entryResultsPage.NextOffset,
}
for _, r := range entryResultsPage.Results {
var jobEntryContent JobEntryContent
unmarshalErr := json.Unmarshal([]byte(r.Content), &jobEntryContent)
if unmarshalErr != nil {
log.Printf("Unable to parse content for entry %s, error: %v", r.Url, unmarshalErr)
jobEntryContent = JobEntryContent{
ContentParseError: true,
ContentRaw: r.Content,
}
}
job := JobResponse{
JobEntryUrl: r.Url,
Title: r.Title,
Content: jobEntryContent,
Tags: r.Tags,
CreatedAt: r.CreatedAt,
UpdatedAt: r.UpdatedAt,
}
jobsResponse.Jobs = append(jobsResponse.Jobs, job)
}
return &jobsResponse, searchErr
}
func CreateJobInJournal(client *spire.SpireClient, signer string) (*spire.Entry, error) {
title := fmt.Sprintf("draft job - signer %s", signer)
entryContext := spire.EntryContext{
ContextType: "waggle",
}
tags := []string{
"type:job",
fmt.Sprintf("signer:%s", signer),
"complete:false",
}
content := string([]byte("{}"))
jobEntry, err := client.CreateEntry(BUGOUT_WAGGLE_ADMIN_ACCESS_TOKEN, BUGOUT_METATX_JOBS_JOURNAL_ID, title, content, tags, entryContext)
return &jobEntry, err
}
func (c *BugoutAPIClient) UpdateJobInJournal(entryId, signer string, pushedCallRequestIds, failedCallRequests []string) (int, error) {
tags := []string{
"type:job",
fmt.Sprintf("signer:%s", signer),
"complete:true",
}
if len(failedCallRequests) != 0 {
tags = append(tags, "failed:true")
}
jobEntryContent := &JobEntryContent{
FailedCallRequests: failedCallRequests,
PushedCallRequestIds: pushedCallRequestIds,
}
jobEntryContentStr, marshalErr := json.Marshal(jobEntryContent)
if marshalErr != nil {
return 0, marshalErr
}
requestBody := RequestJobEntry{
Title: fmt.Sprintf("job - signer %s", signer),
Content: string(jobEntryContentStr),
Tags: tags,
}
reqBodyBytes := new(bytes.Buffer)
json.NewEncoder(reqBodyBytes).Encode(requestBody)
request, requestErr := http.NewRequest("PUT", fmt.Sprintf("%s/journals/%s/entries/%s?tags_action=replace", c.SpireBaseURL, BUGOUT_METATX_JOBS_JOURNAL_ID, entryId), reqBodyBytes)
if requestErr != nil {
return 0, requestErr
}
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", BUGOUT_WAGGLE_ADMIN_ACCESS_TOKEN))
request.Header.Add("Accept", "application/json")
request.Header.Add("Content-Type", "application/json")
response, responseErr := c.HTTPClient.Do(request)
if responseErr != nil {
return 0, responseErr
}
defer response.Body.Close()
return response.StatusCode, nil
}