Skip to content

Commit 30dd528

Browse files
committed
Keyring fixes
1 parent ec6e847 commit 30dd528

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

key-value/key-value-aio/src/key_value/aio/stores/keyring/store.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Python keyring-based key-value store."""
22

3+
import os
4+
35
from key_value.shared.errors.key_value import ValueTooLargeError
46
from key_value.shared.utils.compound import compound_key
57
from key_value.shared.utils.managed_entry import ManagedEntry
@@ -18,6 +20,14 @@
1820

1921
DEFAULT_KEYCHAIN_SERVICE = "py-key-value"
2022

23+
24+
def is_value_too_large(value: bytes) -> bool:
25+
value_length = len(value)
26+
if os.name == "nt":
27+
return value_length > WINDOWS_MAX_VALUE_LENGTH
28+
return False
29+
30+
2131
WINDOWS_MAX_VALUE_LENGTH = 2560 # bytes
2232

2333
MAX_KEY_COLLECTION_LENGTH = 256
@@ -111,7 +121,7 @@ async def _put_managed_entry(self, *, key: str, collection: str, managed_entry:
111121
json_str: str = self._serialization_adapter.dump_json(entry=managed_entry, key=key, collection=collection)
112122
encoded_json_bytes: bytes = json_str.encode(encoding="utf-8")
113123

114-
if len(encoded_json_bytes) > WINDOWS_MAX_VALUE_LENGTH:
124+
if is_value_too_large(value=encoded_json_bytes):
115125
raise ValueTooLargeError(size=len(encoded_json_bytes), max_size=2560, collection=sanitized_collection, key=sanitized_key)
116126

117127
keyring.set_password(service_name=self._service_name, username=combo_key, password=json_str)

key-value/key-value-aio/tests/stores/keyring/test_keyring.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ async def test_get_large_put_get(self, store: BaseStore, data: dict[str, Any], j
3434

3535

3636
@pytest.mark.skipif(condition=not detect_on_macos(), reason="Keyrings do not support large values on MacOS")
37+
@pytest.mark.filterwarnings("ignore:A configured store is unstable and may change in a backwards incompatible way. Use at your own risk.")
3738
class TestMacOSKeychainStore(BaseTestKeychainStore):
3839
pass
3940

key-value/key-value-sync/src/key_value/sync/code_gen/stores/keyring/store.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# DO NOT CHANGE! Change the original file instead.
44
"""Python keyring-based key-value store."""
55

6+
import os
7+
68
from key_value.shared.errors.key_value import ValueTooLargeError
79
from key_value.shared.utils.compound import compound_key
810
from key_value.shared.utils.managed_entry import ManagedEntry
@@ -21,6 +23,14 @@
2123

2224
DEFAULT_KEYCHAIN_SERVICE = "py-key-value"
2325

26+
27+
def is_value_too_large(value: bytes) -> bool:
28+
value_length = len(value)
29+
if os.name == "nt":
30+
return value_length > WINDOWS_MAX_VALUE_LENGTH
31+
return False
32+
33+
2434
WINDOWS_MAX_VALUE_LENGTH = 2560 # bytes
2535

2636
MAX_KEY_COLLECTION_LENGTH = 256
@@ -110,7 +120,7 @@ def _put_managed_entry(self, *, key: str, collection: str, managed_entry: Manage
110120
json_str: str = self._serialization_adapter.dump_json(entry=managed_entry, key=key, collection=collection)
111121
encoded_json_bytes: bytes = json_str.encode(encoding="utf-8")
112122

113-
if len(encoded_json_bytes) > WINDOWS_MAX_VALUE_LENGTH:
123+
if is_value_too_large(value=encoded_json_bytes):
114124
raise ValueTooLargeError(size=len(encoded_json_bytes), max_size=2560, collection=sanitized_collection, key=sanitized_key)
115125

116126
keyring.set_password(service_name=self._service_name, username=combo_key, password=json_str)

key-value/key-value-sync/tests/code_gen/stores/keyring/test_keyring.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def test_get_large_put_get(self, store: BaseStore, data: dict[str, Any], json: s
4141

4242

4343
@pytest.mark.skipif(condition=not detect_on_macos(), reason="Keyrings do not support large values on MacOS")
44+
@pytest.mark.filterwarnings("ignore:A configured store is unstable and may change in a backwards incompatible way. Use at your own risk.")
4445
class TestMacOSKeychainStore(BaseTestKeychainStore):
4546
pass
4647

0 commit comments

Comments
 (0)