Remove obsolete Document property. Use resolved_object in documents links to avoid context variable clashes with signatures.

This commit is contained in:
Roberto Rosario
2016-03-25 04:10:20 -04:00
parent 7e801ef02e
commit ffb29e0f54
4 changed files with 52 additions and 29 deletions

View File

@@ -39,7 +39,9 @@ class KeyManager(models.Manager):
if not decrypt_result.status or decrypt_result.status == 'no data was provided': if not decrypt_result.status or decrypt_result.status == 'no data was provided':
raise DecryptionError('Unable to decrypt file') raise DecryptionError('Unable to decrypt file')
return str(decrypt_result) file_object.close()
return io.BytesIO(str(decrypt_result))
def receive_key(self, key_id): def receive_key(self, key_id):
temporary_directory = tempfile.mkdtemp() temporary_directory = tempfile.mkdtemp()
@@ -109,8 +111,9 @@ class KeyManager(models.Manager):
try: try:
key = self.get(fingerprint__endswith=key_id) key = self.get(fingerprint__endswith=key_id)
except self.model.DoesNotExist: except self.model.DoesNotExist:
shutil.rmtree(temporary_directory) pass
raise KeyDoesNotExist('Specified key for verification not found in keyring') #shutil.rmtree(temporary_directory)
#raise KeyDoesNotExist('Specified key for verification not found in keyring')
else: else:
result = gpg.import_keys(key_data=key.key_data) result = gpg.import_keys(key_data=key.key_data)
@@ -135,12 +138,17 @@ class KeyManager(models.Manager):
logger.debug('verify_result.status: %s', verify_result.status) logger.debug('verify_result.status: %s', verify_result.status)
shutil.rmtree(temporary_directory)
if verify_result: if verify_result:
shutil.rmtree(temporary_directory) # Signed and key present
return SignatureVerification(verify_result.__dict__) return SignatureVerification(verify_result.__dict__)
elif verify_result.status == 'no public key' and not (key_fingerprint or all_keys or key_id): elif verify_result.status == 'no public key' and not (key_fingerprint or all_keys or key_id):
# Signed but key not present, retry with key fetch
file_object.seek(0) file_object.seek(0)
return self.verify_file(file_object=file_object, signature_file=signature_file, key_id=verify_result.key_id) return self.verify_file(file_object=file_object, signature_file=signature_file, key_id=verify_result.key_id)
elif verify_result.key_id:
# Signed, retried and key still not found
return SignatureVerification(verify_result.__dict__)
else: else:
shutil.rmtree(temporary_directory)
raise VerificationError('File not signed') raise VerificationError('File not signed')

View File

