Skip to content

Commit

Permalink
Merge pull request #34 from agoravoting/release-17-04
Browse files Browse the repository at this point in the history
Release 17 04
  • Loading branch information
Findeton committed Apr 29, 2017
2 parents b56a866 + 07c3e69 commit 16e11b1
Show file tree
Hide file tree
Showing 15 changed files with 2,328 additions and 61 deletions.
107 changes: 102 additions & 5 deletions admin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with agora_elections. If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function
import requests
import json
import time
Expand All @@ -31,10 +32,18 @@
import hashlib
import codecs
import traceback
import string

import os.path
import os
from prettytable import PrettyTable
import random
import shutil

import warnings as _warnings
import os as _os

from tempfile import mkdtemp

from sqlalchemy import create_engine, select, func, text
from sqlalchemy import Table, Column, Integer, String, TIMESTAMP, MetaData, ForeignKey
Expand All @@ -60,6 +69,92 @@
authapi_db_port = 5432
node = '/usr/local/bin/node'

class TemporaryDirectory(object):
"""Create and return a temporary directory. This has the same
behavior as mkdtemp but can be used as a context manager. For
example:
with TemporaryDirectory() as tmpdir:
...
Upon exiting the context, the directory and everything contained
in it are removed.
"""

def __init__(self, suffix="", prefix="tmp", dir=None):
self._closed = False
self.name = None # Handle mkdtemp raising an exception
self.name = mkdtemp(suffix, prefix, dir)

def __repr__(self):
return "<{} {!r}>".format(self.__class__.__name__, self.name)

def __enter__(self):
return self.name

def cleanup(self, _warn=False):
if self.name and not self._closed:
try:
self._rmtree(self.name)
except (TypeError, AttributeError) as ex:
# Issue #10188: Emit a warning on stderr
# if the directory could not be cleaned
# up due to missing globals
if "None" not in str(ex):
raise
print("ERROR: {!r} while cleaning up {!r}".format(ex, self,),
file=_sys.stderr)
return
self._closed = True
if _warn:
self._warn("Implicitly cleaning up {!r}".format(self),
ResourceWarning)
def __exit__(self, exc, value, tb):
self.cleanup()

def __del__(self):
# Issue a ResourceWarning if implicit cleanup needed
self.cleanup(_warn=True)

# XXX (ncoghlan): The following code attempts to make
# this class tolerant of the module nulling out process
# that happens during CPython interpreter shutdown
# Alas, it doesn't actually manage it. See issue #10188
_listdir = staticmethod(_os.listdir)
_path_join = staticmethod(_os.path.join)
_isdir = staticmethod(_os.path.isdir)
_islink = staticmethod(_os.path.islink)
_remove = staticmethod(_os.remove)
_rmdir = staticmethod(_os.rmdir)
_warn = _warnings.warn

def _rmtree(self, path):
# Essentially a stripped down version of shutil.rmtree. We can't
# use globals because they may be None'ed out at shutdown.
for name in self._listdir(path):
fullname = self._path_join(path, name)
try:
isdir = self._isdir(fullname) and not self._islink(fullname)
except OSError:
isdir = False
if isdir:
self._rmtree(fullname)
else:
try:
self._remove(fullname)
except OSError:
pass
try:
self._rmdir(path)
except OSError:
pass







def get_local_hostport():
return app_host, app_port

Expand Down Expand Up @@ -286,7 +381,7 @@ def cast_votes(cfg, args):
vote_string = json.dumps(vote)
vote_hash = hashlib.sha256(vote_string).hexdigest()
vote = {
"vote": json.dumps(vote),
"vote": vote_string,
"vote_hash": vote_hash
}

Expand Down Expand Up @@ -607,7 +702,7 @@ def encrypt(cfg, args):
output, error = subprocess.Popen(["bash", "encrypt.sh", pkPath, votesPath, str(votesCount)], stdout = subprocess.PIPE).communicate()

print("Received encrypt.sh output (" + str(len(output)) + " chars)")
parsed = json.loads(output)
parsed = json.loads(output.decode('utf-8'))

print("Writing file to " + ctextsPath)
with codecs.open(ctextsPath, encoding='utf-8', mode='w+') as votes_file:
Expand Down Expand Up @@ -640,9 +735,9 @@ def get_hmac(cfg, userId, objType, objId, perm):
import hmac

secret = shared_secret
now = 1000*long(time.time())
now = 1000*int(time.time())
message = "%s:%s:%d:%s:%d" % (userId, objType, objId, perm, now)
_hmac = hmac.new(str(secret), str(message), hashlib.sha256).hexdigest()
_hmac = hmac.new(str.encode(secret), str.encode(message), hashlib.sha256).hexdigest()
ret = 'khmac:///sha-256;%s/%s' % (_hmac, message)

