Skip to content

Commit 38c5c5e

Browse files
committed
feat: allow using any keyring and use bytes instead of Unicode strings
1 parent b39f48a commit 38c5c5e

File tree

5 files changed

+228
-79
lines changed

5 files changed

+228
-79
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88

9+
## [0.2.0] - 2025-01-02
10+
11+
### Changed
12+
* BREAKING: allow using any keyring, not only session. Functions are renamed from get_session_* to get_*
13+
* BREAKING: store secrets as bytes instead of Unicode strings
14+
15+
916
## [0.1.0] - 2024-12-31
1017

1118
### Added

Cargo.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "python-linux-keyutils"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -9,6 +9,6 @@ name = "python_linux_keyutils"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
pyo3 = "0.22.0"
12+
pyo3 = "0.22.3"
1313
linux-keyutils = "0.2"
1414
zeroize = "1.8"

README.rst

+31-6
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ unsupported.
1212
Why?
1313
####
1414

15-
Existing `keyring https://pypi.org/project/keyring/` package is very powerful, but somewhat complex
15+
Existing `keyring <https://pypi.org/project/keyring/>`_ package is very powerful, but somewhat complex
1616
and heavy.
1717

18-
`keyctl https://pypi.org/project/keyctl/` uses subprocess instead of system call, which introduces
18+
`keyctl <https://pypi.org/project/keyctl/>`_ uses subprocess instead of system call, which introduces
1919
possible points of failure and requires keyctl utility.
2020

2121
This package uses rust and PyO3 to make system calls directly to the kernel.
@@ -26,12 +26,37 @@ Usage
2626

2727
Use following code snippet for inspiration::
2828

29-
import python_linux_keyutils
29+
from python_linux_keyutils import get_secret, set_secret, invalidate_secret, KeyRingIdentifier
3030

31-
python_linux_keyutils.set_session_secret("secret_name","secret_value")
32-
python_linux_keyutils.get_session_secret("secret_name")
33-
python_linux_keyutils.invalidate_session_secret("secret_name")
31+
# By default, Session keyring is used
32+
set_secret("test_key", b"test value")
33+
print(get_secret("test_key"))
34+
# b'test value'
3435

36+
# You can also specify a different keyring
37+
set_secret("test_key_2", b"\0\0\0", key_ring=KeyRingIdentifier.User)
38+
print(get_secret("test_key_2", key_ring=KeyRingIdentifier.User))
39+
# b'\x00\x00\x00'
40+
41+
# set_secret doesn't automatically create keyring if it doesn't exist, but this can be changed with
42+
# `create` keyword argument
43+
set_secret("test_key_3", b"Hello kernel secrets", key_ring=KeyRingIdentifier.Process)
44+
# Raises KeyError
45+
set_secret("test_key_3", b"Hello kernel secrets", key_ring=KeyRingIdentifier.Process, create=True)
46+
get_secret("test_key_3", key_ring=KeyRingIdentifier.Process)
47+
# b'Hello kernel secrets'
48+
49+
**********
50+
Exceptions
51+
**********
52+
53+
The module may raise following exceptions
54+
55+
- **OSError**: If system call fails due to access being denied, quota exceeded, bad address, write error, etc.
56+
- **ValueError**: If key name is invalid
57+
- **KeyError**: If key doesn't exist, or is expired, or keyring doesn't exist
58+
- **MemoryError**: If memory allocation fails
59+
- **RuntimeError**: If underlying rust library reports that operation is not supported
3560

3661
############
3762
Contributing

0 commit comments

Comments
 (0)