Skip to content

Commit 389a1b8

Browse files
author
Piotr Kaleta
committed
Fixed decryption when retrieving file.
1 parent 6f7174c commit 389a1b8

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

glacier.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,17 @@ def recent_enough(job):
352352

353353

354354
def find_complete_job(jobs):
355-
for job in sorted(filter(lambda job: job.completed, jobs), key=lambda job: iso8601.parse_date(job.completion_date), reverse=True):
355+
complete_jobs = filter(lambda job: job.completed, jobs)
356+
357+
def most_recent_job(job):
358+
return iso8601.parse_date(job.completion_date)
359+
360+
sorted_completed_jobs = sorted(
361+
complete_jobs,
362+
key=most_recent_job, reverse=True
363+
)
364+
365+
for job in sorted_completed_jobs:
356366
return job
357367

358368

@@ -526,10 +536,15 @@ def multipart_archive_upload(self, args, encryptor=None):
526536
@staticmethod
527537
def _write_archive_retrieval_job(f, job, multipart_size,
528538
encryptor=None):
539+
if encryptor:
540+
destfile = tempfile.NamedTemporaryFile()
541+
else:
542+
destfile = f
543+
529544
if job.archive_size > multipart_size:
530545
def fetch(start, end):
531546
byte_range = start, end - 1
532-
f.write(job.get_output(byte_range).read())
547+
destfile.write(job.get_output(byte_range).read())
533548

534549
whole_parts = job.archive_size // multipart_size
535550
for first_byte in xrange(0, whole_parts * multipart_size,
@@ -539,21 +554,23 @@ def fetch(start, end):
539554
if remainder:
540555
fetch(job.archive_size - remainder, job.archive_size)
541556
else:
542-
data = job.get_output().read()
543-
if encryptor:
544-
data = encryptor.decrypt(data)
545-
f.write(data)
557+
destfile.write(job.get_output().read())
546558

547559
# Make sure that the file now exactly matches the downloaded archive,
548560
# even if the file existed before and was longer.
549561
try:
550-
f.truncate(job.archive_size)
562+
destfile.truncate(job.archive_size)
551563
except IOError, e:
552564
# Allow ESPIPE, since the "file" couldn't have existed before in
553565
# this case.
554566
if e.errno != errno.ESPIPE:
555567
raise
556568

569+
# Decrypt file if encryptor is given
570+
if encryptor:
571+
encryptor.decrypt_file(destfile.name, f.name)
572+
destfile.close()
573+
557574
@classmethod
558575
def _archive_retrieve_completed(cls, args, job, name, encryptor=None):
559576
if args.output_filename == '-':

gpg.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ def export_keypair(self, key):
2929

3030
return public_key, private_key
3131

32+
def _get_fingerprint(self):
33+
return self.gpg.list_keys()[0]["fingerprint"]
34+
3235
def encrypt_file(self, input_filename, output_filename):
33-
fingerprint = self.gpg.list_keys()[0]["fingerprint"]
36+
fingerprint = self._get_fingerprint()
3437

35-
with open(input_filename) as input_file:
38+
with open(input_filename, "r") as input_file:
3639
self.gpg.encrypt_file(input_file, fingerprint,
3740
output=output_filename)
3841

42+
def decrypt_file(self, input_filename, output_filename):
43+
with open(input_filename, "r") as input_file:
44+
self.gpg.decrypt_file(input_file, output=output_filename)
45+
3946
def decrypt(self, data):
4047
return self.gpg.decrypt(data)

0 commit comments

Comments
 (0)