From 966f0bb2a4fd5226ae037c387537a559f8b97eaf Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Fri, 28 Jun 2024 12:21:33 +0200 Subject: [PATCH] Get ssh key from user-data with incus incus has no way to set ssh key in meta-data, instead we can set it in user-data. This means that we cannot set ssh key if user-data is a shell script. --- lib/tiny-cloud/cloud/incus/imds | 6 ++++++ tests/imds.test | 26 +++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/tiny-cloud/cloud/incus/imds b/lib/tiny-cloud/cloud/incus/imds index 9442a5e..9754566 100644 --- a/lib/tiny-cloud/cloud/incus/imds +++ b/lib/tiny-cloud/cloud/incus/imds @@ -19,3 +19,9 @@ _imds_userdata() { printf "GET /1.0/config/user.user-data HTTP/1.0\n\n" \ | nc "$IMDS_ENDPOINT" | sed '1,/^\r$/d' } + +_imds_ssh_keys() { + # extract ssh_authorized_keys from user-data + _imds_userdata \ + | awk '/^ssh_authorized_keys:/ {flag=1; next} /^[a-z]/ {flag=0} flag && /^ *- / {sub(/^ *- /, ""); print}' +} diff --git a/tests/imds.test b/tests/imds.test index 787727b..6898803 100755 --- a/tests/imds.test +++ b/tests/imds.test @@ -14,28 +14,29 @@ init_tests \ imds_hostname_aws \ imds_hostname_azure \ imds_hostname_gcp \ + imds_hostname_hetzner \ + imds_hostname_incus \ imds_hostname_nocloud \ imds_hostname_oci \ imds_hostname_scaleway \ - imds_hostname_hetzner \ - imds_hostname_incus \ \ imds_local_hostname_aws \ imds_local_hostname_azure \ imds_local_hostname_gcp \ + imds_local_hostname_hetzner \ + imds_local_hostname_incus \ imds_local_hostname_nocloud \ imds_local_hostname_oci \ imds_local_hostname_scaleway \ - imds_local_hostname_hetzner \ - imds_local_hostname_incus \ \ imds_ssh_keys_aws \ imds_ssh_keys_azure \ imds_ssh_keys_gcp \ + imds_ssh_keys_hetzner \ + imds_ssh_keys_incus \ imds_ssh_keys_nocloud \ imds_ssh_keys_oci \ imds_ssh_keys_scaleway \ - imds_ssh_keys_hetzner \ \ imds_nocloud_cmdline_local_hostname \ imds_nocloud_smbios_local_hostname \ @@ -171,6 +172,21 @@ imds_ssh_keys_hetzner_body() { CLOUD="hetzner" atf_check -o match:"$key" imds @ssh-keys } +imds_ssh_keys_incus_body() { + local key="ssh-ed25519 keydata" + fake_userdata_incus <<-EOF + ssh_authorized_keys: + - asdasdf + # other key + - $key + foo: bar + EOF + CLOUD="incus" atf_check \ + -o match:"$key" \ + -o not-match:"other key" \ + imds @ssh-keys +} + imds_nocloud_cmdline_local_hostname_body() { atf_require_prog yx mkdir proc