Skip to content

utils.keepass

KeePassSettings

Bases: ServiceSettings

Settings for the KeePass service.

This class provides configuration settings for accessing KeePass password databases. It supports authentication using both keyfiles and passwords, with automatic YAML configuration loading through the ServiceSettings base class.

Attributes:

Name Type Description
database Path

Path to the KeePass database file (.kdbx).

keyfile Optional[Path]

Path to the keyfile for database authentication. Can be None if using password-only authentication.

password Optional[str]

Master password for the database. Can be None if using keyfile-only authentication.

Example
# Using default settings
settings = KeePassSettings()

# Using custom settings
settings = KeePassSettings(
    database=Path("/path/to/database.kdbx"),
    keyfile=Path("/path/to/keyfile.key"),
    password="master_password"
)

settings_customise_sources classmethod

settings_customise_sources(
    settings_cls: Type[BaseSettings],
    init_settings: PydanticBaseSettingsSource,
    env_settings: PydanticBaseSettingsSource,
    dotenv_settings: PydanticBaseSettingsSource,
    file_secret_settings: PydanticBaseSettingsSource,
) -> Tuple[PydanticBaseSettingsSource, ...]

Customizes the settings sources to include the safe YAML settings source.

Parameters:

Name Type Description Default
settings_cls Type[BaseSettings]

The settings class.

required
init_settings PydanticBaseSettingsSource

The initial settings source.

required
env_settings PydanticBaseSettingsSource

The environment settings source.

required
dotenv_settings PydanticBaseSettingsSource

The dotenv settings source.

required
file_secret_settings PydanticBaseSettingsSource

The file secret settings source.

required

Returns:

Type Description
Tuple[PydanticBaseSettingsSource, ...]

A tuple of settings sources.

Source code in src\clabe\services.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
@classmethod
def settings_customise_sources(
    cls,
    settings_cls: t.Type[ps.BaseSettings],
    init_settings: ps.PydanticBaseSettingsSource,
    env_settings: ps.PydanticBaseSettingsSource,
    dotenv_settings: ps.PydanticBaseSettingsSource,
    file_secret_settings: ps.PydanticBaseSettingsSource,
) -> t.Tuple[ps.PydanticBaseSettingsSource, ...]:
    """
    Customizes the settings sources to include the safe YAML settings source.

    Args:
        settings_cls: The settings class.
        init_settings: The initial settings source.
        env_settings: The environment settings source.
        dotenv_settings: The dotenv settings source.
        file_secret_settings: The file secret settings source.

    Returns:
        A tuple of settings sources.
    """
    return (
        init_settings,
        _SafeYamlSettingsSource(settings_cls),
        env_settings,
        dotenv_settings,
        file_secret_settings,
    )

KeePass

KeePass(settings: KeePassSettings)

Bases: Service

KeePass password manager service for accessing password database entries.

This service provides a simple interface for connecting to and retrieving entries from KeePass password databases. It handles authentication using the provided settings and supports both keyfile and password-based authentication methods.

The service automatically establishes a connection to the database upon initialization and provides methods for retrieving password entries by title.

Attributes:

Name Type Description
_settings KeePassSettings

Configuration settings for the KeePass database.

_keepass PyKeePass

The underlying PyKeePass instance for database operations.

Example
# Create settings and service
settings = KeePassSettings(
    database=Path("/path/to/database.kdbx"),
    password="master_password"
)
keepass = KeePass(settings)

# Retrieve an entry
entry = keepass.get_entry("my_service_credentials")
username = entry.username
password = entry.password

Initialize the KeePass service with the provided settings.

Creates a connection to the KeePass database using the authentication credentials specified in the settings. The connection is established immediately upon initialization.

Parameters:

Name Type Description Default
settings KeePassSettings

Configuration settings containing database path and authentication credentials.

required

Raises:

Type Description
FileNotFoundError

If the database file cannot be found.

CredentialsError

If the provided authentication credentials are invalid.

IOError

If there's an error reading the database or keyfile.

Example
settings = KeePassSettings(database=Path("passwords.kdbx"))
keepass = KeePass(settings)
Source code in src\clabe\utils\keepass.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def __init__(self, settings: KeePassSettings):
    """
    Initialize the KeePass service with the provided settings.

    Creates a connection to the KeePass database using the authentication
    credentials specified in the settings. The connection is established
    immediately upon initialization.

    Args:
        settings (KeePassSettings): Configuration settings containing database
            path and authentication credentials.

    Raises:
        FileNotFoundError: If the database file cannot be found.
        CredentialsError: If the provided authentication credentials are invalid.
        IOError: If there's an error reading the database or keyfile.

    Example:
        ```python
        settings = KeePassSettings(database=Path("passwords.kdbx"))
        keepass = KeePass(settings)
        ```
    """
    self._settings = settings
    self._keepass = PyKeePass(
        filename=self._settings.database,
        password=self._settings.password,
        keyfile=self._settings.keyfile,
    )

get_entry

get_entry(title: str) -> Entry

Retrieve a password entry from the database by title.

Searches the KeePass database for entries matching the specified title and returns the first match found. Entry titles are typically unique within a database, but if multiple entries share the same title, only the first one encountered will be returned.

Parameters:

Name Type Description Default
title str

The title of the entry to retrieve. This should match the entry title exactly (case-sensitive).

required

Returns:

Name Type Description
Entry Entry

The KeePass entry object containing username, password, and other metadata associated with the specified title.

Raises:

Type Description
ValueError

If no entry is found with the specified title.

Example
# Retrieve credentials for a service
entry = keepass.get_entry("GitHub API Token")
token = entry.password

# Access other entry properties
username = entry.username
url = entry.url
notes = entry.notes
Source code in src\clabe\utils\keepass.py
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
def get_entry(self, title: str) -> Entry:
    """
    Retrieve a password entry from the database by title.

    Searches the KeePass database for entries matching the specified title
    and returns the first match found. Entry titles are typically unique
    within a database, but if multiple entries share the same title,
    only the first one encountered will be returned.

    Args:
        title (str): The title of the entry to retrieve. This should match
            the entry title exactly (case-sensitive).

    Returns:
        Entry: The KeePass entry object containing username, password, and
            other metadata associated with the specified title.

    Raises:
        ValueError: If no entry is found with the specified title.

    Example:
        ```python
        # Retrieve credentials for a service
        entry = keepass.get_entry("GitHub API Token")
        token = entry.password

        # Access other entry properties
        username = entry.username
        url = entry.url
        notes = entry.notes
        ```
    """
    entries = self._keepass.find_entries(title=title)
    if not entries:
        raise ValueError(f"No entry found with title '{title}'")
    else:
        return entries[0]