forked from freedomofpress/securedrop-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request freedomofpress#113 from freedomofpress/rest-of-mvp
file decryption and open
- Loading branch information
Showing
8 changed files
with
249 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
""" | ||
Copyright (C) 2018 The Freedom of the Press Foundation. | ||
This program 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, or | ||
(at your option) any later version. | ||
This program 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 this program. If not, see <http://www.gnu.org/licenses/>. | ||
""" | ||
|
||
import gzip | ||
import logging | ||
import os | ||
import shutil | ||
import subprocess | ||
import tempfile | ||
|
||
from securedrop_client.models import make_engine | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def decrypt_submission(filepath, target_filename, home_dir, is_qubes=True, | ||
is_doc=False): | ||
out = tempfile.NamedTemporaryFile(suffix=".message") | ||
err = tempfile.NamedTemporaryFile(suffix=".message-error", delete=False) | ||
if is_qubes: | ||
gpg_binary = "qubes-gpg-client" | ||
else: | ||
gpg_binary = "gpg" | ||
cmd = [gpg_binary, "--decrypt", filepath] | ||
res = subprocess.call(cmd, stdout=out, stderr=err) | ||
|
||
os.unlink(filepath) # original file | ||
|
||
if res != 0: | ||
out.close() | ||
err.close() | ||
|
||
with open(err.name) as e: | ||
msg = e.read() | ||
logger.error("GPG error: {}".format(msg)) | ||
|
||
os.unlink(err.name) | ||
dest = "" | ||
else: | ||
if is_doc: | ||
# Docs are gzipped, so gunzip the file | ||
with gzip.open(out.name, 'rb') as infile: | ||
unzipped_decrypted_data = infile.read() | ||
|
||
# Need to split twice as filename is e.g. | ||
# 1-impractical_thing-doc.gz.gpg | ||
fn_no_ext, _ = os.path.splitext( | ||
os.path.splitext(os.path.basename(filepath))[0]) | ||
dest = os.path.join(home_dir, "data", fn_no_ext) | ||
|
||
with open(dest, 'wb') as outfile: | ||
outfile.write(unzipped_decrypted_data) | ||
else: | ||
fn_no_ext, _ = os.path.splitext(target_filename) | ||
dest = os.path.join(home_dir, "data", fn_no_ext) | ||
shutil.copy(out.name, dest) | ||
|
||
out.close() | ||
err.close() | ||
logger.info("Downloaded and decrypted: {}".format(dest)) | ||
|
||
return res, dest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import pytest | ||
import os | ||
from unittest import mock | ||
|
||
from securedrop_client.crypto import decrypt_submission | ||
|
||
|
||
def test_gunzip_logic(safe_tmpdir): | ||
""" | ||
Ensure that gzipped documents/files are handled | ||
""" | ||
# Make data dir since we do need it for this test | ||
data_dir = os.path.join(str(safe_tmpdir), 'data') | ||
if not os.path.exists(data_dir): | ||
os.makedirs(data_dir) | ||
|
||
test_gzip = 'tests/files/test-doc.gz.gpg' | ||
expected_output_filename = 'test-doc' | ||
|
||
with mock.patch('subprocess.call', | ||
return_value=0) as mock_gpg, \ | ||
mock.patch('os.unlink') as mock_unlink: | ||
res, dest = decrypt_submission( | ||
test_gzip, expected_output_filename, | ||
str(safe_tmpdir), is_qubes=False, | ||
is_doc=True) | ||
|
||
assert mock_gpg.call_count == 1 | ||
assert res == 0 | ||
assert dest == '{}/data/{}'.format( | ||
str(safe_tmpdir), expected_output_filename) |
Oops, something went wrong.