diff --git a/b2sdk/_internal/api.py b/b2sdk/_internal/api.py index 0588f446..8fa97547 100644 --- a/b2sdk/_internal/api.py +++ b/b2sdk/_internal/api.py @@ -265,7 +265,7 @@ def create_bucket( bucket = self.BUCKET_FACTORY_CLASS.from_api_bucket_dict(self, response) assert ( name == bucket.name - ), f'API created a bucket with different name than requested: {name} != {name}' + ), f'API created a bucket with different name than requested: {name} != {bucket.name}' assert ( bucket_type == bucket.type_ ), f'API created a bucket with different type than requested: {bucket_type} != {bucket.type_}' diff --git a/b2sdk/_internal/b2http.py b/b2sdk/_internal/b2http.py index b9d5efd5..4ea47747 100644 --- a/b2sdk/_internal/b2http.py +++ b/b2sdk/_internal/b2http.py @@ -591,16 +591,16 @@ def _translate_errors(cls, fcn, post_params=None): raise # pass through exceptions from just above except requests.ConnectionError as e0: - e1 = e0.args[0] + e1 = e0.args[0] if e0.args else None if isinstance(e1, requests.packages.urllib3.exceptions.MaxRetryError): - msg = e1.args[0] + msg = e1.args[0] if e1.args else '' if 'nodename nor servname provided, or not known' in msg: # Unknown host, or DNS failing. In the context of calling # B2, this means that something is down between here and # Backblaze, so we treat it like 503 Service Unavailable. raise UnknownHost() elif isinstance(e1, requests.packages.urllib3.exceptions.ProtocolError): - e2 = e1.args[1] + e2 = e1.args[1] if len(e1.args) > 1 else None if isinstance(e2, TimeoutError): raise B2RequestTimeout(str(e0)) diff --git a/b2sdk/_internal/bucket.py b/b2sdk/_internal/bucket.py index 72591115..e3b3d30d 100644 --- a/b2sdk/_internal/bucket.py +++ b/b2sdk/_internal/bucket.py @@ -910,7 +910,8 @@ def upload( :param str file_name: the file name of the new B2 file :param str,None content_type: the MIME type, or ``None`` to accept the default based on file extension of the B2 file name :param dict,None file_info: a file info to store with the file or ``None`` to not store anything - :param min_part_size: lower limit of part size for the transfer planner, in bytes + :param min_part_size: lower limit of part size for the transfer planner, in bytes; + the account's recommended part size is used when larger :param b2sdk.v2.AbstractProgressListener,None progress_listener: a progress listener object to use, or ``None`` to not report progress :param b2sdk.v2.EncryptionSetting encryption: encryption settings (``None`` if unknown) :param b2sdk.v2.FileRetentionSetting file_retention: file retention setting @@ -937,8 +938,7 @@ def upload( content_type=content_type, file_info=file_info, progress_listener=progress_listener, - # FIXME: Bucket.upload documents wrong logic - recommended_upload_part_size=min_part_size, + min_part_size=min_part_size, encryption=encryption, file_retention=file_retention, legal_hold=legal_hold, diff --git a/b2sdk/_internal/session.py b/b2sdk/_internal/session.py index 31f5461b..125df020 100644 --- a/b2sdk/_internal/session.py +++ b/b2sdk/_internal/session.py @@ -22,7 +22,7 @@ from b2sdk._internal.b2http import B2Http from b2sdk._internal.cache import AbstractCache, AuthInfoCache, DummyCache from b2sdk._internal.encryption.setting import EncryptionSetting -from b2sdk._internal.exception import InvalidAuthToken, Unauthorized +from b2sdk._internal.exception import InvalidAuthToken, RestrictedBucketMissing, Unauthorized from b2sdk._internal.file_lock import BucketRetentionSetting, FileRetentionSetting, LegalHold from b2sdk._internal.raw_api import ALL_CAPABILITIES, REALM_URLS, LifecycleRule from b2sdk._internal.replication.setting import ReplicationConfiguration @@ -123,6 +123,8 @@ def authorize_account(self, realm, application_key_id, application_key): allowed = self._construct_allowed_dict(storage_api_info) + self._validate_bucket_restrictions(allowed) + # Clear the cache if new account has been used if not self.account_info.is_same_account(account_id, realm): self.cache.clear() @@ -142,6 +144,14 @@ def authorize_account(self, realm, application_key_id, application_key): application_key_id=application_key_id, ) + @staticmethod + def _validate_bucket_restrictions(allowed): + # Reject deleted-bucket restrictions before changing credentials or caches. + if any(bucket['name'] is None for bucket in allowed.get('buckets') or []): + raise RestrictedBucketMissing + if allowed.get('bucketId') is not None and allowed.get('bucketName') is None: + raise RestrictedBucketMissing + def _construct_allowed_dict(self, storage_api_info): # `allowed` object has been deprecated in the v3 of the API, but we still # construct it artificially to avoid changes in all the reliant parts. diff --git a/b2sdk/_internal/types.py b/b2sdk/_internal/types.py index 5e3dbbd3..277f2143 100644 --- a/b2sdk/_internal/types.py +++ b/b2sdk/_internal/types.py @@ -32,7 +32,7 @@ try: import pydantic - if getattr(pydantic, '__version__', '') < '2': + if int(getattr(pydantic, '__version__', '0').split('.')[0] or '0') < 2: raise ImportError if sys.version_info < (3, 10): # https://github.com/pydantic/pydantic/issues/7873 diff --git a/b2sdk/v1/session.py b/b2sdk/v1/session.py index 075feab8..c7ae3fc8 100644 --- a/b2sdk/v1/session.py +++ b/b2sdk/v1/session.py @@ -60,6 +60,8 @@ def authorize_account(self, realm, application_key_id, application_key): 'namePrefix': storage_api_info['namePrefix'], } + self._validate_bucket_restrictions(allowed) + # Clear the cache if new account has been used if not self.account_info.is_same_account(account_id, realm): self.cache.clear() diff --git a/changelog.d/+c28-bucket-assert.fixed.md b/changelog.d/+c28-bucket-assert.fixed.md new file mode 100644 index 00000000..53e5aaa0 --- /dev/null +++ b/changelog.d/+c28-bucket-assert.fixed.md @@ -0,0 +1 @@ +Show the returned bucket name in the assertion message when bucket creation returns a different name. diff --git a/changelog.d/+c31-pydantic-version.fixed.md b/changelog.d/+c31-pydantic-version.fixed.md new file mode 100644 index 00000000..e163c2b3 --- /dev/null +++ b/changelog.d/+c31-pydantic-version.fixed.md @@ -0,0 +1 @@ +Compare the Pydantic major version numerically when detecting optional validation support. diff --git a/changelog.d/+c33-upload-part-size.fixed.md b/changelog.d/+c33-upload-part-size.fixed.md new file mode 100644 index 00000000..4564e363 --- /dev/null +++ b/changelog.d/+c33-upload-part-size.fixed.md @@ -0,0 +1 @@ +Treat Bucket.upload min_part_size as a lower limit while retaining the account recommended upload part size. diff --git a/changelog.d/+c51-http-error-args.fixed.md b/changelog.d/+c51-http-error-args.fixed.md new file mode 100644 index 00000000..1fc88389 --- /dev/null +++ b/changelog.d/+c51-http-error-args.fixed.md @@ -0,0 +1 @@ +Map connection errors with missing exception arguments to B2 connection errors without raising IndexError. diff --git a/changelog.d/+c55-auth-session.fixed.md b/changelog.d/+c55-auth-session.fixed.md new file mode 100644 index 00000000..d2c6f400 --- /dev/null +++ b/changelog.d/+c55-auth-session.fixed.md @@ -0,0 +1 @@ +Preserve existing credentials and bucket caches when authorization is rejected because a restricted bucket no longer exists. diff --git a/test/unit/bucket/test_regression_bucket.py b/test/unit/bucket/test_regression_bucket.py index 4df8b149..818ccb85 100644 --- a/test/unit/bucket/test_regression_bucket.py +++ b/test/unit/bucket/test_regression_bucket.py @@ -206,11 +206,6 @@ def test_c45_explicit_resume_requires_matching_timestamp(self): self.bucket.create_file(**kwargs) assert list(self.bucket.list_unfinished_large_files(prefix=name)) - @known_defect( - 'C33', - 'Bucket.upload passes min_part_size as the recommended part size instead of a lower limit', - raises=DefectStillPresent, - ) def test_c33_upload_min_part_size_is_only_a_lower_limit(self): """C33: min_part_size is a lower limit; parts must still follow the recommended part size. @@ -340,11 +335,64 @@ def fail_delete(*args): api.delete_bucket(bucket) assert api.cache.get_bucket_id_or_none_from_bucket_name(bucket.name) == bucket.id_ - @known_defect( - 'C28', - 'the create_bucket mismatch message prints the same name twice', - raises=AssertionError, - ) + @pytest.mark.parametrize('account_kind', ['fresh', 'same', 'different']) + @pytest.mark.parametrize('cache_kind', ['memory', 'account_info', 'sqlite']) + def test_c55_failed_authorization_preserves_session(self, account_kind, cache_kind, tmp_path): + from apiver_deps_exception import MissingAccountData, RestrictedBucketMissing + + from test.unit.test_base import create_key + + info = ( + SqliteAccountInfo(file_name=str(tmp_path / 'auth.sqlite')) + if cache_kind == 'sqlite' + else InMemoryAccountInfo() + ) + cache = InMemoryCache() if cache_kind == 'memory' else AuthInfoCache(info) + api, _ = self._api(account_info=info, cache=cache) + old_bucket = api.create_bucket('old-bucket', 'allPrivate') + old_token = info.get_account_auth_token() + old_key = info.get_application_key_id() + old_allowed = info.get_allowed() + if account_kind == 'different': + other = B2Api( + InMemoryAccountInfo(), api_config=B2HttpApiConfig(_raw_api_class=RawSimulator) + ) + other.session.raw_api = api.session.raw_api + account_id, master_key = other.session.raw_api.create_account() + other.authorize_account( + application_key_id=account_id, application_key=master_key, realm='production' + ) + else: + other = api + key = create_key( + other, + key_name='deleted-bucket', + capabilities=['listBuckets'], + bucket_id='missing-bucket', + ) + if account_kind == 'fresh': + info.clear() + cache.clear() + old_cache = cache.list_bucket_names_ids() + + with pytest.raises(RestrictedBucketMissing): + api.authorize_account( + application_key_id=key.id_, application_key=key.application_key, realm='production' + ) + + if account_kind == 'fresh': + with pytest.raises(MissingAccountData): + info.get_application_key_id() + assert not api.authorize_automatically() + else: + assert info.get_account_auth_token() == old_token + assert info.get_application_key_id() == old_key + assert info.get_allowed() == old_allowed + assert api.get_bucket_by_name(old_bucket.name).id_ == old_bucket.id_ + assert cache.list_bucket_names_ids() == old_cache + if cache_kind == 'sqlite': + info.close() + def test_c28_create_bucket_mismatch_message_names_both_buckets(self): """C28: the assertion message must show requested and returned names, not name twice.""" from unittest import mock diff --git a/test/unit/test_regression_misc.py b/test/unit/test_regression_misc.py index a05b5c4f..a0971a9a 100644 --- a/test/unit/test_regression_misc.py +++ b/test/unit/test_regression_misc.py @@ -27,7 +27,7 @@ from apiver_deps import B2Http, B2HttpApiConfig from apiver_deps_exception import B2ConnectionError, B2RequestTimeout -from test.known_defect import DefectStillPresent, defect_raises, expect, known_defect +from test.known_defect import expect pytestmark = [pytest.mark.apiver(from_ver=2)] @@ -39,23 +39,28 @@ def _b2_http(): class TestTypes: - @known_defect( - 'C31', - 'the pydantic version is compared as text, so "10.0" counts as older than "2"', - raises=AssertionError, + @pytest.mark.parametrize( + 'version, supported', + [ + ('1.10.20', False), + ('2.0.0', True), + ('2.0.0rc1', True), + ('10.0.0', True), + ('', False), + ], ) - def test_c31_pydantic_version_ten_counts_as_v2_or_newer(self, monkeypatch): + def test_c31_pydantic_version_is_compared_numerically(self, monkeypatch, version, supported): """C31: pydantic version comparison must be numeric ('10.0' is newer than '2').""" pydantic = pytest.importorskip('pydantic') types_module = importlib.import_module('b2sdk._internal.types') - monkeypatch.setattr(pydantic, '__version__', '10.0.0') + monkeypatch.setattr(pydantic, '__version__', version) try: importlib.reload(types_module) detected = types_module.pydantic finally: monkeypatch.undo() importlib.reload(types_module) - assert detected is not None, 'pydantic was not detected' + assert (detected is not None) == supported class TestHttpLayer: @@ -70,17 +75,21 @@ def test_c10_create_style_post_is_not_blindly_resent_after_timeout(self): http.post_json_return_json(url, {}, {'bucketName': 'b'}) expect(len(responses.calls) == 1, f'request was sent {len(responses.calls)} times') - @pytest.mark.parametrize('args', [()]) - @known_defect( - 'C51', - 'error mapping raises IndexError for a ConnectionError with no arguments', - raises=DefectStillPresent, + @pytest.mark.parametrize( + 'args', + [ + (), + (requests.packages.urllib3.exceptions.MaxRetryError(None, URL),), + (requests.packages.urllib3.exceptions.ProtocolError(),), + (requests.packages.urllib3.exceptions.ProtocolError('incomplete'),), + ], ) def test_c51_connection_error_with_missing_args_is_mapped_without_indexerror(self, args): """C51: ConnectionError carrying fewer than 2 args must map to a B2 error, not IndexError.""" from b2sdk._internal.b2http import B2Http as InternalB2Http + if args and isinstance(args[0], requests.packages.urllib3.exceptions.MaxRetryError): + args[0].args = () error = requests.ConnectionError(*args) - with defect_raises(IndexError, match='tuple index out of range'): - with pytest.raises(B2ConnectionError): - InternalB2Http._translate_errors(lambda: (_ for _ in ()).throw(error)) + with pytest.raises(B2ConnectionError): + InternalB2Http._translate_errors(lambda: (_ for _ in ()).throw(error)) diff --git a/test/unit/test_session.py b/test/unit/test_session.py index a19cb522..1b9d36cb 100644 --- a/test/unit/test_session.py +++ b/test/unit/test_session.py @@ -69,6 +69,22 @@ def test_simple_authorization(self, authorize_call_kwargs): assert self.b2_session.cache.clear.called is False self.b2_session.account_info.set_auth_data.assert_called_once_with(**authorize_call_kwargs) + def test_c55_deleted_bucket_does_not_replace_auth_data(self, apiver_int): + from apiver_deps_exception import RestrictedBucketMissing + + storage = self.b2_session.raw_api.authorize_account.return_value['apiInfo']['storageApi'] + if apiver_int >= 3: + storage['allowed']['buckets'] = [{'id': 'deleted', 'name': None}] + else: + storage.update(bucketId='deleted', bucketName=None) + self.b2_session.account_info.is_same_account.return_value = False + + with pytest.raises(RestrictedBucketMissing): + self.b2_session.authorize_account('dev', '123', '456') + + self.b2_session.account_info.set_auth_data.assert_not_called() + self.b2_session.cache.clear.assert_not_called() + def test_clear_cache(self): self.b2_session.account_info.is_same_account.return_value = False