/
v2.go
989 lines (892 loc) · 26.3 KB
/
v2.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
// Copyright (c) 2020-2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"runtime/debug"
"time"
v2 "github.com/decred/politeia/politeiad/api/v2"
"github.com/decred/politeia/politeiad/backendv2"
"github.com/decred/politeia/util"
)
func (p *politeia) handleRecordNew(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleRecordNew")
// Decode request
var rn v2.RecordNew
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&rn); err != nil {
respondWithErrorV2(w, r, "handleRecordNew: unmarshal",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeRequestPayloadInvalid,
})
return
}
challenge, err := hex.DecodeString(rn.Challenge)
if err != nil || len(challenge) != v2.ChallengeSize {
respondWithErrorV2(w, r, "handleRecordNew: decode challenge",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeChallengeInvalid,
})
return
}
// Create new record
var (
metadata = convertMetadataStreamsToBackend(rn.Metadata)
files = convertFilesToBackend(rn.Files)
)
rc, err := p.backendv2.RecordNew(metadata, files)
if err != nil {
respondWithErrorV2(w, r,
"handleRecordNew: RecordNew: %v", err)
return
}
// Prepare reply
response := p.identity.SignMessage(challenge)
rnr := v2.RecordNewReply{
Response: hex.EncodeToString(response[:]),
Record: p.convertRecordToV2(*rc),
}
log.Infof("%v Record created %v",
util.RemoteAddr(r), rc.RecordMetadata.Token)
util.RespondWithJSON(w, http.StatusOK, rnr)
}
func (p *politeia) handleRecordEdit(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleRecordEdit")
// Decode request
var re v2.RecordEdit
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&re); err != nil {
respondWithErrorV2(w, r, "handleRecordEdit: unmarshal",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeRequestPayloadInvalid,
})
return
}
challenge, err := hex.DecodeString(re.Challenge)
if err != nil || len(challenge) != v2.ChallengeSize {
respondWithErrorV2(w, r, "handleRecordEdit: decode challenge",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeChallengeInvalid,
})
return
}
token, err := decodeToken(re.Token)
if err != nil {
respondWithErrorV2(w, r, "handleRecordEdit: decode token",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeTokenInvalid,
ErrorContext: util.TokenRegexp(),
})
return
}
// Edit record
var (
mdAppend = convertMetadataStreamsToBackend(re.MDAppend)
mdOverwrite = convertMetadataStreamsToBackend(re.MDOverwrite)
filesAdd = convertFilesToBackend(re.FilesAdd)
)
rc, err := p.backendv2.RecordEdit(token, mdAppend,
mdOverwrite, filesAdd, re.FilesDel)
if err != nil {
respondWithErrorV2(w, r,
"handleRecordEdit: RecordEdit: %v", err)
return
}
// Prepare reply
response := p.identity.SignMessage(challenge)
rer := v2.RecordEditReply{
Response: hex.EncodeToString(response[:]),
Record: p.convertRecordToV2(*rc),
}
log.Infof("%v Record edited %v",
util.RemoteAddr(r), rc.RecordMetadata.Token)
util.RespondWithJSON(w, http.StatusOK, rer)
}
func (p *politeia) handleRecordEditMetadata(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleRecordEditMetadata")
// Decode request
var re v2.RecordEditMetadata
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&re); err != nil {
respondWithErrorV2(w, r, "handleRecordEditMetadata: unmarshal",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeRequestPayloadInvalid,
})
return
}
challenge, err := hex.DecodeString(re.Challenge)
if err != nil || len(challenge) != v2.ChallengeSize {
respondWithErrorV2(w, r, "handleRecordEditMetadata: decode challenge",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeChallengeInvalid,
})
return
}
token, err := decodeToken(re.Token)
if err != nil {
respondWithErrorV2(w, r, "handleRecordEditMetadata: decode token",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeTokenInvalid,
ErrorContext: util.TokenRegexp(),
})
return
}
// Edit record metadata
var (
mdAppend = convertMetadataStreamsToBackend(re.MDAppend)
mdOverwrite = convertMetadataStreamsToBackend(re.MDOverwrite)
)
rc, err := p.backendv2.RecordEditMetadata(token, mdAppend, mdOverwrite)
if err != nil {
respondWithErrorV2(w, r,
"handleRecordEditMetadata: RecordEditMetadata: %v", err)
return
}
// Prepare reply
response := p.identity.SignMessage(challenge)
rer := v2.RecordEditMetadataReply{
Response: hex.EncodeToString(response[:]),
Record: p.convertRecordToV2(*rc),
}
util.RespondWithJSON(w, http.StatusOK, rer)
}
func (p *politeia) handleRecordSetStatus(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleRecordSetStatus")
// Decode request
var rss v2.RecordSetStatus
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&rss); err != nil {
respondWithErrorV2(w, r, "handleRecordSetStatus: unmarshal",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeRequestPayloadInvalid,
})
return
}
challenge, err := hex.DecodeString(rss.Challenge)
if err != nil || len(challenge) != v2.ChallengeSize {
respondWithErrorV2(w, r, "handleRecordSetStatus: decode challenge",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeChallengeInvalid,
})
return
}
token, err := decodeToken(rss.Token)
if err != nil {
respondWithErrorV2(w, r, "handleRecordSetStatus: decode token",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeTokenInvalid,
ErrorContext: util.TokenRegexp(),
})
return
}
// Set record status
var (
mdAppend = convertMetadataStreamsToBackend(rss.MDAppend)
mdOverwrite = convertMetadataStreamsToBackend(rss.MDOverwrite)
status = backendv2.StatusT(rss.Status)
)
rc, err := p.backendv2.RecordSetStatus(token, status,
mdAppend, mdOverwrite)
if err != nil {
respondWithErrorV2(w, r,
"handleRecordSetStatus: RecordSetStatus: %v", err)
return
}
// Prepare reply
response := p.identity.SignMessage(challenge)
rer := v2.RecordSetStatusReply{
Response: hex.EncodeToString(response[:]),
Record: p.convertRecordToV2(*rc),
}
log.Infof("%v Record status set %v %v", util.RemoteAddr(r),
rc.RecordMetadata.Token, backendv2.Statuses[rc.RecordMetadata.Status])
util.RespondWithJSON(w, http.StatusOK, rer)
}
func (p *politeia) handleRecords(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleRecords")
// Decode request
var rgb v2.Records
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&rgb); err != nil {
respondWithErrorV2(w, r, "handleRecords: unmarshal",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeRequestPayloadInvalid,
})
return
}
challenge, err := hex.DecodeString(rgb.Challenge)
if err != nil || len(challenge) != v2.ChallengeSize {
respondWithErrorV2(w, r, "handleRecords: decode challenge",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeChallengeInvalid,
})
return
}
// Verify page size
if len(rgb.Requests) > int(v2.RecordsPageSize) {
respondWithErrorV2(w, r, "handleRecords: unmarshal",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodePageSizeExceeded,
})
return
}
// Get record batch
reqs := convertRecordRequestsToBackend(rgb.Requests)
brecords, err := p.backendv2.Records(reqs)
if err != nil {
respondWithErrorV2(w, r,
"handleRecordGet: Records: %v", err)
return
}
// Prepare reply
records := make(map[string]v2.Record, len(brecords))
for k, v := range brecords {
records[k] = p.convertRecordToV2(v)
}
response := p.identity.SignMessage(challenge)
reply := v2.RecordsReply{
Response: hex.EncodeToString(response[:]),
Records: records,
}
util.RespondWithJSON(w, http.StatusOK, reply)
}
func (p *politeia) handleRecordTimestamps(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleRecordTimestamps")
// Decode request
var rgt v2.RecordTimestamps
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&rgt); err != nil {
respondWithErrorV2(w, r, "handleRecordTimestamps: unmarshal",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeRequestPayloadInvalid,
})
return
}
challenge, err := hex.DecodeString(rgt.Challenge)
if err != nil || len(challenge) != v2.ChallengeSize {
respondWithErrorV2(w, r, "handleRecordTimestamps: decode challenge",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeChallengeInvalid,
})
return
}
token, err := decodeTokenAnyLength(rgt.Token)
if err != nil {
respondWithErrorV2(w, r, "handleRecordTimestamps: decode token",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeTokenInvalid,
ErrorContext: util.TokenRegexp(),
})
return
}
// Get record timestamps
rt, err := p.backendv2.RecordTimestamps(token, rgt.Version)
if err != nil {
respondWithErrorV2(w, r,
"handleRecordTimestamps: RecordTimestamps: %v", err)
return
}
// Prepare reply
response := p.identity.SignMessage(challenge)
rtr := v2.RecordTimestampsReply{
Response: hex.EncodeToString(response[:]),
RecordMetadata: convertTimestampToV2(rt.RecordMetadata),
Metadata: convertMetadataTimestampsToV2(rt.Metadata),
Files: convertFileTimestampsToV2(rt.Files),
}
util.RespondWithJSON(w, http.StatusOK, rtr)
}
func (p *politeia) handleInventory(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleInventory")
// Decode request
var i v2.Inventory
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&i); err != nil {
respondWithErrorV2(w, r, "handleInventory: unmarshal",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeRequestPayloadInvalid,
})
return
}
challenge, err := hex.DecodeString(i.Challenge)
if err != nil || len(challenge) != v2.ChallengeSize {
respondWithErrorV2(w, r, "handleInventory: decode challenge",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeChallengeInvalid,
})
return
}
// Verify inventory arguments. These arguments are optional. Only
// return an error if the arguments have been provided.
var (
state backendv2.StateT
status backendv2.StatusT
pageSize = v2.InventoryPageSize
pageNumber = i.Page
)
if i.State != v2.RecordStateInvalid {
state = convertRecordStateToBackend(i.State)
if state == backendv2.StateInvalid {
respondWithErrorV2(w, r, "",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeRecordStateInvalid,
})
return
}
}
if i.Status != v2.RecordStatusInvalid {
status = convertRecordStatusToBackend(i.Status)
if status == backendv2.StatusInvalid {
respondWithErrorV2(w, r, "",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeRecordStatusInvalid,
})
return
}
}
// Get inventory
inv, err := p.backendv2.Inventory(state, status, pageSize, pageNumber)
if err != nil {
respondWithErrorV2(w, r,
"handleInventory: Inventory: %v", err)
return
}
// Prepare reply
unvetted := make(map[string][]string, len(inv.Unvetted))
for k, v := range inv.Unvetted {
key := backendv2.Statuses[k]
unvetted[key] = v
}
vetted := make(map[string][]string, len(inv.Vetted))
for k, v := range inv.Vetted {
key := backendv2.Statuses[k]
vetted[key] = v
}
response := p.identity.SignMessage(challenge)
ir := v2.InventoryReply{
Response: hex.EncodeToString(response[:]),
Unvetted: unvetted,
Vetted: vetted,
}
util.RespondWithJSON(w, http.StatusOK, ir)
}
func (p *politeia) handleInventoryOrdered(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleInventoryOrdered")
// Decode request
var i v2.InventoryOrdered
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&i); err != nil {
respondWithErrorV2(w, r, "handleInventoryOrdered: unmarshal",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeRequestPayloadInvalid,
})
return
}
challenge, err := hex.DecodeString(i.Challenge)
if err != nil || len(challenge) != v2.ChallengeSize {
respondWithErrorV2(w, r, "handleInventoryOrdered: decode challenge",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeChallengeInvalid,
})
return
}
// Verify record state
var state backendv2.StateT
if i.State != v2.RecordStateInvalid {
state = convertRecordStateToBackend(i.State)
if state == backendv2.StateInvalid {
respondWithErrorV2(w, r, "",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeRecordStateInvalid,
})
return
}
}
// Get inventory
tokens, err := p.backendv2.InventoryOrdered(state,
v2.InventoryPageSize, i.Page)
if err != nil {
respondWithErrorV2(w, r,
"handleInventoryOrdered: InventoryOrdered: %v", err)
return
}
response := p.identity.SignMessage(challenge)
ir := v2.InventoryOrderedReply{
Response: hex.EncodeToString(response[:]),
Tokens: tokens,
}
util.RespondWithJSON(w, http.StatusOK, ir)
}
func (p *politeia) handlePluginWrite(w http.ResponseWriter, r *http.Request) {
log.Tracef("handlePluginWrite")
// Decode request
var pw v2.PluginWrite
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&pw); err != nil {
respondWithErrorV2(w, r, "handlePluginWrite: unmarshal",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeRequestPayloadInvalid,
})
return
}
challenge, err := hex.DecodeString(pw.Challenge)
if err != nil || len(challenge) != v2.ChallengeSize {
respondWithErrorV2(w, r, "handlePluginWrite: decode challenge",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeChallengeInvalid,
})
return
}
token, err := decodeToken(pw.Cmd.Token)
if err != nil {
respondWithErrorV2(w, r, "handlePluginWrite: decode token",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeTokenInvalid,
ErrorContext: util.TokenRegexp(),
})
return
}
// Execute plugin cmd
payload, err := p.backendv2.PluginWrite(token, pw.Cmd.ID,
pw.Cmd.Command, pw.Cmd.Payload)
if err != nil {
respondWithErrorV2(w, r,
"handlePluginWrite: PluginWrite: %v", err)
return
}
// Prepare reply
response := p.identity.SignMessage(challenge)
pwr := v2.PluginWriteReply{
Response: hex.EncodeToString(response[:]),
Payload: payload,
}
log.Infof("%v Plugin '%v' write cmd '%v' executed",
util.RemoteAddr(r), pw.Cmd.ID, pw.Cmd.Command)
util.RespondWithJSON(w, http.StatusOK, pwr)
}
func (p *politeia) handlePluginReads(w http.ResponseWriter, r *http.Request) {
log.Tracef("handlePluginReads")
// Decode request
var pr v2.PluginReads
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&pr); err != nil {
respondWithErrorV2(w, r, "handlePluginReads: unmarshal",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeRequestPayloadInvalid,
})
return
}
challenge, err := hex.DecodeString(pr.Challenge)
if err != nil || len(challenge) != v2.ChallengeSize {
respondWithErrorV2(w, r, "handlePluginReads: decode challenge",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeChallengeInvalid,
})
return
}
// Execute the batch of read cmds
batch := newBatch(pr.Cmds)
batch.execConcurrently(p.backendv2.PluginRead)
// Prepare the replies
replies := make([]v2.PluginCmdReply, len(pr.Cmds))
for k, v := range batch.entries {
if v.err == nil {
// Command executed successfully
replies[k] = v2.PluginCmdReply{
Token: v.cmd.Token,
ID: v.cmd.ID,
Command: v.cmd.Command,
Payload: v.reply,
}
continue
}
// An error was encountered. Plugin and user errors
// are returned in valid replies. Unexpected errors
// cause a 500 and the whole batch to be aborted.
var (
pluginErr backendv2.PluginError
userErr v2.UserErrorReply
userErrCode = convertErrorToV2(v.err)
)
switch {
case errors.As(v.err, &pluginErr):
// A plugin error was returned
replies[k] = v2.PluginCmdReply{
PluginError: &v2.PluginErrorReply{
PluginID: pluginErr.PluginID,
ErrorCode: pluginErr.ErrorCode,
ErrorContext: pluginErr.ErrorContext,
},
}
case errors.As(v.err, &userErr):
// A user error was returned
replies[k] = v2.PluginCmdReply{
UserError: &userErr,
}
case userErrCode != v2.ErrorCodeInvalid:
// Backend error was returned that was
// converted into a valid user error.
replies[k] = v2.PluginCmdReply{
UserError: &v2.UserErrorReply{
ErrorCode: userErrCode,
},
}
default:
// Internal server error. Log it and return a 500.
t := time.Now().Unix()
e := fmt.Sprintf("PluginRead %v %v %v: %v",
v.cmd.ID, v.cmd.Command, v.cmd.Payload, v.err)
log.Errorf("%v %v %v %v Internal error %v: %v",
util.RemoteAddr(r), r.Method, r.URL, r.Proto, t, e)
log.Errorf("Stacktrace (NOT A REAL CRASH): %s", debug.Stack())
util.RespondWithJSON(w, http.StatusInternalServerError,
v2.ServerErrorReply{
ErrorCode: t,
})
return
}
}
// Prepare reply
response := p.identity.SignMessage(challenge)
prr := v2.PluginReadsReply{
Response: hex.EncodeToString(response[:]),
Replies: replies,
}
util.RespondWithJSON(w, http.StatusOK, prr)
}
func (p *politeia) handlePluginInventory(w http.ResponseWriter, r *http.Request) {
log.Tracef("handlePluginInventory")
// Decode request
var pi v2.PluginInventory
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&pi); err != nil {
respondWithErrorV2(w, r, "handlePluginInventory: unmarshal",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeRequestPayloadInvalid,
})
return
}
challenge, err := hex.DecodeString(pi.Challenge)
if err != nil || len(challenge) != v2.ChallengeSize {
respondWithErrorV2(w, r, "handlePluginInventory: decode challenge",
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeChallengeInvalid,
})
return
}
// Get plugin inventory
plugins := p.backendv2.PluginInventory()
// Prepare reply
response := p.identity.SignMessage(challenge)
ir := v2.PluginInventoryReply{
Response: hex.EncodeToString(response[:]),
Plugins: convertPluginsToV2(plugins),
}
util.RespondWithJSON(w, http.StatusOK, ir)
}
// decodeToken decodes a v2 token and errors if the token is not the full
// length token.
func decodeToken(token string) ([]byte, error) {
return util.TokenDecode(util.TokenTypeTstore, token)
}
// decodeTokenAnyLength decodes a v2 token. It accepts both the full length
// token and the short token.
func decodeTokenAnyLength(token string) ([]byte, error) {
return util.TokenDecodeAnyLength(util.TokenTypeTstore, token)
}
func (p *politeia) convertRecordToV2(r backendv2.Record) v2.Record {
var (
metadata = convertMetadataStreamsToV2(r.Metadata)
files = convertFilesToV2(r.Files)
rm = r.RecordMetadata
sig = p.identity.SignMessage([]byte(rm.Merkle + rm.Token))
)
return v2.Record{
State: v2.RecordStateT(rm.State),
Status: v2.RecordStatusT(rm.Status),
Version: rm.Version,
Timestamp: rm.Timestamp,
Metadata: metadata,
Files: files,
CensorshipRecord: v2.CensorshipRecord{
Token: rm.Token,
Merkle: rm.Merkle,
Signature: hex.EncodeToString(sig[:]),
},
}
}
func convertMetadataStreamsToBackend(metadata []v2.MetadataStream) []backendv2.MetadataStream {
ms := make([]backendv2.MetadataStream, 0, len(metadata))
for _, v := range metadata {
ms = append(ms, backendv2.MetadataStream{
PluginID: v.PluginID,
StreamID: v.StreamID,
Payload: v.Payload,
})
}
return ms
}
func convertMetadataStreamsToV2(metadata []backendv2.MetadataStream) []v2.MetadataStream {
ms := make([]v2.MetadataStream, 0, len(metadata))
for _, v := range metadata {
ms = append(ms, v2.MetadataStream{
PluginID: v.PluginID,
StreamID: v.StreamID,
Payload: v.Payload,
})
}
return ms
}
func convertFilesToBackend(files []v2.File) []backendv2.File {
fs := make([]backendv2.File, 0, len(files))
for _, v := range files {
fs = append(fs, backendv2.File{
Name: v.Name,
MIME: v.MIME,
Digest: v.Digest,
Payload: v.Payload,
})
}
return fs
}
func convertFilesToV2(files []backendv2.File) []v2.File {
fs := make([]v2.File, 0, len(files))
for _, v := range files {
fs = append(fs, v2.File{
Name: v.Name,
MIME: v.MIME,
Digest: v.Digest,
Payload: v.Payload,
})
}
return fs
}
func convertRecordRequestsToBackend(reqs []v2.RecordRequest) []backendv2.RecordRequest {
r := make([]backendv2.RecordRequest, 0, len(reqs))
for _, v := range reqs {
token, err := decodeTokenAnyLength(v.Token)
if err != nil {
// Records with errors will not be included in the reply
log.Debugf("convertRecordRequestsToBackend: decode token: %v", err)
continue
}
r = append(r, backendv2.RecordRequest{
Token: token,
Version: v.Version,
Filenames: v.Filenames,
OmitAllFiles: v.OmitAllFiles,
})
}
return r
}
func convertProofToV2(p backendv2.Proof) v2.Proof {
return v2.Proof{
Type: p.Type,
Digest: p.Digest,
MerkleRoot: p.MerkleRoot,
MerklePath: p.MerklePath,
ExtraData: p.ExtraData,
}
}
func convertTimestampToV2(t backendv2.Timestamp) v2.Timestamp {
proofs := make([]v2.Proof, 0, len(t.Proofs))
for _, v := range t.Proofs {
proofs = append(proofs, convertProofToV2(v))
}
return v2.Timestamp{
Data: t.Data,
Digest: t.Digest,
TxID: t.TxID,
MerkleRoot: t.MerkleRoot,
Proofs: proofs,
}
}
func convertMetadataTimestampsToV2(metadata map[string]map[uint32]backendv2.Timestamp) map[string]map[uint32]v2.Timestamp {
md := make(map[string]map[uint32]v2.Timestamp, 16)
for pluginID, v := range metadata {
timestamps, ok := md[pluginID]
if !ok {
timestamps = make(map[uint32]v2.Timestamp, 16)
}
for streamID, ts := range v {
timestamps[streamID] = convertTimestampToV2(ts)
}
md[pluginID] = timestamps
}
return md
}
func convertFileTimestampsToV2(files map[string]backendv2.Timestamp) map[string]v2.Timestamp {
fs := make(map[string]v2.Timestamp, len(files))
for k, v := range files {
fs[k] = convertTimestampToV2(v)
}
return fs
}
func convertRecordStateToBackend(s v2.RecordStateT) backendv2.StateT {
switch s {
case v2.RecordStateUnvetted:
return backendv2.StateUnvetted
case v2.RecordStateVetted:
return backendv2.StateVetted
}
return backendv2.StateInvalid
}
func convertRecordStatusToBackend(s v2.RecordStatusT) backendv2.StatusT {
switch s {
case v2.RecordStatusUnreviewed:
return backendv2.StatusUnreviewed
case v2.RecordStatusPublic:
return backendv2.StatusPublic
case v2.RecordStatusCensored:
return backendv2.StatusCensored
case v2.RecordStatusArchived:
return backendv2.StatusArchived
}
return backendv2.StatusInvalid
}
func convertPluginSettingToV2(p backendv2.PluginSetting) v2.PluginSetting {
return v2.PluginSetting{
Key: p.Key,
Value: p.Value,
}
}
func convertPluginsToV2(bplugins []backendv2.Plugin) []v2.Plugin {
plugins := make([]v2.Plugin, 0, len(bplugins))
for _, v := range bplugins {
settings := make([]v2.PluginSetting, 0, len(v.Settings))
for _, v := range v.Settings {
settings = append(settings, convertPluginSettingToV2(v))
}
plugins = append(plugins, v2.Plugin{
ID: v.ID,
Settings: settings,
})
}
return plugins
}
func respondWithErrorV2(w http.ResponseWriter, r *http.Request, format string, err error) {
var (
errCode = convertErrorToV2(err)
ue v2.UserErrorReply
ce backendv2.ContentError
ste backendv2.StatusTransitionError
pe backendv2.PluginError
)
switch {
case errCode != v2.ErrorCodeInvalid:
// Backend error
log.Infof("%v User error: %v %v", util.RemoteAddr(r),
errCode, v2.ErrorCodes[errCode])
util.RespondWithJSON(w, http.StatusBadRequest,
v2.UserErrorReply{
ErrorCode: errCode,
})
return
case errors.As(err, &ue):
// Politeiad user error
m := fmt.Sprintf("%v User error: %v %v", util.RemoteAddr(r),
ue.ErrorCode, v2.ErrorCodes[ue.ErrorCode])
if ce.ErrorContext != "" {
m += fmt.Sprintf(": %v", ce.ErrorContext)
}
log.Infof(m)
util.RespondWithJSON(w, http.StatusBadRequest, ue)
return
case errors.As(err, &ce):
// Backend content error
errCode := convertContentErrorToV2(ce.ErrorCode)
m := fmt.Sprintf("%v User error: %v %v", util.RemoteAddr(r),
errCode, v2.ErrorCodes[errCode])
if ce.ErrorContext != "" {
m += fmt.Sprintf(": %v", ce.ErrorContext)
}
log.Infof(m)
util.RespondWithJSON(w, http.StatusBadRequest,
v2.UserErrorReply{
ErrorCode: errCode,
ErrorContext: ce.ErrorContext,
})
return
case errors.As(err, &ste):
// Backend status transition error
log.Infof("%v User error: %v", util.RemoteAddr(r), ste.Error())
util.RespondWithJSON(w, http.StatusBadRequest,
v2.UserErrorReply{
ErrorCode: v2.ErrorCodeStatusChangeInvalid,
ErrorContext: ste.Error(),
})
return
case errors.As(err, &pe):
// Plugin user error
m := fmt.Sprintf("%v Plugin error: %v %v",
util.RemoteAddr(r), pe.PluginID, pe.ErrorCode)
if pe.ErrorContext != "" {
m += fmt.Sprintf(": %v", pe.ErrorContext)
}
log.Infof(m)
util.RespondWithJSON(w, http.StatusBadRequest,
v2.PluginErrorReply{
PluginID: pe.PluginID,
ErrorCode: pe.ErrorCode,
ErrorContext: pe.ErrorContext,
})
return
}
// Internal server error. Log it and return a 500.
t := time.Now().Unix()
e := fmt.Sprintf(format, err)
log.Errorf("%v %v %v %v Internal error %v: %v",
util.RemoteAddr(r), r.Method, r.URL, r.Proto, t, e)
log.Errorf("Stacktrace (NOT A REAL CRASH): %s", debug.Stack())
util.RespondWithJSON(w, http.StatusInternalServerError,
v2.ServerErrorReply{
ErrorCode: t,
})
}
func convertErrorToV2(e error) v2.ErrorCodeT {
switch e {
case backendv2.ErrTokenInvalid:
return v2.ErrorCodeTokenInvalid
case backendv2.ErrRecordNotFound:
return v2.ErrorCodeRecordNotFound
case backendv2.ErrRecordLocked:
return v2.ErrorCodeRecordLocked
case backendv2.ErrNoRecordChanges:
return v2.ErrorCodeNoRecordChanges
case backendv2.ErrPluginIDInvalid:
return v2.ErrorCodePluginIDInvalid
case backendv2.ErrPluginCmdInvalid:
return v2.ErrorCodePluginCmdInvalid
case backendv2.ErrDuplicatePayload:
return v2.ErrorCodeDuplicatePayload
}
return v2.ErrorCodeInvalid
}
func convertContentErrorToV2(e backendv2.ContentErrorCodeT) v2.ErrorCodeT {
switch e {
case backendv2.ContentErrorMetadataStreamInvalid:
return v2.ErrorCodeMetadataStreamInvalid
case backendv2.ContentErrorMetadataStreamDuplicate:
return v2.ErrorCodeMetadataStreamDuplicate
case backendv2.ContentErrorFilesEmpty:
return v2.ErrorCodeFilesEmpty
case backendv2.ContentErrorFileNameInvalid:
return v2.ErrorCodeFileNameInvalid
case backendv2.ContentErrorFileNameDuplicate:
return v2.ErrorCodeFileNameDuplicate
case backendv2.ContentErrorFileDigestInvalid:
return v2.ErrorCodeFileDigestInvalid
case backendv2.ContentErrorFilePayloadInvalid:
return v2.ErrorCodeFilePayloadInvalid
case backendv2.ContentErrorFileMIMETypeInvalid:
return v2.ErrorCodeFileMIMETypeInvalid
case backendv2.ContentErrorFileMIMETypeUnsupported:
return v2.ErrorCodeFileMIMETypeUnsupported
}
return v2.ErrorCodeInvalid
}