From 9d19a820a0ca17fcb7c1cc6e90afc0458b35b5e5 Mon Sep 17 00:00:00 2001 From: Lutz Reinhardt Date: Fri, 20 Nov 2026 10:38:43 +0000 Subject: [PATCH] feat: Pregenerate EBcLfSA host keys First boot takes more time, because SSH keys are generated. For testing this causes more frequent timeouts and test failures. Thus pregenerate those keys to save some boot time. --- ebclfsa_aarch64/BUILD | 84 +++++++++++++++++++++++++++++ ebclfsa_aarch64/README.md | 14 ++++- ebclfsa_aarch64/build_image.sh | 2 +- tests/BUILD | 2 + tests/test_ebclfsa_ssh_host_keys.py | 48 +++++++++++++++++ 5 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 tests/test_ebclfsa_ssh_host_keys.py diff --git a/ebclfsa_aarch64/BUILD b/ebclfsa_aarch64/BUILD index c691309..781bc08 100644 --- a/ebclfsa_aarch64/BUILD +++ b/ebclfsa_aarch64/BUILD @@ -20,6 +20,13 @@ load("//:qemu_config.bzl", "qemu_config") IMAGE_PACKAGES = [ ":network_config_deb", ":sshd_config_deb", + ":ssh_host_keys_deb", +] + +SSH_HOST_KEY_ALGORITHMS = [ + "rsa", + "ecdsa", + "ed25519", ] exports_files( @@ -81,6 +88,83 @@ pkg_deb( visibility = ["//visibility:public"], ) +# The toolkit's first-boot key generator skips keys whose public files exist. +genrule( + name = "ssh_host_keys", + outs = [ + "ssh-host-keys/ssh_host_%s_key%s" % (algorithm, suffix) + for algorithm in SSH_HOST_KEY_ALGORITHMS + for suffix in [ + "", + ".pub", + ] + ], + cmd = "mkdir -p $(RULEDIR)/ssh-host-keys && " + + " && ".join([ + "ssh-keygen -q -t %s -N '' -C '' -f $(RULEDIR)/ssh-host-keys/ssh_host_%s_key" % (algorithm, algorithm) + for algorithm in SSH_HOST_KEY_ALGORITHMS + ]), + local = True, + tags = ["no-remote-cache"], +) + +filegroup( + name = "ssh_host_public_keys", + srcs = [ + "ssh-host-keys/ssh_host_%s_key.pub" % algorithm + for algorithm in SSH_HOST_KEY_ALGORITHMS + ], + visibility = ["//visibility:public"], +) + +pkg_tar( + name = "ssh_host_private_keys_tar", + srcs = [ + "ssh-host-keys/ssh_host_%s_key" % algorithm + for algorithm in SSH_HOST_KEY_ALGORITHMS + ], + mode = "0600", + package_dir = "/etc/ssh", + strip_prefix = "ssh-host-keys", + tags = ["no-remote-cache"], +) + +pkg_tar( + name = "ssh_host_public_keys_tar", + srcs = [ + "ssh-host-keys/ssh_host_%s_key.pub" % algorithm + for algorithm in SSH_HOST_KEY_ALGORITHMS + ], + mode = "0644", + package_dir = "/etc/ssh", + strip_prefix = "ssh-host-keys", + tags = ["no-remote-cache"], +) + +pkg_tar( + name = "ssh_host_keys_tar", + tags = ["no-remote-cache"], + deps = [ + ":ssh_host_private_keys_tar", + ":ssh_host_public_keys_tar", + ], +) + +pkg_deb( + name = "ssh_host_keys_deb", + architecture = "all", + data = ":ssh_host_keys_tar", + depends = ["ebclfsa-openssh-server-keys-crinit"], + description = "Development SSH server host keys for integration testing", + homepage = "https://github.com/eclipse-score/os_images", + maintainer = "Eclipse S-CORE contributors ", + package = "score-itf-ssh-host-keys", + replaces = ["ebclfsa-openssh-server-keys-crinit"], + tags = ["no-remote-cache"], + version = "1.0", + visibility = ["//visibility:public"], +) + # Builds the image with KAS/Isar in a privileged container. This takes a long # time, so the target is only built when explicitly requested. genrule( diff --git a/ebclfsa_aarch64/README.md b/ebclfsa_aarch64/README.md index 4e4ad92..33a1134 100644 --- a/ebclfsa_aarch64/README.md +++ b/ebclfsa_aarch64/README.md @@ -38,10 +38,12 @@ only built when selected explicitly. | `:qemu_config` | Generated QEMU configuration with the image's SSH key path | | `:ssh_target_key` | Development private key generated with the image | | `:network_config_deb` | The `score-network-config` Debian package | +| `:ssh_host_keys_deb` | Generated SSH server host keys as a Debian package | ## Requirements -- An x86_64 Linux host with Docker, `dpkg-deb` (from `dpkg`), and permission to +- An x86_64 Linux host with Docker, `dpkg-deb` (from `dpkg`), `ssh-keygen` + (from `openssh-client`), and permission to run privileged containers. The Docker daemon must see the Bazel output directory at the same absolute path as the build process (no remote daemon). - Network access to GitHub, GHCR and the package repositories referenced by the @@ -85,6 +87,14 @@ rejected. Packages must be compatible with the arm64 image, and their dependencies must be available from the configured repositories or supplied in the same list. +The `score-itf-ssh-host-keys` package supplies RSA, ECDSA and Ed25519 host +key pairs at build time, replacing the toolkit's empty private-key placeholders. +This avoids generating them at boot. Images built from the same package share +these keys; Bazel may reuse the package across builds. The private keys are +present in the disk image and build outputs, so they are not secret and must +not be trusted for host authentication. Use these images only for isolated +testing. Generate unique host keys for each device before any non-test use. + ## Tests The ping, multicast, SSH authentication, no-disk, and DHCP QEMU integration @@ -142,7 +152,7 @@ These keys are deliberately public development credentials: anyone downloading the release can log in as root on a reachable image. Use only on isolated test networks, never reuse the keys elsewhere, and replace the authorized key and disable passwordless login before any non-test deployment. SSH server host -private keys are not included in the archives. +private keys are included in the disk image inside each archive. Both platform builds must succeed before any assets are published. Re-running a successful workflow replaces assets with the same names. If releases are created diff --git a/ebclfsa_aarch64/build_image.sh b/ebclfsa_aarch64/build_image.sh index 050f5f2..fafe2a6 100755 --- a/ebclfsa_aarch64/build_image.sh +++ b/ebclfsa_aarch64/build_image.sh @@ -170,7 +170,7 @@ for artifact in "${artifacts[@]}"; do done if [[ "$release" == true ]]; then - # Publish only the development login key, never SSH server host keys. + # The disk image contains development SSH server host keys. tar --sparse -czf "$output/$image.tar.gz" -C "$deploy" \ "$image.wic" "$image-vmlinux" "$image.spdx.json" "$image-ssh-target-key" cp -L "$deploy/$sdk.tar.gz" "$deploy/$sdk.spdx.json" "$output/" diff --git a/tests/BUILD b/tests/BUILD index 3645784..fb158f4 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -141,6 +141,7 @@ py_itf_test( py_itf_test( name = "ebclfsa_qemu_test", srcs = [ + "test_ebclfsa_ssh_host_keys.py", "test_multicast.py", "test_no_disk.py", "test_ping.py", @@ -155,6 +156,7 @@ py_itf_test( "//ebclfsa_aarch64:image", "//ebclfsa_aarch64:kernel", "//ebclfsa_aarch64:qemu_config", + "//ebclfsa_aarch64:ssh_host_public_keys", "//ebclfsa_aarch64:ssh_target_key", ], plugins = ["@score_itf//score/itf/plugins:qemu_plugin"], diff --git a/tests/test_ebclfsa_ssh_host_keys.py b/tests/test_ebclfsa_ssh_host_keys.py new file mode 100644 index 0000000..621b1ed --- /dev/null +++ b/tests/test_ebclfsa_ssh_host_keys.py @@ -0,0 +1,48 @@ +# ******************************************************************************* +# Copyright (c) 2026 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + +import os +from pathlib import Path + + +def test_packaged_host_keys_survive_boot(target): + built_keys = ( + Path(os.environ["TEST_SRCDIR"]) + / os.environ["TEST_WORKSPACE"] + / "ebclfsa_aarch64" + / "ssh-host-keys" + ) + + for algorithm in ("rsa", "ecdsa", "ed25519"): + key = f"/etc/ssh/ssh_host_{algorithm}_key" + + # Check that key packages are installed + exit_code, output = target.execute(f"dpkg-query -S {key} {key}.pub") + assert exit_code == 0, output + assert output.splitlines() == [ + f"score-itf-ssh-host-keys: {key}".encode(), + f"score-itf-ssh-host-keys: {key}.pub".encode(), + ] + + # Check file permissions + exit_code, output = target.execute(f"stat -c '%U:%G %a' {key} {key}.pub") + assert exit_code == 0, output + assert output.splitlines() == [b"root:root 600", b"root:root 644"] + + # Check key content is the same as the built keys + exit_code, private_public = target.execute(f"ssh-keygen -y -f {key}") + assert exit_code == 0, private_public + exit_code, public = target.execute(f"cat {key}.pub") + assert exit_code == 0, public + assert public == (built_keys / f"ssh_host_{algorithm}_key.pub").read_bytes() + assert private_public.split()[:2] == public.split()[:2]