use of com.google.gerrit.extensions.common.SshKeyInfo in project gerrit by GerritCodeReview.
the class GetSshKeys method newSshKeyInfo.
public static SshKeyInfo newSshKeyInfo(AccountSshKey sshKey) {
SshKeyInfo info = new SshKeyInfo();
info.seq = sshKey.getKey().get();
info.sshPublicKey = sshKey.getSshPublicKey();
info.encodedKey = sshKey.getEncodedKey();
info.algorithm = sshKey.getAlgorithm();
info.comment = Strings.emptyToNull(sshKey.getComment());
info.valid = sshKey.isValid();
return info;
}
use of com.google.gerrit.extensions.common.SshKeyInfo in project gerrit by GerritCodeReview.
the class AccountIT method sshKeys.
@Test
@UseSsh
public void sshKeys() throws Exception {
//
// The test account should initially have exactly one ssh key
List<SshKeyInfo> info = gApi.accounts().self().listSshKeys();
assertThat(info).hasSize(1);
assertSequenceNumbers(info);
SshKeyInfo key = info.get(0);
String inital = AccountCreator.publicKey(admin.sshKey, admin.email);
assertThat(key.sshPublicKey).isEqualTo(inital);
accountIndexedCounter.assertNoReindex();
// Add a new key
String newKey = AccountCreator.publicKey(AccountCreator.genSshKey(), admin.email);
gApi.accounts().self().addSshKey(newKey);
info = gApi.accounts().self().listSshKeys();
assertThat(info).hasSize(2);
assertSequenceNumbers(info);
accountIndexedCounter.assertReindexOf(admin);
// Add an existing key (the request succeeds, but the key isn't added again)
gApi.accounts().self().addSshKey(inital);
info = gApi.accounts().self().listSshKeys();
assertThat(info).hasSize(2);
assertSequenceNumbers(info);
accountIndexedCounter.assertNoReindex();
// Add another new key
String newKey2 = AccountCreator.publicKey(AccountCreator.genSshKey(), admin.email);
gApi.accounts().self().addSshKey(newKey2);
info = gApi.accounts().self().listSshKeys();
assertThat(info).hasSize(3);
assertSequenceNumbers(info);
accountIndexedCounter.assertReindexOf(admin);
// Delete second key
gApi.accounts().self().deleteSshKey(2);
info = gApi.accounts().self().listSshKeys();
assertThat(info).hasSize(2);
assertThat(info.get(0).seq).isEqualTo(1);
assertThat(info.get(1).seq).isEqualTo(3);
accountIndexedCounter.assertReindexOf(admin);
}
use of com.google.gerrit.extensions.common.SshKeyInfo in project gerrit by GerritCodeReview.
the class AddSshKey method apply.
public Response<SshKeyInfo> apply(IdentifiedUser user, Input input) throws BadRequestException, IOException, ConfigInvalidException {
if (input == null) {
input = new Input();
}
if (input.raw == null) {
throw new BadRequestException("SSH public key missing");
}
final RawInput rawKey = input.raw;
String sshPublicKey = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return rawKey.getInputStream();
}
}.asCharSource(UTF_8).read();
try {
AccountSshKey sshKey = authorizedKeys.addKey(user.getAccountId(), sshPublicKey);
try {
addKeyFactory.create(user, sshKey).send();
} catch (EmailException e) {
log.error("Cannot send SSH key added message to " + user.getAccount().getPreferredEmail(), e);
}
sshKeyCache.evict(user.getUserName());
return Response.<SshKeyInfo>created(GetSshKeys.newSshKeyInfo(sshKey));
} catch (InvalidSshKeyException e) {
throw new BadRequestException(e.getMessage());
}
}
Aggregations