Skip to content

Pull stuff out into functions so other python code can use it. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions scripts/cqe-fetch-event-bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,38 @@
import json
import sys
import os
import logging

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--creds", help="path to CGC credentials JSON file", type=str, required=True)
parser.add_argument("-e", "--event_name", help="Event name", type=str, required=True)

args = parser.parse_args()
def fetch(creds, event_name):

logger = logging.getLogger(__name__)

# set filename based on event
filename = args.event_name + ".ar.gz.enc"
filename = event_name + ".ar.gz.enc"

# sanity checks
if not os.path.exists(args.creds):
sys.exit("Error: cannot find creds file '%s'" % args.creds)
if not os.path.exists(creds):
logger.error("Error: cannot find creds file '%s'" % creds)
sys.exit("Error: cannot find creds file '%s'" % creds)

# load credentials
creds = json.loads(open(args.creds, "r").read())
creds = json.loads(open(creds, "r").read())

# connect to S3
s3 = boto.connect_s3(creds['access_id'], creds['access_key'])
bucket = s3.get_bucket(creds['distribution_bucket'])

# download the bundle
k = Key(bucket)
k.key = args.event_name + "/" + filename
k.key = event_name + "/" + filename
k.get_contents_to_filename(filename)

print "bundle downloaded to " + filename
logger.info("bundle downloaded to %s" % filename);

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--creds", help="path to CGC credentials JSON file", type=str, required=True)
parser.add_argument("-e", "--event_name", help="Event name", type=str, required=True)

args = parser.parse_args()
fetch(args.creds, args.event_name)
59 changes: 35 additions & 24 deletions scripts/cqe-package-solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import subprocess
import sys
import re
import logging

def verify(csid, pov, files):
"""
Expand Down Expand Up @@ -50,7 +51,7 @@ def verify(csid, pov, files):
sys.exit("Error: %s returned %d" % (cpe.cmd, cpe.returncode))


def package(csid, pov, files, passphrase):
def package_aux(csid, pov, files, passphrase):
"""
Packaging method for building a solution for submission. Both
companion file and encrypted solution package are created
Expand Down Expand Up @@ -87,6 +88,38 @@ def package(csid, pov, files, passphrase):

return (commitment_name, pkg_name_s)

def package(creds, csid, pov, files):

logger = logging.getLogger(__name__)

# sanity checks
if not os.path.exists(creds):
logger.error("Error: cannot find creds file '%s'" % creds)
sys.exit("Error: cannot find creds file '%s'" % creds)

if not os.path.exists(pov):
logger.error("Error: cannot find PoV file '%s'" % pov)
sys.exit("Error: cannot find PoV file '%s'" % pov)

for f in files:
if not os.path.exists(f):
logger.error("Error: cannot find replacement CB '%s'" % f)
sys.exit("Error: cannot find replacement CB '%s'" % f)

# verify components and naming
verify(csid, pov, files)

# load credentials
creds = json.loads(open(creds, "r").read())

# package solution and generate commitment
(commitment_file, solution_file) = package_aux(csid, pov, files, creds['cqe_encryption_key'])

logger.info("Commitment file: %s" % commitment_file)
logger.info("Encrypted solution package: %s" % solution_file)

return (commitment_file, solution_file)

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--creds",
Expand All @@ -103,26 +136,4 @@ def package(csid, pov, files, passphrase):
help="List of replacement CBs")

args = parser.parse_args()

# sanity checks
if not os.path.exists(args.creds):
sys.exit("Error: cannot find creds file '%s'" % args.creds)

if not os.path.exists(args.pov):
sys.exit("Error: cannot find PoV file '%s'" % args.pov)

for f in args.files:
if not os.path.exists(f):
sys.exit("Error: cannot find replacement CB '%s'" % f)

# verify components and naming
verify(args.csid, args.pov, args.files)

# load credentials
creds = json.loads(open(args.creds, "r").read())

# package solution and generate commitment
(commitment_file, solution_file) = package(args.csid, args.pov, args.files, creds['cqe_encryption_key'])
print "Commitment file: " + commitment_file
print "Encrypted solution package: " + solution_file

package(args.creds, args.csid, args.pov, args.files)
78 changes: 44 additions & 34 deletions scripts/cqe-submit-solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,61 +22,71 @@
import json
import sys
import re
import logging

def submit(creds, encrypted_package, commitment_file, event_name):

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--creds",
help="path to CGC credentials JSON file",
type=str, required=True)
parser.add_argument("-p", "--encrypted_package",
help="encrypted package to submit",
type=str, required=True)
parser.add_argument("-m", "--commitment_file",
help="commitment file for the encrypted package",
type=str, required=True)
parser.add_argument("-e", "--event_name",
help="name of the event",
type=str, required=True)

args = parser.parse_args()
logger = logging.getLogger(__name__)

# sanity checks
if not os.path.exists(args.creds):
sys.exit("Error: cannot find creds file '%s'" % args.creds)
if not os.path.exists(creds):
logger.error("cannot find creds file '%s'" % creds)
sys.exit("Error: cannot find creds file '%s'" % creds)

if not os.path.exists(args.encrypted_package):
if not os.path.exists(encrypted_package):
logger.error("cannot find the encrypted package")
sys.exit("Error: cannot find the encrypted package")

if not os.path.exists(args.commitment_file):
if not os.path.exists(commitment_file):
logger.error("cannot find the commitment file")
sys.exit("Error: cannot find the commitment file")

# name check
if not re.match(r"^[0-9a-f]{8}_[0-9a-f]{64}\.ar\.enc$", args.encrypted_package):
if not re.match(r"^[0-9a-f]{8}_[0-9a-f]{64}\.ar\.enc$", encrypted_package):
logger.error("Encrypted package incorrectly named")
sys.exit("Error: Encrypted package incorrectly named")

if not re.match(r"^[0-9a-f]{8}_[0-9a-f]{64}\.txt$", args.commitment_file):
if not re.match(r"^[0-9a-f]{8}_[0-9a-f]{64}\.txt$", commitment_file):
logger.error("Commitment file incorrectly named")
sys.exit("Error: Commitment file incorrectly named")

if args.commitment_file[0:-4] != args.encrypted_package[0:-7]:
if commitment_file[0:-4] != encrypted_package[0:-7]:
logger.error("Commitment file and encrypted package must be named the same")
sys.exit("Error: Commitment file and encrypted package must be named the same")

creds = json.loads(open(args.creds, "r").read())
creds = json.loads(open(creds, "r").read())

print "Authenticating to Amazon S3..."
logger.info("Authenticating to Amazon S3...")
s3 = boto.connect_s3(creds['access_id'], creds['access_key'])
bucket = s3.get_bucket(creds['submission_bucket'])
print "complete"
logger.info("complete")

print "Uploading commitment file..."
logger.info("Uploading commitment file...")
k = Key(bucket)
k.key = args.event_name + args.commitment_file
k.set_contents_from_filename(args.commitment_file)
print "complete"
k.key = event_name + "/" + commitment_file
k.set_contents_from_filename(commitment_file)
logger.info("complete")

print "Uploading encrypted solution package..."
logger.info("Uploading encrypted solution package...")
k = Key(bucket)
k.key = args.event_name + args.encrypted_package
k.set_contents_from_filename(args.encrypted_package)
print "complete"
k.key = event_name + "/" + encrypted_package
k.set_contents_from_filename(encrypted_package)
logger.info("complete")

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--creds",
help="path to CGC credentials JSON file",
type=str, required=True)
parser.add_argument("-p", "--encrypted_package",
help="encrypted package to submit",
type=str, required=True)
parser.add_argument("-m", "--commitment_file",
help="commitment file for the encrypted package",
type=str, required=True)
parser.add_argument("-e", "--event_name",
help="name of the event",
type=str, required=True)

args = parser.parse_args()
submit(args.creds, args.encrypted_package, args.commitment_file, args.event_name)
59 changes: 35 additions & 24 deletions scripts/cqe-unpack-event-bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,81 @@
import re
import os
import json
import logging