@@ -34,12 +34,25 @@ class KeyTestCase(TestCase):
Key.objects.receive_key(key_id=TEST_SEARCH_FINGERPRINT) Key.objects.receive_key(key_id=TEST_SEARCH_FINGERPRINT)
self.assertEqual(Key.objects.all().count(), 1) self.assertEqual(Key.objects.all().count(), 1)
self.assertEqual(Key.objects.first().fingerprint, TEST_SEARCH_FINGERPRINT) self.assertEqual(
Key.objects.first().fingerprint, TEST_SEARCH_FINGERPRINT
)
def test_cleartext_file_verification(self):
cleartext_file = tempfile.TemporaryFile()
cleartext_file.write('test')
cleartext_file.seek(0)
with self.assertRaises(VerificationError):
Key.objects.verify_file(file_object=cleartext_file)
cleartext_file.close()
def test_embedded_verification_no_key(self): def test_embedded_verification_no_key(self):
with open(TEST_SIGNED_FILE) as signed_file: with open(TEST_SIGNED_FILE) as signed_file:
with self.assertRaises(KeyDoesNotExist): result = Key.objects.verify_file(signed_file)
Key.objects.verify_file(signed_file)
self.assertTrue(result.key_id in TEST_KEY_FINGERPRINT)
def test_embedded_verification_with_key(self): def test_embedded_verification_with_key(self):
Key.objects.create(key_data=TEST_KEY_DATA) Key.objects.create(key_data=TEST_KEY_DATA)
@@ -47,14 +60,15 @@ class KeyTestCase(TestCase):
with open(TEST_SIGNED_FILE) as signed_file: with open(TEST_SIGNED_FILE) as signed_file:
result = Key.objects.verify_file(signed_file) result = Key.objects.verify_file(signed_file)
self.assertTrue(result.valid)
self.assertEqual(result.fingerprint, TEST_KEY_FINGERPRINT) self.assertEqual(result.fingerprint, TEST_KEY_FINGERPRINT)
def test_embedded_verification_with_correct_fingerprint(self): def test_embedded_verification_with_correct_fingerprint(self):
Key.objects.create(key_data=TEST_KEY_DATA) Key.objects.create(key_data=TEST_KEY_DATA)
with open(TEST_SIGNED_FILE) as signed_file: with open(TEST_SIGNED_FILE) as signed_file:
result = Key.objects.verify_file(signed_file, key_fingerprint=TEST_KEY_FINGERPRINT) result = Key.objects.verify_file(
signed_file, key_fingerprint=TEST_KEY_FINGERPRINT
)
self.assertTrue(result.valid) self.assertTrue(result.valid)
self.assertEqual(result.fingerprint, TEST_KEY_FINGERPRINT) self.assertEqual(result.fingerprint, TEST_KEY_FINGERPRINT)
@@ -72,7 +86,7 @@ class KeyTestCase(TestCase):
with open(TEST_SIGNED_FILE) as signed_file: with open(TEST_SIGNED_FILE) as signed_file:
result = Key.objects.decrypt_file(file_object=signed_file) result = Key.objects.decrypt_file(file_object=signed_file)
self.assertEqual(result, TEST_SIGNED_FILE_CONTENT) self.assertEqual(result.read(), TEST_SIGNED_FILE_CONTENT)
def test_cleartext_file_decryption(self): def test_cleartext_file_decryption(self):
cleartext_file = tempfile.TemporaryFile() cleartext_file = tempfile.TemporaryFile()
@@ -87,15 +101,20 @@ class KeyTestCase(TestCase):
def test_detached_verification_no_key(self): def test_detached_verification_no_key(self):
with open(TEST_DETACHED_SIGNATURE) as signature_file: with open(TEST_DETACHED_SIGNATURE) as signature_file:
with open(TEST_FILE) as test_file: with open(TEST_FILE) as test_file:
with self.assertRaises(KeyDoesNotExist): result = Key.objects.verify_file(
Key.objects.verify_file(file_object=test_file, signature_file=signature_file) file_object=test_file, signature_file=signature_file
)
self.assertTrue(result.key_id in TEST_KEY_FINGERPRINT)
def test_detached_verification_with_key(self): def test_detached_verification_with_key(self):
Key.objects.create(key_data=TEST_KEY_DATA) Key.objects.create(key_data=TEST_KEY_DATA)
with open(TEST_DETACHED_SIGNATURE) as signature_file: with open(TEST_DETACHED_SIGNATURE) as signature_file:
with open(TEST_FILE) as test_file: with open(TEST_FILE) as test_file:
result = Key.objects.verify_file(file_object=test_file, signature_file=signature_file) result = Key.objects.verify_file(
file_object=test_file, signature_file=signature_file
)
self.assertTrue(result) self.assertTrue(result)
self.assertEqual(result.fingerprint, TEST_KEY_FINGERPRINT) self.assertEqual(result.fingerprint, TEST_KEY_FINGERPRINT)

View File

