-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplicate-peers.js
571 lines (540 loc) · 14.8 KB
/
replicate-peers.js
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
/*
Copyright 2017 The BioBricks Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var AJV = require('ajv')
var attachmentPath = require('./util/attachment-path')
var concat = require('concat-stream')
var encoding = require('./encoding')
var flushWriteStream = require('flush-write-stream')
var fs = require('fs')
var http = require('http-https')
var latest = require('./latest')
var mkdirp = require('mkdirp')
var parse = require('json-parse-errback')
var path = require('path')
var pump = require('pump')
var readRecord = require('./util/read-record')
var recordDirectoryPath = require('./util/record-directory-path')
var recordPath = require('./util/record-path')
var runParallel = require('run-parallel')
var runSeries = require('run-series')
var signaturePath = require('./util/timestamp-path')
var sodium = require('sodium-prebuilt').api
var split2 = require('split2')
var stringify = require('json-stable-stringify')
var through2 = require('through2')
var timestampPath = require('./util/timestamp-path')
var url = require('url')
var xtend = require('xtend')
var timestampSchema = latest(require('./schemas/timestamp'))
var validatePublication = new AJV({allErrors: true})
.compile(latest(require('./schemas/publication')))
var validateTimestamp = new AJV({allErrors: true})
.compile(timestampSchema)
module.exports = function (configuration, log) {
var directory = configuration.directory
readPeers(directory, log, function (error, peers) {
if (error) return logError(error)
log.info({
peers: peers.map(function (peer) {
return url.format(peer.url)
})
}, 'read peers')
runParallel(
peers.map(function (peer) {
return function (done) {
var peerLog = log.child({peer: peer.url.hostname})
peerLog.info('replicating')
replicatePeer(configuration, peerLog, peer, done)
}
}),
function (error) {
if (error) return logError(error)
if (peers.length === 0) {
log.info('done')
} else {
writePeers(directory, peers, function (error) {
if (error) return logError(error)
log.info('done')
})
}
}
)
})
function logError (error) {
log.error(error)
}
}
function replicatePeer (configuration, log, peer, done) {
var request = xtend(peer.url, {
path: peer.url.path + '/accessions?from=' + (peer.last + 1),
headers: {accept: 'text/csv'}
})
http.request(request)
.once('error', function (error) {
done(error)
})
.once('response', function (response) {
var counter = peer.last
pump(
response,
split2(),
through2.obj(function (line, _, done) {
counter++
done(null, {
accessionNumber: counter,
digest: line.toString().split(',')[1]
})
}),
flushWriteStream.obj(function (publication, _, done) {
var recordLog = log.child({digest: publication.digest})
republish(configuration, recordLog, peer, publication, done)
}),
function (error) {
if (error) {
done(error)
} else {
done()
}
}
)
})
.end()
}
function republish (configuration, log, peer, publication, done) {
var time = new Date().toISOString()
var files = []
var haveRecord
var havePeerTimestamp
var record
var peerTimestamp
runSeries([
runParallel.bind(null, [
checkHaveRecord,
checkHavePeerTimestamp
]),
runParallel.bind(null, [
getRecord,
unless(havePeerTimestamp, getPeerTimestamp)
]),
unless(haveRecord, validateRecord),
unless(havePeerTimestamp, validatePeerTimestamp),
unless(haveRecord, makeDirectory),
runParallel.bind(null, [
unless(haveRecord, saveOwnTimestamp),
unless(havePeerTimestamp, savePeerTimestamp),
unless(haveRecord, saveRecord),
unless(haveRecord, getAndSaveAttachments)
]),
unless(haveRecord, appendToAccessions),
bumpAccessionNumber
], function (error) {
if (error) {
log.error(error)
unlinkFiles(done)
} else {
log.info('done')
done()
}
})
function unless (flag, step) {
return function (done) {
if (flag) {
done()
} else {
step(done)
}
}
}
function checkHaveRecord (done) {
var file = recordPath(configuration.directory, publication.digest)
fileExists(file, function (error, exists) {
if (error) return done(error)
haveRecord = exists
log.info({haveRecord: exists})
done()
})
}
function checkHavePeerTimestamp (done) {
var file = timestampPath(
configuration.directory,
publication.digest,
peer.publicKey
)
fileExists(file, function (error, exists) {
if (error) return done(error)
havePeerTimestamp = exists
log.info({havePeerTimestamp: exists})
done()
})
}
function getRecord (done) {
if (haveRecord) {
readRecord(
configuration.directory, publication.digest,
function (error, parsed) {
if (error) return done(error)
record = parsed
done()
}
)
} else {
http.request(xtend(peer.url, {
path: peer.url.path + '/publications/' + publication.digest,
headers: {accept: 'application/json'}
}))
.once('error', function (error) {
done(error)
})
.once('response', function (response) {
pump(
response,
concat(function (buffer) {
parse(buffer, function (error, parsed) {
if (error) return done(error)
record = parsed
log.info('got record')
done()
})
}),
function (error) {
if (error) {
done(error)
}
}
)
})
.end()
}
}
function getPeerTimestamp (done) {
var request = xtend(peer.url, {
path: (
peer.url.path +
'publications/' + publication.digest +
'/timestamps/' + encoding.encode(peer.publicKey)
)
})
http.request(request)
.once('error', function (error) {
done(error)
})
.once('response', function (response) {
if (response.statusCode !== 200) {
var error = new Error('Could not get timestamp')
error.statusCode = response.statusCode
log.error({
statusCode: response.statusCode
}, 'failed to get timestamp')
done(error)
} else {
pump(
response,
concat(function (buffer) {
parse(buffer, function (error, parsed) {
if (error) return done(error)
peerTimestamp = parsed
log.info('got timestamp')
done()
})
}),
function (error) {
if (error) {
done(error)
}
}
)
}
})
.end()
}
function validateRecord (done) {
validatePublication(record)
var errors = validatePublication.errors
if (errors) {
log.info({errors: errors}, 'invalid publication')
var error = new Error('invalid record')
error.validationErrors = errors
return done(error)
}
var computedDigest = encoding.encode(
sodium.crypto_hash_sha256(
Buffer.from(stringify(record), 'utf8')
)
)
if (computedDigest !== publication.digest) {
done(new Error(
'reported and computed digests do not match'
))
}
log.info('validated record')
done()
}
function validatePeerTimestamp (done) {
validateTimestamp(peerTimestamp)
var errors = validateTimestamp.errors
if (errors) {
log.info({errors: errors}, 'invalid timestamp')
var error = new Error('invalid timestamp')
error.validationErrors = errors
return done(error)
}
var digestsMatch = (
peerTimestamp.timestamp.digest ===
encoding.encode(
sodium.crypto_hash_sha256(
Buffer.from(stringify(record), 'utf8')
)
)
)
if (!digestsMatch) {
log.error('timestamp digest mismatch')
return done(new Error(
'peer timestamp digest does not match record'
))
}
var validSignature = sodium.crypto_sign_verify_detached(
encoding.decode(peerTimestamp.signature),
Buffer.from(stringify(peerTimestamp.timestamp)),
peer.publicKey
)
if (!validSignature) {
log.error('invalid peer signature')
return done(new Error('invalid peer signature'))
}
log.info('validated timestamp')
done()
}
function makeDirectory (done) {
mkdirp(
recordDirectoryPath(configuration.directory, publication.digest),
done
)
}
function saveOwnTimestamp (done) {
var timestamp = {
digest: publication,
uri: (
'https://' + configuration.hostname +
'/publications/' + publication.digest
),
time: time
}
var signature = encoding.encode(
sodium.crypto_sign_detached(
Buffer.from(stringify(timestamp)),
configuration.keypair.secret
)
)
var file = signaturePath(
configuration.directory,
publication.digest,
encoding.encode(configuration.keypair.public)
)
files.push(file)
var data = stringify({
timestamp: timestamp,
signature: signature,
version: timestampSchema.properties.version.constant
})
fs.writeFile(file, data, 'utf8', function (error) {
if (error) return done(error)
log.info('saved own timestamp')
done()
})
}
function savePeerTimestamp (done) {
var file = timestampPath(
configuration.directory,
publication.digest,
peer.publicKey
)
files.push(file)
var data = stringify(peerTimestamp)
fs.writeFile(file, data, 'utf8', function (error) {
if (error) return done(error)
log.info('saved peer timestamp')
done()
})
}
function saveRecord (done) {
var file = recordPath(configuration.directory, publication.digest)
files.push(file)
var data = stringify(record)
writeIfMissing(file, data, function (error) {
if (error) return done(error)
log.info('saved record')
done()
})
}
function getAndSaveAttachments (done) {
runParallel(
record.attachments.map(function (attachmentDigest) {
return function (done) {
var attachmentLog = log.child({
attachment: attachmentDigest
})
var request = xtend(peer.url, {
path: (
peer.url.path +
'/publications/' + publication.digest +
'/attachments/' + attachmentDigest
)
})
var file = attachmentPath(
configuration.directory,
publication.digest,
attachmentDigest
)
download(request, file, function (error, contentType) {
if (error) return done(error)
attachmentLog.info('downloaded')
writeIfMissing(
file + '.type', contentType,
function (error) {
if (error) return done(error)
attachmentLog.info('wrote type file')
done()
}
)
})
}
}),
done
)
}
function appendToAccessions (done) {
fs.appendFile(
path.join(configuration.directory, 'accessions'),
(time || new Date().toISOString()) + ',' +
publication.digest + '\n',
done
)
}
function bumpAccessionNumber (done) {
peer.last = publication.accessionNumber
log.info('bumped')
done()
}
function unlinkFiles (done) {
runParallel(files.map(function (file) {
return function (done) {
var fileLog = log.child({file: file})
fs.unlink(file, function (error) {
if (error) {
fileLog.error(error)
done()
} else {
fileLog.info('unlinked')
done()
}
})
}
}), done)
}
}
function writeIfMissing (path, data, done) {
fs.writeFile(path, data, {flag: 'wx'}, function (error) {
if (error && error.code !== 'EEXIST') {
done(error)
} else {
done()
}
})
}
function download (from, to, done) {
var contentType
http.request(from)
.once('error', function (error) {
done(error)
})
.once('response', function (response) {
contentType = response.headers['Content-Type']
pump(
response,
fs.createWriteStream(to, {flags: 'wx'}),
function (error) {
if (error) {
if (error.code === 'EEXIST') {
done()
} else {
done(error)
}
} else {
done(null, contentType)
}
}
)
})
.end()
}
function readPeers (directory, log, callback) {
var file = path.join(directory, 'peers')
fs.readFile(file, 'utf8', function (error, string) {
if (error) {
if (error.code === 'ENOENT') {
callback(null, [])
} else {
callback(error)
}
} else {
var peers = string
.split('\n')
.reduce(function (peers, line, index) {
var split = line.split(',')
if (split.length === 3) {
try {
var peer = {
url: url.parse(split[0]),
publicKey: encoding.decode(split[1]),
last: parseInt(split[2])
}
peers.push(peer)
} catch (error) {
log.error('peers parse error', {
line: line,
number: index + 1
})
}
}
return peers
}, [])
callback(null, peers)
}
})
}
function writePeers (directory, peers, callback) {
var file = path.join(directory, 'peers')
var data = peers
.map(function (peer) {
return (
url.format(peer.url) + ',' +
encoding.encode(peer.publicKey) + ',' +
peer.last.toString()
)
})
.join('\n')
fs.writeFile(file, data, callback)
}
function fileExists (path, callback) {
fs.access(path, fs.constants.R_OK, function (error) {
if (error) {
if (error.code === 'ENOENT') {
callback(null, false)
} else {
callback(error)
}
} else {
callback(null, true)
}
})
}