Skip to content

Commit 271f75d

Browse files
committed
[ssh] Introduce ssh factory
1 parent 052c98f commit 271f75d

10 files changed

Lines changed: 29 additions & 22 deletions

File tree

src/platform/backends/shared/base_virtual_machine.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <multipass/snapshot.h>
3232
#include <multipass/ssh/plain_ssh_process.h>
3333
#include <multipass/ssh/plain_ssh_session.h>
34+
#include <multipass/ssh/ssh_factory.h>
3435
#include <multipass/ssh/ssh_key_provider.h>
3536
#include <multipass/top_catch_all.h>
3637
#include <multipass/vm_specs.h>
@@ -314,7 +315,7 @@ std::unique_ptr<multipass::SSHSession> multipass::BaseVirtualMachine::new_ssh_se
314315
}
315316

316317
mpl::trace(vm_name, "New SSH session");
317-
return std::make_unique<PlainSSHSession>(ssh_coordinates());
318+
return MP_SSH_FACTORY.make_session(ssh_coordinates());
318319
}
319320

320321
bool multipass::BaseVirtualMachine::unplugged()
@@ -942,7 +943,7 @@ auto mp::BaseVirtualMachine::try_to_ssh() -> utils::TimeoutAction
942943

943944
void mp::BaseVirtualMachine::ssh_and_cross_to_running()
944945
{
945-
auto new_session = std::make_unique<PlainSSHSession>(ssh_coordinates());
946+
auto new_session = MP_SSH_FACTORY.make_session(ssh_coordinates());
946947

947948
std::lock_guard lock{state_mutex};
948949
ssh_session = std::move(new_session);

src/ssh/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
function(add_ssh_target TARGET_NAME)
1717
add_library(${TARGET_NAME} STATIC
18+
ssh_factory.cpp
1819
libssh_scope_guard.cpp
1920
openssh_key_provider.cpp
2021
plain_ssh_process.cpp
21-
plain_ssh_session.cpp
22-
ssh_client_key_provider.cpp)
22+
plain_ssh_session.cpp)
2323

2424
target_link_libraries(${TARGET_NAME}
2525
fmt::fmt-header-only
@@ -35,6 +35,7 @@ endif()
3535

3636
function(add_sftp_client_target TARGET_NAME)
3737
add_library(${TARGET_NAME} STATIC
38+
ssh_factory.cpp
3839
sftp_client.cpp
3940
sftp_dir_iterator.cpp
4041
sftp_utils.cpp
@@ -54,6 +55,7 @@ endif()
5455

5556
function(add_ssh_client_target TARGET_NAME)
5657
add_library(${TARGET_NAME} STATIC
58+
ssh_factory.cpp
5759
ssh_client.cpp
5860
plain_ssh_session.cpp)
5961

src/ssh/plain_ssh_session.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
*
1616
*/
1717

18-
#include "ssh_client_key_provider.h"
19-
2018
#include <multipass/exceptions/ssh_exception.h>
2119
#include <multipass/format.h>
2220
#include <multipass/logging/log.h>
2321
#include <multipass/platform.h>
2422
#include <multipass/socket.h>
2523
#include <multipass/ssh/plain_ssh_session.h>
24+
#include <multipass/ssh/ssh_factory.h>
2625
#include <multipass/ssh/ssh_key_provider.h>
2726
#include <multipass/ssh/throw_on_error.h>
2827
#include <multipass/standard_paths.h>
@@ -40,7 +39,7 @@ namespace
4039
constexpr auto category = "ssh session";
4140
}
4241

43-
mp::PlainSSHSession::PlainSSHSession(const mp::SSHCoordinates& coordinates)
42+
mp::PlainSSHSession::PlainSSHSession(const mp::SSHCoordinates& ssh_coordinates)
4443
: session{ssh_new(), ssh_free}, mut{}
4544
{
4645
if (session == nullptr)
@@ -64,16 +63,16 @@ mp::PlainSSHSession::PlainSSHSession(const mp::SSHCoordinates& coordinates)
6463
.toStdString();
6564

6665
// Setup (common)
67-
set_option(SSH_OPTIONS_USER, coordinates.username.c_str());
66+
set_option(SSH_OPTIONS_USER, ssh_coordinates.username.c_str());
6867
set_option(SSH_OPTIONS_TIMEOUT, &connect_timeout_secs);
6968
set_option(SSH_OPTIONS_NODELAY, &nodelay);
7069
set_option(SSH_OPTIONS_CIPHERS_C_S, "chacha20-poly1305@openssh.com,aes256-ctr");
7170
set_option(SSH_OPTIONS_CIPHERS_S_C, "chacha20-poly1305@openssh.com,aes256-ctr");
7271
set_option(SSH_OPTIONS_SSH_DIR, ssh_dir.c_str());
7372

7473
// TCP setup
75-
set_option(SSH_OPTIONS_HOST, coordinates.tcp_host.c_str());
76-
set_option(SSH_OPTIONS_PORT, &coordinates.port);
74+
set_option(SSH_OPTIONS_HOST, ssh_coordinates.tcp_host.c_str());
75+
set_option(SSH_OPTIONS_PORT, &ssh_coordinates.port);
7776

7877
// Connect (common)
7978
SSH::throw_on_error(session, "ssh connection failed", ssh_connect);
@@ -82,7 +81,7 @@ mp::PlainSSHSession::PlainSSHSession(const mp::SSHCoordinates& coordinates)
8281
"ssh failed to authenticate",
8382
ssh_userauth_publickey,
8483
nullptr,
85-
mp::SSHClientKeyProvider{coordinates.private_key_as_base64}.private_key());
84+
MP_SSH_FACTORY.make_key(ssh_coordinates.private_key_as_base64).get());
8685
}
8786

8887
multipass::PlainSSHSession::PlainSSHSession(multipass::PlainSSHSession&& other)

src/ssh/sftp_client.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <multipass/platform.h>
2323
#include <multipass/ssh/plain_ssh_session.h>
2424
#include <multipass/ssh/sftp_utils.h>
25+
#include <multipass/ssh/ssh_factory.h>
2526
#include <multipass/ssh/throw_on_error.h>
2627
#include <multipass/utils.h>
2728

@@ -48,7 +49,7 @@ SFTPSessionUPtr make_sftp_session(ssh_session session)
4849
}
4950

5051
SFTPClient::SFTPClient(const SSHCoordinates& coordinates)
51-
: SFTPClient{std::make_unique<PlainSSHSession>(coordinates)}
52+
: SFTPClient{MP_SSH_FACTORY.make_session(coordinates)}
5253
{
5354
}
5455

src/ssh/ssh_client.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <multipass/rpc/multipass.pb.h>
2020
#include <multipass/ssh/plain_ssh_session.h>
2121
#include <multipass/ssh/ssh_client.h>
22+
#include <multipass/ssh/ssh_factory.h>
2223
#include <multipass/ssh/throw_on_error.h>
2324
#include <multipass/utils.h>
2425

@@ -46,7 +47,7 @@ mp::SSHClient::ChannelUPtr make_channel(ssh_session session)
4647
} // namespace
4748

4849
mp::SSHClient::SSHClient(const mp::SSHCoordinates& coordinates, ConsoleCreator console_creator)
49-
: SSHClient{std::make_unique<mp::PlainSSHSession>(coordinates), console_creator}
50+
: SSHClient{MP_SSH_FACTORY.make_session(coordinates), console_creator}
5051
{
5152
}
5253

src/sshfs_mount/sshfs_server.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
#include <multipass/platform.h>
2626
#include <multipass/ssh/libssh_scope_guard.h>
2727
#include <multipass/ssh/plain_ssh_session.h>
28-
29-
#include <ssh/ssh_client_key_provider.h>
28+
#include <multipass/ssh/ssh_factory.h>
3029

3130
#include <QStringList>
3231

@@ -116,7 +115,7 @@ int main(int argc, char* argv[])
116115
auto watchdog = mpp::make_quit_watchdog(
117116
std::chrono::milliseconds{500}); // called while there is only one thread
118117

