Search in sources :

Example 1 with SshKeyInfo

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;
}
Also used : SshKeyInfo(com.google.gerrit.extensions.common.SshKeyInfo)

Example 2 with SshKeyInfo

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);
}
Also used : SshKeyInfo(com.google.gerrit.extensions.common.SshKeyInfo) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test) UseSsh(com.google.gerrit.acceptance.UseSsh)

Example 3 with SshKeyInfo

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());
    }
}
Also used : RawInput(com.google.gerrit.extensions.restapi.RawInput) Input(com.google.gerrit.server.account.AddSshKey.Input) InvalidSshKeyException(com.google.gerrit.common.errors.InvalidSshKeyException) SshKeyInfo(com.google.gerrit.extensions.common.SshKeyInfo) AccountSshKey(com.google.gerrit.reviewdb.client.AccountSshKey) RawInput(com.google.gerrit.extensions.restapi.RawInput) EmailException(com.google.gerrit.common.errors.EmailException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ByteSource(com.google.common.io.ByteSource)

Aggregations

SshKeyInfo (com.google.gerrit.extensions.common.SshKeyInfo)3 ByteSource (com.google.common.io.ByteSource)1 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)1 UseSsh (com.google.gerrit.acceptance.UseSsh)1 EmailException (com.google.gerrit.common.errors.EmailException)1 InvalidSshKeyException (com.google.gerrit.common.errors.InvalidSshKeyException)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 RawInput (com.google.gerrit.extensions.restapi.RawInput)1 PublicKeyStore.keyToString (com.google.gerrit.gpg.PublicKeyStore.keyToString)1 AccountSshKey (com.google.gerrit.reviewdb.client.AccountSshKey)1 Input (com.google.gerrit.server.account.AddSshKey.Input)1 Test (org.junit.Test)1