[pve-devel] [PATCH] - preserve authorized_key key order - identify double keys by key and not by comment

Stefan Priebe s.priebe at profihost.ag
Mon Aug 27 11:41:47 CEST 2012


Signed-off-by: Stefan Priebe <s.priebe at profihost.ag>
---
 data/PVE/Cluster.pm |   26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/data/PVE/Cluster.pm b/data/PVE/Cluster.pm
index a877d41..4d5b9ac 100644
--- a/data/PVE/Cluster.pm
+++ b/data/PVE/Cluster.pm
@@ -1002,29 +1002,27 @@ sub ssh_merge_keys {
 
     my $data = '';
     if (-f $sshauthkeys) {
-	$data = PVE::Tools::file_get_contents($sshauthkeys, 128*1024);
-	chomp($data);
+        $data = PVE::Tools::file_get_contents($sshauthkeys, 128*1024);
+        chomp($data);
     }
 
     # always add ourself
     if (-f $ssh_rsa_id) {
-	my $pub = PVE::Tools::file_get_contents($ssh_rsa_id);
-	chomp($pub);
-	$data .= "\n$pub\n";
+        my $pub = PVE::Tools::file_get_contents($ssh_rsa_id);
+        chomp($pub);
+        $data .= "\n$pub\n";
     }
 
     my $newdata = "";
     my $vhash = {};
-    while ($data && $data =~ s/^((.*?)(\n|$))//) {
-	my $line = "$2\n";
-	if ($line =~ m/^ssh-rsa\s+\S+\s+(\S+)$/) {
-	    $vhash->{$1} = $line;
-	} else {
-	    $newdata .= $line;
-	}
+    my @lines = split(/\n/, $data);
+    foreach my $line (@lines) {
+        if ($line =~ m/^ssh-rsa\s+(\S+)\s+\S+$/) { 
+            next if ($vhash->{$1});
+            $vhash->{$1} = 1;
+        } 
+        $newdata .= $line . "\n";
     }
-    
-    $newdata .= join("", values(%$vhash));
 
     PVE::Tools::file_set_contents($sshauthkeys, $newdata, 0600);
 }
-- 
1.7.9.5



More information about the pve-devel mailing list