Skip to content

Commit

Permalink
Merge pull request #117 from unbekanntes-pferd/bugfix/1.11.2
Browse files Browse the repository at this point in the history
add file key distribution to encrypted uploads
  • Loading branch information
unbekanntes-pferd authored Sep 2, 2023
2 parents 0a473e0 + 4ae1341 commit 425b09e
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 77 deletions.
2 changes: 1 addition & 1 deletion dracoon/config/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class GeneralSettingsInfo(BaseModel):
s3TagsEnabled: bool
homeRoomsActive: bool
homeRoomParentId: Optional[int]
subscriptionPlan: int
subscriptionPlan: Optional[int]

class SystemDefaults(BaseModel):
languageDefault: Optional[str]
Expand Down
55 changes: 54 additions & 1 deletion dracoon/nodes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import httpx
from tenacity import retry

from dracoon.crypto import FileEncryptionCipher, encrypt_bytes, encrypt_file_key, create_file_key
from dracoon.crypto import FileEncryptionCipher, decrypt_file_key, encrypt_bytes, encrypt_file_key, create_file_key, encrypt_file_key_public
from dracoon.crypto.models import FileKey, PlainUserKeyPairContainer, UserKeyPairContainer
from dracoon.groups.models import Expiration
from dracoon.client import DRACOONClient, OAuth2ConnectionType, RETRY_CONFIG
Expand All @@ -48,6 +48,7 @@
MIN_CHUNK_SIZE = 5242880
MAX_CHUNKS = 9999
POLL_WAIT = 0.1
FILE_KEY_LIMIT = 50

class DRACOONNodes:

Expand Down Expand Up @@ -477,6 +478,31 @@ async def upload_encrypted(self, file_path: str, upload_channel: CreateFileUploa
resolution_strategy=resolution_strategy, file_key=file_key)

node = await self.complete_upload(upload_channel=upload_channel, payload=complete_upload, raise_on_err=raise_on_err)

missing_keys = await self.get_missing_file_keys(file_id=node.id, limit=FILE_KEY_LIMIT)

if missing_keys.range.total > 0:
keys = self.make_set_file_keys(file_key_list=[])

for key in missing_keys.items:
# get file key
for file_item in missing_keys.files:
if key.fileId == file_item.id:
file_key = file_item.fileKeyContainer
plain_file_key = decrypt_file_key(file_key=file_key, keypair=plain_keypair)

# add requests per user
for user in missing_keys.users:
if key.userId == user.id:
public_key = user.publicKeyContainer

user_file_key = encrypt_file_key_public(plain_file_key=plain_file_key, public_key=public_key)

file_key_item = self.make_set_file_key_item(file_id=key.fileId, user_id=key.userId, file_key=user_file_key)

keys.items.append(file_key_item)

await self.set_file_keys(file_keys=keys)

return node

Expand Down Expand Up @@ -804,6 +830,33 @@ async def upload_s3_encrypted(self, file_path: str, upload_channel: CreateFileUp
while True:
upload_status = await self.check_s3_upload(upload_id=upload_channel.uploadId, raise_on_err=raise_on_err)
if upload_status.status == S3Status.done.value:
missing_keys = await self.get_missing_file_keys(file_id=upload_status.node.id, limit=FILE_KEY_LIMIT)

if missing_keys.range.total == 0:
break

keys = self.make_set_file_keys(file_key_list=[])

for key in missing_keys.items:
# get file key
for file_item in missing_keys.files:
if key.fileId == file_item.id:
file_key = file_item.fileKeyContainer
plain_file_key = decrypt_file_key(file_key=file_key, keypair=plain_keypair)

# add requests per user
for user in missing_keys.users:
if key.userId == user.id:
public_key = user.publicKeyContainer

user_file_key = encrypt_file_key_public(plain_file_key=plain_file_key, public_key=public_key)

file_key_item = self.make_set_file_key_item(file_id=key.fileId, user_id=key.userId, file_key=user_file_key)

keys.items.append(file_key_item)

await self.set_file_keys(file_keys=keys)

break
if upload_status.status == S3Status.error.value:
break
Expand Down
Loading

0 comments on commit 425b09e

Please sign in to comment.