@@ -18,7 +18,7 @@ from .settings import setting_zoom_max_level, setting_zoom_min_level
def is_not_current_version(context): def is_not_current_version(context):
return context['object'].document.latest_version.timestamp != context['object'].timestamp return context['resolved_object'].document.latest_version.timestamp != context['resolved_object'].timestamp
def is_first_page(context): def is_first_page(context):
@@ -40,12 +40,12 @@ def is_min_zoom(context):
# Facet # Facet
link_document_preview = Link( link_document_preview = Link(
icon='fa fa-eye', permissions=(permission_document_view,), icon='fa fa-eye', permissions=(permission_document_view,),
text=_('Preview'), view='documents:document_preview', args='object.id' text=_('Preview'), view='documents:document_preview', args='resolved_object.id'
) )
link_document_properties = Link( link_document_properties = Link(
icon='fa fa-info', permissions=(permission_document_view,), icon='fa fa-info', permissions=(permission_document_view,),
text=_('Properties'), view='documents:document_properties', text=_('Properties'), view='documents:document_properties',
args='object.id' args='resolved_object.id'
) )
link_document_version_list = Link( link_document_version_list = Link(
icon='fa fa-code-fork', permissions=(permission_document_view,), icon='fa fa-code-fork', permissions=(permission_document_view,),
@@ -61,32 +61,32 @@ link_document_pages = Link(
link_document_clear_transformations = Link( link_document_clear_transformations = Link(
permissions=(permission_transformation_delete,), permissions=(permission_transformation_delete,),
text=_('Clear transformations'), text=_('Clear transformations'),
view='documents:document_clear_transformations', args='object.id' view='documents:document_clear_transformations', args='resolved_object.id'
) )
link_document_delete = Link( link_document_delete = Link(
permissions=(permission_document_delete,), tags='dangerous', permissions=(permission_document_delete,), tags='dangerous',
text=_('Delete'), view='documents:document_delete', args='object.id' text=_('Delete'), view='documents:document_delete', args='resolved_object.id'
) )
link_document_trash = Link( link_document_trash = Link(
permissions=(permission_document_trash,), tags='dangerous', permissions=(permission_document_trash,), tags='dangerous',
text=_('Move to trash'), view='documents:document_trash', args='object.id' text=_('Move to trash'), view='documents:document_trash', args='resolved_object.id'
) )
link_document_edit = Link( link_document_edit = Link(
permissions=(permission_document_properties_edit,), permissions=(permission_document_properties_edit,),
text=_('Edit properties'), view='documents:document_edit', text=_('Edit properties'), view='documents:document_edit',
args='object.id' args='resolved_object.id'
) )
link_document_document_type_edit = Link( link_document_document_type_edit = Link(
permissions=(permission_document_properties_edit,), text=_('Change type'), permissions=(permission_document_properties_edit,), text=_('Change type'),
view='documents:document_document_type_edit', args='object.id' view='documents:document_document_type_edit', args='resolved_object.id'
) )
link_document_download = Link( link_document_download = Link(
permissions=(permission_document_download,), text=_('Download'), permissions=(permission_document_download,), text=_('Download'),
view='documents:document_download', args='object.id' view='documents:document_download', args='resolved_object.id'
) )
link_document_print = Link( link_document_print = Link(
permissions=(permission_document_print,), text=_('Print'), permissions=(permission_document_print,), text=_('Print'),
view='documents:document_print', args='object.id' view='documents:document_print', args='resolved_object.id'
) )
link_document_update_page_count = Link( link_document_update_page_count = Link(
permissions=(permission_document_tools,), text=_('Recalculate page count'), permissions=(permission_document_tools,), text=_('Recalculate page count'),
@@ -125,7 +125,7 @@ link_document_multiple_restore = Link(
) )
link_document_version_download = Link( link_document_version_download = Link(
args='object.pk', permissions=(permission_document_download,), args='object.pk', permissions=(permission_document_download,),
text=_('Download'), view='documents:document_version_download' text=_('Download version'), view='documents:document_version_download'
) )
# Views # Views

View File

@@ -322,10 +322,6 @@ class Document(models.Model):
# Document has no version yet # Document has no version yet
return 0 return 0
@property
def signature_state(self):
return self.latest_version.signature_state
class DeletedDocument(Document): class DeletedDocument(Document):
objects = TrashCanManager() objects = TrashCanManager()