git: 19fb9ad74651 - main - nuageinit: readd ssh key parsing when key is in meta_data.json

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Tue, 20 Aug 2024 10:12:07 UTC
The branch main has been updated by bapt:

URL: https://cgit.FreeBSD.org/src/commit/?id=19fb9ad746517c7af9d79a982334b2550f285355

commit 19fb9ad746517c7af9d79a982334b2550f285355
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2024-08-20 10:04:01 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2024-08-20 10:08:29 +0000

    nuageinit: readd ssh key parsing when key is in meta_data.json
    
    in openstack when no user is specified but a sshkey is provided
    the information is stored in meta_data.json under "public_keys"
    
    PR:     280461
    Reported by:    tdb
---
 libexec/nuageinit/nuageinit          |  6 +++++
 libexec/nuageinit/tests/nuageinit.sh | 48 ++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/libexec/nuageinit/nuageinit b/libexec/nuageinit/nuageinit
index f268f9b0f52c..622e294bb531 100755
--- a/libexec/nuageinit/nuageinit
+++ b/libexec/nuageinit/nuageinit
@@ -164,6 +164,12 @@ if citype == "config-2" then
 		nuage.err("nuageinit: error parsing config-2: meta_data.json: " .. err)
 	end
 	local obj = parser:get_object()
+	if obj.public_keys then
+		local homedir = nuage.adduser(default_user)
+		for _,v in pairs(obj.public_keys) do
+			nuage.addsshkey(homedir, v)
+		end
+	end
 	nuage.sethostname(obj["hostname"])
 
 	-- network
diff --git a/libexec/nuageinit/tests/nuageinit.sh b/libexec/nuageinit/tests/nuageinit.sh
index c6a86bc15486..b5078e256853 100644
--- a/libexec/nuageinit/tests/nuageinit.sh
+++ b/libexec/nuageinit/tests/nuageinit.sh
@@ -8,6 +8,7 @@ atf_test_case nocloud_network
 atf_test_case config2
 atf_test_case config2_pubkeys
 atf_test_case config2_pubkeys_user_data
+atf_test_case config2_pubkeys_meta_data
 atf_test_case config2_network
 atf_test_case config2_network_static_v4
 
@@ -242,6 +243,52 @@ EOF
 	atf_check -o inline:"ssh-rsa AAAAB3NzaC1y...== Generated by Nova\n" cat home/freebsd/.ssh/authorized_keys
 }
 
+config2_pubkeys_meta_data_body()
+{
+	here=$(pwd)
+	export NUAGE_FAKE_ROOTDIR=$(pwd)
+	if [ $(id -u) -ne 0 ]; then
+		atf_skip "root required"
+	fi
+	mkdir -p media/nuageinit
+	cat > media/nuageinit/meta_data.json <<EOF
+{
+	"uuid": "uuid_for_this_instance",
+	"admin_pass": "a_generated_password",
+	"public_keys": {
+		"tdb": "ssh-ed25519 my_key_id tdb@host"
+	},
+	"keys": [
+	{
+		"name": "tdb",
+		"type": "ssh",
+		"data": "ssh-ed25519 my_key_id tdb@host"
+	}
+	],
+	"hostname": "freebsd-14-test.novalocal",
+	"name": "freebsd-14-test",
+	"launch_index": 0,
+	"availability_zone": "nova",
+	"random_seed": "long_random_seed",
+	"project_id": "my_project_id",
+	"devices": [],
+	"dedicated_cpus": []
+}
+EOF
+	mkdir -p etc
+	cat > etc/master.passwd <<EOF
+root:*:0:0::0:0:Charlie &:/root:/bin/csh
+sys:*:1:0::0:0:Sys:/home/sys:/bin/csh
+EOF
+	pwd_mkdb -d etc ${here}/etc/master.passwd
+	cat > etc/group <<EOF
+wheel:*:0:root
+users:*:1:
+EOF
+	atf_check /usr/libexec/nuageinit ${here}/media/nuageinit config-2
+	atf_check -o inline:"ssh-ed25519 my_key_id tdb@host\n" cat home/freebsd/.ssh/authorized_keys
+}
+
 config2_network_body() {
 	here=$(pwd)
 	mkdir -p media/nuageinit
@@ -378,6 +425,7 @@ atf_init_test_cases()
 	atf_add_test_case config2
 	atf_add_test_case config2_pubkeys
 	atf_add_test_case config2_pubkeys_user_data
+	atf_add_test_case config2_pubkeys_meta_data
 	atf_add_test_case config2_network
 	atf_add_test_case config2_network_static_v4
 }