Merge changes I39b56689,I7b428b06,I893c0fd0,I772cc9b0
* changes:
AccountSshKey: Split common getter code to utility method
AccountSshKey.Id: Add isValid() method
InitAdminUser: Fix invalid sequence number for SSH key
Fix migration of SSH keys with invalid sequence numbers (<= 0)
diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitAdminUser.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitAdminUser.java
index 6cbd40d..2de71cc 100644
--- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitAdminUser.java
+++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/InitAdminUser.java
@@ -164,6 +164,6 @@
"Cannot add public SSH key: %s is not a file", keyFile));
}
String content = new String(Files.readAllBytes(p), UTF_8);
- return new AccountSshKey(new AccountSshKey.Id(id, 0), content);
+ return new AccountSshKey(new AccountSshKey.Id(id, 1), content);
}
}
diff --git a/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/AccountSshKey.java b/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/AccountSshKey.java
index 7b1e874..f185a76 100644
--- a/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/AccountSshKey.java
+++ b/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/AccountSshKey.java
@@ -53,6 +53,10 @@
protected void set(int newValue) {
seq = newValue;
}
+
+ public boolean isValid() {
+ return seq > 0;
+ }
}
@Column(id = 1, name = Column.NONE)
@@ -70,7 +74,7 @@
public AccountSshKey(final AccountSshKey.Id i, final String pub) {
id = i;
sshPublicKey = pub;
- valid = true; // We can assume it is fine.
+ valid = id.isValid();
}
public Account.Id getAccount() {
@@ -85,47 +89,31 @@
return sshPublicKey;
}
- public String getAlgorithm() {
- final String s = getSshPublicKey();
- if (s == null || s.length() == 0) {
- return "none";
+ private String getPublicKeyPart(int index, String defaultValue) {
+ String s = getSshPublicKey();
+ if (s != null && s.length() > 0) {
+ String[] parts = s.split(" ");
+ if (parts.length > index) {
+ return parts[index];
+ }
}
+ return defaultValue;
+ }
- final String[] parts = s.split(" ");
- if (parts.length < 1) {
- return "none";
- }
- return parts[0];
+ public String getAlgorithm() {
+ return getPublicKeyPart(0, "none");
}
public String getEncodedKey() {
- final String s = getSshPublicKey();
- if (s == null || s.length() == 0) {
- return null;
- }
-
- final String[] parts = s.split(" ");
- if (parts.length < 2) {
- return null;
- }
- return parts[1];
+ return getPublicKeyPart(1, null);
}
public String getComment() {
- final String s = getSshPublicKey();
- if (s == null || s.length() == 0) {
- return "";
- }
-
- final String[] parts = s.split(" ", 3);
- if (parts.length < 3) {
- return "";
- }
- return parts[2];
+ return getPublicKeyPart(2, "");
}
public boolean isValid() {
- return valid;
+ return valid && id.isValid();
}
public void setInvalid() {
diff --git a/gerrit-reviewdb/src/test/java/com/google/gerrit/reviewdb/client/AccountSshKeyTest.java b/gerrit-reviewdb/src/test/java/com/google/gerrit/reviewdb/client/AccountSshKeyTest.java
new file mode 100644
index 0000000..139d360
--- /dev/null
+++ b/gerrit-reviewdb/src/test/java/com/google/gerrit/reviewdb/client/AccountSshKeyTest.java
@@ -0,0 +1,50 @@
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gerrit.reviewdb.client;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import org.junit.Test;
+
+public class AccountSshKeyTest {
+ private static final String KEY =
+ "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCgug5VyMXQGnem2H1KVC4/HcRcD4zzBqS"
+ + "uJBRWVonSSoz3RoAZ7bWXCVVGwchtXwUURD689wFYdiPecOrWOUgeeyRq754YWRhU+W28"
+ + "vf8IZixgjCmiBhaL2gt3wff6pP+NXJpTSA4aeWE5DfNK5tZlxlSxqkKOS8JRSUeNQov5T"
+ + "w== john.doe@example.com";
+
+ private final Account.Id accountId = new Account.Id(1);
+
+ @Test
+ public void testValidity() throws Exception {
+ AccountSshKey key = new AccountSshKey(
+ new AccountSshKey.Id(accountId, -1), KEY);
+ assertThat(key.isValid()).isFalse();
+ key = new AccountSshKey(new AccountSshKey.Id(accountId, 0), KEY);
+ assertThat(key.isValid()).isFalse();
+ key = new AccountSshKey(new AccountSshKey.Id(accountId, 1), KEY);
+ assertThat(key.isValid()).isTrue();
+ }
+
+ @Test
+ public void testGetters() throws Exception {
+ AccountSshKey key = new AccountSshKey(
+ new AccountSshKey.Id(accountId, 1), KEY);
+ assertThat(key.getSshPublicKey()).isEqualTo(KEY);
+ assertThat(key.getAlgorithm()).isEqualTo(KEY.split(" ")[0]);
+ assertThat(key.getEncodedKey()).isEqualTo(KEY.split(" ")[1]);
+ assertThat(key.getComment()).isEqualTo(KEY.split(" ")[2]);
+ }
+}
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_124.java b/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_124.java
index 957d683..6354b68 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_124.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/schema/Schema_124.java
@@ -14,8 +14,10 @@
package com.google.gerrit.server.schema;
+import com.google.common.base.Function;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
+import com.google.common.collect.Ordering;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountSshKey;
import com.google.gerrit.reviewdb.server.ReviewDb;
@@ -42,7 +44,10 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
+import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
import java.util.Map;
public class Schema_124 extends SchemaVersion {
@@ -106,7 +111,7 @@
md.getCommitBuilder().setCommitter(serverUser);
authorizedKeys.load(md);
- authorizedKeys.setKeys(e.getValue());
+ authorizedKeys.setKeys(fixInvalidSequenceNumbers(e.getValue()));
authorizedKeys.commit(md);
}
}
@@ -116,4 +121,26 @@
throw new OrmException(ex);
}
}
+
+ private Collection<AccountSshKey> fixInvalidSequenceNumbers(
+ Collection<AccountSshKey> keys) {
+ Ordering<AccountSshKey> o =
+ Ordering.natural().onResultOf(new Function<AccountSshKey, Integer>() {
+ @Override
+ public Integer apply(AccountSshKey sshKey) {
+ return sshKey.getKey().get();
+ }
+ });
+ List<AccountSshKey> fixedKeys = new ArrayList<>(keys);
+ AccountSshKey minKey = o.min(keys);
+ while (minKey.getKey().get() <= 0) {
+ AccountSshKey fixedKey = new AccountSshKey(
+ new AccountSshKey.Id(minKey.getKey().getParentKey(),
+ Math.max(o.max(keys).getKey().get() + 1, 1)),
+ minKey.getSshPublicKey());
+ Collections.replaceAll(fixedKeys, minKey, fixedKey);
+ minKey = o.min(fixedKeys);
+ }
+ return fixedKeys;
+ }
}