if __name__ == "__main__":
#
# Here's what we need to do:
#
# 1. Decrypt the compressed archive with event password provided
# 2. Decompress the archive
# 3. Extract the files from the archive
# 4. Populate (and print?) a map from CSIDs to constituent files
#
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--event_bundle",
help="Event bundle file downloaded from S3", required=True)
parser.add_argument("-p", "--password",
help="Event password to use for decrypting the bundle", required=True)
args = parser.parse_args()
def unpack(event_bundle, password):

logger = logging.getLogger(__name__)

enc_bundle_name = args.event_bundle
enc_bundle_name = event_bundle
# remove .enc
gz_bundle_name = enc_bundle_name[:-4]
# remove .gz
bundle_name = gz_bundle_name[:-3]

# 1. decrypt the archive
if not os.path.exists(args.event_bundle):
sys.exit("Error: cannot find event bundle %s", args.event_bundle)
if not os.path.exists(event_bundle):
logger.error("cannot find event bundle %s", event_bundle)
sys.exit("Error: cannot find event bundle %s", event_bundle)
try:
cmd = ["openssl", "aes-256-cbc", "-d", "-pass", "pass:%s" % args.password,
cmd = ["openssl", "aes-256-cbc", "-d", "-pass", "pass:%s" % password,
"-in", enc_bundle_name, "-out", gz_bundle_name]
subprocess.check_call(cmd)
except subprocess.CalledProcessError as cpe:
logger.error("%s return %d" % (cpe.cmd, cpe.returncode))
sys.exit("Error: %s return %d" % (cpe.cmd, cpe.returncode))

# 2. decompress the archive
if not os.path.exists(gz_bundle_name):
logger.error("cannot find compressed archive %s; did decryption fail?" % gz_bundle_name)
sys.exit("Error: cannot find compressed archive %s; did decryption fail?" % gz_bundle_name)
try:
cmd = ["gunzip", gz_bundle_name]
subprocess.check_call(cmd)
except subprocess.CalledProcessError as cpe:
logger.error("%s returned %d" % (cpe.cmd, cpe.returncode))
sys.exit("Error: %s returned %d" % (cpe.cmd, cpe.returncode))

# 3. extract files from archive
if not os.path.exists(bundle_name):
logger.error("cannot find uncompressed archive %s; did decompression fail?" % bundle_name)
sys.exit("Error: cannot find uncompressed archive %s; did decompression fail?" % bundle_name)
try:
cmd = ["ar", "x", bundle_name]
subprocess.check_call(cmd)
except subprocess.CalledProcessError as cpe:
logger.error("%s returned %d" % (cpe.cmd, cpe.returncode))
sys.exit("Error: %s returned %d" % (cpe.cmd, cpe.returncode))

# 4. populate (and print?) a map from CSIDs to constituent files
if not os.path.exists("manifest.json"):
logger.error("cannot find the manifest.json file; did archive extraction fail?")
sys.exit("Error: cannot find the manifest.json file; did archive extraction fail?")

csets = json.loads(open("manifest.json", "r").read())

print "Challenges in this bundle:"
logger.info("Challenges in this bundle:")
for csid in csets:
print "CSID: ", csid
print "\tCBs: ", ", ".join(csets[csid]['cbs'])
logger.info("CSID: %s" % str(csid))
logger.info("\tCBs: %s" % ", ".join(csets[csid]['cbs']))
if csets[csid]['pcap'] != '':
print "\tpcap: ", csets[csid]['pcap']
logger.info("\tpcap: %s" % csets[csid]['pcap'])
else:
print "\tpcap: None"
logger.info("\tpcap: None")

if __name__ == "__main__":
#
# Here's what we need to do:
#
# 1. Decrypt the compressed archive with event password provided
# 2. Decompress the archive
# 3. Extract the files from the archive
# 4. Populate (and print?) a map from CSIDs to constituent files
#
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--event_bundle",
help="Event bundle file downloaded from S3", required=True)
parser.add_argument("-p", "--password",
help="Event password to use for decrypting the bundle", required=True)
args = parser.parse_args()
unpack(args.event_bundle, args.password)