mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 14:10:49 -05:00
fix(ec2): install packages with yum on Amazon Linux 2 guests (#2448)
Two package-manager probes in Ec2ContainerManager try dnf, apt-get and apk.
public.ecr.aws/amazonlinux/amazonlinux:2 ships **yum** and none of those
three, so both chains fall through on an Amazon Linux 2 guest:
- startSshd(): the trailing "command -v sshd" then fails, the method returns
early, and the instance is never reachable over SSH -- despite Floci
allocating a host port for it and injecting authorized_keys.
- metadataProxyInstallCommand(): reaches its else branch and exits 1 with
"No supported package manager found for IMDS proxy dependencies", leaving
the instance without a link-local IMDS endpoint.
This affects instances launched explicitly from ami-amazonlinux2 (or its ID
ami-0abcdef1234567890), not every instance: image-catalog.yaml pins
defaultDockerImage to amazonlinux:2023, which has dnf and already works. A
missing ImageId and unrecognised IDs both fall back to that default.
Verified against the image itself:
$ docker run --rm public.ecr.aws/amazonlinux/amazonlinux:2 sh -c '
command -v yum; for m in apt-get dnf apk; do command -v $m; done'
/usr/bin/yum # and nothing else
$ yum install -y openssh-server openssh-clients -> sshd, scp
$ yum install -y iproute socat curl ca-certificates -> ip, socat, curl
yum is probed after dnf so that dnf still wins on Amazon Linux 2023 and
modern Fedora/RHEL, where both exist and yum is only a compatibility shim.
The yum branch installs openssh-clients alongside the server, matching what
the dnf and apt-get branches do since #2449 -- an AL2 guest needs scp for
the same reason every other guest does.
Also extracts the probe into sshdInstallProbeCommand() so the tests can
assert against the string production actually issues rather than against a
copy of it declared in the test file.
This commit is contained in:
1 parent
b27b32f044
commit
c9f456ea7a
2 files changed
+91
-31
No files matched your search
@@ -683,37 +683,10 @@ public class Ec2ContainerManager {
|
||||
|
||||
private void startSshd(String containerId, String instanceId) {
|
||||
try {
|
||||
// Install openssh-server if absent. The trailing "command -v sshd" makes the script's
|
||||
// own exit code the source of truth for whether sshd is actually available afterward -
|
||||
// without it, a shell if/elif chain with no matching branch (no dnf/apt-get/apk found) or
|
||||
// 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 || ! 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 || exit 1;" +
|
||||
"command -v scp >/dev/null 2>&1 || exit 2"}, 120);
|
||||
// Exit 1 means sshd is absent and there is nothing to start; exit 2 means sshd is
|
||||
// there but the client package did not land. Only the first is fatal - see
|
||||
// sshdInstallProbeCommand() for why the probe distinguishes them.
|
||||
ContainerExecResult install = execInContainerForResult(containerId, sshdInstallProbeCommand(), 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}",
|
||||
@@ -889,6 +862,52 @@ public class Ec2ContainerManager {
|
||||
return new String[]{USER_DATA_SCRIPT_PATH};
|
||||
}
|
||||
|
||||
/**
|
||||
* The install-and-verify probe {@link #startSshd} execs in the guest. Extracted so tests can
|
||||
* assert against the string production actually issues rather than against a copy of it.
|
||||
*
|
||||
* <p>Installs openssh-server if absent. The trailing "command -v" checks make the script's own
|
||||
* exit code the source of truth for whether sshd is actually available afterward - without
|
||||
* them, a shell if/elif chain with no matching branch (no dnf/yum/apt-get/apk found) or whose
|
||||
* install command itself failed (e.g. no network yet, apt lock held) still exits 0 by bash
|
||||
* convention, so every later step in startSshd would silently no-op against a daemon that was
|
||||
* never installed while still logging success.
|
||||
*
|
||||
* <p>The client package is installed alongside the server because provisioning tools need scp
|
||||
* <em>on the instance</em>: 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.
|
||||
*
|
||||
* <p>The two failures 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.
|
||||
*/
|
||||
static String[] sshdInstallProbeCommand() {
|
||||
return new String[]{"sh", "-c",
|
||||
"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;" +
|
||||
// yum is probed after dnf, and the order is load-bearing: Amazon Linux
|
||||
// 2023 and modern Fedora/RHEL ship both, and there dnf is the supported
|
||||
// front end while yum is only a compatibility shim over it. Amazon Linux
|
||||
// 2 -- reached by explicitly requesting ami-amazonlinux2, not the default
|
||||
// image -- ships only yum, so without this branch the chain falls through
|
||||
// and sshd is never installed.
|
||||
" elif command -v yum >/dev/null 2>&1; then yum 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 || exit 1;" +
|
||||
"command -v scp >/dev/null 2>&1 || exit 2"};
|
||||
}
|
||||
|
||||
static String[] metadataProxyInstallCommand() {
|
||||
return new String[]{"sh", "-c", String.join("\n",
|
||||
"set -eu",
|
||||
@@ -898,6 +917,12 @@ public class Ec2ContainerManager {
|
||||
" DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends iproute2 socat curl ca-certificates >/dev/null",
|
||||
"elif command -v dnf >/dev/null 2>&1; then",
|
||||
" dnf install -y iproute socat curl ca-certificates >/dev/null",
|
||||
// Same gap as the sshd probe: Amazon Linux 2 has only yum, so on an instance
|
||||
// launched from ami-amazonlinux2 this chain reached its else branch and exited 1
|
||||
// with "No supported package manager found for IMDS proxy dependencies" --
|
||||
// leaving the instance without a link-local IMDS endpoint.
|
||||
"elif command -v yum >/dev/null 2>&1; then",
|
||||
" yum install -y iproute socat curl ca-certificates >/dev/null",
|
||||
"elif command -v apk >/dev/null 2>&1; then",
|
||||
" apk add --no-cache iproute2 socat curl ca-certificates >/dev/null",
|
||||
"else",
|
||||
|
||||
@@ -270,6 +270,40 @@ class Ec2ContainerManagerTest {
|
||||
+ Ec2ContainerManager.SSH_CLIENT_MISSING_EXIT_CODE), script);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sshdInstallProbeHandlesYumForAmazonLinux2() {
|
||||
// public.ecr.aws/amazonlinux/amazonlinux:2 ships only yum -- no dnf, no apt-get, no
|
||||
// apk. It is NOT the default image (image-catalog.yaml pins that to amazonlinux:2023,
|
||||
// which has dnf); it is reached by explicitly requesting ami-amazonlinux2. Without a
|
||||
// yum branch the if/elif chain falls through, the trailing "command -v sshd" fails,
|
||||
// and startSshd() returns early, so such an instance is never reachable over SSH.
|
||||
//
|
||||
// Asserted against the production accessor, not the SSHD_INSTALL_PROBE_CMD copy below,
|
||||
// so removing the yum branch fails THIS test rather than only the withCmd verify.
|
||||
String script = Ec2ContainerManager.sshdInstallProbeCommand()[2];
|
||||
|
||||
assertTrue(script.contains("command -v yum"), script);
|
||||
// The yum branch installs the client package alongside the server, exactly as the dnf
|
||||
// branch does -- an AL2 guest needs scp for the same reason every other guest does.
|
||||
assertTrue(script.contains("yum install -y openssh-server openssh-clients"), script);
|
||||
// dnf must still win where both exist (Amazon Linux 2023, modern Fedora/RHEL), where
|
||||
// yum is only a compatibility shim over dnf.
|
||||
assertTrue(script.indexOf("command -v dnf") < script.indexOf("command -v yum"),
|
||||
"dnf must be probed before yum");
|
||||
}
|
||||
|
||||
@Test
|
||||
void metadataProxyInstallCommandHandlesYumForAmazonLinux2() {
|
||||
// The IMDS proxy dependency install had the same gap, and unlike the sshd probe it
|
||||
// ends in an explicit failure: on an ami-amazonlinux2 instance it reached the else
|
||||
// branch and exited 1 with "No supported package manager found for IMDS proxy
|
||||
// dependencies", leaving the instance without a link-local metadata endpoint.
|
||||
String script = Ec2ContainerManager.metadataProxyInstallCommand()[2];
|
||||
|
||||
assertTrue(script.contains("command -v yum"), script);
|
||||
assertTrue(script.contains("yum install -y iproute socat curl ca-certificates"), script);
|
||||
}
|
||||
|
||||
@Test
|
||||
void metadataProxyStartCommandBindsAwsLinkLocalMetadataAddress() {
|
||||
String[] command = Ec2ContainerManager.metadataProxyStartCommand("floci", 9169);
|
||||
@@ -577,6 +611,7 @@ class Ec2ContainerManagerTest {
|
||||
private static final String[] SSHD_INSTALL_PROBE_CMD = {"sh", "-c",
|
||||
"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 yum >/dev/null 2>&1; then yum 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;"
|
||||
|
||||
Reference in new issue
Block a user