Marco Ricci
Support passing expected SSH agent response codes
Marco Ricci commited 2bb23da at 2024-09-21 11:45:20
# SPDX-FileCopyrightText: 2024 Marco Ricci <software@the13thletter.info>
#
# SPDX-License-Identifier: MIT
"""A bare-bones SSH agent client supporting signing and key listing."""
from __future__ import annotations
import collections
import errno
import os
import socket
from typing import TYPE_CHECKING, overload
from typing_extensions import Self
from derivepassphrase import _types
if TYPE_CHECKING:
from collections.abc import Iterable, Sequence
from types import TracebackType
__all__ = ('SSHAgentClient',)
__author__ = 'Marco Ricci <software@the13thletter.info>'
# In SSH bytestrings, the "length" of the byte string is stored as
# a 4-byte/32-bit unsigned integer at the beginning.
HEAD_LEN = 4
_socket = socket
class TrailingDataError(RuntimeError):
"""The result contained trailing data."""
def __init__(self) -> None:
super().__init__('Overlong response from SSH agent')
class SSHAgentFailedError(RuntimeError):
"""The SSH agent failed to complete the requested operation."""
def __str__(self) -> str:
match self.args:
case (_types.SSH_AGENT.FAILURE.value, b''): # pragma: no branch
return 'The SSH agent failed to complete the request'
case (_, _msg) if _msg: # pragma: no cover
code = self.args[0]
msg = self.args[1].decode('utf-8', 'surrogateescape')
return f'[Code {code:d}] {msg:s}'
case _: # pragma: no cover
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX