-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #296 from cgwalters/install-inject-ssh
install: Add support for `--root-ssh-authorized-keys`
- Loading branch information
Showing
3 changed files
with
71 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,11 +132,13 @@ jobs: | |
- name: Integration tests | ||
run: | | ||
set -xeuo pipefail | ||
sudo podman run --rm -ti --privileged --env RUST_LOG=debug -v /:/target -v /var/lib/containers:/var/lib/containers -v ./usr/bin/bootc:/usr/bin/bootc --pid=host --security-opt label=disable \ | ||
echo 'ssh-ed25519 ABC0123 [email protected]' > test_authorized_keys | ||
sudo podman run --rm -ti --privileged -v ./test_authorized_keys:/test_authorized_keys --env RUST_LOG=debug -v /:/target -v /var/lib/containers:/var/lib/containers -v ./usr/bin/bootc:/usr/bin/bootc --pid=host --security-opt label=disable \ | ||
quay.io/centos-bootc/fedora-bootc-dev:eln bootc install to-filesystem \ | ||
--karg=foo=bar --disable-selinux --replace=alongside /target | ||
--karg=foo=bar --disable-selinux --replace=alongside --root-ssh-authorized-keys=/test_authorized_keys /target | ||
ls -al /boot/loader/ | ||
sudo grep foo=bar /boot/loader/entries/*.conf | ||
grep authorized_keys /ostree/deploy/default/deploy/*/etc/tmpfiles.d/bootc-root-ssh.conf | ||
# TODO fix https://github.com/containers/bootc/pull/137 | ||
sudo chattr -i / /ostree/deploy/default/deploy/* | ||
sudo rm /ostree/deploy/default -rf | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use anyhow::Result; | ||
use camino::Utf8Path; | ||
use cap_std::fs::Dir; | ||
use cap_std_ext::{cap_std, dirext::CapStdExtDirExt}; | ||
use fn_error_context::context; | ||
|
||
const ETC_TMPFILES: &str = "etc/tmpfiles.d"; | ||
const ROOT_SSH_TMPFILE: &str = "bootc-root-ssh.conf"; | ||
|
||
#[context("Injecting root authorized_keys")] | ||
pub(crate) fn inject_root_ssh_authorized_keys(root: &Dir, contents: &str) -> Result<()> { | ||
// While not documented right now, this one looks like it does not newline wrap | ||
let b64_encoded = ostree_ext::glib::base64_encode(contents.as_bytes()); | ||
// See the example in https://systemd.io/CREDENTIALS/ | ||
let tmpfiles_content = format!("f~ /root/.ssh/authorized_keys 600 root root - {b64_encoded}\n"); | ||
|
||
let tmpfiles_dir = Utf8Path::new(ETC_TMPFILES); | ||
root.create_dir_all(tmpfiles_dir)?; | ||
let target = tmpfiles_dir.join(ROOT_SSH_TMPFILE); | ||
root.atomic_write(&target, &tmpfiles_content)?; | ||
println!("Injected: {target}"); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_inject_root_ssh() -> Result<()> { | ||
let root = &cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?; | ||
|
||
inject_root_ssh_authorized_keys(root, "ssh-ed25519 ABCDE example@demo\n").unwrap(); | ||
|
||
let content = root.read_to_string(format!("etc/tmpfiles.d/{ROOT_SSH_TMPFILE}"))?; | ||
assert_eq!( | ||
content, | ||
"f~ /root/.ssh/authorized_keys 600 root root - c3NoLWVkMjU1MTkgQUJDREUgZXhhbXBsZUBkZW1vCg==\n" | ||
); | ||
Ok(()) | ||
} |