return ret
Expand Down Expand Up @@ -671,18 +766,20 @@ def main(argv):
dump_votes [election_id, [election_id], ...]: dump voter ids
list_votes <election_dir>: list votes
list_elections: list elections
cast_votes <election_dir>: cast votes from ciphertetxs
dump_pks <election_id>: dumps pks for an election (public datastore)
encrypt <election_id>: encrypts votes using scala (public key must be in datastore)
encryptNode <election_id>: encrypts votes using node (public key must be in datastore)
dump_votes <election_id>: dumps votes for an election (private datastore)
change_social <election_id>: changes the social netoworks share buttons configuration
authapi_ensure_acls --acls-path <acl_path>: ensure that the acls inside acl_path exist.
authapi_ensure_acls --acls-path <acl_path>: ensure that the acls inside acl_path exist
''')
parser.add_argument('--ciphertexts', help='file to write ciphertetxs (used in dump, load and encrypt)')
parser.add_argument('--acls-path', help='''the file has one line per acl with format: '(email:[email protected]|tlf:+34666777888),permission_name,object_type,object_id,user_election_id' ''')
parser.add_argument('--plaintexts', help='json file to read votes from when encrypting', default = 'votes.json')
parser.add_argument('--filter-config', help='file with filter configuration', default = None)
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('--vote-count', help='number of votes to generate', type=int, default = 0)
parser.add_argument('--results-config', help='config file for agora-results')
parser.add_argument('--voter-ids', help='json file with list of valid voter ids to tally (used with tally_voter_ids)')
parser.add_argument('--ips-log', help='')
Expand Down
12 changes: 0 additions & 12 deletions admin/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,6 @@ def main(argv):

elif args.command == 'list-tally-with-ids':
for eid in args.election_ids:
print('next id %d, dumping votes with matching ids (in private datastore)' % eid)
ret = cycle.dump_votes_with_ids(eid)
if ret in [400, 500]:
print("dump_votes_with_ids returned %d, continuing without it" % ret)
continue

print('next id %d, stopping election' % eid)
ret = cycle.stop(eid)
if ret in [400, 500]:
Expand Down Expand Up @@ -223,12 +217,6 @@ def main(argv):
else:
next_id = cfg['id']

print('next id %d, dumping votes with matching ids (in private datastore)' % next_id)
ret = cycle.dump_votes_with_ids(next_id)
if ret in [400, 500]:
print("dump_votes_with_ids returned %d, continuing without it" % ret)
continue

print('next id %d, stopping election' % next_id)
ret = cycle.stop(next_id)
if ret in [400, 500]:
Expand Down
180 changes: 180 additions & 0 deletions admin/test.jmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="3.1" jmeter="3.1 r1770033">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
<stringProp name="TestPlan.comments"></stringProp>
<boolProp name="TestPlan.functional_mode">false</boolProp>
<boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">${loop_count}</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">${num_threads}</stringProp>
<stringProp name="ThreadGroup.ramp_time">${rampup_secs}</stringProp>
<longProp name="ThreadGroup.start_time">1489135288000</longProp>
<longProp name="ThreadGroup.end_time">1489135288000</longProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration"></stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
</ThreadGroup>
<hashTree>
<ConfigTestElement guiclass="HttpDefaultsGui" testclass="ConfigTestElement" testname="HTTP Request Defaults" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="HTTPSampler.domain">${server_host}</stringProp>
<stringProp name="HTTPSampler.port">${server_port}</stringProp>
<stringProp name="HTTPSampler.connect_timeout"></stringProp>
<stringProp name="HTTPSampler.response_timeout"></stringProp>
<stringProp name="HTTPSampler.protocol"></stringProp>
<stringProp name="HTTPSampler.contentEncoding"></stringProp>
<stringProp name="HTTPSampler.path"></stringProp>
<stringProp name="HTTPSampler.concurrentPool">6</stringProp>
</ConfigTestElement>
<hashTree/>
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP Request" enabled="true">
<boolProp name="HTTPSampler.postBodyRaw">true</boolProp>
<elementProp name="HTTPsampler.Arguments" elementType="Arguments">
<collectionProp name="Arguments.arguments">
<elementProp name="" elementType="HTTPArgument">
<boolProp name="HTTPArgument.always_encode">false</boolProp>
<stringProp name="Argument.value">${ballot}</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
</collectionProp>
</elementProp>
<stringProp name="HTTPSampler.domain"></stringProp>
<stringProp name="HTTPSampler.port"></stringProp>
<stringProp name="HTTPSampler.connect_timeout"></stringProp>
<stringProp name="HTTPSampler.response_timeout"></stringProp>
<stringProp name="HTTPSampler.protocol">https</stringProp>
<stringProp name="HTTPSampler.contentEncoding"></stringProp>
<stringProp name="HTTPSampler.path">${service_path}/api/election/${electionid}/voter/${userid}</stringProp>
<stringProp name="HTTPSampler.method">POST</stringProp>
<boolProp name="HTTPSampler.follow_redirects">true</boolProp>
<boolProp name="HTTPSampler.auto_redirects">false</boolProp>
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
<boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
<boolProp name="HTTPSampler.monitor">false</boolProp>
<stringProp name="HTTPSampler.embedded_url_re"></stringProp>
</HTTPSamplerProxy>
<hashTree/>
<ResultCollector guiclass="GraphVisualizer" testclass="ResultCollector" testname="Graph Results" enabled="true">
<boolProp name="ResultCollector.error_logging">false</boolProp>
<objProp>
<name>saveConfig</name>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>true</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>false</xml>
<fieldNames>true</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
<sentBytes>true</sentBytes>
<threadCounts>true</threadCounts>
<idleTime>true</idleTime>
<connectTime>true</connectTime>
</value>
</objProp>
<stringProp name="filename"></stringProp>
</ResultCollector>
<hashTree/>
<CookieManager guiclass="CookiePanel" testclass="CookieManager" testname="HTTP Cookie Manager" enabled="true">
<collectionProp name="CookieManager.cookies"/>
<boolProp name="CookieManager.clearEachIteration">false</boolProp>
<stringProp name="CookieManager.policy">standard</stringProp>
<stringProp name="CookieManager.implementation">org.apache.jmeter.protocol.http.control.HC4CookieHandler</stringProp>
</CookieManager>
<hashTree/>
<CSVDataSet guiclass="TestBeanGUI" testclass="CSVDataSet" testname="CSV Data Set Config" enabled="true">
<stringProp name="filename">${csv_file_path}</stringProp>
<stringProp name="fileEncoding"></stringProp>
<stringProp name="variableNames">electionid,userid,ballot,khmac</stringProp>
<stringProp name="delimiter">|</stringProp>
<boolProp name="quotedData">false</boolProp>
<boolProp name="recycle">false</boolProp>
<boolProp name="stopThread">false</boolProp>
<stringProp name="shareMode">shareMode.all</stringProp>
</CSVDataSet>
<hashTree/>
<HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP Header Manager" enabled="true">
<collectionProp name="HeaderManager.headers">
<elementProp name="" elementType="Header">
<stringProp name="Header.name">Authorization</stringProp>
<stringProp name="Header.value">${khmac}</stringProp>
</elementProp>
<elementProp name="" elementType="Header">
<stringProp name="Header.name">Content-Type</stringProp>
<stringProp name="Header.value">application/json;charset=UTF-8 </stringProp>
</elementProp>
</collectionProp>
</HeaderManager>
<hashTree/>
<Arguments guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments">
<elementProp name="num_threads" elementType="Argument">
<stringProp name="Argument.name">num_threads</stringProp>
<stringProp name="Argument.value">${__P(num_threads,10)}</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="loop_count" elementType="Argument">
<stringProp name="Argument.name">loop_count</stringProp>
<stringProp name="Argument.value">${__P(loop_count,100)}</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="rampup_secs" elementType="Argument">
<stringProp name="Argument.name">rampup_secs</stringProp>
<stringProp name="Argument.value">${__P(rampup_secs,10)}</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="csv_file_path" elementType="Argument">
<stringProp name="Argument.name">csv_file_path</stringProp>
<stringProp name="Argument.value">${__P(csv_file_path,/home/agoraelections/jmeter/ciphertexts-khmac.csv)}</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="server_host" elementType="Argument">
<stringProp name="Argument.name">server_host</stringProp>
<stringProp name="Argument.value">${__P(server_host,localhost)}</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="server_port" elementType="Argument">
<stringProp name="Argument.name">server_port</stringProp>
<stringProp name="Argument.value">${__P(server_port,9000)}</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="service_path" elementType="Argument">
<stringProp name="Argument.name">service_path</stringProp>
<stringProp name="Argument.value">${__P(service_path,/elections)}</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
</collectionProp>
</Arguments>
<hashTree/>
</hashTree>
</hashTree>
</hashTree>
</jmeterTestPlan>
Loading

0 comments on commit 16e11b1

Please sign in to comment.