public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Alexandre Derumier <aderumier@odiso.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH V2 qemu-server] fix #1739: cloudinit: add cisshdeletehostkeys option
Date: Sun,  7 Feb 2021 15:18:32 +0100	[thread overview]
Message-ID: <20210207141832.3283706-1-aderumier@odiso.com> (raw)

This define behaviour of ssh server keys generation on cloudinit
config change.

different value:

- once : only once at vmstart  (default value)
- no|off|false : never generate ssh key
- yes|on|true: always generate ssh key

When value is defined to 'once', the value is rewriten to 'no'
in vmconfig after vm start

We need to change instance-id at each config change because
network configuration is only done once in pre-init local service,
and a lock is done on instance-id, and they are no way to change
that in cloudinit vm config.

The ssh host keys are regenerated by ssh module, once by instance.
user could change that,and do only once,
but in this same ssh module, the user public keys are also deployed.

Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
---
 PVE/QemuServer.pm           |  9 ++++++++-
 PVE/QemuServer/Cloudinit.pm | 10 +++++++++-
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index f401baf..c899b35 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -760,6 +760,13 @@ my $confdesc_cloudinit = {
 	format => 'urlencoded',
 	description => "cloud-init: Setup public SSH keys (one key per line, OpenSSH format).",
     },
+    cisshdeletehostkeys => {
+	optional => 1,
+	type => 'string',
+	enum => [qw(once yes no on off true false)],
+	default_key => 1,
+	description => "cloud-init: Regenerate host SSH keys on config change.",
+    },
 };
 
 # what about other qemu settings ?
@@ -4951,7 +4958,7 @@ sub vm_start_nolock {
 	$conf = PVE::QemuConfig->load_config($vmid); # update/reload
     }
 
-    PVE::QemuServer::Cloudinit::generate_cloudinitconfig($conf, $vmid);
+    PVE::QemuServer::Cloudinit::generate_cloudinitconfig($conf, $vmid, 1);
 
     my $defaults = load_defaults();
 
diff --git a/PVE/QemuServer/Cloudinit.pm b/PVE/QemuServer/Cloudinit.pm
index c464bf3..7b24f93 100644
--- a/PVE/QemuServer/Cloudinit.pm
+++ b/PVE/QemuServer/Cloudinit.pm
@@ -135,6 +135,7 @@ sub cloudinit_userdata {
 	foreach my $k (@$keys) {
 	    $content .= "  - $k\n";
 	}
+	$content .= "ssh_deletekeys: false\n" if defined($conf->{cisshdeletehostkeys}) && $conf->{cisshdeletehostkeys} =~ m/^(off|no|false)/; 
     }
     $content .= "chpasswd:\n";
     $content .= "  expire: False\n";
@@ -554,9 +555,10 @@ my $cloudinit_methods = {
 };
 
 sub generate_cloudinitconfig {
-    my ($conf, $vmid) = @_;
+    my ($conf, $vmid, $vmstart) = @_;
 
     my $format = get_cloudinit_format($conf);
+    my $generated = undef;
 
     PVE::QemuConfig->foreach_volume($conf, sub {
         my ($ds, $drive) = @_;
@@ -569,7 +571,13 @@ sub generate_cloudinitconfig {
 	    or die "missing cloudinit methods for format '$format'\n";
 
 	$generator->($conf, $vmid, $drive, $volname, $storeid);
+	$generated = 1;
     });
+
+    if ($vmstart && $generated && (!defined($conf->{cisshdeletehostkeys}) || $conf->{cisshdeletehostkeys} eq 'once')) {
+	$conf->{cisshdeletehostkeys} = 'no';
+	PVE::QemuConfig->write_config($vmid, $conf);
+    }
 }
 
 sub dump_cloudinit_config {
-- 
2.20.1




             reply	other threads:[~2021-02-07 14:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-07 14:18 Alexandre Derumier [this message]
2021-02-17 15:01 ` Mira Limbeck
2021-02-20 18:34   ` aderumier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210207141832.3283706-1-aderumier@odiso.com \
    --to=aderumier@odiso.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal