Skip to content
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

fix large file missing SSE-C key ID #423

Merged
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
* Fix missing key ID for large file encrypted with SSE-C
* Fix concatenating error message when message is None


## [1.24.0] - 2023-08-31

### Added
Expand Down
3 changes: 3 additions & 0 deletions b2sdk/raw_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,9 @@ def start_large_file(
)
kwargs['serverSideEncryption'] = server_side_encryption.serialize_to_json_for_request()

if server_side_encryption.mode == EncryptionMode.SSE_C:
file_info = server_side_encryption.add_key_id_to_file_info(file_info)

if legal_hold is not None:
kwargs['legalHold'] = legal_hold.to_server()

Expand Down
4 changes: 1 addition & 3 deletions b2sdk/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,7 @@ def _add_app_key_info_to_unauthorized(self, unauthorized):
key_messages.append('with no restrictions')

# Make a new message
new_message = unauthorized.message
if new_message == '':
new_message = 'unauthorized'
new_message = unauthorized.message or 'unauthorized'
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(unrelated) fix for the following error

  File "/Users/yeef/dev/b2/b2-sdk-python/b2sdk/session.py", line 462, in _wrap_token
    return self._reauthorization_loop(partial_callback)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/yeef/dev/b2/b2-sdk-python/b2sdk/session.py", line 486, in _reauthorization_loop
    raise self._add_app_key_info_to_unauthorized(e)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/yeef/dev/b2/b2-sdk-python/b2sdk/session.py", line 515, in _add_app_key_info_to_unauthorized
    new_message += ' for application key ' + ', '.join(key_messages)
TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str'

new_message += ' for application key ' + ', '.join(key_messages)

return Unauthorized(new_message, unauthorized.code)
Expand Down
41 changes: 41 additions & 0 deletions test/integration/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@

import io

from b2sdk.b2http import B2Http
from b2sdk.encryption.setting import EncryptionKey, EncryptionSetting
from b2sdk.encryption.types import EncryptionAlgorithm, EncryptionMode
from b2sdk.v2 import B2RawHTTPApi

from .base import IntegrationTestBase
from .fixtures import b2_auth_data # noqa
from .test_raw_api import authorize_raw_api


class TestUnboundStreamUpload(IntegrationTestBase):
Expand All @@ -38,3 +44,38 @@ def test_streamed_large_buffer_small_part_size(self):
data = b'a large data content' * 512 * 1024
# 5mb, the smallest allowed part size
self.assert_data_uploaded_via_stream(data, part_size=5 * 1024 * 1024)


class TestUploadLargeFile(IntegrationTestBase):
def test_ssec_key_id(self):
sse_c = EncryptionSetting(
mode=EncryptionMode.SSE_C,
algorithm=EncryptionAlgorithm.AES256,
key=EncryptionKey(secret=b'********************************', key_id='some-id'),
)

raw_api = B2RawHTTPApi(B2Http())

auth_dict = authorize_raw_api(raw_api)
account_auth_token = auth_dict['authorizationToken']
api_url = auth_dict['apiUrl']
bucket = self.create_bucket()

large_info = raw_api.start_large_file(
api_url,
account_auth_token,
bucket.id_,
'test_largefile_sse_c.txt',
'text/plain',
None,
server_side_encryption=sse_c,
)

assert large_info['fileInfo'] == {
'sse_c_key_id': sse_c.key.key_id,
}
assert large_info['serverSideEncryption'] == {
'algorithm': 'AES256',
'customerKeyMd5': 'SaaDheEjzuynJH8eW6AEpQ==',
'mode': 'SSE-C',
}