119-
mp::SshfsMount sshfs_mount(std::make_unique<mp::PlainSSHSession>(coordinates),
118+
mp::SshfsMount sshfs_mount(MP_SSH_FACTORY.make_session(coordinates),
120119
source_path,
121120
target_path,
122121
gid_mappings,

tests/unit/test_sftp_client.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include <multipass/ssh/plain_ssh_session.h>
3030
#include <multipass/ssh/sftp_client.h>
31+
#include <multipass/ssh/ssh_factory.h>
3132

3233
#include <fmt/std.h>
3334

@@ -89,7 +90,7 @@ struct SFTPClient : public testing::Test
8990
mp::SFTPClient make_sftp_client()
9091
{
9192
mp::SSHCoordinates coord{"ubuntu", key_provider.private_key_as_base64(), 43, "b"};
92-
return {std::make_unique<mp::PlainSSHSession>(coord)};
93+
return {MP_SSH_FACTORY.make_session(coord)};
9394
}
9495

9596
// this is a macro since REPLACE only applies to the current scope and cannot be moved out.

tests/unit/test_sftpserver.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <multipass/format.h>
3737
#include <multipass/platform.h>
3838
#include <multipass/ssh/plain_ssh_session.h>
39+
#include <multipass/ssh/ssh_factory.h>
3940

4041
#include <algorithm>
4142
#include <queue>
@@ -82,7 +83,7 @@ struct SftpServer : public mp::test::SftpServerTest
8283
key_provider.private_key_as_base64(),
8384
42,
8485
"theanswertoeverything"};
85-
return {std::make_unique<mp::PlainSSHSession>(coord),
86+
return {MP_SSH_FACTORY.make_session(coord),
8687
path,
8788
target.empty() ? path : target,
8889
gid_mappings,
@@ -418,7 +419,7 @@ TEST_F(SftpServer, throwsWhenSshfsErrorsOnStart)
418419
key_provider.private_key_as_base64(),
419420
42,
420421
"theanswertoeverything"};
421-
return mp::SftpServer{std::make_unique<mp::PlainSSHSession>(coord),
422+
return mp::SftpServer{MP_SSH_FACTORY.make_session(coord),
422423
"",
423424
"",
424425
{{default_uid, mp::default_id}},
@@ -495,7 +496,7 @@ TEST_F(SftpServer, sshfsRestartsOnTimeout)
495496
key_provider.private_key_as_base64(),
496497
42,
497498
"theanswertoeverything"};
498-
auto sftp = mp::SftpServer{std::make_unique<mp::PlainSSHSession>(coord),
499+
auto sftp = mp::SftpServer{MP_SSH_FACTORY.make_session(coord),
499500
"",
500501
"",
501502
{{default_gid, mp::default_id}},

tests/unit/test_ssh_client.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <multipass/ssh/plain_ssh_session.h>
2828
#include <multipass/ssh/ssh_client.h>
29+
#include <multipass/ssh/ssh_factory.h>
2930

3031
namespace mp = multipass;
3132
namespace mpt = multipass::test;
@@ -41,7 +42,7 @@ struct SSHClient : public testing::Test
4142
key_provider.private_key_as_base64(),
4243
42,
4344
"theanswertoeverything"};
44-
return {std::make_unique<mp::PlainSSHSession>(coord), console_creator};
45+
return {MP_SSH_FACTORY.make_session(coord), console_creator};
4546
}
4647

4748
const mpt::StubSSHKeyProvider key_provider;

tests/unit/test_sshfsmount.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <multipass/logging/log.h>
2929
#include <multipass/signal.h>
3030
#include <multipass/ssh/plain_ssh_session.h>
31+
#include <multipass/ssh/ssh_factory.h>
3132
#include <multipass/utils.h>
3233

3334
#include <algorithm>
@@ -52,7 +53,7 @@ struct SshfsMount : public mp::test::SftpServerTest
5253
key_provider.private_key_as_base64(),
5354
42,
5455
"theanswertoeverything"};
55-
return {std::make_unique<mp::PlainSSHSession>(coord),
56+
return {MP_SSH_FACTORY.make_session(coord),
5657
default_source,
5758
target.value_or(default_target),
5859
default_mappings,

0 commit comments

Comments
 (0)