Wrapper library make it easier to create and modify Logstash Secrets keystores for secure settings
Project description
ls-keystore-utils
Wrapper library to make it easier to create and modify Logstash Secrets keystores for secure settings.
Problem Solved
Logstash keystores are encrypted PKCS#12 files used to securely store sensitive configuration values, such as passwords or API keys, for Logstash pipelines. Traditionally, managing these keystores requires manual use of the logstash-keystore CLI binary, which is slow because of the need to spin up a JVM, however small, and lacks programmatic integration. The LogstashKeystore class partly solves this by providing a Python-based interface for creating, reading, updating, and deleting keystore entries programmatically, while ensuring compatibility with Logstash's proprietary format.
How It Works
The LogstashKeystore class actually still leverages the official logstash-keystore binary (typically located at /usr/share/logstash/bin/logstash-keystore or best-guess auto-detected) for all write operations (adding, removing, or updating keys) to maintain full compatibility and security. For read operations, it uses custom cryptographic parsing via the cryptography and asn1crypto modules to efficiently retrieve key values without relying on the binary (which does not reveal key values at all). Unfortunately, these libraries do not appear to be able to write new keys to a logstash-keystore generated file. But it is still quite useful to be able to read the values and cache them in an AES-obfuscated memory store.
To reduce redundant calls to the logstash-keystore binary and minimize disk I/O, the class caches keystore data in memory, including obfuscated values and timestamps. Before each operation, it checks for external modifications by comparing cached timestamps with the keystore file's latest timestamp, and/or detecting additions, removals, or value changes. This caching mechanism ensures data consistency, prevents silent overwrites, and allows for efficient batch operations pass multiple keys in a single CLI call with serialized input. With value obfuscated value caching, it can reduce the need to overwrite existing values or execute a full Logstash pipeline just to see if the value is correct, or needs updating.
Installation
pip install -e .
or
pip install ls_keystore_utils
Usage
Instantiation
Create a new keystore or load an existing one. The class automatically detects the logstash-keystore binary path if not provided.
from ls_keystore_utils import LogstashKeystore
# Create a new keystore
ks = LogstashKeystore.create(
path_settings="/path/to/logstash/config",
password="mypassword"
)
# Load an existing keystore
ks = LogstashKeystore.load(
path_settings="/path/to/logstash/config",
password="mypassword"
)
Adding Keys
Add single or multiple keys. Batch mode is more efficient as it uses one CLI call for all keys.
# Add a single key
ks.add_key("MY_KEY", "secret_value")
# Add multiple keys in batch mode (must be dict of key/value pairs)
ks.add_key({"KEY1": "value1", "KEY2": "value2"})
Removing Keys
Remove single or multiple keys. Batch mode removes all keys in one CLI call.
# Remove a single key
ks.remove_key("MY_KEY")
# Remove multiple keys in batch mode
ks.remove_key(["KEY1", "KEY2"])
Updating Keys
Update existing keys (overwrites values). Same as adding; batch mode supported.
# Update a single key
ks.update_key("EXISTING_KEY", "new_value")
# Update multiple keys in batch mode
ks.update_key({"KEY1": "new_val1", "KEY2": "new_val2"})
Deleting Keys
Delete keys (alias for removing). Supports single and batch modes like removal.
# Delete a single key
ks.delete_key("MY_KEY")
# Delete multiple keys in batch mode
ks.delete_key(["KEY1", "KEY2"])
Compare value to keystore value
More information on the ObfuscatedValue class may be found in the code in crypto.py or in future documentation.
# Compare a known value to the value in the keystore
key = "MY_KEY"
secret = "my_secret_value"
assert secret == ks._current[key].obfuscated_value.deobfuscate()
Features
- Create/Update/Delete: Full CRUD operations via
logstash-keystorebinary with batch support for efficiency. - Read Operations: Retrieve individual or all key values using cached, parsed data.
- List Keys: Get a list of all key names in the keystore.
- Validation: Verify keystore integrity and detect external modifications.
- Backups: Create file backups of the keystore.
- Metadata: Track timestamps for change detection and age monitoring.
- Change Tracking: Automatically detects modifications and flags when Logstash restart is required.
- Caching: In-memory cache reduces redundant binary calls and ensures consistency.
Change Tracking
If a key has been updated with a new value, Logstash might need to be restarted to get that value since it is converted to an environment variable. If this is the case, then the LogstashKeystore class will set property needs_restart to True.
if ks.needs_restart is True:
# do stuff here
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ls_keystore_utils-0.1.2.tar.gz.
File metadata
- Download URL: ls_keystore_utils-0.1.2.tar.gz
- Upload date:
- Size: 20.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b5078dab7cb52b925bb695dd1538005f9743513419f7beab7e1aaaeb33241d1f
|
|
| MD5 |
160576f5da97e2580720babafb536fb3
|
|
| BLAKE2b-256 |
d5b8a53952da5a37c07b84aca49c6515608ceb9e949f4431301f6df70d2ad06e
|
Provenance
The following attestation bundles were made for ls_keystore_utils-0.1.2.tar.gz:
Publisher:
release.yml on untergeek/ls-keystore-utils
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ls_keystore_utils-0.1.2.tar.gz -
Subject digest:
b5078dab7cb52b925bb695dd1538005f9743513419f7beab7e1aaaeb33241d1f - Sigstore transparency entry: 1013287130
- Sigstore integration time:
-
Permalink:
untergeek/ls-keystore-utils@e01722799dc423651d95b7df9f2fb19632602e17 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/untergeek
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@e01722799dc423651d95b7df9f2fb19632602e17 -
Trigger Event:
release
-
Statement type:
File details
Details for the file ls_keystore_utils-0.1.2-py3-none-any.whl.
File metadata
- Download URL: ls_keystore_utils-0.1.2-py3-none-any.whl
- Upload date:
- Size: 24.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e7970f58be2d6540eee7e2a8aacc6bdadc25ed66f7bdceb332bc6807bff6e88
|
|
| MD5 |
26864c48e3c962d8cd61b439c6f7d195
|
|
| BLAKE2b-256 |
0360c1fc3f349df3097017770215817f59fb3a040e55d1c46ea314b55e8deedf
|
Provenance
The following attestation bundles were made for ls_keystore_utils-0.1.2-py3-none-any.whl:
Publisher:
release.yml on untergeek/ls-keystore-utils
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ls_keystore_utils-0.1.2-py3-none-any.whl -
Subject digest:
4e7970f58be2d6540eee7e2a8aacc6bdadc25ed66f7bdceb332bc6807bff6e88 - Sigstore transparency entry: 1013287154
- Sigstore integration time:
-
Permalink:
untergeek/ls-keystore-utils@e01722799dc423651d95b7df9f2fb19632602e17 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/untergeek
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@e01722799dc423651d95b7df9f2fb19632602e17 -
Trigger Event:
release
-
Statement type: