Host Keys API¶
spindlex.hostkeys ¶
SSH Host Key Module
Provides host key management including storage, verification policies, and key fingerprinting for secure host authentication.
Classes¶
AutoAddPolicy ¶
Bases: MissingHostKeyPolicy
Automatically add unknown host keys.
WARNING: This policy is insecure and should only be used in trusted environments or for testing purposes. It trusts every first-seen host key and disables MITM protection.
Source code in spindlex/hostkeys/policy.py
Methods:¶
__init__(accept_risk=False) ¶
Initialize auto-add policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
accept_risk | bool | Must be True to acknowledge security risks | False |
Source code in spindlex/hostkeys/policy.py
missing_host_key(client, hostname, key) ¶
Automatically accept and store unknown host key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client | Any | SSH client instance | required |
hostname | str | Server hostname | required |
key | Any | Server's host key | required |
Source code in spindlex/hostkeys/policy.py
HostKeyStorage ¶
Host key storage implementation.
Manages storage and retrieval of known host keys for host verification and security policy enforcement.
Source code in spindlex/hostkeys/storage.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 83 84 85 86 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 116 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
Methods:¶
__init__(filename=None) ¶
Initialize host key storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename | Optional[str] | Path to known_hosts file (optional) | None |
Source code in spindlex/hostkeys/storage.py
add(hostname, key) ¶
Add host key to storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hostname | str | Server hostname | required |
key | PKey | Host key to store | required |
Source code in spindlex/hostkeys/storage.py
copy_from(other) ¶
Merge all keys from another storage instance into this one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other | HostKeyStorage | Source storage whose keys are merged into self | required |
Source code in spindlex/hostkeys/storage.py
get(hostname, key_type=None) ¶
Get host key for hostname.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hostname | str | Server hostname | required |
key_type | Optional[str] | Optional algorithm name to filter by | None |
Returns:
| Type | Description |
|---|---|
Optional[PKey] | Host key if found, None otherwise |
Source code in spindlex/hostkeys/storage.py
get_all(hostname) ¶
Get all host keys for hostname.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hostname | str | Server hostname | required |
Returns:
| Type | Description |
|---|---|
list[PKey] | List of host keys |
load(filename=None) ¶
Load host keys from storage file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename | Optional[str] | Path to file (defaults to storage's filename) | None |
Raises:
| Type | Description |
|---|---|
SSHException | If loading fails |
Source code in spindlex/hostkeys/storage.py
remove(hostname, key=None) ¶
Remove host key(s) for hostname.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hostname | str | Server hostname | required |
key | Optional[PKey] | Specific key to remove (if None, removes all keys for hostname) | None |
Returns:
| Type | Description |
|---|---|
bool | True if any keys were removed |
Source code in spindlex/hostkeys/storage.py
save() ¶
Save host keys to storage file.
Raises:
| Type | Description |
|---|---|
SSHException | If saving fails |
Source code in spindlex/hostkeys/storage.py
MissingHostKeyPolicy ¶
Bases: ABC
Abstract base class for host key policies.
Defines the interface for handling unknown host keys during SSH connection establishment.
Source code in spindlex/hostkeys/policy.py
Methods:¶
missing_host_key(client, hostname, key) abstractmethod ¶
Handle unknown host key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client | Any | SSH client instance | required |
hostname | str | Server hostname | required |
key | Any | Server's host key | required |
Raises:
| Type | Description |
|---|---|
BadHostKeyException | If host key should be rejected |
Source code in spindlex/hostkeys/policy.py
RejectPolicy ¶
Bases: MissingHostKeyPolicy
Reject all unknown host keys.
This is the secure default policy that rejects any unknown host keys to prevent man-in-the-middle attacks.
Source code in spindlex/hostkeys/policy.py
Methods:¶
missing_host_key(client, hostname, key) ¶
Reject unknown host key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client | Any | SSH client instance | required |
hostname | str | Server hostname | required |
key | Any | Server's host key | required |
Raises:
| Type | Description |
|---|---|
BadHostKeyException | Always raised for unknown keys |
Source code in spindlex/hostkeys/policy.py
WarningPolicy ¶
Bases: MissingHostKeyPolicy
Warn and persist unknown host keys (Trust On First Use).
Logs a warning about unknown host keys and persists them to storage so subsequent connections are verified. This is TOFU behavior: the first connection is trusted unconditionally, with no MITM protection for that initial handshake. Use with caution in untrusted networks.
Source code in spindlex/hostkeys/policy.py
Methods:¶
__init__() ¶
missing_host_key(client, hostname, key) ¶
Log warning and accept unknown host key (TOFU - stores on first use).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client | Any | SSH client instance | required |
hostname | str | Server hostname | required |
key | Any | Server's host key | required |