From 4ccc16f18f615be305c3a676f3d2bc45decec7c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jake=20Buchholz=20G=C3=B6kt=C3=BCrk?= Date: Mon, 8 May 2023 04:41:41 +0000 Subject: [PATCH] Enable sshd & Source UserData Handler --- lib/tiny-cloud/init | 61 +++++++++++++++------------------ lib/tiny-cloud/user-data/script | 14 +++++++- tests/init-early.test | 12 ++++++- tests/init-final.test | 5 +++ 4 files changed, 57 insertions(+), 35 deletions(-) diff --git a/lib/tiny-cloud/init b/lib/tiny-cloud/init index 616a360..cb80197 100644 --- a/lib/tiny-cloud/init +++ b/lib/tiny-cloud/init @@ -15,21 +15,21 @@ INIT_ACTIONS_EARLY=" install_hotplugs set_default_interfaces create_default_user + enable_sshd " INIT_ACTIONS_MAIN=" save_userdata set_hostname set_ssh_keys " -INIT_ACTIONS_FINAL=" - run_userdata -" +INIT_ACTIONS_FINAL="" # try to ensure existence of output directories, but otherwise don't panic [ ! -d "$TINY_CLOUD_LOGS" ] && mkdir -p "$TINY_CLOUD_LOGS" || true [ ! -d "$TINY_CLOUD_VAR" ] && mkdir -p "$TINY_CLOUD_VAR" || true -### init-early functions... + +### standard init-early functions... init__expand_root() { local dev=$(awk '$2 == "/" {print $1}' "$ROOT"/proc/mounts) @@ -181,8 +181,14 @@ init__create_default_user() { echo 'permit nopass :wheel' > "$TARGET/etc/doas.d/wheel.conf" } +init__enable_sshd() { + $MOCK rc-update add sshd default + # in case something else has enabled/disabled dservices + $MOCK rc-update --update +} -### init-main functions + +### standard init-main functions init__set_hostname() { local fqdn=$(imds @hostname) @@ -237,46 +243,35 @@ init__save_userdata() { rm "$tmpfile" } -### init-final functions -init__run_userdata() { - local log="$TINY_CLOUD_LOGS/user-data.log" - local exit="$TINY_CLOUD_LOGS/user-data.exit" - local userdata="$TINY_CLOUD_VAR/user-data" +### standard init-final functions would be here, if there were any - if [ $(userdata_type) != "script" ]; then - printf '(Not Executable) ' >&2 - log info "$phase $ACTION - not exectutable" - return - fi - chmod +x "$userdata" - { "$userdata" 2>& 1; echo $? > "$exit"; } | tee "$log" +### load cloud-specific init functions / vars (potentially overriding) - return $(cat "$exit") -} - -### potentially override the above, per cloud - -# load cloud-specific init functions / vars if [ -f "$LIBDIR/tiny-cloud/cloud/$CLOUD/init" ]; then . "$LIBDIR/tiny-cloud/cloud/$CLOUD/init" fi +### load user-data type-specific init functions / vars (potentially overriding) + # this should be non-overrideable, but need this before we... userdata_type() { - if [ -f "$TINY_CLOUD_VAR/user-data" ]; then - header=$(head -n1 "$TINY_CLOUD_VAR/user-data" | sed -e 's/[[:space:]].*//g') - case "$header" in - '#cloud-config') echo cloud-config;; - '#!'*) echo script;; - *) echo unknown;; - esac - else + if [ ! -f "$TINY_CLOUD_VAR/user-data" ]; then echo missing + return fi + header=$(head -n1 "$TINY_CLOUD_VAR/user-data" | sed -e 's/[[:space:]].*//g') + case "$header" in + '#!'*) echo script;; + '#'*) echo ${header#\#};; + *) echo unknown;; + esac } -# ...load user-data type-specific init functions / vars -# TODO +USERDATA_TYPE="$(userdata_type)" +if [ -f "$LIBDIR/tiny-cloud/user-data/$USERDATA_TYPE" ]; then + . "$LIBDIR/tiny-cloud/user-data/$USERDATA_TYPE" +fi +# TODO: some indication that the user-data type is unsupported? diff --git a/lib/tiny-cloud/user-data/script b/lib/tiny-cloud/user-data/script index f6a65d4..f545384 100644 --- a/lib/tiny-cloud/user-data/script +++ b/lib/tiny-cloud/user-data/script @@ -1,4 +1,16 @@ # Script UserData Functions # vim:set ts=4 et ft=sh: -# TODO +init__run_userdata() { + local log="$TINY_CLOUD_LOGS/user-data.log" + local exit="$TINY_CLOUD_LOGS/user-data.exit" + local userdata="$TINY_CLOUD_VAR/user-data" + + chmod +x "$userdata" + { "$userdata" 2>& 1; echo $? > "$exit"; } | tee "$log" + + return $(cat "$exit") +} + +# add init actions +INIT_ACTIONS_FINAL="${INIT_ACTIONS_FINAL} run_userdata" diff --git a/tests/init-early.test b/tests/init-early.test index 3e52c0f..57bb3af 100755 --- a/tests/init-early.test +++ b/tests/init-early.test @@ -13,7 +13,8 @@ init_tests \ ethernets \ find_first_interface_up \ auto_detect_ethernet_interface \ - set_default_interfaces + set_default_interfaces \ + enable_sshd PROVIDERS="aws azure gcp nocloud oci" @@ -99,3 +100,12 @@ set_default_interfaces_body() { -o match:"use dhcp" \ cat etc/network/interfaces } + +enable_sshd_body() { + for provider in $PROVIDERS; do + CLOUD="$provider" atf_check \ + -o match:"rc-update.* add sshd default" \ + -o match:"rc-update.* --update" \ + sh -c ". $lib; init__enable_sshd" + done +} diff --git a/tests/init-final.test b/tests/init-final.test index 2360965..d75cf6c 100755 --- a/tests/init-final.test +++ b/tests/init-final.test @@ -21,6 +21,11 @@ userdata_type_body() { sh -c ". \"$lib\"; userdata_type" echo "#tiny-cloud-config" > var/lib/cloud/user-data + CLOUD="$c" atf_check \ + -o match:"tiny-cloud-config" \ + sh -c ". \"$lib\"; userdata_type" + + echo "no-content-type" > var/lib/cloud/user-data CLOUD="$c" atf_check \ -o match:"unknown" \ sh -c ". \"$lib\"; userdata_type"