]> source.dussan.org Git - gitblit.git/commitdiff
Preserve key index when re-adding an existing key
authorJames Moger <james.moger@gitblit.com>
Thu, 27 Mar 2014 17:48:51 +0000 (13:48 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 10 Apr 2014 23:00:05 +0000 (19:00 -0400)
src/main/java/com/gitblit/transport/ssh/FileKeyManager.java

index 8a3d2ff595da67a7061ed78e01cc7f1c1b850508..77f818c37fec5a10fa78b1a9b855a335a55f8634 100644 (file)
@@ -130,7 +130,7 @@ public class FileKeyManager extends IPublicKeyManager {
        public boolean addKey(String username, SshKey key) {
                try {
                        String newKey = stripCommentFromKey(key.getRawData());
-
+                       boolean replaced = false;
                        List<String> lines = new ArrayList<String>();
                        File keystore = getKeystore(username);
                        if (keystore.exists()) {
@@ -147,16 +147,22 @@ public class FileKeyManager extends IPublicKeyManager {
                                                continue;
                                        }
 
-                                       // only add keys that do not match the new key
                                        String oldKey = stripCommentFromKey(line);
-                                       if (!newKey.equals(oldKey)) {
+                                       if (newKey.equals(oldKey)) {
+                                               // replace key
+                                               lines.add(key.getRawData());
+                                               replaced = true;
+                                       } else {
+                                               // retain key
                                                lines.add(entry);
                                        }
                                }
                        }
 
-                       // add new key
-                       lines.add(key.getRawData());
+                       if (!replaced) {
+                               // new key, append
+                               lines.add(key.getRawData());
+                       }
 
                        // write keystore
                        String content = Joiner.on("\n").join(lines).trim().concat("\n");