From ab07d0d86d8b64b4f7c88b45bc81f1eec22105db Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 27 Mar 2014 13:48:51 -0400 Subject: [PATCH] Preserve key index when re-adding an existing key --- .../gitblit/transport/ssh/FileKeyManager.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/gitblit/transport/ssh/FileKeyManager.java b/src/main/java/com/gitblit/transport/ssh/FileKeyManager.java index 8a3d2ff5..77f818c3 100644 --- a/src/main/java/com/gitblit/transport/ssh/FileKeyManager.java +++ b/src/main/java/com/gitblit/transport/ssh/FileKeyManager.java @@ -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 lines = new ArrayList(); 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"); -- 2.39.5