mirror of
https://github.com/floci-io/floci.git
synced 2026-11-26 23:29:28 -05:00
feat(ec2): support GetConsoleOutput with the guest's SSH host keys (#7385)
Returns a console log carrying the instance's SSH host public keys between the markers cloud-init prints, read from the running instance container, so clients can verify the host before connecting. An instance without a running container has an empty log. Look the instance up in the request's region, read host keys only from a running container and treat a failed read as an empty log, build the response with XmlBuilder, and cover the SDK reading of the response. AWS returns DryRunOperation for DryRun=true even for an empty or malformed instance id, so check the dry run first. The key files are the guest's to write, so capture at most 16 KiB of stdout (and 1 KiB of stderr) through bounded streams, and answer an empty log, with a warning, when the output is larger than host keys can be.
This commit is contained in:
1 parent
e95af9cbf6
commit
ecd1138d86
7 files changed
+291
-1
No files matched your search
@@ -91,6 +91,9 @@ public class Ec2ContainerManager {
|
||||
private static final int LAUNCH_QUEUE_CAPACITY = 64;
|
||||
// ponytail: a fixed cap on concurrent post-start IMDS setups; a start beyond it skips the setup.
|
||||
private static final int METADATA_SETUP_CONCURRENCY = 16;
|
||||
/** Real host public keys, one line per type, come to well under a kilobyte together. */
|
||||
static final int SSH_HOST_KEYS_MAX_BYTES = 16 * 1024;
|
||||
private static final int SSH_HOST_KEYS_STDERR_BYTES = 1024;
|
||||
private static final long USER_DATA_EXECUTION_TIMEOUT_MINUTES = 30;
|
||||
// Wait for capacity so accepted launches are not dropped, but never run launch work on callers.
|
||||
static final RejectedExecutionHandler BLOCKING_BACKPRESSURE = (runnable, executor) -> {
|
||||
@@ -1975,4 +1978,27 @@ public class Ec2ContainerManager {
|
||||
static Optional<String> preferredMetadataSourceIp(Map<String, ContainerNetwork> networks) {
|
||||
return Ec2MetadataProxy.preferredMetadataSourceIp(networks);
|
||||
}
|
||||
|
||||
/**
|
||||
* The instance's SSH host public keys, read from its running container. AWS prints these to
|
||||
* the console log at first boot (cloud-init's "SSH HOST KEY KEYS" block), which is how a
|
||||
* client verifies the host it connects to.
|
||||
*/
|
||||
public String readSshHostKeys(String containerId) {
|
||||
String[] cmd = {"sh", "-c", "for f in /etc/ssh/ssh_host_*_key.pub; do [ -f \"$f\" ] && cat \"$f\"; done"};
|
||||
// The files are the guest's to write, so capture no more than real host keys can take.
|
||||
UserDataPipeline.BoundedOutput stdout = new UserDataPipeline.BoundedOutput(SSH_HOST_KEYS_MAX_BYTES);
|
||||
UserDataPipeline.BoundedOutput stderr = new UserDataPipeline.BoundedOutput(SSH_HOST_KEYS_STDERR_BYTES);
|
||||
ContainerExec.Result result = ContainerExec.run(dockerClient, containerId, cmd, 15, stdout, stderr);
|
||||
if (result.exitCode() != 0) {
|
||||
return "";
|
||||
}
|
||||
if (stdout.truncated()) {
|
||||
LOG.warnv("Ignoring SSH host keys of container {0}: {1} bytes exceed the {2}-byte limit",
|
||||
containerId, stdout.totalBytes(), SSH_HOST_KEYS_MAX_BYTES);
|
||||
return "";
|
||||
}
|
||||
return new String(stdout.toByteArray(), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -108,6 +108,7 @@ public class Ec2QueryHandler {
|
||||
case "StartInstances" -> handleStartInstances(params, region);
|
||||
case "StopInstances" -> handleStopInstances(params, region);
|
||||
case "RebootInstances" -> handleRebootInstances(params, region);
|
||||
case "GetConsoleOutput" -> handleGetConsoleOutput(params, region);
|
||||
case "MonitorInstances" -> handleMonitoring(params, region, "MonitorInstances", true);
|
||||
case "UnmonitorInstances" -> handleMonitoring(params, region, "UnmonitorInstances", false);
|
||||
case "DescribeInstanceStatus" -> handleDescribeInstanceStatus(params, region);
|
||||
@@ -1558,6 +1559,26 @@ public class Ec2QueryHandler {
|
||||
return xmlResponse(buildInstanceStateChangeXml("StopInstancesResponse", changes));
|
||||
}
|
||||
|
||||
private Response handleGetConsoleOutput(MultivaluedMap<String, String> p, String region) {
|
||||
// AWS answers a dry run before it looks at the instance, even for an empty or malformed id.
|
||||
checkDryRun(p);
|
||||
String instanceId = p.getFirst("InstanceId");
|
||||
if (instanceId == null || instanceId.isBlank()) {
|
||||
throw new AwsException("MissingParameter", "The request must contain the parameter InstanceId", 400);
|
||||
}
|
||||
String output = service.getConsoleOutput(region, instanceId);
|
||||
String encoded = Base64.getEncoder().encodeToString(
|
||||
output.getBytes(StandardCharsets.UTF_8));
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("GetConsoleOutputResponse", AwsNamespaces.EC2)
|
||||
.elem("requestId", UUID.randomUUID().toString())
|
||||
.elem("instanceId", instanceId)
|
||||
.elem("timestamp", Instant.now().toString())
|
||||
.elem("output", encoded)
|
||||
.end("GetConsoleOutputResponse");
|
||||
return xmlResponse(xml.build());
|
||||
}
|
||||
|
||||
private Response handleRebootInstances(MultivaluedMap<String, String> p, String region) {
|
||||
List<String> ids = getList(p, "InstanceId");
|
||||
service.rebootInstances(region, ids);
|
||||
|
||||
@@ -16007,4 +16007,32 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
}
|
||||
return converted;
|
||||
}
|
||||
|
||||
/**
|
||||
* GetConsoleOutput: a console log carrying the instance's SSH host keys between the markers
|
||||
* cloud-init prints, the part clients read from it. An instance that is not running, or whose
|
||||
* container cannot be read, has an empty log, as a just-launched one does on AWS.
|
||||
*/
|
||||
public String getConsoleOutput(String region, String instanceId) {
|
||||
Instance instance = getRequiredInstance(region, instanceId);
|
||||
String containerId = instance.getDockerContainerId();
|
||||
if (containerId == null || containerId.isBlank() || instance.getState() == null
|
||||
|| !"running".equals(instance.getState().getName())) {
|
||||
return "";
|
||||
}
|
||||
String keys;
|
||||
try {
|
||||
keys = containerManager.readSshHostKeys(containerId);
|
||||
} catch (RuntimeException e) {
|
||||
// The container stopped or went away between the state check and the read.
|
||||
LOG.debugv("No console output for {0}: {1}", instanceId, e.getMessage());
|
||||
return "";
|
||||
}
|
||||
if (keys == null || keys.isBlank()) {
|
||||
return "";
|
||||
}
|
||||
return "Cloud-init finished for " + instanceId + "\n"
|
||||
+ "-----BEGIN SSH HOST KEY KEYS-----\n" + keys.strip() + "\n-----END SSH HOST KEY KEYS-----\n";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user