libvirtpy 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- libvirtpy/__init__.py +5 -0
- libvirtpy/client/__init__.py +54 -0
- libvirtpy/client/_bases.py +326 -0
- libvirtpy/client/_runner.py +57 -0
- libvirtpy/client/aio.py +5922 -0
- libvirtpy/client/client.py +5695 -0
- libvirtpy/client/operations/__init__.py +631 -0
- libvirtpy/client/operations/_common.py +495 -0
- libvirtpy/client/operations/checkpoint.py +196 -0
- libvirtpy/client/operations/connection.py +93 -0
- libvirtpy/client/operations/connection_extra.py +256 -0
- libvirtpy/client/operations/domain.py +306 -0
- libvirtpy/client/operations/domain_agent.py +115 -0
- libvirtpy/client/operations/domain_block.py +368 -0
- libvirtpy/client/operations/domain_info.py +400 -0
- libvirtpy/client/operations/domain_lifecycle.py +338 -0
- libvirtpy/client/operations/domain_migration.py +164 -0
- libvirtpy/client/operations/domain_network.py +113 -0
- libvirtpy/client/operations/domain_tuning.py +494 -0
- libvirtpy/client/operations/host.py +246 -0
- libvirtpy/client/operations/host_interface.py +100 -0
- libvirtpy/client/operations/interface_extra.py +45 -0
- libvirtpy/client/operations/network.py +135 -0
- libvirtpy/client/operations/network_info.py +155 -0
- libvirtpy/client/operations/network_port.py +142 -0
- libvirtpy/client/operations/node_device.py +76 -0
- libvirtpy/client/operations/node_device_info.py +189 -0
- libvirtpy/client/operations/nwfilter.py +67 -0
- libvirtpy/client/operations/nwfilter_binding.py +74 -0
- libvirtpy/client/operations/secret.py +93 -0
- libvirtpy/client/operations/secret_extra.py +24 -0
- libvirtpy/client/operations/snapshot.py +100 -0
- libvirtpy/client/operations/snapshot_tree.py +114 -0
- libvirtpy/client/operations/storage.py +267 -0
- libvirtpy/client/operations/storage_info.py +188 -0
- libvirtpy/client/operations/streams.py +201 -0
- libvirtpy/client/references.py +249 -0
- libvirtpy/client/subscribing.py +88 -0
- libvirtpy/client/testing/__init__.py +24 -0
- libvirtpy/client/testing/_common.py +167 -0
- libvirtpy/client/testing/_domains.py +294 -0
- libvirtpy/client/testing/_objects.py +205 -0
- libvirtpy/client/testing/_state.py +246 -0
- libvirtpy/client/testing/_storage.py +171 -0
- libvirtpy/client/testing/async_transport.py +30 -0
- libvirtpy/client/testing/fake.py +81 -0
- libvirtpy/client/transport.py +87 -0
- libvirtpy/client/types.py +59 -0
- libvirtpy/client/wire.py +436 -0
- libvirtpy/client/wire_aio.py +348 -0
- libvirtpy/errors.py +7 -0
- libvirtpy/py.typed +0 -0
- libvirtpy/rpc/__init__.py +1 -0
- libvirtpy/rpc/aio.py +387 -0
- libvirtpy/rpc/cpumap.py +48 -0
- libvirtpy/rpc/errors.py +186 -0
- libvirtpy/rpc/events.py +259 -0
- libvirtpy/rpc/options.py +66 -0
- libvirtpy/rpc/procedure.py +27 -0
- libvirtpy/rpc/protocol/__init__.py +0 -0
- libvirtpy/rpc/protocol/procedures.py +5163 -0
- libvirtpy/rpc/protocol/typed_hints.py +210 -0
- libvirtpy/rpc/protocol/types.py +9299 -0
- libvirtpy/rpc/remote.py +295 -0
- libvirtpy/rpc/remote_aio.py +106 -0
- libvirtpy/rpc/sasl.py +467 -0
- libvirtpy/rpc/session.py +424 -0
- libvirtpy/rpc/sockets.py +251 -0
- libvirtpy/rpc/stream.py +323 -0
- libvirtpy/rpc/subscription.py +451 -0
- libvirtpy/rpc/sync.py +460 -0
- libvirtpy/rpc/typed_params.py +97 -0
- libvirtpy/rpc/uri.py +85 -0
- libvirtpy/rpc/xdr.py +552 -0
- libvirtpy/xml/__init__.py +49 -0
- libvirtpy/xml/_base.py +145 -0
- libvirtpy/xml/_shared/__init__.py +1 -0
- libvirtpy/xml/_shared/metadata.py +67 -0
- libvirtpy/xml/_shared/network.py +108 -0
- libvirtpy/xml/_shared/storage.py +131 -0
- libvirtpy/xml/_shared/sysinfo.py +83 -0
- libvirtpy/xml/_types.py +197 -0
- libvirtpy/xml/_xml_element.py +134 -0
- libvirtpy/xml/backup/__init__.py +25 -0
- libvirtpy/xml/backup/backup.py +89 -0
- libvirtpy/xml/backup/checkpoint.py +48 -0
- libvirtpy/xml/capabilities/__init__.py +43 -0
- libvirtpy/xml/capabilities/domain.py +436 -0
- libvirtpy/xml/capabilities/host.py +413 -0
- libvirtpy/xml/capabilities/storagepool.py +59 -0
- libvirtpy/xml/domain/__init__.py +493 -0
- libvirtpy/xml/domain/blkiotune.py +81 -0
- libvirtpy/xml/domain/clock.py +79 -0
- libvirtpy/xml/domain/cpu.py +265 -0
- libvirtpy/xml/domain/devices/__init__.py +495 -0
- libvirtpy/xml/domain/devices/address.py +152 -0
- libvirtpy/xml/domain/devices/audio.py +204 -0
- libvirtpy/xml/domain/devices/chardev.py +197 -0
- libvirtpy/xml/domain/devices/common.py +75 -0
- libvirtpy/xml/domain/devices/controller.py +262 -0
- libvirtpy/xml/domain/devices/devices.py +79 -0
- libvirtpy/xml/domain/devices/disk.py +471 -0
- libvirtpy/xml/domain/devices/filesystem.py +206 -0
- libvirtpy/xml/domain/devices/graphics.py +245 -0
- libvirtpy/xml/domain/devices/hostdev.py +221 -0
- libvirtpy/xml/domain/devices/input.py +89 -0
- libvirtpy/xml/domain/devices/interface.py +459 -0
- libvirtpy/xml/domain/devices/iommu.py +54 -0
- libvirtpy/xml/domain/devices/memballoon.py +42 -0
- libvirtpy/xml/domain/devices/memory_device.py +101 -0
- libvirtpy/xml/domain/devices/misc.py +169 -0
- libvirtpy/xml/domain/devices/nvram.py +20 -0
- libvirtpy/xml/domain/devices/shmem.py +46 -0
- libvirtpy/xml/domain/devices/sound.py +42 -0
- libvirtpy/xml/domain/devices/tpm.py +103 -0
- libvirtpy/xml/domain/devices/usb.py +86 -0
- libvirtpy/xml/domain/devices/video.py +101 -0
- libvirtpy/xml/domain/domain.py +157 -0
- libvirtpy/xml/domain/features.py +312 -0
- libvirtpy/xml/domain/iothread.py +41 -0
- libvirtpy/xml/domain/launch_security.py +82 -0
- libvirtpy/xml/domain/memory.py +149 -0
- libvirtpy/xml/domain/misc.py +113 -0
- libvirtpy/xml/domain/namespaces.py +96 -0
- libvirtpy/xml/domain/numa.py +140 -0
- libvirtpy/xml/domain/os.py +267 -0
- libvirtpy/xml/domain/qemu.py +102 -0
- libvirtpy/xml/domain/seclabel.py +29 -0
- libvirtpy/xml/domain/storage_source.py +466 -0
- libvirtpy/xml/domain/sysinfo.py +34 -0
- libvirtpy/xml/interface/__init__.py +17 -0
- libvirtpy/xml/interface/interface.py +153 -0
- libvirtpy/xml/network/__init__.py +101 -0
- libvirtpy/xml/network/network.py +311 -0
- libvirtpy/xml/network/port.py +84 -0
- libvirtpy/xml/nodedev/__init__.py +13 -0
- libvirtpy/xml/nodedev/nodedev.py +490 -0
- libvirtpy/xml/nwfilter/__init__.py +81 -0
- libvirtpy/xml/nwfilter/binding.py +51 -0
- libvirtpy/xml/nwfilter/filter.py +92 -0
- libvirtpy/xml/nwfilter/rules.py +534 -0
- libvirtpy/xml/secret/__init__.py +11 -0
- libvirtpy/xml/secret/secret.py +36 -0
- libvirtpy/xml/snapshot/__init__.py +29 -0
- libvirtpy/xml/snapshot/snapshot.py +136 -0
- libvirtpy/xml/storage/__init__.py +75 -0
- libvirtpy/xml/storage/pool.py +219 -0
- libvirtpy/xml/storage/volume.py +102 -0
- libvirtpy/xml/sysinfo.py +40 -0
- libvirtpy-0.1.0.dist-info/METADATA +109 -0
- libvirtpy-0.1.0.dist-info/RECORD +154 -0
- libvirtpy-0.1.0.dist-info/WHEEL +4 -0
- libvirtpy-0.1.0.dist-info/licenses/LICENSE +501 -0
- libvirtpy-0.1.0.dist-info/licenses/NOTICE +23 -0
libvirtpy/rpc/remote.py
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
"""Connecting to a libvirt daemon on another host: SSH, TLS or plain TCP.
|
|
2
|
+
|
|
3
|
+
The pure parts (the ``ssh`` command line, the TLS settings) are separate
|
|
4
|
+
functions so that ``remote_aio`` shares them.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import contextlib
|
|
8
|
+
import os
|
|
9
|
+
import shlex
|
|
10
|
+
import socket
|
|
11
|
+
import ssl
|
|
12
|
+
import subprocess
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
from libvirtpy.rpc.options import ConnectionOptions
|
|
16
|
+
from libvirtpy.rpc.session import ConnectionLostError, ProtocolError
|
|
17
|
+
from libvirtpy.rpc.sync import SocketStream, Stream, SyncRpc
|
|
18
|
+
from libvirtpy.rpc.uri import ConnectionTarget
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"ProcessStream",
|
|
22
|
+
"TlsFiles",
|
|
23
|
+
"connect_remote",
|
|
24
|
+
"ssh_command",
|
|
25
|
+
"tls_context",
|
|
26
|
+
"tls_files",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
CONNECT_TIMEOUT_SECONDS = 30
|
|
30
|
+
DEFAULT_REMOTE_SOCKET = "/var/run/libvirt/libvirt-sock"
|
|
31
|
+
TLS_ACCEPTED = b"\x01"
|
|
32
|
+
"""The byte a server sends when it has accepted the client certificate."""
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class TlsFiles:
|
|
36
|
+
"""The certificate files of a TLS connection."""
|
|
37
|
+
|
|
38
|
+
def __init__(self, ca: Path, certificate: Path, key: Path) -> None:
|
|
39
|
+
"""Remember the three paths.
|
|
40
|
+
|
|
41
|
+
:param ca: the certificate authority that signed the server certificate.
|
|
42
|
+
:param certificate: our client certificate.
|
|
43
|
+
:param key: the private key of the client certificate.
|
|
44
|
+
"""
|
|
45
|
+
self.ca = ca
|
|
46
|
+
self.certificate = certificate
|
|
47
|
+
self.key = key
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class ProcessStream:
|
|
51
|
+
"""A ``Stream`` over the standard input and output of a child process."""
|
|
52
|
+
|
|
53
|
+
def __init__(self, process: subprocess.Popen[bytes]) -> None:
|
|
54
|
+
"""Wrap a process started with both pipes.
|
|
55
|
+
|
|
56
|
+
:param process: the process, e.g. ``ssh host virt-ssh-helper``.
|
|
57
|
+
"""
|
|
58
|
+
self._process = process
|
|
59
|
+
|
|
60
|
+
def send(self, data: bytes) -> None:
|
|
61
|
+
"""Send all of ``data`` to the process.
|
|
62
|
+
|
|
63
|
+
:param data: the bytes.
|
|
64
|
+
:raises ProtocolError: if the process has gone away.
|
|
65
|
+
"""
|
|
66
|
+
stdin = self._process.stdin
|
|
67
|
+
if stdin is None:
|
|
68
|
+
raise ProtocolError("the process has no standard input")
|
|
69
|
+
try:
|
|
70
|
+
stdin.write(data)
|
|
71
|
+
stdin.flush()
|
|
72
|
+
except BrokenPipeError as error:
|
|
73
|
+
raise ConnectionLostError(
|
|
74
|
+
"the ssh process closed the connection"
|
|
75
|
+
) from error
|
|
76
|
+
|
|
77
|
+
def receive(self) -> bytes:
|
|
78
|
+
"""Wait for bytes from the process.
|
|
79
|
+
|
|
80
|
+
:returns: some bytes, or nothing at its end.
|
|
81
|
+
:raises ProtocolError: if the process has no standard output.
|
|
82
|
+
"""
|
|
83
|
+
stdout = self._process.stdout
|
|
84
|
+
if stdout is None:
|
|
85
|
+
raise ProtocolError("the process has no standard output")
|
|
86
|
+
return stdout.read1(65536) # type: ignore[attr-defined,no-any-return] # BufferedReader has read1; Popen types it as IO[bytes]
|
|
87
|
+
|
|
88
|
+
def close(self) -> None:
|
|
89
|
+
"""End the process; a thread reading its output sees the end of the file."""
|
|
90
|
+
self._process.terminate()
|
|
91
|
+
self._process.wait()
|
|
92
|
+
for pipe in (self._process.stdin, self._process.stdout):
|
|
93
|
+
if pipe is not None:
|
|
94
|
+
with contextlib.suppress(OSError, ValueError):
|
|
95
|
+
pipe.close()
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def connect_remote(
|
|
99
|
+
target: ConnectionTarget, options: ConnectionOptions | None = None
|
|
100
|
+
) -> SyncRpc:
|
|
101
|
+
"""Open a connection to the daemon of a remote target.
|
|
102
|
+
|
|
103
|
+
:param target: a target with ``ssh``, ``tls`` or ``tcp`` transport.
|
|
104
|
+
:param options: the call timeout and the keepalive.
|
|
105
|
+
:returns: the connection, before authentication.
|
|
106
|
+
:raises ConnectionError: if the host cannot be reached.
|
|
107
|
+
:raises ProtocolError: if a TLS server rejects our certificate.
|
|
108
|
+
"""
|
|
109
|
+
if target.transport == "ssh":
|
|
110
|
+
return SyncRpc(_start_ssh(target), options)
|
|
111
|
+
return SyncRpc(_connect_tcp(target), options)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def ssh_command(target: ConnectionTarget) -> list[str]:
|
|
115
|
+
"""Build the ``ssh`` command that reaches the daemon of a host.
|
|
116
|
+
|
|
117
|
+
URI parameters, as in libvirt: ``command`` (the ssh program), ``keyfile``,
|
|
118
|
+
``proxy`` (``auto``, ``native`` for ``virt-ssh-helper``, ``netcat``),
|
|
119
|
+
``socket`` and ``netcat`` (for the netcat proxy), ``no_tty``,
|
|
120
|
+
``known_hosts_verify`` (``ignore``).
|
|
121
|
+
|
|
122
|
+
:param target: a target with the ``ssh`` transport.
|
|
123
|
+
:returns: the program and its arguments; the last one is the command that the
|
|
124
|
+
remote shell runs.
|
|
125
|
+
:raises ValueError: if ``proxy`` is not one of the known values.
|
|
126
|
+
"""
|
|
127
|
+
parameters = target.parameters
|
|
128
|
+
command = [parameters.get("command", "ssh"), "-T", "-e", "none"]
|
|
129
|
+
|
|
130
|
+
if target.port is not None:
|
|
131
|
+
command += ["-p", str(target.port)]
|
|
132
|
+
if parameters.get("keyfile"):
|
|
133
|
+
command += ["-i", parameters["keyfile"]]
|
|
134
|
+
if parameters.get("no_tty") == "1":
|
|
135
|
+
command += ["-o", "BatchMode=yes"]
|
|
136
|
+
if parameters.get("known_hosts_verify") == "ignore":
|
|
137
|
+
command += ["-o", "StrictHostKeyChecking=no"]
|
|
138
|
+
|
|
139
|
+
destination = f"{target.user}@{target.host}" if target.user else str(target.host)
|
|
140
|
+
# ssh joins its arguments with spaces and the remote login shell parses the
|
|
141
|
+
# result again, so the script must reach it as one quoted word
|
|
142
|
+
remote_command = f"sh -c {shlex.quote(_remote_script(target))}"
|
|
143
|
+
return [*command, destination, remote_command]
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def tls_files(target: ConnectionTarget) -> TlsFiles:
|
|
147
|
+
"""Find the certificate files, as libvirt does.
|
|
148
|
+
|
|
149
|
+
``pkipath`` names one directory with ``cacert.pem``, ``clientcert.pem`` and
|
|
150
|
+
``clientkey.pem``; ``~`` and ``$VARIABLES`` in it are expanded, so a quoted
|
|
151
|
+
``?pkipath=$HOME/.pki/x`` works. Otherwise root uses ``/etc/pki`` and others
|
|
152
|
+
``~/.pki/libvirt``.
|
|
153
|
+
|
|
154
|
+
:param target: a target with the ``tls`` transport.
|
|
155
|
+
:returns: the three paths.
|
|
156
|
+
"""
|
|
157
|
+
pki_path = target.parameters.get("pkipath")
|
|
158
|
+
if pki_path:
|
|
159
|
+
directory = Path(os.path.expandvars(pki_path)).expanduser()
|
|
160
|
+
return TlsFiles(
|
|
161
|
+
directory / "cacert.pem",
|
|
162
|
+
directory / "clientcert.pem",
|
|
163
|
+
directory / "clientkey.pem",
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
if os.geteuid() == 0:
|
|
167
|
+
return TlsFiles(
|
|
168
|
+
Path("/etc/pki/CA/cacert.pem"),
|
|
169
|
+
Path("/etc/pki/libvirt/clientcert.pem"),
|
|
170
|
+
Path("/etc/pki/libvirt/private/clientkey.pem"),
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
home = Path.home() / ".pki" / "libvirt"
|
|
174
|
+
return TlsFiles(
|
|
175
|
+
home / "cacert.pem", home / "clientcert.pem", home / "clientkey.pem"
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def tls_context(target: ConnectionTarget) -> ssl.SSLContext:
|
|
180
|
+
"""Build the TLS settings of a connection.
|
|
181
|
+
|
|
182
|
+
``no_verify=1`` accepts any server certificate, like libvirt. Without
|
|
183
|
+
``pkipath``, a missing CA file falls back to the system's trusted CAs; with
|
|
184
|
+
it, a missing CA file is an error, because the user named the directory.
|
|
185
|
+
|
|
186
|
+
:param target: a target with the ``tls`` transport.
|
|
187
|
+
:returns: the client context, with our certificate loaded if the files exist.
|
|
188
|
+
:raises ConnectionError: if ``pkipath`` is given and has no ``cacert.pem``.
|
|
189
|
+
"""
|
|
190
|
+
files = tls_files(target)
|
|
191
|
+
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
|
192
|
+
|
|
193
|
+
if target.parameters.get("no_verify") == "1":
|
|
194
|
+
context.check_hostname = False
|
|
195
|
+
context.verify_mode = ssl.CERT_NONE
|
|
196
|
+
elif files.ca.exists():
|
|
197
|
+
context.load_verify_locations(cafile=str(files.ca))
|
|
198
|
+
elif target.parameters.get("pkipath"):
|
|
199
|
+
raise ConnectionError(f"pkipath: no CA certificate at {files.ca}")
|
|
200
|
+
else:
|
|
201
|
+
context.load_default_certs()
|
|
202
|
+
|
|
203
|
+
if files.certificate.exists() and files.key.exists():
|
|
204
|
+
context.load_cert_chain(str(files.certificate), str(files.key))
|
|
205
|
+
return context
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def _remote_script(target: ConnectionTarget) -> str:
|
|
209
|
+
"""Build the shell script run on the remote host.
|
|
210
|
+
|
|
211
|
+
:param target: a target with the ``ssh`` transport.
|
|
212
|
+
:returns: a script that connects its standard input and output to the daemon.
|
|
213
|
+
:raises ValueError: if ``proxy`` is unknown.
|
|
214
|
+
"""
|
|
215
|
+
parameters = target.parameters
|
|
216
|
+
helper = f"exec virt-ssh-helper {shlex.quote(target.open_name)}"
|
|
217
|
+
netcat = "exec {} -U {}".format(
|
|
218
|
+
shlex.quote(parameters.get("netcat", "nc")),
|
|
219
|
+
shlex.quote(parameters.get("socket", DEFAULT_REMOTE_SOCKET)),
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
proxy = parameters.get("proxy", "auto")
|
|
223
|
+
if proxy == "native":
|
|
224
|
+
return helper
|
|
225
|
+
if proxy == "netcat":
|
|
226
|
+
return netcat
|
|
227
|
+
if proxy == "auto":
|
|
228
|
+
return (
|
|
229
|
+
f"if type virt-ssh-helper >/dev/null 2>&1; then {helper}; else {netcat}; fi"
|
|
230
|
+
)
|
|
231
|
+
raise ValueError(f"unknown proxy {proxy!r}; use auto, native or netcat")
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def _start_ssh(target: ConnectionTarget) -> Stream:
|
|
235
|
+
"""Start ``ssh`` and wrap its pipes.
|
|
236
|
+
|
|
237
|
+
:param target: a target with the ``ssh`` transport.
|
|
238
|
+
:returns: the stream.
|
|
239
|
+
:raises ConnectionError: if the ssh program cannot be started.
|
|
240
|
+
"""
|
|
241
|
+
try:
|
|
242
|
+
process = subprocess.Popen(
|
|
243
|
+
ssh_command(target), stdin=subprocess.PIPE, stdout=subprocess.PIPE
|
|
244
|
+
)
|
|
245
|
+
except OSError as error:
|
|
246
|
+
raise ConnectionError(f"cannot start ssh: {error}") from error
|
|
247
|
+
return ProcessStream(process)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def _connect_tcp(target: ConnectionTarget) -> Stream:
|
|
251
|
+
"""Connect to a TCP port, with TLS if the transport asks for it.
|
|
252
|
+
|
|
253
|
+
:param target: a target with the ``tls`` or ``tcp`` transport.
|
|
254
|
+
:returns: the stream.
|
|
255
|
+
:raises ConnectionError: if the host cannot be reached.
|
|
256
|
+
:raises ProtocolError: if a TLS server does not accept our certificate.
|
|
257
|
+
"""
|
|
258
|
+
try:
|
|
259
|
+
connection = socket.create_connection(
|
|
260
|
+
(str(target.host), int(target.port or 0)), CONNECT_TIMEOUT_SECONDS
|
|
261
|
+
)
|
|
262
|
+
except OSError as error:
|
|
263
|
+
raise ConnectionError(
|
|
264
|
+
f"cannot reach {target.host}:{target.port}: {error}"
|
|
265
|
+
) from error
|
|
266
|
+
|
|
267
|
+
connection.settimeout(None)
|
|
268
|
+
if target.transport != "tls":
|
|
269
|
+
return SocketStream(connection)
|
|
270
|
+
return _tls_stream(target, connection)
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def _tls_stream(target: ConnectionTarget, connection: socket.socket) -> Stream:
|
|
274
|
+
"""Do the TLS handshake and the libvirt acceptance byte.
|
|
275
|
+
|
|
276
|
+
:param target: a target with the ``tls`` transport.
|
|
277
|
+
:param connection: the connected TCP socket.
|
|
278
|
+
:returns: the stream over TLS.
|
|
279
|
+
:raises ConnectionError: if the handshake fails.
|
|
280
|
+
:raises ProtocolError: if the server does not accept our certificate.
|
|
281
|
+
"""
|
|
282
|
+
secure: ssl.SSLSocket | None = None
|
|
283
|
+
try:
|
|
284
|
+
secure = tls_context(target).wrap_socket(
|
|
285
|
+
connection, server_hostname=target.host
|
|
286
|
+
)
|
|
287
|
+
accepted = secure.recv(1)
|
|
288
|
+
except (ssl.SSLError, OSError) as error:
|
|
289
|
+
(secure or connection).close()
|
|
290
|
+
raise ConnectionError(f"TLS to {target.host} failed: {error}") from error
|
|
291
|
+
|
|
292
|
+
if accepted != TLS_ACCEPTED:
|
|
293
|
+
secure.close()
|
|
294
|
+
raise ProtocolError("the server did not accept our client certificate")
|
|
295
|
+
return SocketStream(secure)
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""The asyncio twin of ``remote.py``: connecting to a daemon on another host."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import ssl
|
|
5
|
+
from asyncio.subprocess import Process
|
|
6
|
+
|
|
7
|
+
from libvirtpy.rpc.aio import AsyncRpc
|
|
8
|
+
from libvirtpy.rpc.options import ConnectionOptions
|
|
9
|
+
from libvirtpy.rpc.remote import (
|
|
10
|
+
CONNECT_TIMEOUT_SECONDS,
|
|
11
|
+
TLS_ACCEPTED,
|
|
12
|
+
ssh_command,
|
|
13
|
+
tls_context,
|
|
14
|
+
)
|
|
15
|
+
from libvirtpy.rpc.session import ProtocolError
|
|
16
|
+
from libvirtpy.rpc.uri import ConnectionTarget
|
|
17
|
+
|
|
18
|
+
__all__ = ["connect_remote_async"]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
async def connect_remote_async(
|
|
22
|
+
target: ConnectionTarget, options: ConnectionOptions | None = None
|
|
23
|
+
) -> AsyncRpc:
|
|
24
|
+
"""Open a connection to the daemon of a remote target.
|
|
25
|
+
|
|
26
|
+
:param target: a target with ``ssh``, ``tls`` or ``tcp`` transport.
|
|
27
|
+
:param options: the call timeout and the keepalive.
|
|
28
|
+
:returns: the connection, before authentication.
|
|
29
|
+
:raises ConnectionError: if the host cannot be reached.
|
|
30
|
+
:raises ProtocolError: if a TLS server rejects our certificate.
|
|
31
|
+
"""
|
|
32
|
+
if target.transport == "ssh":
|
|
33
|
+
return await _start_ssh(target, options)
|
|
34
|
+
return await _connect_tcp(target, options)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
async def _start_ssh(
|
|
38
|
+
target: ConnectionTarget, options: ConnectionOptions | None
|
|
39
|
+
) -> AsyncRpc:
|
|
40
|
+
"""Start ``ssh`` and use its pipes.
|
|
41
|
+
|
|
42
|
+
:param target: a target with the ``ssh`` transport.
|
|
43
|
+
:param options: the call timeout and the keepalive.
|
|
44
|
+
:returns: the connection.
|
|
45
|
+
:raises ConnectionError: if the ssh program cannot be started.
|
|
46
|
+
"""
|
|
47
|
+
command = ssh_command(target)
|
|
48
|
+
try:
|
|
49
|
+
process = await asyncio.create_subprocess_exec(
|
|
50
|
+
*command, stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE
|
|
51
|
+
)
|
|
52
|
+
except OSError as error:
|
|
53
|
+
raise ConnectionError(f"cannot start ssh: {error}") from error
|
|
54
|
+
|
|
55
|
+
if process.stdin is None or process.stdout is None:
|
|
56
|
+
raise ConnectionError("ssh was started without pipes")
|
|
57
|
+
|
|
58
|
+
async def end_process() -> None:
|
|
59
|
+
"""End the ``ssh`` process."""
|
|
60
|
+
await _end(process)
|
|
61
|
+
|
|
62
|
+
return AsyncRpc(process.stdout, process.stdin, end_process, options)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
async def _end(process: Process) -> None:
|
|
66
|
+
"""Terminate a process and wait for it.
|
|
67
|
+
|
|
68
|
+
:param process: the ``ssh`` process.
|
|
69
|
+
"""
|
|
70
|
+
if process.returncode is None:
|
|
71
|
+
process.terminate()
|
|
72
|
+
await process.wait()
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
async def _connect_tcp(
|
|
76
|
+
target: ConnectionTarget, options: ConnectionOptions | None
|
|
77
|
+
) -> AsyncRpc:
|
|
78
|
+
"""Connect to a TCP port, with TLS if the transport asks for it.
|
|
79
|
+
|
|
80
|
+
:param target: a target with the ``tls`` or ``tcp`` transport.
|
|
81
|
+
:param options: the call timeout and the keepalive.
|
|
82
|
+
:returns: the connection.
|
|
83
|
+
:raises ConnectionError: if the host cannot be reached or TLS fails.
|
|
84
|
+
:raises ProtocolError: if a TLS server does not accept our certificate.
|
|
85
|
+
"""
|
|
86
|
+
context = tls_context(target) if target.transport == "tls" else None
|
|
87
|
+
try:
|
|
88
|
+
reader, writer = await asyncio.wait_for(
|
|
89
|
+
asyncio.open_connection(
|
|
90
|
+
target.host,
|
|
91
|
+
target.port,
|
|
92
|
+
ssl=context,
|
|
93
|
+
server_hostname=target.host if context else None,
|
|
94
|
+
),
|
|
95
|
+
CONNECT_TIMEOUT_SECONDS,
|
|
96
|
+
)
|
|
97
|
+
accepted = await reader.readexactly(1) if context else TLS_ACCEPTED
|
|
98
|
+
except (TimeoutError, ssl.SSLError, OSError, asyncio.IncompleteReadError) as error:
|
|
99
|
+
raise ConnectionError(
|
|
100
|
+
f"cannot reach {target.host}:{target.port}: {error!r}"
|
|
101
|
+
) from error
|
|
102
|
+
|
|
103
|
+
if accepted != TLS_ACCEPTED:
|
|
104
|
+
writer.close()
|
|
105
|
+
raise ProtocolError("the server did not accept our client certificate")
|
|
106
|
+
return AsyncRpc(reader, writer, options=options)
|