mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(ec2): install the SSH client package in guests, not just the server (#2449)
* fix(ec2): install the SSH client package in guests, not just the server
Guests get openssh-server but no client package, so the instance has sshd
and sftp-server but no /usr/bin/scp. Real AMIs carry both -- Amazon Linux 2
ships openssh-clients in the base image.
Provisioning tools need scp *on the instance*. Packer's default file
transfer for a shell provisioner uploads the script with scp, so a build
fails immediately after connecting successfully:
==> amazon-ebs.floci: Connected to SSH!
Error uploading script: SCP failed to start. This usually means that
SCP is not properly installed on the remote system.
Confirmed on a guest: /usr/libexec/openssh/sftp-server is present (it comes
with openssh-server) but /usr/bin/scp and /usr/bin/sftp are not.
Package names verified against the images rather than assumed:
amazonlinux:2 yum install -y openssh-clients -> /usr/bin/scp, sftp
ubuntu:24.04 apt-get install -y openssh-client -> /usr/bin/scp, sftp
alpine:3 apk add openssh -> /usr/bin/scp, sftp
so apk needs no change -- its openssh package already contains both -- and
only the rpm and deb branches gain a package.
The install guard now tests for scp as well. Keying it on sshd alone would
skip the install on an image that already has the server but no client,
leaving provisioning broken with nothing in the logs to explain it.
Without this, builds need ssh_file_transfer_method = "sftp" in the Packer
template as a workaround, which would not appear in a real template.
* fix(ec2): report a failed SSH client install instead of silent success
The install probe's trailing check was still "command -v sshd", so a guest that
already had the server but whose client package failed to install exited 0 and
initialization carried on -- the exact state the widened guard was added to
catch, and the one where Packer's default scp-based file transfer breaks, with
nothing in the logs to explain it.
The two outcomes are not equally fatal, so the script now distinguishes them:
exit 1 for no sshd, where there is nothing to start and startSshd returns as
before, and exit 2 for sshd present but scp missing, which warns and continues.
An instance that can serve SSH but cannot scp is still worth making reachable;
failing the whole setup there would trade a silent gap for an unreachable
instance.
Tests: the launch test that drives a failing probe is parameterized by exit
code, adding launchWhenOnlySshClientInstallFails_stillStartsSshd for the exit-2
path (keygen and sshd are still issued), and the probe-script assertion now
pins the trailing status check itself.
Ec2ContainerManagerTest: 23 tests, 0 failures, 0 errors.
This commit is contained in:
1 parent
3887a6ca65
commit
f3029ee251
2 files changed
+88
-14
No files matched your search
@@ -55,6 +55,8 @@ public class Ec2ContainerManager {
|
||||
private static final String USER_DATA_SCRIPT_PATH = "/tmp/user-data.sh";
|
||||
private static final Pattern MIME_BOUNDARY = Pattern.compile("(?im)^content-type:\\s*multipart/[^;]+;\\s*boundary=\"?([^\";\\n\\r]+)\"?.*$");
|
||||
private static final List<String> ALLOWED_SSHD_PATHS = List.of("/usr/sbin/sshd", "/usr/local/sbin/sshd", "/sbin/sshd");
|
||||
/** Exit code the sshd install probe uses for "sshd is present but scp is not". See startSshd. */
|
||||
static final int SSH_CLIENT_MISSING_EXIT_CODE = 2;
|
||||
|
||||
static int containerBridgeIpAttempts = 30;
|
||||
static long containerBridgeIpPollMillis = 500;
|
||||
@@ -492,15 +494,36 @@ public class Ec2ContainerManager {
|
||||
// whose install command itself failed (e.g. no network yet, apt lock held) still exits 0
|
||||
// by bash convention, so every later step here would silently no-op against a daemon that
|
||||
// was never installed while still logging success.
|
||||
// The client package is installed alongside the server because provisioning tools
|
||||
// need scp *on the instance*: Packer's default file transfer for a shell
|
||||
// provisioner uploads the script with scp, and real AMIs carry it. Installing only
|
||||
// openssh-server leaves sftp-server present but /usr/bin/scp absent, so the upload
|
||||
// fails with "SCP failed to start. This usually means that SCP is not properly
|
||||
// installed on the remote system." The guard tests for scp too: keying it on sshd
|
||||
// alone would skip the install entirely on an image that already has the server but
|
||||
// no client. Package names differ -- openssh-clients on rpm distributions,
|
||||
// openssh-client on Debian; apk's openssh already contains both.
|
||||
// The two are not equally fatal, so the script separates them: exit 1 means no sshd
|
||||
// and there is nothing to start, while exit 2 means sshd is there but the client
|
||||
// package did not land. A guest that can serve SSH but cannot scp is still worth
|
||||
// starting - it just cannot run a Packer shell provisioner - so that case warns and
|
||||
// continues rather than leaving the instance unreachable. Checking only sshd at the
|
||||
// end would report that state as outright success, which is the silent failure this
|
||||
// probe exists to prevent.
|
||||
ContainerExecResult install = execInContainerForResult(containerId, new String[]{"sh", "-c",
|
||||
"if ! command -v sshd >/dev/null 2>&1; then" +
|
||||
" if command -v dnf >/dev/null 2>&1; then dnf install -y openssh-server >/dev/null 2>&1;" +
|
||||
" elif command -v apt-get >/dev/null 2>&1; then DEBIAN_FRONTEND=noninteractive apt-get install -y openssh-server >/dev/null 2>&1;" +
|
||||
"if ! command -v sshd >/dev/null 2>&1 || ! command -v scp >/dev/null 2>&1; then" +
|
||||
" if command -v dnf >/dev/null 2>&1; then dnf install -y openssh-server openssh-clients >/dev/null 2>&1;" +
|
||||
" elif command -v apt-get >/dev/null 2>&1; then DEBIAN_FRONTEND=noninteractive apt-get install -y openssh-server openssh-client >/dev/null 2>&1;" +
|
||||
" elif command -v apk >/dev/null 2>&1; then apk add --no-cache openssh >/dev/null 2>&1;" +
|
||||
" fi;" +
|
||||
"fi;" +
|
||||
"command -v sshd >/dev/null 2>&1"}, 120);
|
||||
if (install.exitCode() != 0) {
|
||||
"command -v sshd >/dev/null 2>&1 || exit 1;" +
|
||||
"command -v scp >/dev/null 2>&1 || exit 2"}, 120);
|
||||
if (install.exitCode() == SSH_CLIENT_MISSING_EXIT_CODE) {
|
||||
LOG.warnv("sshd is available on EC2 instance {0} but the OpenSSH client package is not:"
|
||||
+ " scp is missing, so provisioners that upload files over scp will fail: {1}",
|
||||
instanceId, install.summary());
|
||||
} else if (install.exitCode() != 0) {
|
||||
LOG.warnv("Could not install openssh-server for EC2 instance {0}: {1}",
|
||||
instanceId, install.summary());
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user