[cdc_rsync] [cdc_stream] Remove SSH port argument (#41)

This CL removes the port arguments for both tools.

The port argument can also be specified via the ssh-command and
scp-command flags. In fact, if a port is specified by both port flags
and ssh/scp commands, they interfere with each other. For ssh, the one
specified in ssh-command wins. For scp, the one specified in
scp-command wins. To fix this, one would have to parse scp-command and
remove the port arg there. Or we could just remove the ssh-port arg.
This is what this CL does. Note that if you need a custom port, it's
very likely that you also have to define custom ssh and scp commands.
This commit is contained in:
Lutz Justen
2022-12-12 10:58:33 +01:00
committed by GitHub
parent f0ef34db2f
commit f8438aec66
21 changed files with 89 additions and 191 deletions

View File

@@ -49,14 +49,14 @@ constexpr char kRemoteNetstatOutFmt[] =
class PortManagerTest : public ::testing::Test {
public:
PortManagerTest()
: remote_util_(/*verbosity=*/0, /*quiet=*/false, &process_factory_,
: remote_util_(kUserHost, /*verbosity=*/0, /*quiet=*/false,
&process_factory_,
/*forward_output_to_log=*/true),
port_manager_(kGuid, kFirstPort, kLastPort, &process_factory_,
&remote_util_, &system_clock_, &steady_clock_) {}
void SetUp() override {
Log::Initialize(std::make_unique<ConsoleLog>(LogLevel::kInfo));
remote_util_.SetUserHostAndPort(kUserHost, kSshPort);
}
void TearDown() override { Log::Shutdown(); }

View File

@@ -20,7 +20,6 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "common/path.h"
#include "common/status.h"
namespace cdc_ft {
namespace {
@@ -35,19 +34,15 @@ std::string GetPortForwardingArg(int local_port, int remote_port,
} // namespace
RemoteUtil::RemoteUtil(int verbosity, bool quiet,
RemoteUtil::RemoteUtil(std::string user_host, int verbosity, bool quiet,
ProcessFactory* process_factory,
bool forward_output_to_log)
: verbosity_(verbosity),
: user_host_(std::move(user_host)),
verbosity_(verbosity),
quiet_(quiet),
process_factory_(process_factory),
forward_output_to_log_(forward_output_to_log) {}
void RemoteUtil::SetUserHostAndPort(std::string user_host, int port) {
user_host_ = std::move(user_host);
ssh_port_ = port;
}
void RemoteUtil::SetScpCommand(std::string scp_command) {
scp_command_ = std::move(scp_command);
}
@@ -58,11 +53,6 @@ void RemoteUtil::SetSshCommand(std::string ssh_command) {
absl::Status RemoteUtil::Scp(std::vector<std::string> source_filepaths,
const std::string& dest, bool compress) {
absl::Status status = CheckUserHostPort();
if (!status.ok()) {
return status;
}
std::string source_args;
for (const std::string& sourceFilePath : source_filepaths) {
// Workaround for scp thinking that C is a host in C:\path\to\foo.
@@ -77,13 +67,10 @@ absl::Status RemoteUtil::Scp(std::vector<std::string> source_filepaths,
ProcessStartInfo start_info;
start_info.flags = ProcessFlags::kNoWindow;
start_info.command = absl::StrFormat(
"%s "
"%s %s -p -T "
"-P %i %s "
"%s:%s",
"%s %s %s -p -T "
"%s %s:%s",
scp_command_, quiet_ || verbosity_ < 2 ? "-q" : "", compress ? "-C" : "",
ssh_port_, source_args, QuoteForWindows(user_host_),
QuoteForWindows(dest));
source_args, QuoteForWindows(user_host_), QuoteForWindows(dest));
start_info.name = "scp";
start_info.forward_output_to_log = forward_output_to_log_;
@@ -100,11 +87,6 @@ absl::Status RemoteUtil::Chmod(const std::string& mode,
}
absl::Status RemoteUtil::Run(std::string remote_command, std::string name) {
absl::Status status = CheckUserHostPort();
if (!status.ok()) {
return status;
}
ProcessStartInfo start_info =
BuildProcessStartInfoForSsh(std::move(remote_command));
start_info.name = std::move(name);
@@ -140,13 +122,12 @@ ProcessStartInfo RemoteUtil::BuildProcessStartInfoForSshInternal(
std::string forward_arg, std::string remote_command_arg) {
ProcessStartInfo start_info;
start_info.command = absl::StrFormat(
"%s "
"%s -tt "
"%s %s -tt %s "
"-oServerAliveCountMax=6 " // Number of lost msgs before ssh terminates
"-oServerAliveInterval=5 " // Time interval between alive msgs
"%s %s -p %i %s",
"%s %s",
ssh_command_, quiet_ || verbosity_ < 2 ? "-q" : "", forward_arg,
QuoteForWindows(user_host_), ssh_port_, remote_command_arg);
QuoteForWindows(user_host_), remote_command_arg);
start_info.forward_output_to_log = forward_output_to_log_;
start_info.flags = ProcessFlags::kNoWindow;
return start_info;
@@ -200,12 +181,4 @@ std::string RemoteUtil::QuoteForSsh(const std::string& argument) {
escaped.substr(slash_pos + 1), "\""));
}
absl::Status RemoteUtil::CheckUserHostPort() {
if (user_host_.empty() || ssh_port_ == 0) {
return MakeStatus("IP or port not set");
}
return absl::OkStatus();
}
} // namespace cdc_ft

View File

@@ -29,46 +29,36 @@ namespace cdc_ft {
// Windows-only.
class RemoteUtil {
public:
static constexpr int kDefaultSshPort = 22;
// |user_host| is the SSH [user@]host of the remote instance.
// If |verbosity| is > 0 and |quiet| is false, output from scp, ssh etc.
// commands is shown.
// If |quiet| is true, scp, ssh etc. commands use quiet mode.
// If |forward_output_to_log| is true, process output is forwarded to logging
// instead of this process's stdout/stderr.
RemoteUtil(int verbosity, bool quiet, ProcessFactory* process_factory,
bool forward_output_to_log);
// Sets the SSH username and hostname of the remote instance, as well as the
// SSH tunnel port. |user_host| must be of the form [user@]host.
void SetUserHostAndPort(std::string user_host, int port);
RemoteUtil(std::string user_host, int verbosity, bool quiet,
ProcessFactory* process_factory, bool forward_output_to_log);
// Sets the SCP command binary path and additional arguments, e.g.
// C:\path\to\scp.exe -F <ssh_config> -i <key_file>
// -oStrictHostKeyChecking=yes -oUserKnownHostsFile="""file"""
// C:\path\to\scp.exe -p 1234 -i <key_file> -oUserKnownHostsFile=known_hosts
// By default, searches scp.exe on the path environment variables.
void SetScpCommand(std::string scp_command);
// Sets the SSH command binary path and additional arguments, e.g.
// C:\path\to\ssh.exe -F <ssh_config> -i <key_file>
// -oStrictHostKeyChecking=yes -oUserKnownHostsFile="""file"""
// C:\path\to\ssh.exe -P 1234 -i <key_file> -oUserKnownHostsFile=known_hosts
// By default, searches ssh.exe on the path environment variables.
void SetSshCommand(std::string ssh_command);
// Copies |source_filepaths| to the remote folder |dest| on the gamelet using
// scp. If |compress| is true, compressed upload is used.
// Must call SetUserHostAndPort before calling this method.
absl::Status Scp(std::vector<std::string> source_filepaths,
const std::string& dest, bool compress);
// Calls 'chmod |mode| |remote_path|' on the gamelet.
// Must call SetUserHostAndPort before calling this method.
absl::Status Chmod(const std::string& mode, const std::string& remote_path,
bool quiet = false);
// Runs |remote_command| on the gamelet. The command must be properly escaped.
// |name| is the name of the command displayed in the logs.
// Must call SetUserHostAndPort before calling this method.
absl::Status Run(std::string remote_command, std::string name);
// Builds an SSH command that executes |remote_command| on the gamelet.
@@ -77,7 +67,6 @@ class RemoteUtil {
// Builds an SSH command that runs SSH port forwarding to the gamelet, using
// the given |local_port| and |remote_port|.
// If |reverse| is true, sets up reverse port forwarding.
// Must call SetUserHostAndPort before calling this method.
ProcessStartInfo BuildProcessStartInfoForSshPortForward(int local_port,
int remote_port,
bool reverse);
@@ -85,7 +74,6 @@ class RemoteUtil {
// Builds an SSH command that executes |remote_command| on the gamelet, using
// port forwarding with given |local_port| and |remote_port|.
// If |reverse| is true, sets up reverse port forwarding.
// Must call SetUserHostAndPort before calling this method.
ProcessStartInfo BuildProcessStartInfoForSshPortForwardAndCommand(
int local_port, int remote_port, bool reverse,
std::string remote_command);
@@ -117,9 +105,6 @@ class RemoteUtil {
static std::string QuoteForSsh(const std::string& argument);
private:
// Verifies that both |user_host_| and |ssh_port_| are set.
absl::Status CheckUserHostPort();
// Common code for BuildProcessStartInfoForSsh*.
ProcessStartInfo BuildProcessStartInfoForSshInternal(
std::string forward_arg, std::string remote_command);
@@ -132,7 +117,6 @@ class RemoteUtil {
std::string scp_command_ = "scp";
std::string ssh_command_ = "ssh";
std::string user_host_;
int ssh_port_ = kDefaultSshPort;
};
} // namespace cdc_ft

View File

@@ -21,9 +21,6 @@
namespace cdc_ft {
namespace {
constexpr int kSshPort = 12345;
constexpr char kSshPortArg[] = "-p 12345";
constexpr char kUserHost[] = "user@example.com";
constexpr char kUserHostArg[] = "\"user@example.com\"";
@@ -39,12 +36,11 @@ constexpr char kCommand[] = "my_command";
class RemoteUtilTest : public ::testing::Test {
public:
RemoteUtilTest()
: util_(/*verbosity=*/0, /*quiet=*/false, &process_factory_,
: util_(kUserHost, /*verbosity=*/0, /*quiet=*/false, &process_factory_,
/*forward_output_to_log=*/true) {}
void SetUp() override {
Log::Initialize(std::make_unique<ConsoleLog>(LogLevel::kInfo));
util_.SetUserHostAndPort(kUserHost, kSshPort);
}
void TearDown() override { Log::Shutdown(); }
@@ -64,31 +60,29 @@ class RemoteUtilTest : public ::testing::Test {
TEST_F(RemoteUtilTest, BuildProcessStartInfoForSsh) {
ProcessStartInfo si = util_.BuildProcessStartInfoForSsh(kCommand);
ExpectContains(si.command, {"ssh", kSshPortArg, kUserHostArg, kCommand});
ExpectContains(si.command, {"ssh", kUserHostArg, kCommand});
}
TEST_F(RemoteUtilTest, BuildProcessStartInfoForSshPortForward) {
ProcessStartInfo si = util_.BuildProcessStartInfoForSshPortForward(
kLocalPort, kRemotePort, kRegular);
ExpectContains(si.command,
{"ssh", kSshPortArg, kUserHostArg, kPortForwardingArg});
ExpectContains(si.command, {"ssh", kUserHostArg, kPortForwardingArg});
si = util_.BuildProcessStartInfoForSshPortForward(kLocalPort, kRemotePort,
kReverse);
ExpectContains(si.command,
{"ssh", kSshPortArg, kUserHostArg, kReversePortForwardingArg});
ExpectContains(si.command, {"ssh", kUserHostArg, kReversePortForwardingArg});
}
TEST_F(RemoteUtilTest, BuildProcessStartInfoForSshPortForwardAndCommand) {
ProcessStartInfo si = util_.BuildProcessStartInfoForSshPortForwardAndCommand(
kLocalPort, kRemotePort, kRegular, kCommand);
ExpectContains(si.command, {"ssh", kSshPortArg, kUserHostArg,
kPortForwardingArg, kCommand});
ExpectContains(si.command,
{"ssh", kUserHostArg, kPortForwardingArg, kCommand});
si = util_.BuildProcessStartInfoForSshPortForwardAndCommand(
kLocalPort, kRemotePort, kReverse, kCommand);
ExpectContains(si.command, {"ssh", kSshPortArg, kUserHostArg,
kReversePortForwardingArg, kCommand});
ExpectContains(si.command,
{"ssh", kUserHostArg, kReversePortForwardingArg, kCommand});
}
TEST_F(RemoteUtilTest, BuildProcessStartInfoForSshWithCustomCommand) {
constexpr char kCustomSshCmd[] = "C:\\path\\to\\ssh.exe --fooarg --bararg=42";