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)
This commit is contained in:
@@ -53,6 +53,10 @@ public final class AccountSshKey {
|
||||
protected void set(int newValue) {
|
||||
seq = newValue;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return seq > 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Column(id = 1, name = Column.NONE)
|
||||
@@ -70,7 +74,7 @@ public final class AccountSshKey {
|
||||
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 @@ public final class AccountSshKey {
|
||||
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() {
|
||||
|
||||
@@ -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]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user