Browse Source

Preserve key index when re-adding an existing key

tags/v1.5.0
James Moger 10 years ago
parent
commit
ab07d0d86d
1 changed files with 11 additions and 5 deletions
  1. 11
    5
      src/main/java/com/gitblit/transport/ssh/FileKeyManager.java

+ 11
- 5
src/main/java/com/gitblit/transport/ssh/FileKeyManager.java View 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");

Loading…
Cancel
Save