Skip to content

Commit ca98eb6

Browse files
authored
Merge pull request #348 from reef-technologies/fix-api-get-key
Fix `B2Api.get_key()` returning wrong key if `key_id` is incorrect
2 parents f2ad9dd + ad061c5 commit ca98eb6

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Fixed
1010
* Replace `ReplicationScanResult.source_has_sse_c_enabled` with `source_encryption_mode`
11+
* Fix `B2Api.get_key()` and `RawSimulator.delete_key()`
1112

1213
### Infrastructure
1314
* Fix nox's deprecated `session.install()` calls

b2sdk/api.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
######################################################################
1010

1111
from typing import Optional, Tuple, List, Generator
12+
from contextlib import suppress
1213

1314
from .account_info.abstract import AbstractAccountInfo
1415
from .api_config import B2HttpApiConfig, DEFAULT_HTTP_API_CONFIG
@@ -539,10 +540,13 @@ def get_key(self, key_id: str) -> Optional[ApplicationKey]:
539540
540541
Raises an exception if profile is not permitted to list keys.
541542
"""
542-
return next(
543-
self.list_keys(start_application_key_id=key_id),
544-
None,
545-
)
543+
with suppress(StopIteration):
544+
key = next(self.list_keys(start_application_key_id=key_id))
545+
546+
# list_keys() may return some other key if `key_id` does not exist;
547+
# thus manually check that we retrieved the right key
548+
if key.id_ == key_id:
549+
return key
546550

547551
# other
548552
def get_file_info(self, file_id: str) -> FileVersion:

b2sdk/raw_simulator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,9 @@ def delete_key(self, api_url, account_auth_token, application_key_id):
14001400
'application key does not exist: %s' % (application_key_id,),
14011401
'bad_request',
14021402
)
1403+
self.all_application_keys = [
1404+
key for key in self.all_application_keys if key.application_key_id != application_key_id
1405+
]
14031406
return key_sim.as_key()
14041407

14051408
def finish_large_file(self, api_url, account_auth_token, file_id, part_sha1_array):

b2sdk/v1/api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,7 @@ def create_key(
204204

205205
def delete_key(self, application_key_id):
206206
return super().delete_key_by_id(application_key_id).as_dict()
207+
208+
def get_key(self, key_id: str) -> Optional[dict]:
209+
keys = self.list_keys(start_application_key_id=key_id)['keys']
210+
return next((key for key in keys if key['applicationKeyId'] == key_id), None)

test/unit/api/test_api.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,22 @@ def test_list_keys_v2(self):
480480
'appKeyId9',
481481
]
482482
assert isinstance(keys[0], ApplicationKey)
483+
484+
def test_get_key(self):
485+
self._authorize_account()
486+
key = self.api.create_key(['readFiles'], 'testkey')
487+
488+
if apiver_deps.V <= 1:
489+
key_id = key['applicationKeyId']
490+
else:
491+
key_id = key.id_
492+
493+
assert self.api.get_key(key_id) is not None
494+
495+
if apiver_deps.V <= 1:
496+
self.api.delete_key(key_id)
497+
else:
498+
self.api.delete_key(key)
499+
500+
assert self.api.get_key(key_id) is None
501+
assert self.api.get_key('non-existent') is None

0 commit comments

Comments
 (0)