-
Notifications
You must be signed in to change notification settings - Fork 2
/
admin.py
766 lines (640 loc) · 26.2 KB
/
admin.py
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
#!/usr/bin/env python
# This file is part of misc-tools.
# Copyright (C) 2014-2016 Sequent Tech Inc <[email protected]>
# misc-tools is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License.
# misc-tools is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with misc-tools. If not, see <http://www.gnu.org/licenses/>.
import requests
import json
import time
import re
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from SocketServer import ThreadingMixIn
import SocketServer
import threading
import subprocess
import argparse
import sys
import __main__
from argparse import RawTextHelpFormatter
from datetime import datetime
import hashlib
import codecs
import traceback
import os.path
import os
from prettytable import PrettyTable
from sqlalchemy import create_engine, select, func, text
from sqlalchemy import Table, Column, Integer, String, TIMESTAMP, MetaData, ForeignKey
PK_TIMEOUT = 60*10 # with three authorities 60 secons was not enough
TALLY_TIMEOUT = 3600*6 # 6 hours should be enough for what we do :-P
CERT = '/srv/certs/selfsigned/cert.pem'
KEY = '/srv/certs/selfsigned/key-nopass.pem'
ELECTIONS_DIR = 'elections'
node = '/usr/local/bin/node'
def get_local_hostport():
me = subprocess.check_output(["./eopeers", "--show-mine"])
pkg = json.loads(me)
return pkg['hostname'], pkg['port']
def get_backend_config():
path = os.path.join('..', 'config.json')
with open(path, 'r') as f:
config = json.loads(f.read())
dbConnection = config['DbConnectString']
def get_pair(l):
if len(l[2]) > 0:
return (l[0], l[2])
elif len(l[1]) > 0:
return (l[0], l[1])
else:
return (l[0], l[3])
dbParams = dict([get_pair(l) for l in re.findall(r'(\S+)=(\'(.*)\'|"(.*)"|(\S+))', dbConnection)])
config['db'] = dbParams
return config
def votes_table():
metadata = MetaData()
votes = Table('votes', metadata,
Column('id', Integer, primary_key=True),
Column('vote', String),
Column('vote_hash', String),
Column('election_id', String),
Column('voter_id', String),
Column('ip', String),
Column('created', TIMESTAMP),
Column('modified', TIMESTAMP),
Column('write_count', Integer)
)
return votes
def show_votes(result):
def truncate(data):
data = str(data)
return (data[:20] + '..') if len(data) > 20 else data
v = PrettyTable(['id', 'vote', 'vote_hash', 'election_id', 'voter_id', 'ip', 'created', 'modified', 'writes'])
v.padding_width = 1
for row in result:
v.add_row(map(truncate, row))
print(v)
def get_db_connection(dbCfg):
engine = create_engine('postgresql+psycopg2://%s:%s@localhost/%s' % (dbCfg['user'], dbCfg['password'], dbCfg['dbname']))
conn = engine.connect()
return conn
def get_election_config(dir, eopeers_dir, force=False):
path = os.path.join(ELECTIONS_DIR, dir)
if not os.path.isdir(path):
print("%s is not a directory" % path)
exit(1)
jsonPath = os.path.join(path, "config.json")
if not os.path.isfile(jsonPath):
print("%s is not a file" % jsonPath)
exit(1)
print("> loading config in %s" % jsonPath)
with open(jsonPath, 'r') as f:
config = json.loads(f.read())
if not 'director' in config:
print("director not found in config")
exit(1)
if not 'authorities' in config:
print("authorities not found in config")
exit(1)
config['path'] = path
config["authorities"] = config['authorities'].replace(' ','').split(',')
# if id not present, use directory name
if not 'election-id' in config:
config['election-id'] = os.path.basename(os.path.normpath(dir))
# authorities and director
authorities = []
try:
auths_data = get_authdata(eopeers_dir, force)
for auth in auths_data:
if auth['hostname'] == config['director']:
authorities.insert(0, auth)
elif auth['hostname'] in config['authorities']:
authorities.append(auth)
# check that all requested authorities were found
required = list(config['authorities'])
required.append(config['director'])
required = set(required)
found = [a['hostname'] for a in authorities]
if len(found) != len(required):
not_found = str(required - set(found))
print("some peers were not found: %s" % not_found)
exit(1)
config['auths'] = authorities
# local host, port
localHostname, localPort = get_local_hostport()
config['local-hostname'] = localHostname
config['local-port'] = localPort
except:
if force:
print("get auth data failed. Forcing to continue")
else:
exit(1)
return config
def get_tally_data(config):
return {
"election_id": config['election-id'],
"callback_url": "http://" + config['local-hostname'] + ":" + str(config['local-port']) + "/receive_tally",
"extra": [],
"votes_url": "http://" + config['local-hostname'] + ":" + str(config['local-port']) + "/",
"votes_hash": "sha512://"
}
def _api_url(path, auth):
'''
Given a full api (without the initial '/') path and a package dict, returns
a well formed url
'''
return "https://%(hostname)s:%(port)d/%(path)s" % dict(
hostname=auth["hostname"],
port=auth.get("port", 5000),
path=path
)
def get_authdata_for_post(config):
i = 1
ret_data = []
print("\nusing the following authorities:")
for auth in config['auths']:
ret_data.append({
"name": "Auth%d" %i,
"orchestra_url": _api_url("api/queues", auth),
"ssl_cert": auth["ssl_certificate"]
})
if i == 1:
print(" 1. %s (this is the orchestra "
"director)" % auth['hostname'])
else:
print(" %d. %s" % (i, auth['hostname']))
i += 1
return ret_data
def get_authdata(eopeers_dir, force=False):
if not os.path.isdir(eopeers_dir):
print("%s is not a directory" % eopeers_dir)
exit(1)
l = os.listdir(eopeers_dir)
if len(l) == 0:
print("%s is an empty directory" % eopeers_dir)
if not force:
exit(1)
auths_data = []
for fname in os.listdir(eopeers_dir):
path = os.path.join(eopeers_dir, fname)
with open(path, 'r') as f:
auths_data.append(json.loads(f.read()))
return auths_data
def getStartData(config):
return {
"election_id": config['election-id'],
"callback_url": "http://" + config['local-hostname'] + ":" + str(config['local-port']) + "/key_done",
"is_recurring": config['is_recurring'],
"extra": config['extra'],
"title": config['title'],
"url": config['url'],
"description": config['description'],
"questions_data": config['questions_data'],
"voting_start_date": config['voting_start_date'],
"voting_end_date": config['voting_end_date'],
"authorities": get_authdata_for_post(config)
}
# thread signalling
cv = threading.Condition()
class ThreadingHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
pass
class RequestHandler(SimpleHTTPRequestHandler):
def do_GET(self):
print("> HTTP received " + self.path)
if(self.path == "/exit"):
self.send_response(204)
cv.acquire()
cv.done = True
cv.notify()
cv.release()
else:
SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
length = int(self.headers['Content-Length'])
print("> HTTP received " + self.path + " (" + str(length) + ")")
raw = self.rfile.read(length).decode('utf-8')
print(raw)
data = json.loads(raw)
self.send_response(200)
cv.acquire()
cv.done = True
cv.data = data
cv.notify()
cv.release()
# misc\utils.py
BUF_SIZE = 10*1024
def hash_file(filePath):
'''
Returns the hexdigest of the hash of the contents of a file, given the file
path.
'''
hash = hashlib.sha512()
f = open(filePath, 'r')
for chunk in f.read(BUF_SIZE):
hash.update(chunk)
f.close()
return hash.hexdigest()
def write_node_votes(votesData, filePath):
# forms/election.py:save
votes = []
for vote in votesData:
data = {
"a": "encrypted-vote-v1",
"proofs": [],
"choices": [],
"voter_username": 'foo',
"issue_date": str(datetime.now()),
"election_hash": {"a": "hash/sha256/value", "value": "foobar"},
}
q_answer = vote['question0']
data["proofs"].append(dict(
commitment=q_answer['commitment'],
response=q_answer['response'],
challenge=q_answer['challenge']
))
data["choices"].append(dict(
alpha=q_answer['alpha'],
beta=q_answer['beta']
))
votes.append(data)
# tasks/election.py:launch_encrypted_tally
# this is the format expected by eo, newline separated
with codecs.open(filePath, encoding='utf-8', mode='w+') as votes_file:
for vote in votes:
votes_file.write(json.dumps(vote, sort_keys=True) + "\n")
def start_server(port):
print("> Starting server on port %s" % str(port))
server = ThreadingHTTPServer(('localhost', port),RequestHandler)
thread = threading.Thread(target = server.serve_forever)
thread.daemon = True
thread.start()
def start_election(config, url, data):
# data['election_id'] = electionId
print("> Creating election " + config['election-id'])
print(json.dumps(data))
cv.done = False
r = requests.post(url, data=json.dumps(data), verify=False, cert=(CERT, KEY))
print("> " + str(r))
def wait_for_publicKey():
start = time.time()
cv.acquire()
cv.wait(PK_TIMEOUT)
pk = ''
if(cv.done):
diff = time.time() - start
try:
pk = [p['pubkey'] for p in cv.data['session_data']]
print("> Election created (" + str(diff) + " sec), public key is")
print(pk)
except:
print("* Could not retrieve public key " + str(cv.data))
print traceback.print_exc()
else:
print("* Timeout waiting for public key")
cv.release()
return pk
def do_tally(config, url, data, votesFile, hash):
data['votes_url'] = data['votes_url'] + config['path'] + "/" + votesFile
data['votes_hash'] = data['votes_hash'] + hash
# data['election_id'] = config['election-id']
# print("> Tally post with " + json.dumps(data))
print("> Requesting tally..")
cv.done = False
r = requests.post(url, data=json.dumps(data), verify=False, cert=(CERT, KEY))
print("> " + str(r))
def wait_for_tally():
start = time.time()
cv.acquire()
cv.wait(TALLY_TIMEOUT)
ret = ''
if(cv.done):
diff = time.time() - start
# print("> Received tally data (" + str(diff) + " sec) " + str(cv.data))
print("> Received tally data (" + str(diff) + " sec)")
if('tally_url' in cv.data['data']):
ret = cv.data['data']
else:
print("* Timeout waiting for tally")
cv.release()
return ret
def download_tally(cfg, url, partial):
tally_file = cfg['tally-file']
if partial > 0:
tally_file = tally_file + "." + str(partial)
path = os.path.join(cfg['electionCfg']['path'], tally_file)
print("> Downloading to %s" % path)
with open(path, 'wb') as handle:
request = requests.get(url, stream=True, verify=False, cert=(CERT, KEY))
for block in request.iter_content(1024):
if not block:
break
handle.write(block)
''' commands '''
def create(cfg, args):
config = cfg['electionCfg']
electionId = config['election-id']
start_server(config['local-port'])
startData = getStartData(config)
startUrl = _api_url("public_api/election", config['auths'][0])
print("> requesting election at %s" % startUrl)
start_election(config, startUrl, startData)
publicKey = wait_for_publicKey()
pkFile = 'pk_' + electionId
if(len(publicKey) > 0):
print("> Saving pk to " + pkFile)
with codecs.open(os.path.join(config['path'], pkFile), encoding='utf-8', mode='w+') as votes_file:
votes_file.write(json.dumps(publicKey))
else:
print("No public key, exiting..")
exit(1)
return pkFile
def tally(cfg, args, do_start_server = True):
config = cfg['electionCfg']
partial = args.partial
if do_start_server:
start_server(config['local-port'])
electionId = config['election-id']
ctexts = cfg['ciphertexts']
if partial > 0:
ctexts = ctexts + "." + str(partial)
ctextsPath = os.path.join(config['path'], ctexts)
# need hash
hash = hash_file(ctextsPath)
print("> Votes hash is " + hash)
tallyData = get_tally_data(config)
tallyUrl = _api_url('public_api/tally', config['auths'][0])
do_tally(config, tallyUrl, tallyData, ctexts, hash)
tallyResponse = wait_for_tally()
print(tallyResponse)
if('tally_url' in tallyResponse):
print("> Downloading tally from " + tallyResponse['tally_url'])
download_tally(cfg, tallyResponse['tally_url'], partial)
else:
print("* Tally not found in http data")
def list_votes(cfg, args):
conn = get_db_connection(cfg['backendCfg']['db'])
votes = votes_table()
# s = select([votes]).where(votes.c.election_id == cfg['electionCfg']['election-id'])
s = select([votes]).where(votes.c.election_id == cfg['electionCfg']['election-id'])
for filter in cfg['filters']:
if "~" in filter:
key, value = filter.split("~")
s = s.where(getattr(votes.c, key).like(value))
else:
key, value = filter.split("==")
s = s.where(getattr(votes.c, key) == (value))
result = conn.execute(s)
show_votes(result)
def count_votes(cfg, args):
conn = get_db_connection(cfg['backendCfg']['db'])
votes = votes_table()
s = select([func.count(votes.c.id)]).where(votes.c.election_id == cfg['electionCfg']['election-id'])
result = conn.execute(text("select count(*) from votes where id in (select distinct on (voter_id) id from votes where election_id in :ids order by voter_id, election_id desc)"), ids=tuple(args.command[1:]))
row = result.fetchall()
print(row[0][0])
# dumps votes in the format expected by eo
def dump_votes_eo(cfg, args):
conn = get_db_connection(cfg['backendCfg']['db'])
votes = votes_table()
vs = conn.execute(text("select distinct on (voter_id) vote, voter_id, election_id from votes where election_id in :ids order by voter_id, election_id desc"), ids=tuple(args.command[1:])).fetchall()
f = cfg['ciphertexts']
path = os.path.join(cfg['electionCfg']['path'], f)
votes_files = dict()
max_size = args.max_count
valid_votes = set()
check_ids = (len(args.voter_ids_path) > 0)
if check_ids:
with codecs.open(args.voter_ids_path, encoding='utf-8', mode='r') as vip:
for line in vip:
valid_votes.add(line.strip())
for election_id in args.command[1:]:
path = os.path.join(ELECTIONS_DIR, election_id, 'ctexts_' + election_id)
votes_files[election_id] = dict(
total=0,
filtered=0,
partial=0,
file=codecs.open(path, encoding='utf-8', mode='w+')
)
for vote in vs:
meta = votes_files[vote['election_id']]
# filter only for valid votes
if check_ids:
if not args.invalid and vote['voter_id'] not in valid_votes:
meta["filtered"] += 1
continue
elif args.invalid and vote['voter_id'] in valid_votes:
meta["filtered"] += 1
continue
meta['file'].write(vote['vote'] + "\n")
meta["total"] += 1
if max_size != 0 and meta['total'] % max_size == 0:
meta['file'].close()
meta['partial'] += 1
path = os.path.join(ELECTIONS_DIR, election_id, 'ctexts_%s.%d' % (election_id, meta['partial']))
meta['file'] = codecs.open(path, encoding='utf-8', mode='w+')
print("creating partial for %s num %d" % (election_id, meta['partial']))
total = 0
filtered = 0
for eid, meta in votes_files.items():
total += meta['total']
filtered += meta['filtered']
print("> dumped %d votes to %s (%d filtered)" % (meta['total'], eid, meta['filtered']))
meta['file'].close()
print("> dumped %d votes in total (%d filtered)" % (total, filtered))
# prints voter ids
def print_voterids(cfg, args):
conn = get_db_connection(cfg['backendCfg']['db'])
votes = votes_table()
vs = conn.execute(text("select distinct on (voter_id) vote, voter_id, election_id from votes where election_id in :ids order by voter_id, election_id desc"), ids=tuple(args.command[1:])).fetchall()
f = cfg['ciphertexts']
path = os.path.join(cfg['electionCfg']['path'], f)
valid_votes = set()
check_ids = (len(args.voter_ids_path) > 0)
if check_ids:
with codecs.open(args.voter_ids_path, encoding='utf-8', mode='r') as vip:
for line in vip:
valid_votes.add(line.strip())
for vote in vs:
# filter only for valid votes
if check_ids:
if vote['voter_id'] not in valid_votes:
continue
print(vote['voter_id'])
# dumps votes a format suitable for backup/restore
def dump_votes_full(cfg, args):
conn = get_db_connection(cfg['backendCfg']['db'])
votes = votes_table()
s = select([votes]).where(votes.c.election_id == cfg['electionCfg']['election-id'])
vs = conn.execute(s)
data = []
for vote in vs:
data.append({
'vote': vote[1],
'vote_hash': vote[2],
'election_id': vote[3],
'voter_id': vote[4]
})
file = cfg['ciphertexts']
path = os.path.join(cfg['electionCfg']['path'], file)
with codecs.open(path, encoding='utf-8', mode='w+') as votes_file:
votes_file.write(json.dumps(data, sort_keys=True))
print("> dumped %d votes to %s" % (len(data), path))
def encrypt(cfg, args):
config = cfg['electionCfg']
electionId = config['election-id']
pkFile = 'pk_' + electionId
votesFile = cfg['plaintexts']
votesCount = cfg['encrypt-count']
ctexts = cfg['ciphertexts']
print("> Encrypting votes (" + votesFile + ", pk = " + pkFile + ", " + str(votesCount) + ")..")
pkPath = os.path.join(config['path'], pkFile)
votesPath = os.path.join(config['path'], votesFile)
ctextsPath = os.path.join(config['path'], ctexts)
if(os.path.isfile(pkPath)) and (os.path.isfile(votesPath)):
output, error = subprocess.Popen([node, "js/encrypt.js", pkPath, votesPath, str(votesCount)], stdout = subprocess.PIPE).communicate()
print("> Received Nodejs output (" + str(len(output)) + " chars)")
parsed = json.loads(output)
print("> Writing file to " + ctextsPath)
write_node_votes(parsed, ctextsPath)
else:
print("No public key or votes file, exiting..")
exit(1)
def eotest(cfg, args):
config = cfg['electionCfg']
pkFile = create(cfg)
if(os.path.isfile(os.path.join(config['path'], pkFile))):
encrypt(cfg)
tally(cfg, None, False)
else:
print("No public key, exiting..")
def list_elections(cfg, args):
e = PrettyTable(['election id','director', 'authorities', 'pk', 'ctexts', 'tally'])
elections = []
for item in os.listdir(ELECTIONS_DIR):
dir = os.path.join(ELECTIONS_DIR, item)
if os.path.isdir(dir):
jsonPath = os.path.join(dir, "config.json")
if os.path.isfile(jsonPath):
with open(jsonPath, 'r') as f:
config = json.loads(f.read())
if not 'election-id' in config:
config['election-id'] = os.path.basename(os.path.normpath(dir))
pkPath = os.path.join(dir, "pk_" + config['election-id'])
config['pk'] = os.path.isfile(pkPath)
ctextsPath = os.path.join(dir, "ctexts_" + config['election-id'])
config['ctexts'] = os.path.isfile(ctextsPath)
tallyPath = os.path.join(dir, config['election-id'] + ".tar.gz")
config['tally'] = os.path.isfile(tallyPath)
e.add_row([
config['election-id'],
config['director'],
config['authorities'],
config['pk'],
config['ctexts'],
config['tally']
])
print e
def console(cfg, args):
conn = get_db_connection(cfg['backendCfg']['db'])
import pdb; pdb.set_trace()
def reload_config(cfg, args):
import hmac
secret = cfg['backendCfg']['SharedSecret']
message = 'admin:%d' % int(time.time())
_hmac = hmac.new(str(secret), str(message), hashlib.sha256).hexdigest()
auth = '%s:%s' % (message,_hmac)
headers = {'content-type': 'application/json', 'Authorization': auth}
url = 'http://localhost:%d/api/v1/ballotbox/reload-config' % args.ballotbox_port
r = requests.post(url, data='{}', headers=headers)
print(r.status_code)
def __load_votes_full(cfg):
conn = get_db_connection(cfg['backendCfg']['db'])
votes = votes_table()
file = cfg['ciphertexts']
path = os.path.join(cfg['electionCfg']['path'], file)
with open(path, 'r') as f:
vs = json.loads(f.read())
conn.execute(votes.insert(), vs)
print("> inserted %d votes" % len(vs))
def __load_node_votes(cfg):
conn = get_db_connection(cfg['backendCfg']['db'])
votes = votes_table()
file = cfg['ciphertexts']
path = os.path.join(cfg['electionCfg']['path'], file)
with open(path, 'r') as f:
# vs = f.readlines()
vs = f.read().splitlines()
vs = map(lambda v: {
'vote': v,
'vote_hash': hashlib.sha512(v).hexdigest(),
'election_id': cfg['electionCfg']['election-id'],
'voter_id': 'BOGUS' + hashlib.sha224(v).hexdigest()
}, vs)
conn.execute(votes.insert(), vs)
print("> inserted %d votes" % len(vs))
def main(argv):
parser = argparse.ArgumentParser(description='sequent-api admin script', formatter_class=RawTextHelpFormatter)
parser.add_argument('command', nargs='+', help='''create <election_dir>: creates an election
tally <election_dir>: launches tally
list_votes <election_dir>: list votes
list_elections: list elections (under ELECTIONS_DIR)
count_votes <election_dir> [<election_dir2> [<election_dir3> [..]]]: count votes
dump_votes_eo <election_dir> [<election_dir2> [<election_dir3> [..]]]: dumps votes ready for eo
print_voterids <election_dir> [<election_dir2> [<election_dir3> [..]]]: prints voter ids
dump_votes_full <election_dir>: dumps votes in local format
console <data>: executes a console
eotest <election_dir>: runs a full eo test cycle''')
parser.add_argument('--eopeers-dir', help='directory with the eopeers packages to use as authorities', default = '/etc/eopeers/')
parser.add_argument('--ciphertexts', help='file to write ciphertetxs (used in dump, load and encrypt)')
parser.add_argument('--tally-file', help='the file to save the tally to (used with "tally")')
parser.add_argument('--force', help='force to continue even if something strange happens, like /etc/eopeers doesnt exist',
action='store_true')
parser.add_argument('--invalid', help='voter ids path are the invalid', action='store_true')
parser.add_argument('--plaintexts', help='json file to read votes from when encrypting', default = 'votes.json')
parser.add_argument('--encrypt-count', help='number of votes to encrypt (generates duplicates if more than in json file)', type=int, default = 0)
parser.add_argument('-m', '--max-count', help='when creating doing a dump, maximum size of each dump part', type=int, default=0)
parser.add_argument('-p', '--partial', help='partial of which to do the tally', type=int, default=-1)
parser.add_argument('-f', '--filters', nargs='+', default=[], help="key==value(s) filters for queries (use ~ for like)")
parser.add_argument('--voter-ids-path', nargs='?', default=[], help="Path to a file with valid voter ids for filtering")
parser.add_argument('--ballotbox-port', help='the port that the ballotbox is listening on', type=int, default = 3000)
args = parser.parse_args()
command = args.command[0]
if hasattr(__main__, command):
backendConfig = get_backend_config()
config = {
"backendCfg": backendConfig
}
if command == "console":
console(config, args)
elif command not in ["list_elections", "reload_config"]:
# election-specific commands
if len(args.command) >= 2:
electionConfig = get_election_config(args.command[1], args.eopeers_dir, args.force)
config['electionCfg'] = electionConfig
config['plaintexts'] = args.plaintexts
config['encrypt-count'] = args.encrypt_count
config['filters'] = args.filters
if args.tally_file is None:
config['tally-file'] = electionConfig['election-id'] + '.tar.gz'
else:
config['tally-file'] = args.tally_file
if args.ciphertexts is None:
config['ciphertexts'] = 'ctexts_' + electionConfig['election-id']
else:
config['ciphertexts'] = args.ciphertexts
else:
parser.print_help()
exit(1)
eval(command + "(config, args)")
else:
parser.print_help()
if __name__ == "__main__":
main(sys.argv[1:])